diff --git a/src/main/java/gregtechmod/api/gui/GT_GUIContainer.java b/src/main/java/gregtechmod/api/gui/GT_GUIContainer.java index 19e74fe..5b6c58a 100644 --- a/src/main/java/gregtechmod/api/gui/GT_GUIContainer.java +++ b/src/main/java/gregtechmod/api/gui/GT_GUIContainer.java @@ -8,6 +8,7 @@ import com.google.common.collect.Lists; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import gregtechmod.api.util.GT_Log; import gregtechmod.api.util.GT_Utility; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; @@ -25,55 +26,72 @@ import net.minecraftforge.fluids.FluidStack; */ public class GT_GUIContainer extends GuiContainer { + + public boolean mCrashed = false; protected FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer; + public ResourceLocation mGUIbackground; public GT_GUIContainer(Container aContainer, String aGUIbackground) { super(aContainer); mGUIbackground = new ResourceLocation(aGUIbackground); + } @SideOnly(Side.CLIENT) protected void drawTooltips(int posX, int posY) { - List fluids = ((GT_Container)inventorySlots).fluidSlots; - for (GT_FluidSlot slot : fluids) { - if (this.func_146978_c(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, posX, posY)) { - GL11.glPushAttrib(GL11.GL_COLOR_BUFFER_BIT); - { - List tooltipData = Lists.newArrayList(); - FluidStack fluid = slot.getFluid(); - if (GT_Utility.isFluidStackValid(fluid)) { - tooltipData.add(fluid.getLocalizedName()); - tooltipData.add(EnumChatFormatting.GRAY + I18n.format("metatileentity.fluid.amount", GT_Utility.parseNumberToString(fluid.amount))); - } else { - tooltipData.add(I18n.format("metatileentity.fluid.empty")); + if (inventorySlots instanceof GT_Container) { + List fluids = ((GT_Container)inventorySlots).fluidSlots; + for (GT_FluidSlot slot : fluids) { + if (this.func_146978_c(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, posX, posY)) { + GL11.glPushAttrib(GL11.GL_COLOR_BUFFER_BIT); + { + List tooltipData = Lists.newArrayList(); + FluidStack fluid = slot.getFluid(); + if (GT_Utility.isFluidStackValid(fluid)) { + tooltipData.add(fluid.getLocalizedName()); + tooltipData.add(EnumChatFormatting.GRAY + I18n.format("metatileentity.fluid.amount", GT_Utility.parseNumberToString(fluid.amount))); + } else { + tooltipData.add(I18n.format("metatileentity.fluid.empty")); + } + + if (slot.canFill) + tooltipData.add(EnumChatFormatting.GRAY.toString() + EnumChatFormatting.ITALIC.toString() + I18n.format("metatileentity.fluid.fill")); + if (slot.canDrain) + tooltipData.add(EnumChatFormatting.GRAY.toString() + EnumChatFormatting.ITALIC.toString() + I18n.format("metatileentity.fluid.drain")); + + this.func_146283_a(tooltipData, posX, posY); } - - if (slot.canFill) - tooltipData.add(EnumChatFormatting.GRAY.toString() + EnumChatFormatting.ITALIC.toString() + I18n.format("metatileentity.fluid.fill")); - if (slot.canDrain) - tooltipData.add(EnumChatFormatting.GRAY.toString() + EnumChatFormatting.ITALIC.toString() + I18n.format("metatileentity.fluid.drain")); - - this.func_146283_a(tooltipData, posX, posY); - } - GL11.glPopAttrib(); + GL11.glPopAttrib(); + } } } } @Override protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) { - for (GT_FluidSlot slot : ((GT_Container)inventorySlots).fluidSlots) - slot.draw(this.guiLeft, this.guiTop, this.func_146978_c(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, par2, par3)); + if (inventorySlots instanceof GT_Container) { + for (GT_FluidSlot slot : ((GT_Container)inventorySlots).fluidSlots) + slot.draw(this.guiLeft, this.guiTop, this.func_146978_c(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, par2, par3)); + } + mc.renderEngine.bindTexture(mGUIbackground); } @Override public void drawScreen(int par1, int par2, float par3) { + if (mCrashed) { + this.mc.thePlayer.closeScreen(); + return; + } + try { super.drawScreen(par1, par2, par3); this.drawTooltips(par1, par2); - } catch(Throwable e) {} + } catch(Throwable e) { + mCrashed = true; + GT_Log.log.catching(new RuntimeException("Crash occured while rendering UI, it will be closed", e)); + } } } diff --git a/src/main/java/gregtechmod/common/containers/GT_Container_Item_Destructopack.java b/src/main/java/gregtechmod/common/containers/GT_Container_Item_Destructopack.java index 89d1997..0d8e0b1 100644 --- a/src/main/java/gregtechmod/common/containers/GT_Container_Item_Destructopack.java +++ b/src/main/java/gregtechmod/common/containers/GT_Container_Item_Destructopack.java @@ -2,12 +2,16 @@ package gregtechmod.common.containers; import gregtechmod.api.gui.GT_Slot_Holo; import gregtechmod.api.util.GT_Utility; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidContainerRegistry; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.IFluidContainerItem; public class GT_Container_Item_Destructopack extends Container { @@ -53,29 +57,36 @@ public class GT_Container_Item_Destructopack extends Container { @Override public ItemStack slotClick(int aSlotIndex, int aMouseclick, int aShifthold, EntityPlayer aPlayer) { if (aSlotIndex < 0) return super.slotClick(aSlotIndex, aMouseclick, aShifthold, aPlayer); + + ItemStack ret = null; if (aSlotIndex < 1) { if (aPlayer.inventory.getItemStack() != null) { - if (aMouseclick == 0) { - if (aShifthold == 1) { - for (int i = 0; i < aPlayer.inventory.getSizeInventory(); i++) { - ItemStack tStack = aPlayer.inventory.getStackInSlot(i); - if (tStack != null) { - if (GT_Utility.areStacksEqual(tStack, aPlayer.inventory.getItemStack())) { - aPlayer.inventory.setInventorySlotContents(i, null); - } - } - } - } - aPlayer.inventory.setItemStack(null); - } else if (aPlayer.inventory.getItemStack().stackSize < 2) { - aPlayer.inventory.setItemStack(null); - } else { - aPlayer.inventory.getItemStack().stackSize--; - return aPlayer.inventory.getItemStack(); + + if (aShifthold == 1 || (ret = emtpyContainer(aPlayer.inventory.getItemStack(), aPlayer, aMouseclick == 0)) == null) { + if (aMouseclick == 0) { + if (aShifthold == 1) { + for (int i = 0; i < aPlayer.inventory.getSizeInventory(); i++) { + ItemStack tStack = aPlayer.inventory.getStackInSlot(i); + if (tStack != null) { + if (GT_Utility.areStacksEqual(tStack, aPlayer.inventory.getItemStack())) { + aPlayer.inventory.setInventorySlotContents(i, null); + } + } + } + } + aPlayer.inventory.setItemStack(null); + } else if (aPlayer.inventory.getItemStack().stackSize < 2) { + aPlayer.inventory.setItemStack(null); + } else { + aPlayer.inventory.getItemStack().stackSize--; + ret = aPlayer.inventory.getItemStack(); + } } } - return null; + + return ret; } + return super.slotClick(aSlotIndex, aMouseclick, aShifthold, aPlayer); } @@ -85,4 +96,45 @@ public class GT_Container_Item_Destructopack extends Container { slotObject.putStack(null); return null; } + + protected ItemStack emtpyContainer(ItemStack held, EntityPlayer player, boolean leftClick) { + ItemStack result = null; + + if (held.stackSize > 1) { + ItemStack copy = this.getContainer(GT_Utility.copyAmount(1, held)); + if (copy != null) { + if (leftClick) { + copy.stackSize = held.stackSize; + player.inventory.setItemStack(copy); + result = copy; + } else { + held.stackSize--; + player.inventory.addItemStackToInventory(copy); + result = held; + } + } + } else { + result = getContainer(held); + if (result != null) + player.inventory.setItemStack(result); + } + + return result; + } + + /** + * Gets an empty container of stack + * @return null if no fluid in stack, otherwise will return container stack + */ + public ItemStack getContainer(ItemStack item) { + if (FluidContainerRegistry.isContainer(item)) + return FluidContainerRegistry.drainFluidContainer(item); + else if (item.getItem() instanceof IFluidContainerItem) { + FluidStack fl = ((IFluidContainerItem)item.getItem()).drain(item, Integer.MAX_VALUE, true); + if (fl != null && fl.amount > 0) + return item; + } + + return null; + } } diff --git a/src/main/java/gregtechmod/common/gui/GT_GUIContainer_Destructopack.java b/src/main/java/gregtechmod/common/gui/GT_GUIContainer_Destructopack.java index 689c766..b129b6a 100644 --- a/src/main/java/gregtechmod/common/gui/GT_GUIContainer_Destructopack.java +++ b/src/main/java/gregtechmod/common/gui/GT_GUIContainer_Destructopack.java @@ -1,21 +1,58 @@ package gregtechmod.common.gui; +import java.lang.reflect.Field; +import java.util.Collections; + +import cpw.mods.fml.relauncher.ReflectionHelper; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + import gregtechmod.api.GregTech_API; import gregtechmod.api.gui.GT_GUIContainer; +import gregtechmod.api.gui.GT_Slot_Holo; import gregtechmod.api.util.GT_LanguageManager; import gregtechmod.common.containers.GT_Container_Item_Destructopack; + +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; -import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fluids.FluidContainerRegistry; +import net.minecraftforge.fluids.IFluidContainerItem; + public class GT_GUIContainer_Destructopack extends GT_GUIContainer { - public ResourceLocation mGUIbackground; + protected static Field theSlot; - public GT_GUIContainer_Destructopack(InventoryPlayer aInventoryPlayer, ItemStack aItem) { + public GT_GUIContainer_Destructopack(InventoryPlayer aInventoryPlayer, ItemStack aItem) { super(new GT_Container_Item_Destructopack(aInventoryPlayer, aItem), GregTech_API.GUI_PATH + "Destructopack.png"); } + + static { + try { + theSlot = ReflectionHelper.findField(GuiContainer.class, "field_147006_u", "theSlot"); + } catch (Throwable e) { + theSlot = null; + } + } + + @Override + @SideOnly(Side.CLIENT) + protected void drawTooltips(int posX, int posY) { + super.drawTooltips(posX, posY); + + if (theSlot != null) { + try { + ItemStack stack = this.mc.thePlayer.inventory.getItemStack(); + if (stack != null && theSlot.get(this) instanceof GT_Slot_Holo && (FluidContainerRegistry.isContainer(stack) || stack.getItem() instanceof IFluidContainerItem)) { + this.func_146283_a(Collections.singletonList(I18n.format("item.destructpack.fluid_info")), posX, posY); + } + } catch (Throwable e) {} + } + } + @Override protected void drawGuiContainerForegroundLayer(int par1, int par2) { //draw text and stuff here diff --git a/src/main/resources/assets/gregtech_addon/lang/en_US.lang b/src/main/resources/assets/gregtech_addon/lang/en_US.lang index f6adef1..ae98508 100644 --- a/src/main/resources/assets/gregtech_addon/lang/en_US.lang +++ b/src/main/resources/assets/gregtech_addon/lang/en_US.lang @@ -1007,6 +1007,7 @@ item.GT_SensorCard_Item.tooltip=Insert into Display Panel item.GT_SensorCard_Item.tooltip.info.1=Device at: item.GT_SensorCard_Item.tooltip.info.2=x: %d y: %d z: %d item.GT_SensorCard_Item.tooltip.info.missing=Missing Coordinates! +item.destructpack.fluid_info=Also working with fluid containers! gt_crops.indigo=Indigo gt_crops.flax=Flax diff --git a/src/main/resources/assets/gregtech_addon/lang/ru_RU.lang b/src/main/resources/assets/gregtech_addon/lang/ru_RU.lang index 5b6de08..75c017d 100644 --- a/src/main/resources/assets/gregtech_addon/lang/ru_RU.lang +++ b/src/main/resources/assets/gregtech_addon/lang/ru_RU.lang @@ -1415,6 +1415,7 @@ item.GT_SensorCard_Item.tooltip=Ввести в панель дисплея item.GT_SensorCard_Item.tooltip.info.1=Устройство на: item.GT_SensorCard_Item.tooltip.info.2=x: %d y: %d z: %d item.GT_SensorCard_Item.tooltip.info.missing=Координаты утеряны! +item.destructpack.fluid_info=Так же работает с жидкостными контейнерами! gt_crops.indigo=Индиго gt_crops.flax=Лён