Java tutorial
/* * This class belongs to the source code of Advanced Chemistry. * It might depend on other Advanced Chemistry classes to work * properly. * * For the full license, please see the licence.txt in the source * directory. */ package rheel.ac.client.gui; import java.awt.Polygon; import java.util.ArrayList; import java.util.List; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import org.lwjgl.opengl.GL11; import rheel.ac.AdvancedChemistry; import rheel.ac.common.handler.GuiHandler; import rheel.ac.common.packet.PacketResearchItemAdd; import rheel.ac.common.tileentity.TileEntityResearch; import rheel.ac.common.util.StackUtils; import rheel.ac.common.util.TextureLoader; import cpw.mods.fml.common.network.PacketDispatcher; /** * @Author Rheel */ public class GuiResearchInventory extends GuiContainer { private final GuiResearch parent; private final EntityPlayer player; private final TileEntityResearch entity; private boolean isMouseOverParent = false; public GuiResearchInventory(GuiResearch parent, EntityPlayer player, TileEntityResearch entity, Container container) { super(container); this.xSize = 176; this.ySize = 100; this.parent = parent; this.player = player; this.entity = entity; } @Override public void drawScreen(int par1, int par2, float par3) { super.drawScreen(par1, par2, par3); } @Override protected void drawGuiContainerBackgroundLayer(float f, int i, int j) { GL11.glPushMatrix(); GL11.glColor3f(0.5F, 0.5F, 0.5F); this.parent.draw(this.mc, this.guiLeft - 100, this.guiTop - 46, true, this.guiLeft, this.guiTop); GL11.glPopMatrix(); this.zLevel += 99; this.mc.getTextureManager().bindTexture(TextureLoader.getGUITexture("Inventory.png")); GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); this.isMouseOverParent = this.isMouseOver(i - this.guiLeft, j - this.guiTop); } @Override protected void drawGuiContainerForegroundLayer(int par1, int par2) { GL11.glTranslatef(0, 0, 100); this.mc.fontRenderer.drawString("Inventory", 8, 6, 0x404040); if (this.isMouseOverParent && this.mc.thePlayer.inventory.getItemStack() == null) { final List<String> list = new ArrayList<String>(); list.add("Click to go back to the main GUI."); list.add(""); list.add("Drag valid items to drop them in "); list.add("the research table."); GL11.glTranslatef(-this.guiLeft, -this.guiTop, 0); this.drawHoveringText(list, par1, par2, this.fontRenderer); GL11.glTranslatef(this.guiLeft, this.guiTop, 0); } else if (this.isMouseOverParent && this.entity.hasItem(this.mc.thePlayer.inventory.getItemStack())) { final List<String> list = new ArrayList<String>(); list.add(String.format("Your Research Table already has \"%s\"", this.mc.thePlayer.inventory.getItemStack().getDisplayName())); GL11.glTranslatef(-this.guiLeft, -this.guiTop, 0); this.drawHoveringText(list, par1, par2, this.fontRenderer); GL11.glTranslatef(this.guiLeft, this.guiTop, 0); } else if (this.isMouseOverParent && !this.entity.hasSpaceForItem(this.mc.thePlayer.inventory.getItemStack())) { final List<String> list = new ArrayList<String>(); list.add("Your Research Table is full."); GL11.glTranslatef(-this.guiLeft, -this.guiTop, 0); this.drawHoveringText(list, par1, par2, this.fontRenderer); GL11.glTranslatef(this.guiLeft, this.guiTop, 0); } } private boolean isMouseOver(int x, int y) { if (new Polygon(new int[] { -97, -61, 118, 126, 0, 0 }, new int[] { 84, -27, -27, 0, 0, 84 }, 6).contains(x, y)) { return true; } return x == 0 && y == 0 || x == 0 && y == 1 || x == 1 && y == 0; } @Override protected void mouseClicked(int par1, int par2, int par3) { if (this.isMouseOverParent) { if (this.mc.thePlayer.inventory.getItemStack() == null) { this.player.openGui(AdvancedChemistry.instance, GuiHandler.GUI_RESEARCH, this.entity.worldObj, this.entity.xCoord, this.entity.yCoord, this.entity.zCoord); } else { if (this.entity.hasSpaceForItem(this.mc.thePlayer.inventory.getItemStack()) && !this.entity.hasItem(this.mc.thePlayer.inventory.getItemStack())) { final int itemX = par1 - 8 - this.guiLeft; final int itemY = par2 - 8 - this.guiTop; this.entity.addItem(StackUtils.copyWithNewSize(this.mc.thePlayer.inventory.getItemStack(), 1), itemX, itemY); PacketDispatcher.sendPacketToServer(new PacketResearchItemAdd(itemX, itemY).getPacket()); if (this.mc.thePlayer.inventory.getItemStack().stackSize == 1) { this.mc.thePlayer.inventory.setItemStack(null); } else { this.mc.thePlayer.inventory .setItemStack(StackUtils.copyWithNewSize(this.mc.thePlayer.inventory.getItemStack(), this.mc.thePlayer.inventory.getItemStack().stackSize - 1)); } } } } else { super.mouseClicked(par1, par2, par3); } } @Override protected void keyTyped(char par1, int par2) { if (par2 == 1 || par2 == this.mc.gameSettings.keyBindInventory.keyCode) { this.player.openGui(AdvancedChemistry.instance, GuiHandler.GUI_RESEARCH, this.entity.worldObj, this.entity.xCoord, this.entity.yCoord, this.entity.zCoord); } else { super.keyTyped(par1, par2); } } }