Fixed gate extension ids synchronization, for #1895.

Added a new concept of NetworkId, allowing to transfer ids over the network
instead of strings.
RPCs are now all handled the same way (except RPC Pipes, to be completed when
actually used).
This commit is contained in:
SpaceToad 2014-06-22 11:49:59 +02:00
parent ff0307702e
commit 9577c53313
25 changed files with 354 additions and 245 deletions

View file

@ -13,14 +13,9 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
public final class GateExpansions {
private static final Map<String, IGateExpansion> expansions = new HashMap<String, IGateExpansion>();
private static final BiMap<Byte, String> serverIDMap = HashBiMap.create();
private static final BiMap<Byte, String> clientIDMap = HashBiMap.create();
private static byte nextID = 0;
private GateExpansions() {
@ -32,37 +27,15 @@ public final class GateExpansions {
public static void registerExpansion(String identifier, IGateExpansion expansion) {
expansions.put(identifier, expansion);
serverIDMap.put(nextID++, identifier);
}
public static IGateExpansion getExpansion(String identifier) {
return expansions.get(identifier);
}
public static IGateExpansion getExpansionClient(int id) {
if (id < 0 || id >= 128) {
return null;
} else {
return expansions.get(clientIDMap.get((byte) id));
}
}
public static byte getServerExpansionID(String identifier) {
return serverIDMap.inverse().get(identifier);
}
public static Set<IGateExpansion> getExpansions() {
Set<IGateExpansion> set = new HashSet<IGateExpansion>();
set.addAll(expansions.values());
return set;
}
public static BiMap<Byte, String> getServerMap() {
return serverIDMap;
}
public static void setClientMap(BiMap<Byte, String> map) {
clientIDMap.clear();
clientIDMap.putAll(map);
}
}

View file

@ -82,6 +82,7 @@ import buildcraft.core.TickHandlerCoreClient;
import buildcraft.core.Version;
import buildcraft.core.network.BuildCraftChannelHandler;
import buildcraft.core.network.EntityIds;
import buildcraft.core.network.NetworkIdRegistry;
import buildcraft.core.network.PacketHandler;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.proxy.CoreProxy;
@ -312,6 +313,8 @@ public class BuildCraftCore extends BuildCraftMod {
channels = NetworkRegistry.INSTANCE.newChannel
(DefaultProps.NET_CHANNEL_NAME + "-CORE", new BuildCraftChannelHandler(), new PacketHandler());
NetworkIdRegistry.instance = new NetworkIdRegistry();
StatementManager.registerTriggerProvider(new DefaultTriggerProvider());
StatementManager.registerActionProvider(new DefaultActionProvider());

View file

@ -124,7 +124,7 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements ITil
public void addBuildingItem(BuildingItem item) {
buildersInAction.add(item);
RPCHandler.rpcBroadcastPlayers(worldObj, this, "launchItem", item);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "launchItem", item);
}
public final double energyAvailable() {

View file

@ -146,7 +146,7 @@ public class TileArchitect extends TileBuildCraft implements IInventory, IBoxPro
@RPC (RPCSide.SERVER)
public void handleClientSetName(String nameSet) {
name = nameSet;
RPCHandler.rpcBroadcastPlayers(worldObj, this, "setName", name);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setName", name);
}
@RPC

View file

@ -447,7 +447,7 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IFluid
if (!worldObj.isRemote) {
if (i == 0) {
RPCHandler.rpcBroadcastPlayers(worldObj, this, "setItemRequirements",
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setItemRequirements",
null, null);
iterateBpt(false);
}
@ -726,10 +726,10 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IFluid
stack.stackSize = 0;
}
RPCHandler.rpcBroadcastPlayers(worldObj, this, "setItemRequirements",
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setItemRequirements",
((BptBuilderBlueprint) bluePrintBuilder).neededItems, realSize);
} else {
RPCHandler.rpcBroadcastPlayers(worldObj, this, "setItemRequirements", null, null);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setItemRequirements", null, null);
}
}

View file

@ -14,7 +14,6 @@ import io.netty.channel.ChannelHandlerContext;
import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec;
import buildcraft.transport.network.PacketFluidUpdate;
import buildcraft.transport.network.PacketGateExpansionMap;
import buildcraft.transport.network.PacketPipeTransportItemStack;
import buildcraft.transport.network.PacketPipeTransportItemStackRequest;
import buildcraft.transport.network.PacketPipeTransportTraveler;
@ -30,17 +29,17 @@ public class BuildCraftChannelHandler extends FMLIndexedMessageToMessageCodec<Bu
addDiscriminator(4, PacketNBT.class);
addDiscriminator(5, PacketPowerUpdate.class);
addDiscriminator(6, PacketSlotChange.class);
addDiscriminator(7, PacketGateExpansionMap.class);
addDiscriminator(8, PacketGuiReturn.class);
addDiscriminator(9, PacketGuiWidget.class);
addDiscriminator(10, PacketPipeTransportItemStack.class);
addDiscriminator(11, PacketPipeTransportItemStackRequest.class);
addDiscriminator(12, PacketPipeTransportTraveler.class);
addDiscriminator(13, PacketUpdate.class);
addDiscriminator(14, PacketRPCTile.class);
addDiscriminator(15, PacketRPCPipe.class);
addDiscriminator(16, PacketRPCGui.class);
addDiscriminator(17, PacketRPCEntity.class);
addDiscriminator(7, PacketGuiReturn.class);
addDiscriminator(8, PacketGuiWidget.class);
addDiscriminator(9, PacketPipeTransportItemStack.class);
addDiscriminator(10, PacketPipeTransportItemStackRequest.class);
addDiscriminator(11, PacketPipeTransportTraveler.class);
addDiscriminator(12, PacketUpdate.class);
addDiscriminator(13, PacketRPCTile.class);
addDiscriminator(14, PacketRPCPipe.class);
addDiscriminator(15, PacketRPCGui.class);
addDiscriminator(16, PacketRPCEntity.class);
addDiscriminator(17, PacketRPCStatic.class);
}
@Override

