Merge pull request #246 from tcooc/network-builders

Fixes for architect and bpt library
This commit is contained in:
SirSengir 2012-09-12 12:07:29 -07:00
commit 6b557f1d99
11 changed files with 330 additions and 87 deletions

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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,17 @@ public class TileArchitect extends TileBuildCraft implements IInventory {
return result;
}
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() {

View file

@ -1,12 +1,17 @@
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.PacketUpdate;
import buildcraft.core.network.TileNetworkData;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import net.minecraft.src.EntityPlayer;
@ -15,7 +20,11 @@ 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 static final int COMMAND_NEXT = 1,
COMMAND_PREV = 2,
COMMAND_LOCK_UPDATE = 3,
COMMAND_DELETE = 4;
public ItemStack[] stack = new ItemStack[4];
@ -24,12 +33,29 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory {
public String owner = "";
public BptBase selected = null;
private ArrayList<BptBase> currentPage;
public boolean locked = false;
public @TileNetworkData(staticSize=BuildCraftBuilders.LIBRARY_PAGE_SIZE) String[] currentNames = new String[BuildCraftBuilders.LIBRARY_PAGE_SIZE];
public @TileNetworkData int selected = -1;
public LinkedList<BptBase> getNextPage(String after) {
LinkedList<BptBase> result = new LinkedList<BptBase>();
public @TileNetworkData boolean locked = false;
public TileBlueprintLibrary(){
for(int i = 0; i < currentNames.length; i++){
currentNames[i] = "";
}
}
@Override
public void initialize() {
super.initialize();
if(CoreProxy.proxy.isSimulating(worldObj)){
setCurrentPage(getNextPage(null));
}
}
public ArrayList<BptBase> getNextPage(String after) {
ArrayList<BptBase> result = new ArrayList<BptBase>();
BptPlayerIndex index = BuildCraftBuilders.getPlayerIndex(BuildersProxy.getOwner(this));
@ -52,8 +78,8 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory {
return result;
}
public LinkedList<BptBase> getPrevPage(String before) {
LinkedList<BptBase> result = new LinkedList<BptBase>();
public ArrayList<BptBase> getPrevPage(String before) {
ArrayList<BptBase> result = new ArrayList<BptBase>();
BptPlayerIndex index = BuildCraftBuilders.getPlayerIndex(BuildersProxy.getOwner(this));
@ -75,6 +101,53 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory {
return result;
}
public void updateCurrentNames(){
currentNames = new String[BuildCraftBuilders.LIBRARY_PAGE_SIZE];
for(int i = 0; i < currentPage.size(); i++){
currentNames[i] = currentPage.get(i).getName();
}
for(int i = currentPage.size(); i < currentNames.length; i++){
currentNames[i] = "";
}
sendNetworkUpdate();
}
public ArrayList<BptBase> getCurrentPage(){
return currentPage;
}
public void setCurrentPage(ArrayList<BptBase> newPage){
currentPage = newPage;
selected = -1;
updateCurrentNames();
}
public void setCurrentPage(boolean nextPage) {
int index = 0;
if (nextPage) {
index = currentPage.size() - 1;
}
if (currentPage.size() > 0) {
setCurrentPage(getNextPage(currentPage.get(index).file.getName()));
} else {
setCurrentPage(getNextPage(null));
}
}
public void deleteSelectedBpt(){
BptPlayerIndex index = BuildCraftBuilders.getPlayerIndex(BuildersProxy.getOwner(this));
if (selected > -1 && selected < currentPage.size()) {
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) {
@ -174,6 +247,9 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory {
@Override
public void updateEntity() {
super.updateEntity();
if(CoreProxy.proxy.isRenderWorld(worldObj)) return;
if (progressIn > 0 && progressIn < 100) {
progressIn++;
}
@ -192,6 +268,8 @@ public class TileBlueprintLibrary extends TileEntity implements IInventory {
try {
index.addBlueprint(bpt.file);
setCurrentPage(true);
setCurrentPage(false);
} catch (IOException e) {
e.printStackTrace();
}
@ -199,8 +277,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 && selected < currentPage.size()) {
setInventorySlotContents(3, new ItemStack(stack[2].itemID, 1, currentPage.get(selected).position));
} else {
setInventorySlotContents(3, new ItemStack(stack[2].itemID, 1, 0));
}

View file

@ -21,9 +21,7 @@ import net.minecraft.src.IInventory;
import net.minecraft.src.Slot;
public class ContainerBlueprintLibrary extends BuildCraftContainer {
public LinkedList<BptBase> contents = new LinkedList<BptBase>();
protected IInventory playerInventory;
protected TileBlueprintLibrary library;

View file

@ -19,10 +19,16 @@ import cpw.mods.fml.client.FMLClientHandler;
import buildcraft.BuildCraftBuilders;
import buildcraft.builders.TileBlueprintLibrary;
import buildcraft.builders.network.PacketLibraryAction;
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 {
@ -30,8 +36,6 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
EntityPlayer player;
TileBlueprintLibrary library;
int highlighted;
ContainerBlueprintLibrary container;
boolean computeInput;
@ -47,7 +51,6 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
this.library = library;
container = (ContainerBlueprintLibrary) inventorySlots;
container.contents = library.getNextPage(null);
index = BuildCraftBuilders.getPlayerIndex(player.username);
}
@ -75,11 +78,11 @@ 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)
if (library.locked) {
lockButton.displayString = StringUtil.localize("gui.unlock");
else
} else {
lockButton.displayString = StringUtil.localize("gui.lock");
}
}
@Override
@ -90,16 +93,19 @@ 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++) {
String name = currentNames[i];
if(name == null) break;
if(name.length() > BuildCraftBuilders.MAX_BLUEPRINTS_NAME_SIZE){
name = name.substring(0, BuildCraftBuilders.MAX_BLUEPRINTS_NAME_SIZE);
}
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);
fontRenderer.drawString(name, 9, 25 + 9 * c, 0x404040);
c++;
@ -125,21 +131,34 @@ 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) {
PacketLibraryAction packet = new PacketLibraryAction(PacketIds.LIBRARY_ACTION,
library.xCoord, library.yCoord, library.zCoord);
if (button == nextPageButton) {
packet.actionId = TileBlueprintLibrary.COMMAND_NEXT;
} else if (button == prevPageButton) {
packet.actionId = TileBlueprintLibrary.COMMAND_PREV;
} else if (lockButton != null && button == lockButton) {
packet.actionId = TileBlueprintLibrary.COMMAND_LOCK_UPDATE;
} else if (deleteButton != null && button == deleteButton) {
packet.actionId = TileBlueprintLibrary.COMMAND_DELETE;
}
CoreProxy.proxy.sendToServer(packet.getPacket());
}
@Override
protected void mouseClicked(int i, int j, int k) {
@ -155,35 +174,16 @@ 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){
PacketPayload payload = new PacketPayload();
payload.intPayload = new int[]{ySlot};
PacketLibraryAction packet = new PacketLibraryAction(PacketIds.LIBRARY_SELECT,
library.xCoord, library.yCoord, library.zCoord);
packet.actionId = ySlot;
CoreProxy.proxy.sendToServer(packet.getPacket());
}
}
}
}
}

View file

@ -19,6 +19,10 @@ import buildcraft.BuildCraftBuilders;
import buildcraft.builders.TileArchitect;
import buildcraft.core.DefaultProps;
import buildcraft.core.gui.GuiBuildCraft;
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 GuiTemplate extends GuiBuildCraft {
@ -77,24 +81,18 @@ 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);
PacketPayload payload = new PacketPayload();
payload.intPayload = new int[]{c};
PacketUpdate packet = new PacketUpdate(PacketIds.ARCHITECT_NAME,
template.xCoord, template.yCoord, template.zCoord, payload);
CoreProxy.proxy.sendToServer(packet.getPacket());
} else {
super.keyTyped(c, i);
}
}
}

