From f4f5920d8859c9816568b714356ed2a101fa0eee Mon Sep 17 00:00:00 2001 From: Lordmau5 <1345036+Lordmau5@users.noreply.github.com> Date: Mon, 9 Mar 2015 15:46:00 +0100 Subject: [PATCH] Fix for Thermal Expansion stuff - Also added HUD for Caches :) --- .../thermalexpansion/HUDHandlerCache.java | 111 ++++++++++++++++++ .../ThermalExpansionModule.java | 51 +++++--- 2 files changed, 145 insertions(+), 17 deletions(-) create mode 100644 src/main/java/mcp/mobius/waila/addons/thermalexpansion/HUDHandlerCache.java diff --git a/src/main/java/mcp/mobius/waila/addons/thermalexpansion/HUDHandlerCache.java b/src/main/java/mcp/mobius/waila/addons/thermalexpansion/HUDHandlerCache.java new file mode 100644 index 0000000..59c39e9 --- /dev/null +++ b/src/main/java/mcp/mobius/waila/addons/thermalexpansion/HUDHandlerCache.java @@ -0,0 +1,111 @@ +package mcp.mobius.waila.addons.thermalexpansion; + +import cpw.mods.fml.common.registry.GameData; +import mcp.mobius.waila.api.IWailaConfigHandler; +import mcp.mobius.waila.api.IWailaDataAccessor; +import mcp.mobius.waila.api.IWailaDataProvider; +import mcp.mobius.waila.api.SpecialChars; +import mcp.mobius.waila.cbcore.LangUtil; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +public class HUDHandlerCache implements IWailaDataProvider { + + @Override + public ItemStack getWailaStack(IWailaDataAccessor accessor, IWailaConfigHandler config) { return null; } + + @Override + public List getWailaHead(ItemStack itemStack, List currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) { + if(!config.getConfig("thermalexpansion.cache")) + return currenttip; + try { + ItemStack storedItem = null; + if(accessor.getNBTData().hasKey("Item")) + storedItem = readItemStack(accessor.getNBTData().getCompoundTag("Item")); + + String name = currenttip.get(0); + String color = ""; + if(name.startsWith("\u00a7")) + color = name.substring(0, 2); + + if(storedItem != null) { + String namex = GameData.getItemRegistry().getNameForObject(storedItem.getItem()); + name += String.format(" < " + SpecialChars.getRenderString("waila.stack", "1", namex, "0", String.valueOf(storedItem.getItemDamage())) + color + " %s >", storedItem.getDisplayName()); + } + else + name += " " + LangUtil.translateG("hud.msg.empty"); + + currenttip.set(0, name); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return currenttip; + } + + @Override + public List getWailaBody(ItemStack itemStack, List currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) { + if (!config.getConfig("thermalexpansion.cache")) + return currenttip; + + NBTTagCompound tag = accessor.getNBTData(); + ItemStack storedItem = null; + if(tag.hasKey("Item")) + storedItem = readItemStack(tag.getCompoundTag("Item")); + + int stored = 0; + int maxStored = 0; + if(tag.hasKey("Stored")) + stored = tag.getInteger("Stored"); + if(tag.hasKey("MaxStored")) + maxStored = tag.getInteger("MaxStored"); + + if(storedItem != null) { + currenttip.add("Stored: " + stored + "/" + maxStored); + } + else + currenttip.add("Capacity: " + maxStored); + + + return currenttip; + } + + @Override + public List getWailaTail(ItemStack itemStack, List currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) { + return currenttip; + } + + @Override + public NBTTagCompound getNBTData(EntityPlayerMP player, TileEntity te, NBTTagCompound tag, World world, int x, int y, int z) { + if(te != null) + te.writeToNBT(tag); + try { + tag.setInteger("MaxStored", (Integer) ThermalExpansionModule.TileCache_getMaxStored.invoke(te)); + tag.setInteger("Stored", (Integer) ThermalExpansionModule.TileCache_getStored.invoke(te)); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return tag; + } + + public ItemStack readItemStack(NBTTagCompound tag) { + ItemStack is = new ItemStack(Item.getItemById(tag.getShort("id"))); + is.stackSize = tag.getInteger("Count"); + is.setItemDamage(Math.max(0, tag.getShort("Damage"))); + if (tag.hasKey("tag", 10)) { + is.stackTagCompound = tag.getCompoundTag("tag"); + } + + return is; + } + +} diff --git a/src/main/java/mcp/mobius/waila/addons/thermalexpansion/ThermalExpansionModule.java b/src/main/java/mcp/mobius/waila/addons/thermalexpansion/ThermalExpansionModule.java index 38d3443..218bbce 100644 --- a/src/main/java/mcp/mobius/waila/addons/thermalexpansion/ThermalExpansionModule.java +++ b/src/main/java/mcp/mobius/waila/addons/thermalexpansion/ThermalExpansionModule.java @@ -1,16 +1,12 @@ package mcp.mobius.waila.addons.thermalexpansion; +import mcp.mobius.waila.Waila; +import mcp.mobius.waila.api.impl.ModuleRegistrar; +import net.minecraftforge.common.util.ForgeDirection; +import org.apache.logging.log4j.Level; + import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.List; - -import org.apache.logging.log4j.Level; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.world.IBlockAccess; -import net.minecraftforge.common.util.ForgeDirection; -import mcp.mobius.waila.Waila; -import mcp.mobius.waila.api.impl.ModuleRegistrar; public class ThermalExpansionModule { @@ -44,6 +40,11 @@ public class ThermalExpansionModule { public static Class ISecureTile = null; public static Method ISecureTile_getAccess = null; public static Method ISecureTile_getOwnerName = null; + + public static Class TileCache = null; + public static Method TileCache_getItemStack = null; + public static Method TileCache_getMaxStored = null; + public static Method TileCache_getStored = null; public static Class BlockMultipart = null; @@ -76,15 +77,15 @@ public class ThermalExpansionModule { } catch (Exception e){ Waila.log.log(Level.WARN, "[Thermal Expansion] Error while loading Energy hooks." + e); - } + } // XXX : We register the energy cell try{ - TileEnergyCell = Class.forName("thermalexpansion.block.cell.TileCell"); + TileEnergyCell = Class.forName("cofh.thermalexpansion.block.cell.TileCell"); TileEnergyCell_Recv = TileEnergyCell.getDeclaredField("energyReceive"); TileEnergyCell_Send = TileEnergyCell.getDeclaredField("energySend"); - ModuleRegistrar.instance().addConfigRemote("Thermal Expansion", "thermalexpansion.energycell"); + ModuleRegistrar.instance().addConfigRemote("Thermal Expansion", "thermalexpansion.energycell"); ModuleRegistrar.instance().registerBodyProvider(new HUDHandlerEnergyCell(), TileEnergyCell); ModuleRegistrar.instance().registerNBTProvider (new HUDHandlerEnergyCell(), TileEnergyCell); @@ -94,7 +95,7 @@ public class ThermalExpansionModule { // XXX : We register the Tank interface try{ - TileTank = Class.forName("thermalexpansion.block.tank.TileTank"); + TileTank = Class.forName("cofh.thermalexpansion.block.tank.TileTank"); TileTank_getTankFluid = TileTank.getMethod("getTankFluid"); TileTank_getTankCapacity = TileTank.getMethod("getTankCapacity"); TileTank_getTankAmount = TileTank.getMethod("getTankFluidAmount"); @@ -113,7 +114,7 @@ public class ThermalExpansionModule { // XXX : We register the Tesseract interface try{ - TileTesseract = Class.forName("thermalexpansion.block.ender.TileTesseract"); + TileTesseract = Class.forName("cofh.thermalexpansion.block.ender.TileTesseract"); TileTesseract_Item = TileTesseract.getDeclaredField("modeItem"); TileTesseract_Fluid = TileTesseract.getDeclaredField("modeFluid"); TileTesseract_Energy = TileTesseract.getDeclaredField("modeEnergy"); @@ -141,9 +142,25 @@ public class ThermalExpansionModule { } catch (Exception e){ Waila.log.log(Level.WARN, "[Thermal Expansion] Error while loading ISecureTile hooks." + e); - } - - // XXX : We register the IBlockInfo interface + } + + // XXX : We register the Cache interface + try{ + TileCache = Class.forName("cofh.thermalexpansion.block.cache.TileCache"); + TileCache_getItemStack = TileCache.getDeclaredMethod("getStoredItemType"); + TileCache_getMaxStored = TileCache.getDeclaredMethod("getMaxStoredCount"); + TileCache_getStored = TileCache.getDeclaredMethod("getStoredCount"); + + ModuleRegistrar.instance().addConfigRemote("Thermal Expansion", "thermalexpansion.cache"); + ModuleRegistrar.instance().registerHeadProvider(new HUDHandlerCache(), TileCache); + ModuleRegistrar.instance().registerBodyProvider(new HUDHandlerCache(), TileCache); + ModuleRegistrar.instance().registerNBTProvider (new HUDHandlerCache(), TileCache); + + } catch (Exception e){ + Waila.log.log(Level.WARN, "[Thermal Expansion] Error while loading Tesseract hooks." + e); + } + + // XXX : We register the IBlockInfo interface /* try{ IBlockInfo = Class.forName("cofh.api.block.IBlockInfo");