Fix gate sync to server

Fix gate values to client
This commit is contained in:
Krapht 2012-05-28 02:39:39 +02:00
parent 92f2ab1d1e
commit 44973bc2b1
4 changed files with 119 additions and 25 deletions

View file

@ -314,11 +314,11 @@ public class GuiGateInterface extends GuiAdvancedInterface {
} }
} }
_container.setTrigger(position, triggerSlot.trigger); _container.setTrigger(position, triggerSlot.trigger, true);
if (_container.getGateOrdinal() >= GateKind.AND_3.ordinal()) { if (_container.getGateOrdinal() >= GateKind.AND_3.ordinal()) {
((TriggerParameterSlot) slots [position + nbEntries * 2]).parameter = null; ((TriggerParameterSlot) slots [position + nbEntries * 2]).parameter = null;
_container.setTriggerParameter(position, null); _container.setTriggerParameter(position, null, true);
} }
} else if (slot instanceof ActionSlot) { } else if (slot instanceof ActionSlot) {
ActionSlot actionSlot = (ActionSlot) slot; ActionSlot actionSlot = (ActionSlot) slot;
@ -348,7 +348,7 @@ public class GuiGateInterface extends GuiAdvancedInterface {
} }
} }
_container.setAction(position - nbEntries, actionSlot.action); _container.setAction(position - nbEntries, actionSlot.action, true);
} else if (slot instanceof TriggerParameterSlot) { } else if (slot instanceof TriggerParameterSlot) {
TriggerSlot trigger = (TriggerSlot) slots [position - nbEntries * 2]; TriggerSlot trigger = (TriggerSlot) slots [position - nbEntries * 2];
@ -359,7 +359,7 @@ public class GuiGateInterface extends GuiAdvancedInterface {
param.set(mc.thePlayer.inventory.getItemStack()); param.set(mc.thePlayer.inventory.getItemStack());
((TriggerParameterSlot) slot).parameter = param; ((TriggerParameterSlot) slot).parameter = param;
_container.setTriggerParameter(position - nbEntries * 2, param); _container.setTriggerParameter(position - nbEntries * 2, param, true);
} }
} }
} }

View file