View file

@ -0,0 +1,93 @@
package buildcraft.builders.network;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.NetworkManager;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.TileEntity;
import buildcraft.builders.TileArchitect;
import buildcraft.builders.TileBlueprintLibrary;
import buildcraft.core.network.PacketCoordinates;
import buildcraft.core.network.PacketIds;
import buildcraft.core.network.PacketUpdate;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
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_ACTION:
PacketLibraryAction packetB = new PacketLibraryAction();
packetB.readData(data);
onLibraryAction((EntityPlayer)player, packetB);
break;
case PacketIds.LIBRARY_SELECT:
PacketLibraryAction packetC = new PacketLibraryAction();
packetC.readData(data);
onLibrarySelect((EntityPlayer)player, packetC);
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 onLibraryAction(EntityPlayer player, PacketLibraryAction packet) {
TileEntity te = player.worldObj.getBlockTileEntity(packet.posX,
packet.posY, packet.posZ);
if(te instanceof TileBlueprintLibrary) {
TileBlueprintLibrary tbl = (TileBlueprintLibrary) te;
switch(packet.actionId){
case TileBlueprintLibrary.COMMAND_DELETE:
tbl.deleteSelectedBpt();
break;
case TileBlueprintLibrary.COMMAND_LOCK_UPDATE:
tbl.locked = !tbl.locked;
tbl.sendNetworkUpdate();
break;
case TileBlueprintLibrary.COMMAND_NEXT:
tbl.setCurrentPage(true);
break;
case TileBlueprintLibrary.COMMAND_PREV:
tbl.setCurrentPage(false);
break;
}
}
}
private void onLibrarySelect(EntityPlayer player, PacketLibraryAction packet) {
TileEntity te = player.worldObj.getBlockTileEntity(packet.posX,
packet.posY, packet.posZ);
if(te instanceof TileBlueprintLibrary){
TileBlueprintLibrary tbl = (TileBlueprintLibrary) te;
int ySlot = packet.actionId;
if (ySlot < tbl.getCurrentPage().size()){
tbl.selected = ySlot;
}
tbl.sendNetworkUpdate();
}
}
}