View file

@ -0,0 +1,160 @@
package buildcraft.core.network;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.NetHandlerPlayServer;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent.ClientConnectedToServerEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent.ServerConnectionFromClientEvent;
import buildcraft.core.utils.Utils;
public class NetworkIdRegistry {
public static NetworkIdRegistry instance;
private static boolean isLocal = false;
private static boolean isMaster = false;
private static BiMap<String, Integer> idMap = HashBiMap.create();
private static LinkedList<EntityPlayerMP> playersToInitialize = new LinkedList<EntityPlayerMP>();
public NetworkIdRegistry() {
FMLCommonHandler.instance().bus().register(this);
}
public static void write(ByteBuf buf, String strId) {
if (!isMaster) {
if (!idMap.containsKey(strId)) {
buf.writeInt(-1);
Utils.writeUTF(buf, strId);
} else {
buf.writeInt(idMap.get(strId));
}
} else {
if (!idMap.containsKey(strId)) {
idMap.put(strId, idMap.size());
if (!isLocal) {
RPCHandler.rpcBroadcastAllPlayers(NetworkIdRegistry.class, "receiveId", strId, idMap.size() - 1);
}
}
buf.writeInt(idMap.get(strId));
}
}
public static String read(ByteBuf buf) {
int id = buf.readInt();
if (!isMaster) {
if (!idMap.inverse().containsKey(id)) {
RPCHandler.rpcServer(NetworkIdRegistry.class, "requestId", id);
throw new IllegalArgumentException("Id " + id + " unknown by the registry.");
} else {
return idMap.inverse().get(id);
}
} else {
if (id == -1) {
String str = Utils.readUTF(buf);
if (!idMap.containsKey(str)) {
idMap.put(str, idMap.size());
}
return str;
} else {
return idMap.inverse().get(id);
}
}
}
@RPC(RPCSide.SERVER)
private static void requestId(int id, RPCMessageInfo info) {
if (!idMap.inverse().containsKey(id)) {
throw new IllegalArgumentException("Id " + id + " unknown by the registry.");
} else {
RPCHandler.rpcPlayer(info.sender, NetworkIdRegistry.class, "receiveId", idMap.inverse().get(id), id);
}
}
@RPC(RPCSide.SERVER)
private static void receiveId(String str, int id) {
idMap.put(str, id);
}
private static void sendAllIdsTo(EntityPlayerMP player) {
ArrayList<String> idStr = new ArrayList<String>();
ArrayList<Integer> ids = new ArrayList<Integer>();
for (Map.Entry<String, Integer> e : idMap.entrySet()) {
idStr.add(e.getKey());
ids.add(e.getValue());
}
RPCHandler.rpcPlayer(player, NetworkIdRegistry.class, "receiveAllIds", idStr, ids);
}
@RPC(RPCSide.CLIENT)
private static void receiveAllIds(ArrayList<String> idStr, ArrayList<Integer> ids) {
idMap.clear();
for (int i = 0; i < idStr.size(); ++i) {
if (!idMap.containsKey(idStr.get(i))) {
System.out.println("INIT " + ids.get(i) + " => " + idStr.get(i));
idMap.put(idStr.get(i), ids.get(i));
}
}
}
@SubscribeEvent
public void serverConnected(ServerConnectionFromClientEvent evt) {
isMaster = true;
if (evt.isLocal) {
isLocal = true;
} else {
isLocal = false;
// the server cannot send messages to the client at this stage, so
// cache the new client to receive ids on the next tick.
playersToInitialize.add(((NetHandlerPlayServer) evt.handler).playerEntity);
}
}
@SubscribeEvent
public void serverConnected(ClientConnectedToServerEvent evt) {
if (!evt.isLocal) {
isMaster = false;
isLocal = false;
}
}
@SubscribeEvent
public void tick(ServerTickEvent evt) {
for (EntityPlayerMP player : playersToInitialize) {
sendAllIdsTo(player);
}
playersToInitialize.clear();
}
static {
// The ids below are the minimal ids necessary to initialize properly
// the server. They are aimed at supporting the provide of receiveAllIds
idMap.put("", 0);
idMap.put(NetworkIdRegistry.class.getCanonicalName(), 1);
idMap.put(String.class.getCanonicalName(), 2);
idMap.put(Integer.class.getCanonicalName(), 3);
}
}

