Started migration to common tile packets, in temporary package v2 for now

This commit is contained in:
Krapht 2012-08-14 00:25:19 +02:00
parent f3569a92ff
commit 796cf27f35
7 changed files with 199 additions and 11 deletions

View file

@ -3,19 +3,15 @@ package buildcraft.core.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.core.network.ISynchronizedTile;
import buildcraft.core.network.PacketIds;
import buildcraft.core.network.PacketTileUpdate;
import net.minecraft.src.ModLoader;
import net.minecraft.src.NetClientHandler;
import net.minecraft.src.NetworkManager;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;
import buildcraft.core.network.v2.ISyncedTile;
import buildcraft.core.network.v2.PacketTileState;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
public class PacketHandler implements IPacketHandler {
@ -46,6 +42,16 @@ public class PacketHandler implements IPacketHandler {
packetT.readData(data);
onTileUpdate(packetT);
break;
case PacketIds.STATE_UPDATE:
PacketTileState inPacket = new PacketTileState();
inPacket.readData(data);
World world = ModLoader.getMinecraftInstance().theWorld;
TileEntity tile = world.getBlockTileEntity(inPacket.posX, inPacket.posY, inPacket.posZ);
if (tile instanceof ISyncedTile){
inPacket.applyStates(data, (ISyncedTile) tile);
}
break;
}
} catch (Exception ex) {
ex.printStackTrace();

View file

@ -18,4 +18,6 @@ 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 STATE_UPDATE = 100;
}

View file

@ -0,0 +1,27 @@
package buildcraft.core.network.v2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* Implemented by classes representing serializable client state
* @author Krapht
*
*/
public interface IClientState {
/**
* Serializes the state to the stream
* @param data
* @throws IOException
*/
public void writeData(DataOutputStream data) throws IOException;
/**
* Deserializes the state from the stream
* @param data
* @throws IOException
*/
public void readData(DataInputStream data) throws IOException;
}

View file

@ -0,0 +1,22 @@
package buildcraft.core.network.v2;
/**
* Implemented by TileEntites
* @author Krapht
*
*/
public interface ISyncedTile {
/**
* called by the PacketHandler for each state contained in a StatePacket
* @param stateId
* @return an object that should be refreshed from the state
*/
public IClientState getStateInstance(byte stateId);
/**
* Called after a state has been updated
* @param stateId
*/
public void stateUpdated(byte stateId);
}

View file

@ -0,0 +1,75 @@
package buildcraft.core.network.v2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import buildcraft.core.network.PacketCoordinates;
import buildcraft.core.network.PacketIds;
public class PacketTileState extends PacketCoordinates {
private class StateWithId{
public byte stateId;
public IClientState state;
public StateWithId(byte stateId, IClientState state) {
this.stateId = stateId;
this.state = state;
}
}
private List<StateWithId> stateList = new LinkedList<StateWithId>();
/**
* Default constructor for incomming packets
*/
public PacketTileState(){}
/**
* Constructor for outgoing packets
* @param x, y, z - the coordinates the tile to sync
*/
public PacketTileState(int x, int y, int z) {
super(PacketIds.STATE_UPDATE, x, y, z);
}
@Override
public int getID() {
return PacketIds.STATE_UPDATE;
}
@Override
public void readData(DataInputStream data) throws IOException {
super.readData(data);
}
public void applyStates(DataInputStream data, ISyncedTile tile) throws IOException {
byte stateCount = data.readByte();
for (int i = 0; i < stateCount; i++) {
byte stateId = data.readByte();
tile.getStateInstance(stateId).readData(data);
tile.stateUpdated(stateId);
}
}
public void addStateForSerialization(byte stateId, IClientState state){
stateList.add(new StateWithId(stateId, state));
}
@Override
public void writeData(DataOutputStream data) throws IOException {
super.writeData(data);
data.writeByte(stateList.size());
for (StateWithId stateWithId : stateList){
data.writeByte(stateWithId.stateId);
stateWithId.state.writeData(data);
}
}
}

View file

