Added ClassMapping for String[] and finished bpt library fixes

This commit is contained in:
tcooc 2012-09-12 12:03:09 -05:00
parent 9ae784309e
commit 2d40f13d5b
8 changed files with 165 additions and 118 deletions

View file

@ -177,18 +177,7 @@ 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)

View file

@ -9,7 +9,9 @@ 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;
@ -19,6 +21,10 @@ import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.TileEntity;
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];
@ -28,15 +34,24 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory {
public String owner = "";
private ArrayList<BptBase> currentPage;
public @TileNetworkData(staticSize=BuildCraftBuilders.LIBRARY_PAGE_SIZE) String[] currentNames = new String[BuildCraftBuilders.LIBRARY_PAGE_SIZE];
public @TileNetworkData int selected = -1;
public @TileNetworkData boolean locked = false;
public TileBlueprintLibrary(){
for(int i = 0; i < currentNames.length; i++){
currentNames[i] = "";
}
}
@Override
public void initialize() {
super.initialize();
setCurrentPage(getNextPage(null));
if(CoreProxy.proxy.isSimulating(worldObj)){
setCurrentPage(getNextPage(null));
}
}
public ArrayList<BptBase> getNextPage(String after) {
@ -89,9 +104,12 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory {
public void updateCurrentNames(){
currentNames = new String[BuildCraftBuilders.LIBRARY_PAGE_SIZE];
for(int i = 0; i < currentNames.length; i++){
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();
}
@ -101,12 +119,25 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory {
public void setCurrentPage(ArrayList<BptBase> newPage){
currentPage = newPage;
selected = -1;
updateCurrentNames();
}
public void deleteSelectedBpt(EntityPlayer player){
BptPlayerIndex index = BuildCraftBuilders.getPlayerIndex(player.username);
if (selected > -1) {
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()));
@ -216,6 +247,9 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory {
@Override
public void updateEntity() {
super.updateEntity();
if(CoreProxy.proxy.isRenderWorld(worldObj)) return;
if (progressIn > 0 && progressIn < 100) {
progressIn++;
}
@ -234,6 +268,8 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory {
try {
index.addBlueprint(bpt.file);
setCurrentPage(true);
setCurrentPage(false);
} catch (IOException e) {
e.printStackTrace();
}
@ -241,7 +277,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory {
}
if (progressOut == 100 && stack[3] == null) {
if (selected > -1) {
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

@ -19,6 +19,7 @@ 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;
@ -35,8 +36,6 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
EntityPlayer player;
TileBlueprintLibrary library;
int highlighted;
ContainerBlueprintLibrary container;
boolean computeInput;
@ -79,6 +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) {
lockButton.displayString = StringUtil.localize("gui.unlock");
} else {
lockButton.displayString = StringUtil.localize("gui.lock");
}
}
@Override
@ -91,13 +95,17 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
int c = 0;
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 = currentNames[i];
name = name.substring(0, BuildCraftBuilders.MAX_BLUEPRINTS_NAME_SIZE);
fontRenderer.drawString(name, 9, 25 + 9 * c, 0x404040);
c++;
@ -138,27 +146,18 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
@Override
protected void actionPerformed(GuiButton button) {
PacketLibraryAction packet = new PacketLibraryAction(PacketIds.LIBRARY_ACTION,
library.xCoord, library.yCoord, library.zCoord);
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());
packet.actionId = TileBlueprintLibrary.COMMAND_NEXT;
} 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());
packet.actionId = TileBlueprintLibrary.COMMAND_PREV;
} 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());
packet.actionId = TileBlueprintLibrary.COMMAND_LOCK_UPDATE;
} else if (deleteButton != null && button == deleteButton) {
PacketCoordinates packet = new PacketCoordinates(PacketIds.LIBRARY_DELETE,
library.xCoord, library.yCoord, library.zCoord);
CoreProxy.proxy.sendToServer(packet.getPacket());
packet.actionId = TileBlueprintLibrary.COMMAND_DELETE;
}
CoreProxy.proxy.sendToServer(packet.getPacket());
}
@Override
@ -177,10 +176,11 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
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};
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,8 @@ 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;
@ -84,7 +86,11 @@ public class GuiTemplate extends GuiBuildCraft {
editMode = false;
return;
}
template.sendClientInput(c);
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

@ -3,26 +3,17 @@ 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;
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 {
@ -38,27 +29,16 @@ public class PacketHandlerBuilders implements IPacketHandler {
packetA.readData(data);
onArchitectName((EntityPlayer)player, packetA);
break;
case PacketIds.LIBRARY_COMMAND:
PacketUpdate packetB = new PacketUpdate();
case PacketIds.LIBRARY_ACTION:
PacketLibraryAction packetB = new PacketLibraryAction();
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);
onLibraryAction((EntityPlayer)player, packetB);
break;
case PacketIds.LIBRARY_SELECT:
PacketUpdate packetE = new PacketUpdate();
packetE.readData(data);
onLibrarySelect((EntityPlayer)player, packetE);
PacketLibraryAction packetC = new PacketLibraryAction();
packetC.readData(data);
onLibrarySelect((EntityPlayer)player, packetC);
break;
}
} catch (Exception ex) {
ex.printStackTrace();
@ -74,61 +54,40 @@ public class PacketHandlerBuilders implements IPacketHandler {
}
}
private void onLibraryCommand(EntityPlayer player, PacketUpdate packet) {
private void onLibraryAction(EntityPlayer player, PacketLibraryAction packet) {
TileEntity te = player.worldObj.getBlockTileEntity(packet.posX,
packet.posY, packet.posZ);
if(te instanceof TileArchitect) {
if(te instanceof TileBlueprintLibrary) {
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));
}
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;
}
tbl.sendNetworkUpdate();
}
}
private void onLibrarySelect(EntityPlayer player, PacketUpdate packet) {
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.payload.intPayload[0];
if (ySlot < tbl.currentNames.length){
int ySlot = packet.actionId;
if (ySlot < tbl.getCurrentPage().size()){
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();
}
}
}

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

@ -20,10 +20,8 @@ public class PacketIds {
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 LIBRARY_ACTION = 61;
public static final int LIBRARY_SELECT = 62;
public static final int STATE_UPDATE = 100;
}