From 9ae784309ee2917d7b7f80fa2bceec24e381e2eb Mon Sep 17 00:00:00 2001 From: tcooc Date: Wed, 12 Sep 2012 01:09:11 -0500 Subject: [PATCH] Architects create bpts properly now + some bpt library fixes --- common/buildcraft/BuildCraftBuilders.java | 5 +- .../builders/BlockBlueprintLibrary.java | 3 +- common/buildcraft/builders/TileArchitect.java | 29 +++- .../builders/TileBlueprintLibrary.java | 60 ++++++-- .../gui/ContainerBlueprintLibrary.java | 4 +- .../builders/gui/GuiBlueprintLibrary.java | 106 +++++++------- .../buildcraft/builders/gui/GuiTemplate.java | 22 +-- .../network/PacketHandlerBuilders.java | 134 ++++++++++++++++++ common/buildcraft/core/network/PacketIds.java | 7 +- 9 files changed, 283 insertions(+), 87 deletions(-) create mode 100644 common/buildcraft/builders/network/PacketHandlerBuilders.java diff --git a/common/buildcraft/BuildCraftBuilders.java b/common/buildcraft/BuildCraftBuilders.java index ebb31b09..aec05d7a 100644 --- a/common/buildcraft/BuildCraftBuilders.java +++ b/common/buildcraft/BuildCraftBuilders.java @@ -69,6 +69,7 @@ import buildcraft.builders.TileBuilder; import buildcraft.builders.TileFiller; import buildcraft.builders.TileMarker; import buildcraft.builders.TilePathMarker; +import buildcraft.builders.network.PacketHandlerBuilders; import buildcraft.core.DefaultProps; import buildcraft.core.Version; import buildcraft.core.blueprints.BptPlayerIndex; @@ -84,12 +85,12 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.Property; @Mod(name="BuildCraft Builders", version=Version.VERSION, useMetadata = false, modid = "BuildCraft|Builders", dependencies = DefaultProps.DEPENDENCY_CORE) -@NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, packetHandler = PacketHandler.class, clientSideRequired = true, serverSideRequired = true) +@NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, packetHandler = PacketHandlerBuilders.class, clientSideRequired = true, serverSideRequired = true) public class BuildCraftBuilders { public static final int LIBRARY_PAGE_SIZE = 12; - public static final int MAX_BLUEPRINTS_NAME_SIZE = 88; + public static final int MAX_BLUEPRINTS_NAME_SIZE = 14; public static BlockMarker markerBlock; public static BlockPathMarker pathMarkerBlock; diff --git a/common/buildcraft/builders/BlockBlueprintLibrary.java b/common/buildcraft/builders/BlockBlueprintLibrary.java index b36f42a9..df953142 100644 --- a/common/buildcraft/builders/BlockBlueprintLibrary.java +++ b/common/buildcraft/builders/BlockBlueprintLibrary.java @@ -74,9 +74,8 @@ public class BlockBlueprintLibrary extends BlockContainer { @Override public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving) { - if (entityliving instanceof EntityPlayer) { + if (CoreProxy.proxy.isSimulating(world) && entityliving instanceof EntityPlayer) { TileBlueprintLibrary tile = (TileBlueprintLibrary) world.getBlockTileEntity(i, j, k); - tile.owner = ((EntityPlayer) entityliving).username; } } diff --git a/common/buildcraft/builders/TileArchitect.java b/common/buildcraft/builders/TileArchitect.java index 128914e6..9905a54d 100644 --- a/common/buildcraft/builders/TileArchitect.java +++ b/common/buildcraft/builders/TileArchitect.java @@ -19,10 +19,13 @@ import buildcraft.core.blueprints.BptBase; import buildcraft.core.blueprints.BptBlueprint; import buildcraft.core.blueprints.BptContext; import buildcraft.core.blueprints.BptTemplate; +import buildcraft.core.network.PacketIds; +import buildcraft.core.network.PacketPayload; import buildcraft.core.network.PacketUpdate; import buildcraft.core.network.TileNetworkData; import buildcraft.core.proxy.CoreProxy; import buildcraft.core.utils.Utils; +import buildcraft.factory.TileAssemblyTable; import net.minecraft.src.EntityPlayer; import net.minecraft.src.IInventory; import net.minecraft.src.ItemStack; @@ -39,7 +42,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory { private boolean isComputing = false; public int computingTime = 0; - public String name = ""; + public @TileNetworkData String name = ""; // Use that field to avoid creating several times the same template if // they're the same! @@ -49,7 +52,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory { public void updateEntity() { super.updateEntity(); - if (isComputing) { + if (CoreProxy.proxy.isSimulating(worldObj) && isComputing) { if (computingTime < 200) { computingTime++; } else { @@ -174,6 +177,28 @@ public class TileArchitect extends TileBuildCraft implements IInventory { return result; } + + public void sendClientInput(char c){ + PacketUpdate packet = new PacketUpdate(PacketIds.ARCHITECT_NAME); + PacketPayload payload = new PacketPayload(); + packet.payload = payload; + packet.posX = xCoord; + packet.posY = yCoord; + packet.posZ = zCoord; + payload.intPayload = new int[]{c}; + CoreProxy.proxy.sendToServer(packet.getPacket()); + } + + public void handleClientInput(char c){ + if (c == 8) { + if (name.length() > 0) + name = name.substring(0, name.length() - 1); + } else if (Character.isLetterOrDigit(c) || c == ' ') { + if (name.length() < BuildCraftBuilders.MAX_BLUEPRINTS_NAME_SIZE) + name += c; + } + sendNetworkUpdate(); + } @Override public int getSizeInventory() { diff --git a/common/buildcraft/builders/TileBlueprintLibrary.java b/common/buildcraft/builders/TileBlueprintLibrary.java index 84edfd97..805d020a 100644 --- a/common/buildcraft/builders/TileBlueprintLibrary.java +++ b/common/buildcraft/builders/TileBlueprintLibrary.java @@ -1,12 +1,15 @@ package buildcraft.builders; import java.io.IOException; +import java.util.ArrayList; import java.util.LinkedList; import buildcraft.BuildCraftBuilders; import buildcraft.builders.BuildersProxy; +import buildcraft.core.TileBuildCraft; import buildcraft.core.blueprints.BptBase; import buildcraft.core.blueprints.BptPlayerIndex; +import buildcraft.core.network.TileNetworkData; import buildcraft.core.utils.Utils; import net.minecraft.src.EntityPlayer; @@ -15,7 +18,7 @@ import net.minecraft.src.ItemStack; import net.minecraft.src.NBTTagCompound; import net.minecraft.src.TileEntity; -public class TileBlueprintLibrary extends TileEntity implements IInventory { +public class TileBlueprintLibrary extends TileBuildCraft implements IInventory { public ItemStack[] stack = new ItemStack[4]; @@ -24,12 +27,20 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory { public String owner = ""; - public BptBase selected = null; + private ArrayList currentPage; + public @TileNetworkData(staticSize=BuildCraftBuilders.LIBRARY_PAGE_SIZE) String[] currentNames = new String[BuildCraftBuilders.LIBRARY_PAGE_SIZE]; + public @TileNetworkData int selected = -1; - public boolean locked = false; + public @TileNetworkData boolean locked = false; - public LinkedList getNextPage(String after) { - LinkedList result = new LinkedList(); + @Override + public void initialize() { + super.initialize(); + setCurrentPage(getNextPage(null)); + } + + public ArrayList getNextPage(String after) { + ArrayList result = new ArrayList(); BptPlayerIndex index = BuildCraftBuilders.getPlayerIndex(BuildersProxy.getOwner(this)); @@ -52,8 +63,8 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory { return result; } - public LinkedList getPrevPage(String before) { - LinkedList result = new LinkedList(); + public ArrayList getPrevPage(String before) { + ArrayList result = new ArrayList(); BptPlayerIndex index = BuildCraftBuilders.getPlayerIndex(BuildersProxy.getOwner(this)); @@ -75,6 +86,37 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory { return result; } + + public void updateCurrentNames(){ + currentNames = new String[BuildCraftBuilders.LIBRARY_PAGE_SIZE]; + for(int i = 0; i < currentNames.length; i++){ + currentNames[i] = currentPage.get(i).getName(); + } + sendNetworkUpdate(); + } + + public ArrayList getCurrentPage(){ + return currentPage; + } + + public void setCurrentPage(ArrayList newPage){ + currentPage = newPage; + updateCurrentNames(); + } + + public void deleteSelectedBpt(EntityPlayer player){ + BptPlayerIndex index = BuildCraftBuilders.getPlayerIndex(player.username); + if (selected > -1) { + index.deleteBluePrint(currentPage.get(selected).file.getName()); + if (currentPage.size() > 0) { + currentPage = getNextPage(index.prevBpt(currentPage.get(0).file.getName())); + } else { + currentPage = getNextPage(null); + } + selected = -1; + updateCurrentNames(); + } + } @Override public void readFromNBT(NBTTagCompound nbttagcompound) { @@ -199,8 +241,8 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory { } if (progressOut == 100 && stack[3] == null) { - if (selected != null) { - setInventorySlotContents(3, new ItemStack(stack[2].itemID, 1, selected.position)); + if (selected > -1) { + setInventorySlotContents(3, new ItemStack(stack[2].itemID, 1, currentPage.get(selected).position)); } else { setInventorySlotContents(3, new ItemStack(stack[2].itemID, 1, 0)); } diff --git a/common/buildcraft/builders/gui/ContainerBlueprintLibrary.java b/common/buildcraft/builders/gui/ContainerBlueprintLibrary.java index a311a77c..ae78956e 100644 --- a/common/buildcraft/builders/gui/ContainerBlueprintLibrary.java +++ b/common/buildcraft/builders/gui/ContainerBlueprintLibrary.java @@ -21,9 +21,7 @@ import net.minecraft.src.IInventory; import net.minecraft.src.Slot; public class ContainerBlueprintLibrary extends BuildCraftContainer { - - public LinkedList contents = new LinkedList(); - + protected IInventory playerInventory; protected TileBlueprintLibrary library; diff --git a/common/buildcraft/builders/gui/GuiBlueprintLibrary.java b/common/buildcraft/builders/gui/GuiBlueprintLibrary.java index e42e2d74..885b4592 100644 --- a/common/buildcraft/builders/gui/GuiBlueprintLibrary.java +++ b/common/buildcraft/builders/gui/GuiBlueprintLibrary.java @@ -23,6 +23,11 @@ import buildcraft.core.DefaultProps; import buildcraft.core.blueprints.BptBase; import buildcraft.core.blueprints.BptPlayerIndex; import buildcraft.core.gui.GuiBuildCraft; +import buildcraft.core.network.PacketCoordinates; +import buildcraft.core.network.PacketIds; +import buildcraft.core.network.PacketPayload; +import buildcraft.core.network.PacketUpdate; +import buildcraft.core.proxy.CoreProxy; import buildcraft.core.utils.StringUtil; public class GuiBlueprintLibrary extends GuiBuildCraft { @@ -47,7 +52,6 @@ public class GuiBlueprintLibrary extends GuiBuildCraft { this.library = library; container = (ContainerBlueprintLibrary) inventorySlots; - container.contents = library.getNextPage(null); index = BuildCraftBuilders.getPlayerIndex(player.username); } @@ -75,11 +79,6 @@ public class GuiBlueprintLibrary extends GuiBuildCraft { lockButton = new GuiButton(3, j + 127, k + 114, 40, 20, StringUtil.localize("gui.lock")); controlList.add(lockButton); - - if (library.locked) - lockButton.displayString = StringUtil.localize("gui.unlock"); - else - lockButton.displayString = StringUtil.localize("gui.lock"); } @Override @@ -90,16 +89,15 @@ public class GuiBlueprintLibrary extends GuiBuildCraft { fontRenderer.drawString(title, getCenteredOffset(title), 6, 0x404040); int c = 0; - for (BptBase bpt : container.contents) { - if (bpt == library.selected) { + String[] currentNames = library.currentNames; + for (int i = 0; i < currentNames.length; i++) { + if (i == library.selected) { int l1 = 8; int i2 = 24; drawGradientRect(l1, i2 + 9 * c, l1 + 88, i2 + 9 * (c + 1), 0x80ffffff, 0x80ffffff); } - String name = bpt.getName(); - - while (fontRenderer.getStringWidth(name) > BuildCraftBuilders.MAX_BLUEPRINTS_NAME_SIZE) - name = name.substring(0, name.length() - 1); + String name = currentNames[i]; + name = name.substring(0, BuildCraftBuilders.MAX_BLUEPRINTS_NAME_SIZE); fontRenderer.drawString(name, 9, 25 + 9 * c, 0x404040); c++; @@ -125,21 +123,43 @@ public class GuiBlueprintLibrary extends GuiBuildCraft { int inP = (int) (library.progressIn / 100.0 * 22.0); int outP = (int) (library.progressOut / 100.0 * 22.0); - if (inP != 0) - computeInput = true; - else if (computeInput) { - // In this case, there was a store computation that has finished. - if (container.contents.size() == 0) - container.contents = library.getNextPage(null); - else - container.contents = library.getNextPage(index.prevBpt(container.contents.getFirst().file.getName())); - - computeInput = false; - } - drawTexturedModalRect(j + 128 + 22 - inP, k + 61, 176 + 22 - inP, 16, inP, 16); drawTexturedModalRect(j + 128, k + 78, 176, 0, outP, 16); } + + @Override + public void updateScreen(){ + if (library.locked) { + lockButton.displayString = StringUtil.localize("gui.unlock"); + } else { + lockButton.displayString = StringUtil.localize("gui.lock"); + } + } + + @Override + protected void actionPerformed(GuiButton button) { + if (button == nextPageButton) { + PacketUpdate packet = new PacketUpdate(PacketIds.LIBRARY_COMMAND); + PacketPayload payload = new PacketPayload(); + packet.payload = payload; + payload.intPayload = new int[]{0}; + CoreProxy.proxy.sendToServer(packet.getPacket()); + } else if (button == prevPageButton) { + PacketUpdate packet = new PacketUpdate(PacketIds.LIBRARY_COMMAND); + PacketPayload payload = new PacketPayload(); + packet.payload = payload; + payload.intPayload = new int[]{1}; + CoreProxy.proxy.sendToServer(packet.getPacket()); + } else if (lockButton != null && button == lockButton) { + PacketCoordinates packet = new PacketCoordinates(PacketIds.LIBRARY_LOCK_UPDATE, + library.xCoord, library.yCoord, library.zCoord); + CoreProxy.proxy.sendToServer(packet.getPacket()); + } else if (deleteButton != null && button == deleteButton) { + PacketCoordinates packet = new PacketCoordinates(PacketIds.LIBRARY_DELETE, + library.xCoord, library.yCoord, library.zCoord); + CoreProxy.proxy.sendToServer(packet.getPacket()); + } + } @Override protected void mouseClicked(int i, int j, int k) { @@ -155,35 +175,15 @@ public class GuiBlueprintLibrary extends GuiBuildCraft { if (x >= 8 && x <= 88) { int ySlot = (y - 24) / 9; - if (ySlot >= 0 && ySlot <= 11) - if (ySlot < container.contents.size()) - library.selected = container.contents.get(ySlot); - } else if (nextPageButton.mousePressed(client, i, j)) { - if (container.contents.size() > 0) - container.contents = library.getNextPage(container.contents.getLast().file.getName()); - else - container.contents = library.getNextPage(null); - } else if (prevPageButton.mousePressed(client, i, j)) { - if (container.contents.size() > 0) - container.contents = library.getPrevPage(container.contents.getFirst().file.getName()); - else - container.contents = library.getNextPage(null); - } else if (lockButton != null && lockButton.mousePressed(client, i, j)) { - library.locked = !library.locked; - - if (library.locked) - lockButton.displayString = StringUtil.localize("gui.unlock"); - else - lockButton.displayString = StringUtil.localize("gui.lock"); - } else if (deleteButton != null && deleteButton.mousePressed(client, i, j)) - if (library.selected != null) { - index.deleteBluePrint(library.selected.file.getName()); - if (container.contents.size() > 0) - container.contents = library.getNextPage(index.prevBpt(container.contents.getFirst().file.getName())); - else - container.contents = library.getNextPage(null); - - library.selected = null; + if (ySlot >= 0 && ySlot <= 11){ + if (ySlot < library.currentNames.length){ + PacketUpdate packet = new PacketUpdate(PacketIds.LIBRARY_SELECT); + PacketPayload payload = new PacketPayload(); + packet.payload = payload; + payload.intPayload = new int[]{ySlot}; + CoreProxy.proxy.sendToServer(packet.getPacket()); + } } + } } } diff --git a/common/buildcraft/builders/gui/GuiTemplate.java b/common/buildcraft/builders/gui/GuiTemplate.java index 9db91543..caaf8a7b 100644 --- a/common/buildcraft/builders/gui/GuiTemplate.java +++ b/common/buildcraft/builders/gui/GuiTemplate.java @@ -19,6 +19,8 @@ import buildcraft.BuildCraftBuilders; import buildcraft.builders.TileArchitect; import buildcraft.core.DefaultProps; import buildcraft.core.gui.GuiBuildCraft; +import buildcraft.core.network.PacketUpdate; +import buildcraft.core.proxy.CoreProxy; import buildcraft.core.utils.StringUtil; public class GuiTemplate extends GuiBuildCraft { @@ -77,24 +79,14 @@ public class GuiTemplate extends GuiBuildCraft { @Override protected void keyTyped(char c, int i) { - if (editMode) + if (i != 1 && editMode) { if (c == 13) { editMode = false; - - return; - } else if (c == 8) { - if (template.name.length() > 0) - template.name = template.name.substring(0, template.name.length() - 1); - - return; - } else if (Character.isLetterOrDigit(c) || c == ' ') { - if (fontRenderer.getStringWidth(template.name + c) <= BuildCraftBuilders.MAX_BLUEPRINTS_NAME_SIZE) - template.name += c; - return; } - - super.keyTyped(c, i); - + template.sendClientInput(c); + } else { + super.keyTyped(c, i); + } } } diff --git a/common/buildcraft/builders/network/PacketHandlerBuilders.java b/common/buildcraft/builders/network/PacketHandlerBuilders.java new file mode 100644 index 00000000..52c187f9 --- /dev/null +++ b/common/buildcraft/builders/network/PacketHandlerBuilders.java @@ -0,0 +1,134 @@ +package buildcraft.builders.network; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; + +import cpw.mods.fml.common.network.IPacketHandler; +import cpw.mods.fml.common.network.Player; + +import buildcraft.builders.TileArchitect; +import buildcraft.builders.TileBlueprintLibrary; +import buildcraft.core.network.PacketCoordinates; +import buildcraft.core.network.PacketIds; +import buildcraft.core.network.PacketPayload; +import buildcraft.core.network.PacketUpdate; +import buildcraft.core.proxy.CoreProxy; +import buildcraft.factory.TileAssemblyTable; +import buildcraft.factory.TileAssemblyTable.SelectionMessage; +import buildcraft.silicon.gui.ContainerAssemblyTable; + +import net.minecraft.src.Container; +import net.minecraft.src.EntityPlayer; +import net.minecraft.src.NetworkManager; +import net.minecraft.src.Packet250CustomPayload; +import net.minecraft.src.TileEntity; +import net.minecraft.src.World; + +public class PacketHandlerBuilders implements IPacketHandler { + + @Override + public void onPacketData(NetworkManager manager, Packet250CustomPayload packet, Player player) { + + DataInputStream data = new DataInputStream(new ByteArrayInputStream(packet.data)); + try { + int packetID = data.read(); + switch (packetID) { + case PacketIds.ARCHITECT_NAME: + PacketUpdate packetA = new PacketUpdate(); + packetA.readData(data); + onArchitectName((EntityPlayer)player, packetA); + break; + case PacketIds.LIBRARY_COMMAND: + PacketUpdate packetB = new PacketUpdate(); + packetB.readData(data); + onLibraryCommand((EntityPlayer)player, packetB); + break; + case PacketIds.LIBRARY_LOCK_UPDATE: + PacketCoordinates packetC = new PacketCoordinates(); + packetC.readData(data); + onLockUpdate((EntityPlayer)player, packetC); + break; + case PacketIds.LIBRARY_DELETE: + PacketCoordinates packetD = new PacketCoordinates(); + packetD.readData(data); + onDeleteBpt((EntityPlayer)player, packetD); + break; + case PacketIds.LIBRARY_SELECT: + PacketUpdate packetE = new PacketUpdate(); + packetE.readData(data); + onLibrarySelect((EntityPlayer)player, packetE); + break; + + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + } + + private void onArchitectName(EntityPlayer player, PacketUpdate packet) { + TileEntity te = player.worldObj.getBlockTileEntity(packet.posX, + packet.posY, packet.posZ); + if(te instanceof TileArchitect){ + ((TileArchitect) te).handleClientInput((char) packet.payload.intPayload[0]); + } + } + + private void onLibraryCommand(EntityPlayer player, PacketUpdate packet) { + TileEntity te = player.worldObj.getBlockTileEntity(packet.posX, + packet.posY, packet.posZ); + if(te instanceof TileArchitect) { + TileBlueprintLibrary tbl = (TileBlueprintLibrary) te; + boolean nextPage = packet.payload.intPayload[0] == 0; + if (nextPage) { + if (tbl.getCurrentPage().size() > 0) { + tbl.setCurrentPage(tbl.getNextPage(tbl.getCurrentPage().get(tbl.currentNames.length - 1).file.getName())); + } else { + tbl.setCurrentPage(tbl.getNextPage(null)); + } + } else { + if (tbl.getCurrentPage().size() > 0) { + tbl.setCurrentPage(tbl.getPrevPage(tbl.getCurrentPage().get(0).file.getName())); + } else { + tbl.setCurrentPage(tbl.getNextPage(null)); + } + } + tbl.sendNetworkUpdate(); + } + } + + private void onLibrarySelect(EntityPlayer player, PacketUpdate packet) { + TileEntity te = player.worldObj.getBlockTileEntity(packet.posX, + packet.posY, packet.posZ); + if(te instanceof TileBlueprintLibrary){ + TileBlueprintLibrary tbl = (TileBlueprintLibrary) te; + int ySlot = packet.payload.intPayload[0]; + if (ySlot < tbl.currentNames.length){ + tbl.selected = ySlot; + } + tbl.sendNetworkUpdate(); + } + } + + private void onLockUpdate(EntityPlayer player, PacketCoordinates packet) { + TileEntity te = player.worldObj.getBlockTileEntity(packet.posX, + packet.posY, packet.posZ); + if(te instanceof TileBlueprintLibrary){ + TileBlueprintLibrary tbl = (TileBlueprintLibrary) te; + tbl.locked = !tbl.locked; + tbl.sendNetworkUpdate(); + } + } + + private void onDeleteBpt(EntityPlayer player, PacketCoordinates packet) { + TileEntity te = player.worldObj.getBlockTileEntity(packet.posX, + packet.posY, packet.posZ); + if(te instanceof TileBlueprintLibrary){ + TileBlueprintLibrary tbl = (TileBlueprintLibrary) te; + tbl.deleteSelectedBpt(player); + tbl.sendNetworkUpdate(); + } + } + + +} diff --git a/common/buildcraft/core/network/PacketIds.java b/common/buildcraft/core/network/PacketIds.java index c015abe6..e3b0da34 100644 --- a/common/buildcraft/core/network/PacketIds.java +++ b/common/buildcraft/core/network/PacketIds.java @@ -19,6 +19,11 @@ public class PacketIds { public static final int GATE_SELECTION_CHANGE = 44; public static final int GATE_TRIGGERS = 45; public static final int REFINERY_FILTER_SET = 50; - + public static final int ARCHITECT_NAME = 60; + public static final int LIBRARY_COMMAND = 61; + public static final int LIBRARY_LOCK_UPDATE = 62; + public static final int LIBRARY_DELETE = 63; + public static final int LIBRARY_SELECT = 64; + public static final int STATE_UPDATE = 100; }