NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemEnderAccess.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;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.api.misc.ILevelData;
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;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
2020-01-24 17:05:41 +01:00
import net.minecraft.entity.player.PlayerInventory;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.ServerPlayer;
2020-01-24 17:05:41 +01:00
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;
2021-12-04 15:40:09 +01:00
import net.minecraft.util.InteractionResult;
2019-10-20 22:30:49 +02:00
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;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.Level;
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
2020-01-26 01:41:49 +01:00
public class ItemEnderAccess extends ItemImpl {
public ItemEnderAccess() {
2020-02-07 23:50:16 +01:00
super("ender_access");
2019-02-18 12:15:18 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public ActionResult<ItemStack> onItemRightClick(Level levelIn, Player playerIn, Hand handIn) {
2019-10-20 22:30:49 +02:00
if (handIn != Hand.MAIN_HAND)
2021-12-04 15:40:09 +01:00
return new ActionResult<>(InteractionResult.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)) {
2021-12-04 15:40:09 +01:00
if (!levelIn.isClientSide && NaturesAuraAPI.instance().extractAuraFromPlayer(playerIn, 10000, false)) {
NetworkHooks.openGui((ServerPlayer) playerIn, new INamedContainerProvider() {
2020-01-24 17:05:41 +01:00
@Override
public ITextComponent getDisplayName() {
return new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".ender_access", TextFormatting.ITALIC + name + TextFormatting.RESET);
}
@Nullable
@Override
2021-12-04 15:40:09 +01:00
public Container createMenu(int windowId, PlayerInventory inv, Player player) {
IItemHandler handler = ILevelData.getOverworldData(inv.player.level).getEnderStorage(name);
2020-01-24 17:05:41 +01:00
return new ContainerEnderCrate(ModContainers.ENDER_ACCESS, windowId, player, handler);
}
}, buffer -> buffer.writeString(name));
}
2021-12-04 15:40:09 +01:00
return new ActionResult<>(InteractionResult.SUCCESS, stack);
2019-02-18 12:15:18 +01:00
}
2021-12-04 15:40:09 +01:00
return new ActionResult<>(InteractionResult.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)
2021-12-04 15:40:09 +01:00
public void addInformation(ItemStack stack, @Nullable Level levelIn, 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
}