@ -60,6 +60,8 @@ public class PacketHandler implements IPacketHandler {
onGateTriggers(packet); onGateTriggers(packet);
break; break;
case PacketIds.GATE_SELECTION: case PacketIds.GATE_SELECTION:
packet.readData(data);
onGateSelection(packet);
break; break;
} }
@ -94,6 +96,19 @@ public class PacketHandler implements IPacketHandler {
((CraftingGateInterface)container).updateTriggers(packet); ((CraftingGateInterface)container).updateTriggers(packet);
} }
/**
* Handles received current gate selection on a gate
* @param packet
*/
private void onGateSelection(PacketUpdate packet) {
Container container = ModLoader.getMinecraftInstance().thePlayer.craftingInventory;
if (!(container instanceof CraftingGateInterface))
return;
((CraftingGateInterface)container).setSelection(packet);
}
/** /**
* Handles a pipe description packet. (Creates the pipe object client side if needed.) * Handles a pipe description packet. (Creates the pipe object client side if needed.)
* @param packet * @param packet

View file

@ -67,7 +67,7 @@ public class PacketHandler implements IPacketHandler {
if(!(playerEntity.craftingInventory instanceof CraftingGateInterface)) if(!(playerEntity.craftingInventory instanceof CraftingGateInterface))
return; return;
((CraftingGateInterface)playerEntity.craftingInventory).setSelection(packet); ((CraftingGateInterface)playerEntity.craftingInventory).handleSelectionChange(packet);
} }
/** /**

View file

@ -129,8 +129,35 @@ public class CraftingGateInterface extends BuildCraftContainer {
* Sets the currently selected actions and triggers according to packet. * Sets the currently selected actions and triggers according to packet.
* @param packet * @param packet
*/ */
public void setSelection(PacketUpdate packet) { public void setSelection(PacketUpdate packet){
PacketPayload payload = packet.payload;
int position = payload.intPayload[0];
if(payload.intPayload[1] >= 0 && payload.intPayload[1] < BuildCraftAPI.triggers.length){
setTrigger(position, BuildCraftAPI.triggers[payload.intPayload[1]], false);
//System.out.println("Trigger["+ position + "]: " + pipe.activatedTriggers[position].getDescription());
} else {
setTrigger(position, null, false);
//System.out.println("Trigger["+ position + "] clear!");
}
if(payload.intPayload[2] >= 0 && payload.intPayload[2] < BuildCraftAPI.actions.length){
setAction(position, BuildCraftAPI.actions[payload.intPayload[2]], false);
//System.out.println("Action["+ position + "]: " + pipe.activatedActions[position].getDescription());
} else {
setAction(position, null, false);
//System.out.println("Action["+ position + "] clear!");
}
int itemID = payload.intPayload[3];
if(itemID <= 0) {
setTriggerParameter(position, null, false);
return;
}
TriggerParameter param = new TriggerParameter();
param.set(new ItemStack(itemID, payload.intPayload[4], payload.intPayload[5]));
setTriggerParameter(position, param, false);
} }
public void sendSelectionChange(int position) { public void sendSelectionChange(int position) {
@ -142,6 +169,7 @@ public class CraftingGateInterface extends BuildCraftContainer {
payload.intPayload[1] = pipe.activatedTriggers[position].id; payload.intPayload[1] = pipe.activatedTriggers[position].id;
else else
payload.intPayload[1] = -1; payload.intPayload[1] = -1;
if(pipe.activatedActions[position] != null) if(pipe.activatedActions[position] != null)
payload.intPayload[2] = pipe.activatedActions[position].id; payload.intPayload[2] = pipe.activatedActions[position].id;
else else
@ -185,24 +213,31 @@ public class CraftingGateInterface extends BuildCraftContainer {
PacketPayload payload = packet.payload; PacketPayload payload = packet.payload;
int position = payload.intPayload[0]; int position = payload.intPayload[0];
if(payload.intPayload[1] >= 0 && payload.intPayload[1] < BuildCraftAPI.triggers.length) if(payload.intPayload[1] >= 0 && payload.intPayload[1] < BuildCraftAPI.triggers.length){
setTrigger(position, BuildCraftAPI.triggers[payload.intPayload[1]]); setTrigger(position, BuildCraftAPI.triggers[payload.intPayload[1]], true);
else //System.out.println("Trigger["+ position + "]: " + pipe.activatedTriggers[position].getDescription());
setTrigger(position, null); } else {
if(payload.intPayload[2] >= 0 && payload.intPayload[2] < BuildCraftAPI.actions.length) setTrigger(position, null, true);
setAction(position, BuildCraftAPI.actions[payload.intPayload[2]]); //System.out.println("Trigger["+ position + "] clear!");
else }
setAction(position, null);
if(payload.intPayload[2] >= 0 && payload.intPayload[2] < BuildCraftAPI.actions.length){
setAction(position, BuildCraftAPI.actions[payload.intPayload[2]], true);
//System.out.println("Action["+ position + "]: " + pipe.activatedActions[position].getDescription());
} else {
setAction(position, null, true);
//System.out.println("Action["+ position + "] clear!");
}
int itemID = payload.intPayload[3]; int itemID = payload.intPayload[3];
if(itemID <= 0) { if(itemID <= 0) {
setTriggerParameter(position, null); setTriggerParameter(position, null, true);
return; return;
} }
TriggerParameter param = new TriggerParameter(); TriggerParameter param = new TriggerParameter();
param.set(new ItemStack(itemID, payload.intPayload[4], payload.intPayload[5])); param.set(new ItemStack(itemID, payload.intPayload[4], payload.intPayload[5]));
setTriggerParameter(position, param); setTriggerParameter(position, param, true);
} }
/** /**
@ -250,7 +285,51 @@ public class CraftingGateInterface extends BuildCraftContainer {
* @param player * @param player
*/ */
public void sendSelection(EntityPlayer player) { public void sendSelection(EntityPlayer player) {
if (pipe == null || pipe.gate == null) return;
int positions = 0;
switch (pipe.gate.kind){
case Single:
positions = 1;
break;
case AND_2:
case OR_2:
positions = 2;
break;
case AND_3:
case OR_3:
positions = 4;
break;
case OR_4:
case AND_4:
positions = 8;
break;
}
for(int position = 0; position < positions; position++){
PacketPayload payload = new PacketPayload(6, 0, 0);
payload.intPayload[0] = position;
if(pipe.activatedTriggers[position] != null)
payload.intPayload[1] = pipe.activatedTriggers[position].id;
else
payload.intPayload[1] = -1;
if(pipe.activatedActions[position] != null)
payload.intPayload[2] = pipe.activatedActions[position].id;
else
payload.intPayload[2] = -1;
if(pipe.triggerParameters[position] != null && pipe.triggerParameters[position].stack != null) {
payload.intPayload[3] = pipe.triggerParameters[position].stack.itemID;
payload.intPayload[4] = pipe.triggerParameters[position].stack.stackSize;
payload.intPayload[5] = pipe.triggerParameters[position].stack.getItemDamage();
}
CoreProxy.sendToPlayer(player, new PacketUpdate(PacketIds.GATE_SELECTION, pipe.xCoord, pipe.yCoord, pipe.zCoord, payload));
}
//System.out.println("Sending current selection to player");
} }
/** TRIGGERS **/ /** TRIGGERS **/
@ -274,16 +353,16 @@ public class CraftingGateInterface extends BuildCraftContainer {
return pipe.isNearbyTriggerActive(trigger, parameter); return pipe.isNearbyTriggerActive(trigger, parameter);
} }
public void setTrigger(int position, Trigger trigger){ public void setTrigger(int position, Trigger trigger, boolean notify){
if(APIProxy.isRemote())
sendSelectionChange(position);
pipe.setTrigger(position, trigger); pipe.setTrigger(position, trigger);
if(APIProxy.isRemote() && notify)
sendSelectionChange(position);
} }
public void setTriggerParameter(int position, TriggerParameter parameter){ public void setTriggerParameter(int position, TriggerParameter parameter, boolean notify){
if(APIProxy.isRemote())
sendSelectionChange(position);
pipe.setTriggerParameter(position, parameter); pipe.setTriggerParameter(position, parameter);
if(APIProxy.isRemote() && notify)
sendSelectionChange(position);
} }
@ -304,10 +383,10 @@ public class CraftingGateInterface extends BuildCraftContainer {
return descending ? _potentialActions.descendingIterator() : _potentialActions.iterator(); return descending ? _potentialActions.descendingIterator() : _potentialActions.iterator();
} }
public void setAction(int position, Action action){ public void setAction(int position, Action action, boolean notify){
if(APIProxy.isRemote())
sendSelectionChange(position);
pipe.setAction(position, action); pipe.setAction(position, action);
if(APIProxy.isRemote() && notify)
sendSelectionChange(position);
} }
/** GATE INFORMATION **/ /** GATE INFORMATION **/