View file

@ -0,0 +1,31 @@
package buildcraft.builders.network;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import buildcraft.core.network.PacketCoordinates;
import buildcraft.core.network.PacketPayload;
public class PacketLibraryAction extends PacketCoordinates {
public int actionId;
public PacketLibraryAction() {}
public PacketLibraryAction(int packetId, int x, int y, int z) {
super(packetId, x, y, z);
}
@Override
public void writeData(DataOutputStream data) throws IOException {
data.writeInt(actionId);
super.writeData(data);
}
@Override
public void readData(DataInputStream data) throws IOException {
actionId = data.readInt();
super.readData(data);
}
}

View file

@ -67,6 +67,7 @@ public class ClassMapping {
private LinkedList<Field> intArrayFields = new LinkedList<Field>();
private LinkedList<Field> booleanArrayFields = new LinkedList<Field>();
private LinkedList<Field> unsignedByteArrayFields = new LinkedList<Field>();
private LinkedList<Field> stringArrayFields = new LinkedList<Field>();
private LinkedList<ClassMapping> objectArrayFields = new LinkedList<ClassMapping>();
private int sizeBytes;
@ -173,6 +174,9 @@ public class ClassMapping {
sizeBytes += updateAnnotation.staticSize() * 4;
intArrayFields.add(f);
}
} else if (cptClass.equals(String.class)) {
sizeString += updateAnnotation.staticSize();
stringArrayFields.add(f);
} else if (cptClass.equals(boolean.class)) {
sizeBytes += updateAnnotation.staticSize();
booleanArrayFields.add(f);
@ -346,6 +350,17 @@ public class ClassMapping {
r.dataInt += 1;
}
}
for (Field f : stringArrayFields) {
TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class);
for (int i = 0; i < updateAnnotation.staticSize(); ++i) {
stringValues[index.stringIndex] = ((String[]) f.get(obj))[i];
r.bytes += stringValues[index.stringIndex].length();
index.stringIndex++;
r.dataString += 1;
}
}
for (ClassMapping c : objectArrayFields) {
TileNetworkData updateAnnotation = c.field.getAnnotation(TileNetworkData.class);
@ -509,6 +524,19 @@ public class ClassMapping {
r.dataInt += 1;
}
}
for (Field f : stringArrayFields) {
TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class);
String[] strs = (String[]) f.get(obj);
for (int i = 0; i < updateAnnotation.staticSize(); ++i) {
strs[i] = stringValues[index.stringIndex];
r.bytes += stringValues[index.stringIndex].length();
index.stringIndex++;
r.dataString += 1;
}
}
for (ClassMapping c : objectArrayFields) {
TileNetworkData updateAnnotation = c.field.getAnnotation(TileNetworkData.class);

View file

@ -19,6 +19,9 @@ 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_ACTION = 61;
public static final int LIBRARY_SELECT = 62;
public static final int STATE_UPDATE = 100;
}