diff options
author | Linnea Gräf <nea@nea.moe> | 2024-01-30 20:47:17 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-01-30 20:47:17 +0100 |
commit | 3904749115fb7a71bf7c7f9d5d1530e1be2c115b (patch) | |
tree | 835993f2f8ac5875d0f02fe2356d26ef173650bf /src/main/java/com/example/ExampleMod.java | |
parent | 4eb2056f2f20366ea49a7cf54d892bd3f25bdb16 (diff) | |
download | forge1.8.9template-3904749115fb7a71bf7c7f9d5d1530e1be2c115b.tar.gz forge1.8.9template-3904749115fb7a71bf7c7f9d5d1530e1be2c115b.tar.bz2 forge1.8.9template-3904749115fb7a71bf7c7f9d5d1530e1be2c115b.zip |
more tutorialtutorial
Diffstat (limited to 'src/main/java/com/example/ExampleMod.java')
-rw-r--r-- | src/main/java/com/example/ExampleMod.java | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/src/main/java/com/example/ExampleMod.java b/src/main/java/com/example/ExampleMod.java index 166c371..6a386e1 100644 --- a/src/main/java/com/example/ExampleMod.java +++ b/src/main/java/com/example/ExampleMod.java @@ -2,12 +2,25 @@ package com.example; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.inventory.GuiChest; +import net.minecraft.inventory.ContainerChest; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.nbt.NBTTagString; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; @Mod(modid = "examplemod", useMetadata = true) public class ExampleMod { @@ -24,11 +37,38 @@ public class ExampleMod { public static final int MOUSE_MIDDLE = 2; public static final int MOUSE_BACKWARD = 3; public static final int MOUSE_FORWARD = 4; + + public static <U extends NBTBase, T> List<T> listFromNBT(NBTTagList nbtList, Function<U, T> reader) { + List<T> ts = new ArrayList<>(nbtList.tagCount()); + for (int i = 0; i < nbtList.tagCount(); i++) { + //noinspection unchecked + ts.add(reader.apply((U) nbtList.get(i))); + } + return ts; + } + @SubscribeEvent public void onTick(TickEvent.ClientTickEvent event) { - if (screenToOpenNextTick != null) { - Minecraft.getMinecraft().displayGuiScreen(screenToOpenNextTick); - screenToOpenNextTick = null; + if (event.phase != TickEvent.Phase.END) return; + GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen; + if (!(currentScreen instanceof GuiChest)) return; + ContainerChest container = (ContainerChest) ((GuiChest) currentScreen).inventorySlots; + LogManager.getLogger("ExampleMod").info("Container Name: " + container.getLowerChestInventory().getDisplayName().getFormattedText()); + Logger logger = LogManager.getLogger("ExampleMod"); + for (int i = 0; i < container.getLowerChestInventory().getSizeInventory(); i++) { + ItemStack stack = container.getLowerChestInventory().getStackInSlot(i); + if (stack == null) continue; + logger.info("Slot " + i + ":"); + + byte STRING_NBT_TAG = new NBTTagString().getId(); + NBTTagCompound tagCompound = stack.getTagCompound(); + if (tagCompound == null) continue; + String displayName = tagCompound.getCompoundTag("display").getString("Name"); + NBTTagList loreList = tagCompound.getCompoundTag("display").getTagList("Lore", STRING_NBT_TAG); + List<String> loreStrings = listFromNBT(loreList, NBTTagString::getString); + for (int i1 = 0; i1 < loreList.tagCount(); i1++) { + String loreLine = loreList.getStringTagAt(i1); + } } } } |