View file

@ -80,20 +80,8 @@ public class PacketHandler extends SimpleChannelInboundHandler<BuildCraftPacket>
break;
}
case PacketIds.RPC_TILE: {
((PacketRPCTile) packet).call(player);
break;
}
case PacketIds.RPC_GUI: {
((PacketRPCGui) packet).call(player);
break;
}
case PacketIds.RPC_ENTITY: {
((PacketRPCEntity) packet).call(player);
case PacketIds.RPC: {
((PacketRPC) packet).call(player);
break;
}

View file

@ -17,7 +17,6 @@ public final class PacketIds {
public static final int PIPE_POWER = 4;
public static final int PIPE_ITEMSTACK_REQUEST = 5;
public static final int PIPE_ITEMSTACK = 6;
public static final int PIPE_GATE_EXPANSION_MAP = 7;
public static final int DIAMOND_PIPE_SELECT = 31;
public static final int EMERALD_PIPE_SELECT = 32;
@ -32,10 +31,8 @@ public final class PacketIds {
public static final int STATE_UPDATE = 100;
public static final int RPC_TILE = 110;
public static final int RPC = 110;
public static final int RPC_PIPE = 111;
public static final int RPC_GUI = 112;
public static final int RPC_ENTITY = 113;
/**
* Deactivate constructor

View file

@ -0,0 +1,21 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.network;
import net.minecraft.entity.player.EntityPlayer;
public abstract class PacketRPC extends BuildCraftPacket {
@Override
public final int getID() {
return PacketIds.RPC;
}
public abstract void call(EntityPlayer sender);
}

View file

@ -14,7 +14,7 @@ import io.netty.buffer.Unpooled;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
public class PacketRPCEntity extends BuildCraftPacket {
public class PacketRPCEntity extends PacketRPC {
private byte[] contents;
private Entity entity;
private int entityId;
@ -28,10 +28,6 @@ public class PacketRPCEntity extends BuildCraftPacket {
}
@Override
public int getID() {
return PacketIds.RPC_ENTITY;
}
public void call(EntityPlayer sender) {
RPCMessageInfo info = new RPCMessageInfo();
info.sender = sender;

View file

@ -13,7 +13,7 @@ import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.EntityPlayer;
public class PacketRPCGui extends BuildCraftPacket {
public class PacketRPCGui extends PacketRPC {
byte [] contents;
public PacketRPCGui() {
@ -23,17 +23,13 @@ public class PacketRPCGui extends BuildCraftPacket {
contents = bytes;
}
@Override
public int getID() {
return PacketIds.RPC_GUI;
}
@Override
public void readData(ByteBuf data) {
contents = new byte [data.readableBytes()];
data.readBytes(contents);
}
@Override
public void call (EntityPlayer sender) {
RPCMessageInfo info = new RPCMessageInfo();
info.sender = sender;

View file

@ -0,0 +1,56 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.network;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.EntityPlayer;
public class PacketRPCStatic extends PacketRPC {
private byte[] contents;
private Class<?> clas;
public PacketRPCStatic() {
}
public PacketRPCStatic(Class iClass, byte[] bytes) {
contents = bytes;
clas = iClass;
}
@Override
public void readData(ByteBuf data) {
contents = new byte [data.readableBytes()];
data.readBytes(contents);
}
@Override
public void call (EntityPlayer sender) {
RPCMessageInfo info = new RPCMessageInfo();
info.sender = sender;
ByteBuf completeData = Unpooled.buffer();
completeData.writeBytes(contents);
String classId = NetworkIdRegistry.read(completeData);
try {
RPCHandler.receiveStaticRPC(Class.forName(classId), info, completeData);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void writeData(ByteBuf data) {
NetworkIdRegistry.write(data, clas.getCanonicalName());
data.writeBytes(contents);
}
}

View file

@ -24,7 +24,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
public class PacketRPCTile extends BuildCraftPacket {
public class PacketRPCTile extends PacketRPC {
public static int GLOBAL_ID = new Random(new Date().getTime()).nextInt();
public static HashMap<Integer, ByteBuf> bufferedPackets = new HashMap<Integer, ByteBuf>();
public TileEntity tile;
@ -40,7 +40,7 @@ public class PacketRPCTile extends BuildCraftPacket {
id = GLOBAL_ID++;
}
public PacketRPCTile (TileEntity tile, byte [] bytes) {
public PacketRPCTile(TileEntity tile, byte[] bytes) {
this.tile = tile;
contents = bytes;
}
@ -49,11 +49,6 @@ public class PacketRPCTile extends BuildCraftPacket {
tile = aTile;
}
@Override
public int getID() {
return PacketIds.RPC_TILE;
}
@Override
public void readData(ByteBuf data) {
dimId = data.readShort();
@ -67,6 +62,7 @@ public class PacketRPCTile extends BuildCraftPacket {
data.readBytes(contents);
}
@Override
public void call (EntityPlayer sender) {
World world = null;

View file

@ -28,6 +28,8 @@ import net.minecraft.inventory.Container;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.FMLCommonHandler;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.JavaTools;
import buildcraft.core.DefaultProps;
@ -108,11 +110,24 @@ public final class RPCHandler {
methods = mappings.toArray(new MethodMapping [mappings.size()]);
}
public static void rpcServer(Object object, String method, Object... actuals) {
if (!handlers.containsKey(object.getClass().getName())) {
handlers.put(object.getClass().getName(), new RPCHandler(object.getClass()));
private static RPCHandler getHandler(Object object) {
Class<?> clas;
if (object instanceof Class<?>) {
clas = (Class<?>) object;
} else {
clas = object.getClass();
}
if (!handlers.containsKey(clas.getName())) {
handlers.put(clas.getName(), new RPCHandler(clas));
}
return handlers.get(clas.getName());
}
public static void rpcServer(Object object, String method, Object... actuals) {
BuildCraftPacket packet = createPacket(object, method, actuals);
if (packet != null) {
@ -127,10 +142,6 @@ public final class RPCHandler {
}
public static void rpcPlayer(EntityPlayer player, Object object, String method, Object... actuals) {
if (!handlers.containsKey(object.getClass().getName())) {
handlers.put(object.getClass().getName(), new RPCHandler(object.getClass()));
}
BuildCraftPacket packet = createPacket(object, method, actuals);
if (packet != null) {
@ -144,16 +155,12 @@ public final class RPCHandler {
}
}
public static void rpcBroadcastPlayers(World world, Object object, String method, Object... actuals) {
public static void rpcBroadcastWorldPlayers(World world, Object object, String method, Object... actuals) {
RPCHandler.rpcBroadcastPlayersAtDistance(world, object, method, DefaultProps.NETWORK_UPDATE_RANGE, actuals);
}
public static void rpcBroadcastPlayersAtDistance(World world, Object object, String method, int maxDistance,
Object... actuals) {
if (!handlers.containsKey(object.getClass().getName())) {
handlers.put(object.getClass().getName(), new RPCHandler(object.getClass()));
}
BuildCraftPacket packet = createPacket(object, method, actuals);
if (packet != null) {
@ -183,15 +190,27 @@ public final class RPCHandler {
}
}
public static void rpcBroadcastAllPlayers(Object object, String method, Object... actuals) {
BuildCraftPacket packet = createPacket(object, method, actuals);
if (packet != null) {
for (Object o : FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().playerEntityList) {
EntityPlayerMP player = (EntityPlayerMP) o;
BuildCraftCore.instance.sendToPlayer(player, packet);
}
}
}
public static void receiveStaticRPC(Class clas, RPCMessageInfo info, ByteBuf data) {
if (clas != null) {
getHandler(clas).internalRpcReceive(clas, info, data);
}
}
public static void receiveRPC(Object obj, RPCMessageInfo info, ByteBuf data) {
if (obj != null) {
if (!handlers.containsKey(obj.getClass().getName())) {
handlers.put(obj.getClass().getName(),
new RPCHandler(obj.getClass()));
}
handlers.get(obj.getClass().getName()).internalRpcReceive(obj,
info, data);
getHandler(obj.getClass()).internalRpcReceive(obj, info, data);
}
}
@ -227,12 +246,13 @@ public final class RPCHandler {
BuildCraftPacket packet = null;
if (object instanceof Container) {
packet = handlers.get(object.getClass().getName()).createRCPPacketContainer(method, actuals);
packet = getHandler(object).createRCPPacketContainer(method, actuals);
} else if (object instanceof TileEntity) {
packet = handlers.get(object.getClass().getName())
.createRCPPacketTile((TileEntity) object, method, actuals);
packet = getHandler(object).createRCPPacketTile((TileEntity) object, method, actuals);
} else if (object instanceof Entity) {
packet = handlers.get(object.getClass().getName()).createRCPPacketEntity((Entity) object, method, actuals);
packet = getHandler(object).createRCPPacketEntity((Entity) object, method, actuals);
} else if (object instanceof Class<?>) {
packet = getHandler(object).createRCPPacketStatic((Class<?>) object, method, actuals);
}
return packet;
@ -269,6 +289,10 @@ public final class RPCHandler {
return new PacketRPCEntity(entity, getBytes(method, actuals));
}
private BuildCraftPacket createRCPPacketStatic(Class<?> clas, String method, Object[] actuals) {
return new PacketRPCStatic(clas, getBytes(method, actuals));
}
private void writeParameters(String method, ByteBuf data, Object... actuals)
throws IOException, IllegalArgumentException,
IllegalAccessException {
@ -321,8 +345,8 @@ public final class RPCHandler {
Object[] array = (Object[]) actual;
Class<?> componentType = formal.getComponentType();
data.writeInt(array.length);
for (int i = 0; i < array.length; i++) {
writePrimitive(data, componentType, array[i]);
for (Object element : array) {
writePrimitive(data, componentType, element);
}
} else {
return false;

View file

@ -31,7 +31,7 @@ import net.minecraftforge.fluids.FluidStack;
import buildcraft.api.core.JavaTools;
import buildcraft.api.core.NetworkData;
import buildcraft.core.network.INBTSerializable;
import buildcraft.core.utils.Utils;
import buildcraft.core.network.NetworkIdRegistry;
/**
* This class implements custom class mapping. There are three advantages in
@ -255,24 +255,8 @@ public class ClassMapping extends ClassSerializer {
if (realClass.equals(this.mappedClass)) {
data.writeByte(0);
} else {
ClassMapping delegateMapping;
if (context.classToId.containsKey(realClass.getCanonicalName())) {
int index = context.classToId.get(realClass.getCanonicalName()) + 1;
data.writeByte(index);
delegateMapping = (ClassMapping) context.idToClass.get(index - 1);
} else {
int index = context.classToId.size() + 1;
delegateMapping = (ClassMapping) get(realClass);
data.writeByte(index);
Utils.writeUTF(data, realClass.getCanonicalName());
context.classToId.put(realClass.getCanonicalName(),
context.classToId.size());
context.idToClass.add(delegateMapping);
}
delegateMapping.writeClass(obj, data, context);
NetworkIdRegistry.write(data, realClass.getCanonicalName());
ClassMapping delegateMapping = (ClassMapping) get(realClass);
return;
}
@ -323,27 +307,15 @@ public class ClassMapping extends ClassSerializer {
// {false} exit
// [int] what is the object real class?
// {0} the same as the declared class
// {1-x} a different one
// [string] if the number is not yet registered, the name of the
// class
// {1-x} the network id of the class
// [bytes] the actual contents
int index = data.readByte();
if (index != 0) {
ClassMapping delegateMapping;
if (context.idToClass.size() < index) {
String className = Utils.readUTF(data);
Class cls = Class.forName(className);
delegateMapping = (ClassMapping) get(cls);
context.idToClass.add(get(cls));
} else {
delegateMapping = (ClassMapping) context.idToClass.get(index - 1);
}
String className = NetworkIdRegistry.read(data);
Class cls = Class.forName(className);
ClassMapping delegateMapping = (ClassMapping) get(cls);
return delegateMapping.readClass(obj, data, context);
}
@ -452,7 +424,7 @@ public class ClassMapping extends ClassSerializer {
for (Enum<?> element : arr) {
data.writeBoolean(element != null);
if (element != null) {
data.writeByte(element.ordinal());
}

View file

@ -0,0 +1,13 @@
package buildcraft.core.network.serializers;
public class NetworkId {
private String str;
private String id;
public NetworkId(String str) {
id = str;
}
}

View file

@ -8,11 +8,6 @@
*/
package buildcraft.core.network.serializers;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
public class SerializationContext {
public ArrayList<ClassSerializer> idToClass = new ArrayList<ClassSerializer> ();
public Map<String, Integer> classToId = new TreeMap<String, Integer>();
}

View file

@ -10,7 +10,7 @@ package buildcraft.core.network.serializers;
import io.netty.buffer.ByteBuf;
import buildcraft.core.utils.Utils;
import buildcraft.core.network.NetworkIdRegistry;
public class SerializerObject extends ClassSerializer {
@ -23,21 +23,8 @@ public class SerializerObject extends ClassSerializer {
data.writeBoolean(true);
Class<? extends Object> realClass = o.getClass();
ClassSerializer delegateMapping;
if (context.classToId.containsKey(realClass.getCanonicalName())) {
int index = context.classToId.get(realClass.getCanonicalName()) + 1;
data.writeByte(index);
delegateMapping = context.idToClass.get(index - 1);
} else {
int index = context.classToId.size() + 1;
delegateMapping = ClassMapping.get(realClass);
data.writeByte(index);
Utils.writeUTF(data, realClass.getCanonicalName());
context.classToId.put(realClass.getCanonicalName(),
context.classToId.size());
context.idToClass.add(delegateMapping);
}
NetworkIdRegistry.write(data, realClass.getCanonicalName());
ClassSerializer delegateMapping = ClassMapping.get(realClass);
if (delegateMapping instanceof ClassMapping) {
((ClassMapping) delegateMapping).writeClass(o, data, context);
@ -54,20 +41,9 @@ public class SerializerObject extends ClassSerializer {
if (!data.readBoolean()) {
return null;
} else {
int index = data.readByte();
ClassSerializer delegateMapping;
if (context.idToClass.size() < index) {
String className = Utils.readUTF(data);
Class<?> cls = Class.forName(className);
delegateMapping = ClassMapping.get(cls);
context.idToClass.add(ClassMapping.get(cls));
} else {
delegateMapping = context.idToClass.get(index - 1);
}
String className = NetworkIdRegistry.read(data);
Class cls = Class.forName(className);
ClassSerializer delegateMapping = ClassMapping.get(cls);
if (delegateMapping instanceof ClassMapping) {
return ((ClassMapping) delegateMapping).readClass(o, data, context);

View file

@ -517,7 +517,7 @@ public class EntityRobot extends EntityRobotBase implements
@Override
public void setItemInUse(ItemStack stack) {
itemInUse = stack;
RPCHandler.rpcBroadcastPlayers(worldObj, this, "clientSetItemInUse", stack);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "clientSetItemInUse", stack);
}
@RPC(RPCSide.CLIENT)
@ -567,7 +567,7 @@ public class EntityRobot extends EntityRobotBase implements
@Override
public void setItemActive(boolean isActive) {
RPCHandler.rpcBroadcastPlayers(worldObj, this, "rpcSetItemActive", isActive);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "rpcSetItemActive", isActive);
}
@RPC(RPCSide.CLIENT)

View file

@ -94,7 +94,7 @@ public class TileEnergyEmitter extends TileBuildCraft {
addLaser(receiver.xCoord, receiver.yCoord,
receiver.zCoord);
RPCHandler.rpcBroadcastPlayers(worldObj, this, "addLaser",
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "addLaser",
receiver.xCoord, receiver.yCoord,
receiver.zCoord);
@ -111,7 +111,7 @@ public class TileEnergyEmitter extends TileBuildCraft {
accumulated++;
if (syncMJ.markTimeIfDelay(worldObj)) {
RPCHandler.rpcBroadcastPlayers(worldObj, this, "synchronizeMJ", mjAcc
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "synchronizeMJ", mjAcc
/ accumulated);
mjAcc = 0;
accumulated = 0;
@ -121,7 +121,7 @@ public class TileEnergyEmitter extends TileBuildCraft {
for (Target t : targets.values()) {
if (t.data.isVisible) {
t.data.isVisible = false;
RPCHandler.rpcBroadcastPlayers(worldObj, this, "disableLaser",
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "disableLaser",
t.receiver.xCoord, t.receiver.yCoord,
t.receiver.zCoord);
}
@ -139,7 +139,7 @@ public class TileEnergyEmitter extends TileBuildCraft {
for (Target t : targets.values()) {
if (!t.data.isVisible) {
t.data.isVisible = true;
RPCHandler.rpcBroadcastPlayers(worldObj, this, "enableLaser",
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "enableLaser",
t.receiver.xCoord, t.receiver.yCoord,
t.receiver.zCoord);
}

View file

@ -14,6 +14,7 @@ import java.util.List;
import java.util.Set;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
@ -40,6 +41,7 @@ import buildcraft.api.transport.IPipePluggable;
import buildcraft.api.transport.IPipeTile;
import buildcraft.core.ItemBuildCraft;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.network.NetworkIdRegistry;
import buildcraft.core.utils.StringUtils;
import buildcraft.transport.Gate;
import buildcraft.transport.TileGenericPipe;
@ -95,7 +97,7 @@ public class ItemGate extends ItemBuildCraft {
final int expansionsSize = expansions.length;
buf.writeInt(expansionsSize);
for (IGateExpansion expansion : expansions) {
buf.writeByte(GateExpansions.getServerExpansionID(expansion.getUniqueIdentifier()));
NetworkIdRegistry.write(buf, expansion.getUniqueIdentifier());
}
}
@ -106,7 +108,7 @@ public class ItemGate extends ItemBuildCraft {
final int expansionsSize = buf.readInt();
expansions = new IGateExpansion[expansionsSize];
for (int i = 0; i < expansionsSize; i++) {
expansions[i] = GateExpansions.getExpansionClient(buf.readByte());
expansions[i] = GateExpansions.getExpansion(NetworkIdRegistry.read(buf));
}
}

View file

@ -1,54 +0,0 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.network;
import java.util.Map;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import io.netty.buffer.ByteBuf;
import buildcraft.api.gates.GateExpansions;
import buildcraft.core.network.BuildCraftPacket;
import buildcraft.core.network.PacketIds;
import buildcraft.core.utils.Utils;
public class PacketGateExpansionMap extends BuildCraftPacket {
public PacketGateExpansionMap() {
}
@Override
public void writeData(ByteBuf data) {
BiMap<Byte, String> map = GateExpansions.getServerMap();
data.writeByte(map.size());
for (Map.Entry<Byte, String> entry : map.entrySet()) {
data.writeByte(entry.getKey());
Utils.writeUTF(data, entry.getValue());
}
}
@Override
public void readData(ByteBuf data) {
int numEntries = data.readByte();
BiMap<Byte, String> map = HashBiMap.create(numEntries);
for (int i = 0; i < numEntries; i++) {
byte id = data.readByte();
String identifier = Utils.readUTF(data);
map.put(id, identifier);
}
GateExpansions.setClientMap(map);
}
@Override
public int getID() {
return PacketIds.PIPE_GATE_EXPANSION_MAP;
}
}

View file

@ -61,10 +61,6 @@ public class PacketHandlerTransport extends SimpleChannelInboundHandler<BuildCra
// action will have happened already at read time
break;
}
case PacketIds.PIPE_GATE_EXPANSION_MAP: {
// action will have happened already at read time
break;
}
/**
* SERVER SIDE *

View file

@ -87,7 +87,7 @@ public class TileTestCase extends TileEntity {
information = "test clear";
}
RPCHandler.rpcBroadcastPlayers(worldObj, this, "setInformation", information);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setInformation", information);
}
@RPC(RPCSide.CLIENT)
@ -154,7 +154,7 @@ public class TileTestCase extends TileEntity {
@RPC(RPCSide.SERVER)
private void setName(String name) {
testName = name;
RPCHandler.rpcBroadcastPlayers(worldObj, this, "setNameClient", name);
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setNameClient", name);
}
@RPC(RPCSide.CLIENT)