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;
2021-12-15 16:24:53 +01:00
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.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.items.IItemHandler;
2021-12-15 16:24:53 +01:00
import net.minecraftforge.network.NetworkHooks;
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 {
2021-12-15 16:24:53 +01:00
2020-01-26 01:41:49 +01:00
public ItemEnderAccess() {
2020-02-07 23:50:16 +01:00
super("ender_access");
2019-02-18 12:15:18 +01:00
}
@Override
2021-12-15 16:24:53 +01:00
public InteractionResultHolder<ItemStack> use(Level levelIn, Player playerIn, InteractionHand handIn) {
if (handIn != InteractionHand.MAIN_HAND)
return new InteractionResultHolder<>(InteractionResult.PASS, playerIn.getItemInHand(handIn));
2021-12-15 16:30:22 +01:00
var stack = playerIn.getMainHandItem();
var name = BlockEnderCrate.getEnderName(stack);
2020-01-24 17:05:41 +01:00
if (!Strings.isNullOrEmpty(name)) {
2021-12-04 15:40:09 +01:00
if (!levelIn.isClientSide && NaturesAuraAPI.instance().extractAuraFromPlayer(playerIn, 10000, false)) {
NetworkHooks.openScreen((ServerPlayer) playerIn, new MenuProvider() {
2020-01-24 17:05:41 +01:00
@Override
2021-12-15 16:24:53 +01:00
public Component getDisplayName() {
2022-06-27 15:24:04 +02:00
return Component.translatable("info." + NaturesAura.MOD_ID + ".ender_access", ChatFormatting.ITALIC + name + ChatFormatting.RESET);
2020-01-24 17:05:41 +01:00
}
@Nullable
@Override
2021-12-15 16:24:53 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory inv, Player player) {
2023-07-08 12:32:27 +02:00
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);
}
2021-12-15 16:24:53 +01:00
}, buffer -> buffer.writeUtf(name));
2020-01-24 17:05:41 +01:00
}
2021-12-15 16:24:53 +01:00
return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
2019-02-18 12:15:18 +01:00
}
2021-12-15 16:24:53 +01:00
return new InteractionResultHolder<>(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-15 16:24:53 +01:00
public void appendHoverText(ItemStack stack, @Nullable Level levelIn, List<Component> tooltip, TooltipFlag flagIn) {
2019-02-18 19:30:35 +01:00
BlockEnderCrate.addEnderNameInfo(stack, tooltip);
}
2019-02-18 12:15:18 +01:00
}