@ -5,13 +5,14 @@ import java.io.DataOutputStream;
import java.io.IOException;
import buildcraft.core.DefaultProps;
import buildcraft.core.network.v2.IClientState;
import buildcraft.transport.utils.ConnectionMatrix;
import buildcraft.transport.utils.FacadeMatrix;
import buildcraft.transport.utils.TextureMatrix;
import buildcraft.transport.utils.WireMatrix;
public class PipeRenderState {
public class PipeRenderState implements IClientState {
private String textureFile = DefaultProps.TEXTURE_BLOCKS;
private boolean hasGate = false;
@ -87,6 +88,7 @@ public class PipeRenderState {
return dirty || pipeConnectionMatrix.isDirty() || textureMatrix.isDirty() || wireMatrix.isDirty() || facadeMatrix.isDirty();
}
@Override
public void writeData(DataOutputStream data) throws IOException {
data.writeUTF(textureFile);
data.writeBoolean(hasGate);
@ -97,6 +99,7 @@ public class PipeRenderState {
facadeMatrix.writeData(data);
}
@Override
public void readData(DataInputStream data) throws IOException {
textureFile = data.readUTF();
hasGate = data.readBoolean();

View file

@ -9,6 +9,9 @@
package buildcraft.transport;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import buildcraft.BuildCraftCore;
@ -43,6 +46,9 @@ import buildcraft.core.network.PacketPayload;
import buildcraft.core.network.PacketTileUpdate;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.network.TileNetworkData;
import buildcraft.core.network.v2.IClientState;
import buildcraft.core.network.v2.ISyncedTile;
import buildcraft.core.network.v2.PacketTileState;
import buildcraft.transport.network.PipeRenderStatePacket;
import net.minecraft.src.Block;
@ -52,9 +58,27 @@ import net.minecraft.src.Packet;
import net.minecraft.src.TileEntity;
public class TileGenericPipe extends TileEntity implements IPowerReceptor, ITankContainer, IPipeEntry,
IPipeTile, IOverrideDefaultTriggers, ITileBufferHolder, IPipeConnection, IDropControlInventory, IPipeRenderState {
IPipeTile, IOverrideDefaultTriggers, ITileBufferHolder, IPipeConnection, IDropControlInventory, IPipeRenderState,
ISyncedTile {
private class CoreState implements IClientState {
public int pipeId;
@Override
public void writeData(DataOutputStream data) throws IOException {
data.writeInt(pipeId);
}
@Override
public void readData(DataInputStream data) throws IOException {
pipeId = data.readInt();
}
}
private PipeRenderState renderState = new PipeRenderState();
private CoreState coreState = new CoreState();
public TileBuffer[] tileBuffer;
public boolean[] pipeConnectionsBuffer = new boolean[6];
@ -350,7 +374,13 @@ public class TileGenericPipe extends TileEntity implements IPowerReceptor, ITank
@Override
public Packet getAuxillaryInfoPacket() {
bindPipe();
PipeRenderStatePacket packet = new PipeRenderStatePacket(this.renderState, this.pipeId, xCoord, yCoord, zCoord);
// PipeRenderStatePacket packet = new PipeRenderStatePacket(this.renderState, this.pipeId, xCoord, yCoord, zCoord);
// return packet.getPacket();
//return super.getAuxillaryInfoPacket();
PacketTileState packet = new PacketTileState(this.xCoord, this.yCoord, this.zCoord);
packet.addStateForSerialization((byte )0, coreState);
packet.addStateForSerialization((byte) 1, renderState);
return packet.getPacket();
}
@ -551,4 +581,27 @@ public class TileGenericPipe extends TileEntity implements IPowerReceptor, ITank
public PipeRenderState getRenderState() {
return renderState;
}
@Override
public IClientState getStateInstance(byte stateId) {
switch(stateId){
case 0: return coreState;
case 1: return renderState;
}
throw new RuntimeException("Unknown state requested: " + stateId + " this is a bug!");
}
@Override
public void stateUpdated(byte stateId) {
if (!worldObj.isRemote) return;
switch (stateId){
case 0:
if (pipe == null && coreState.pipeId != 0){
initialize(BlockGenericPipe.createPipe(coreState.pipeId));
}
break;
}
worldObj.markBlockAsNeedsUpdate(xCoord, yCoord, zCoord);
}
}