diff --git a/src/main/java/storagecraft/SC.java b/src/main/java/storagecraft/SC.java index c7b224e2d..dcc0d57dc 100644 --- a/src/main/java/storagecraft/SC.java +++ b/src/main/java/storagecraft/SC.java @@ -2,6 +2,7 @@ package storagecraft; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; +import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; @@ -15,6 +16,10 @@ import storagecraft.proxy.CommonProxy; @Mod(modid = SC.ID, version = SC.VERSION) public class SC { + public static class GUI { + public static final int CONTROLLER = 0; + } + public static final String ID = "storagecraft"; public static final String VERSION = "1.0"; public static final SimpleNetworkWrapper NETWORK = NetworkRegistry.INSTANCE.newSimpleChannel(ID); @@ -26,6 +31,8 @@ public class SC { }; @SidedProxy(clientSide = "storagecraft.proxy.ClientProxy", serverSide = "storagecraft.proxy.ServerProxy") public static CommonProxy PROXY; + @Instance + public static SC INSTANCE; @EventHandler public void preInit(FMLPreInitializationEvent e) { diff --git a/src/main/java/storagecraft/block/BlockController.java b/src/main/java/storagecraft/block/BlockController.java index 60ab2e76f..56933fa0c 100644 --- a/src/main/java/storagecraft/block/BlockController.java +++ b/src/main/java/storagecraft/block/BlockController.java @@ -3,8 +3,8 @@ package storagecraft.block; import net.minecraft.block.ITileEntityProvider; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.ChatComponentText; import net.minecraft.world.World; +import storagecraft.SC; import storagecraft.tile.TileController; public class BlockController extends BlockSC implements ITileEntityProvider { @@ -20,10 +20,7 @@ public class BlockController extends BlockSC implements ITileEntityProvider { @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) { if (!world.isRemote) { - TileController controller = (TileController) world.getTileEntity(x, y, z); - - player.addChatComponentMessage(new ChatComponentText("RF stored: " + controller.getEnergyStored(null))); - player.addChatComponentMessage(new ChatComponentText("RF/t usage: " + controller.getEnergyUsage())); + player.openGui(SC.INSTANCE, SC.GUI.CONTROLLER, world, x, y, z); } return true; diff --git a/src/main/java/storagecraft/gui/GuiController.java b/src/main/java/storagecraft/gui/GuiController.java new file mode 100644 index 000000000..8fa7be9a4 --- /dev/null +++ b/src/main/java/storagecraft/gui/GuiController.java @@ -0,0 +1,60 @@ +package storagecraft.gui; + +import java.util.ArrayList; +import java.util.List; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.inventory.Container; +import net.minecraft.util.ResourceLocation; +import org.lwjgl.opengl.GL11; +import storagecraft.tile.TileController; + +public class GuiController extends GuiContainer { + public static final ResourceLocation CONTROLLER_RESOURCE = new ResourceLocation("storagecraft:textures/gui/controller.png"); + + private TileController controller; + + public GuiController(Container container, TileController controller) { + super(container); + + this.controller = controller; + + this.xSize = 176; + this.ySize = 190; + } + + @Override + protected void drawGuiContainerBackgroundLayer(float renderPartialTicks, int mouseX, int mouseY) { + GL11.glColor3f(1.0F, 1.0F, 1.0F); + + mc.getTextureManager().bindTexture(CONTROLLER_RESOURCE); + + int x = (this.width - xSize) / 2; + int y = (this.height - ySize) / 2; + + drawTexturedModalRect(x, y, 0, 0, xSize, ySize); + + int barWidth = 16; + int barHeight = 59; + int barX = x + 17; + int barY = y + 25; + + int energy = controller.getEnergyStored(null); + int maxEnergy = controller.getMaxEnergyStored(null); + + int height = (int) ((float) energy / (float) maxEnergy * (float) barHeight); + + drawTexturedModalRect(barX, barY + barHeight - height, 178, 0, barWidth, height); + + fontRendererObj.drawString("Controller", x + 7, y + 7, 4210752); + fontRendererObj.drawString("Inventory", x + 7, y + 96, 4210752); + fontRendererObj.drawString("Energy usage: " + controller.getEnergyUsage() + " RF/t", x + 45, y + 24, 4210752); + + if (mouseX >= barX && mouseX <= barX + barWidth && mouseY >= barY && mouseY <= barY + barHeight) { + List lines = new ArrayList(); + + lines.add(energy + " / " + maxEnergy + " RF"); + + drawHoveringText(lines, mouseX, mouseY, fontRendererObj); + } + } +} diff --git a/src/main/java/storagecraft/gui/GuiHandler.java b/src/main/java/storagecraft/gui/GuiHandler.java new file mode 100644 index 000000000..4b2157375 --- /dev/null +++ b/src/main/java/storagecraft/gui/GuiHandler.java @@ -0,0 +1,38 @@ +package storagecraft.gui; + +import cpw.mods.fml.common.network.IGuiHandler; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import storagecraft.SC; +import storagecraft.inventory.ContainerController; +import storagecraft.tile.TileController; + +public class GuiHandler implements IGuiHandler { + private Container getContainer(int ID, EntityPlayer player) { + switch (ID) { + case SC.GUI.CONTROLLER: + return new ContainerController(player); + default: + return null; + } + } + + @Override + public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { + return getContainer(ID, player); + } + + @Override + public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { + TileEntity tile = world.getTileEntity(x, y, z); + + switch (ID) { + case SC.GUI.CONTROLLER: + return new GuiController(getContainer(ID, player), (TileController) tile); + default: + return null; + } + } +} diff --git a/src/main/java/storagecraft/inventory/ContainerController.java b/src/main/java/storagecraft/inventory/ContainerController.java new file mode 100644 index 000000000..3dd82f240 --- /dev/null +++ b/src/main/java/storagecraft/inventory/ContainerController.java @@ -0,0 +1,11 @@ +package storagecraft.inventory; + +import net.minecraft.entity.player.EntityPlayer; + +public class ContainerController extends ContainerSC { + public ContainerController(EntityPlayer player) { + super(player); + + addPlayerInventory(8, 108); + } +} diff --git a/src/main/java/storagecraft/inventory/ContainerSC.java b/src/main/java/storagecraft/inventory/ContainerSC.java new file mode 100644 index 000000000..eba9322d0 --- /dev/null +++ b/src/main/java/storagecraft/inventory/ContainerSC.java @@ -0,0 +1,40 @@ +package storagecraft.inventory; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +public class ContainerSC extends Container { + private EntityPlayer player; + + public ContainerSC(EntityPlayer player) { + this.player = player; + } + + protected void addPlayerInventory(int xInventory, int yInventory) { + int id = 0; + + for (int i = 0; i < 9; i++) { + addSlotToContainer(new Slot(player.inventory, id, xInventory + i * 18, yInventory + 4 + (3 * 18))); + id++; + } + + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 9; x++) { + addSlotToContainer(new Slot(player.inventory, id, xInventory + x * 18, yInventory + y * 18)); + id++; + } + } + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { + return null; + } + + @Override + public boolean canInteractWith(EntityPlayer player) { + return true; + } +} diff --git a/src/main/java/storagecraft/inventory/InventoryBasic.java b/src/main/java/storagecraft/inventory/InventoryBasic.java new file mode 100644 index 000000000..265e13e2b --- /dev/null +++ b/src/main/java/storagecraft/inventory/InventoryBasic.java @@ -0,0 +1,103 @@ +package storagecraft.inventory; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +public class InventoryBasic implements IInventory { + private ItemStack[] inventory; + private int inventorySize; + private String name; + + public InventoryBasic(String name, int size) { + this.name = name; + this.inventorySize = size; + this.inventory = new ItemStack[inventorySize]; + } + + public void setInventoryStack(ItemStack[] stack) { + this.inventory = stack; + } + + @Override + public int getSizeInventory() { + return inventory.length; + } + + @Override + public ItemStack getStackInSlot(int slotIndex) { + return inventory[slotIndex]; + } + + @Override + public ItemStack decrStackSize(int slotIndex, int decrementAmount) { + ItemStack itemStack = getStackInSlot(slotIndex); + if (itemStack != null) { + if (itemStack.stackSize <= decrementAmount) { + setInventorySlotContents(slotIndex, null); + } else { + itemStack = itemStack.splitStack(decrementAmount); + + if (itemStack.stackSize == 0) { + setInventorySlotContents(slotIndex, null); + } + } + } + return itemStack; + } + + @Override + public ItemStack getStackInSlotOnClosing(int slotIndex) { + ItemStack itemStack = getStackInSlot(slotIndex); + if (itemStack != null) { + setInventorySlotContents(slotIndex, null); + } + return itemStack; + } + + @Override + public void setInventorySlotContents(int slotIndex, ItemStack itemStack) { + if (itemStack != null && itemStack.stackSize > getInventoryStackLimit()) { + itemStack.stackSize = getInventoryStackLimit(); + } + + inventory[slotIndex] = itemStack; + } + + @Override + public String getInventoryName() { + return this.name; + } + + @Override + public boolean hasCustomInventoryName() { + return true; + } + + @Override + public int getInventoryStackLimit() { + return 64; + } + + @Override + public boolean isUseableByPlayer(EntityPlayer var1) { + return true; + } + + @Override + public void openInventory() { + } + + @Override + public void closeInventory() { + } + + @Override + public boolean isItemValidForSlot(int var1, ItemStack var2) { + return true; + } + + @Override + public void markDirty() { + } +} diff --git a/src/main/java/storagecraft/proxy/CommonProxy.java b/src/main/java/storagecraft/proxy/CommonProxy.java index 04f2227b8..ab6d2c27c 100644 --- a/src/main/java/storagecraft/proxy/CommonProxy.java +++ b/src/main/java/storagecraft/proxy/CommonProxy.java @@ -3,10 +3,12 @@ package storagecraft.proxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import storagecraft.SC; import storagecraft.SCBlocks; +import storagecraft.gui.GuiHandler; import storagecraft.network.MessageTileUpdate; import storagecraft.tile.TileCable; import storagecraft.tile.TileController; @@ -15,19 +17,21 @@ import storagecraft.tile.TileGrid; public class CommonProxy { public void preInit(FMLPreInitializationEvent e) { SC.NETWORK.registerMessage(MessageTileUpdate.class, MessageTileUpdate.class, 0, Side.CLIENT); - + + NetworkRegistry.INSTANCE.registerGuiHandler(SC.INSTANCE, new GuiHandler()); + GameRegistry.registerTileEntity(TileController.class, "controller"); GameRegistry.registerTileEntity(TileCable.class, "cable"); GameRegistry.registerTileEntity(TileGrid.class, "grid"); - + GameRegistry.registerBlock(SCBlocks.CONTROLLER, "controller"); GameRegistry.registerBlock(SCBlocks.CABLE, "cable"); GameRegistry.registerBlock(SCBlocks.GRID, "grid"); } - + public void init(FMLInitializationEvent e) { } - + public void postInit(FMLPostInitializationEvent e) { } } diff --git a/src/main/java/storagecraft/tile/TileController.java b/src/main/java/storagecraft/tile/TileController.java index b8e00ddf0..7bd29dde8 100644 --- a/src/main/java/storagecraft/tile/TileController.java +++ b/src/main/java/storagecraft/tile/TileController.java @@ -2,15 +2,17 @@ package storagecraft.tile; import cofh.api.energy.EnergyStorage; import cofh.api.energy.IEnergyHandler; +import io.netty.buffer.ByteBuf; import java.util.ArrayList; import java.util.List; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.ForgeDirection; -public class TileController extends TileSC implements IEnergyHandler { +public class TileController extends TileSC implements IEnergyHandler, INetworkTile { public static final int BASE_ENERGY_USAGE = 100; + private int energyUsage; private EnergyStorage storage = new EnergyStorage(32000); private List connectedMachines = new ArrayList(); private int ticks = 0; @@ -51,6 +53,12 @@ public class TileController extends TileSC implements IEnergyHandler { connectedMachines = machines; } } + + energyUsage = BASE_ENERGY_USAGE; + + for (IMachine machine : connectedMachines) { + energyUsage += machine.getEnergyUsage(); + } } storage.extractEnergy(getEnergyUsage(), false); @@ -103,12 +111,6 @@ public class TileController extends TileSC implements IEnergyHandler { } public int getEnergyUsage() { - int energyUsage = BASE_ENERGY_USAGE; - - for (IMachine machine : connectedMachines) { - energyUsage += machine.getEnergyUsage(); - } - return energyUsage; } @@ -120,4 +122,16 @@ public class TileController extends TileSC implements IEnergyHandler { public boolean isActive() { return storage.getEnergyStored() >= getEnergyUsage(); } + + @Override + public void fromBytes(ByteBuf buf) { + storage.setEnergyStored(buf.readInt()); + energyUsage = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) { + buf.writeInt(storage.getEnergyStored()); + buf.writeInt(energyUsage); + } } diff --git a/src/main/resources/assets/storagecraft/textures/gui/controller.png b/src/main/resources/assets/storagecraft/textures/gui/controller.png new file mode 100644 index 000000000..85b4b9ac7 Binary files /dev/null and b/src/main/resources/assets/storagecraft/textures/gui/controller.png differ