NaturesAura/src/main/java/de/ellpeck/naturesaura/items/EnderAccess.java

70 lines
3.1 KiB
Java
Raw Normal View History

2019-02-18 12:15:18 +01:00
package de.ellpeck.naturesaura.items;
2019-02-18 19:30:35 +01:00
import com.google.common.base.Strings;
2019-02-18 12:15:18 +01:00
import de.ellpeck.naturesaura.NaturesAura;
2019-02-18 13:00:54 +01:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2020-01-24 17:05:41 +01:00
import de.ellpeck.naturesaura.api.misc.IWorldData;
2019-02-18 19:30:35 +01:00
import de.ellpeck.naturesaura.blocks.BlockEnderCrate;
2020-01-24 17:05:41 +01:00
import de.ellpeck.naturesaura.gui.ContainerEnderCrate;
import de.ellpeck.naturesaura.gui.ModContainers;
2019-02-18 19:30:35 +01:00
import net.minecraft.client.util.ITooltipFlag;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.player.PlayerEntity;
2020-01-24 17:05:41 +01:00
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
2019-02-18 12:15:18 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.text.ITextComponent;
2020-01-24 17:05:41 +01:00
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
2019-02-18 12:15:18 +01:00
import net.minecraft.world.World;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2020-01-24 17:05:41 +01:00
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.IItemHandler;
2019-02-18 19:30:35 +01:00
import javax.annotation.Nullable;
import java.util.List;
2019-02-18 12:15:18 +01:00
2019-11-04 19:08:49 +01:00
public class EnderAccess extends ItemImpl {
public EnderAccess() {
super("ender_access", new Properties().group(NaturesAura.CREATIVE_TAB));
2019-02-18 12:15:18 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
if (handIn != Hand.MAIN_HAND)
return new ActionResult<>(ActionResultType.PASS, playerIn.getHeldItem(handIn));
2019-02-18 12:15:18 +01:00
ItemStack stack = playerIn.getHeldItemMainhand();
2020-01-24 17:05:41 +01:00
String name = BlockEnderCrate.getEnderName(stack);
if (!Strings.isNullOrEmpty(name)) {
if (!worldIn.isRemote && NaturesAuraAPI.instance().extractAuraFromPlayer(playerIn, 10000, false)) {
NetworkHooks.openGui((ServerPlayerEntity) playerIn, new INamedContainerProvider() {
@Override
public ITextComponent getDisplayName() {
return new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".ender_access", TextFormatting.ITALIC + name + TextFormatting.RESET);
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory inv, PlayerEntity player) {
IItemHandler handler = IWorldData.getOverworldData(inv.player.world).getEnderStorage(name);
return new ContainerEnderCrate(ModContainers.ENDER_ACCESS, windowId, player, handler);
}
}, buffer -> buffer.writeString(name));
}
2019-10-20 22:30:49 +02:00
return new ActionResult<>(ActionResultType.SUCCESS, stack);
2019-02-18 12:15:18 +01:00
}
2019-10-20 22:30:49 +02:00
return new ActionResult<>(ActionResultType.FAIL, stack);
2019-02-18 12:15:18 +01:00
}
2019-02-18 19:30:35 +01:00
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2019-11-04 19:08:49 +01:00
public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
2019-02-18 19:30:35 +01:00
BlockEnderCrate.addEnderNameInfo(stack, tooltip);
}
2019-02-18 12:15:18 +01:00
}