Posted February 3, 20169 yr hi, I need to send a BlockPos from server to client. I use SimpleNetworkWrapper. //test BlockPos zz = new BlockPos(3, 435, 12); @Override public void fromBytes(ByteBuf buf) { this.zz = new Gson().fromJson(ByteBufUtils.readUTF8String(buf), new TypeToken<BlockPos>() { }.getType()); System.out.println("from: " + this.zz); } @Override public void toBytes(ByteBuf buf) { ByteBufUtils.writeUTF8String(buf, new Gson().toJson(this.zz)); System.out.println("to: " + this.zz); } It works fine on client. [19:42:11] [server thread/INFO] [sTDOUT]: [mrriegel.ifinder.SyncMessage:toBytes:82]: to: BlockPos{x=3, y=435, z=12} [19:42:11] [Netty Local Client IO #0/INFO] [sTDOUT]: [mrriegel.ifinder.SyncMessage:fromBytes:75]: from: BlockPos{x=3, y=435, z=12} But on server it doesn't work. [19:48:52] [server thread/INFO] [sTDOUT]: [mrriegel.ifinder.SyncMessage:toBytes:82]: to: BlockPos{x=3, y=435, z=12} [19:48:52] [Netty Epoll Client IO #1/INFO] [sTDOUT]: [mrriegel.ifinder.SyncMessage:fromBytes:75]: from: BlockPos{x=0, y=0, z=0} I hope someone can help
February 3, 20169 yr A blockpos is just 3 ints... why are you using json? I do Forge for free, however the servers to run it arn't free, so anything is appreciated. Consider supporting the team on Patreon
February 3, 20169 yr Author Actually it is a list of BlockPos. The result is the same. Every BlockPos is 0,0,0
February 3, 20169 yr sending a list is pretty easy. just send the size of the list first, after that iterate over the list and send each of the 3 ints of the blockpos. when reconstructing the list just read the first int, you know the size of the list, after that iterate over the size and create x blockpos
February 3, 20169 yr Author @diesieben public class SyncMessage implements IMessage, IMessageHandler<SyncMessage, IMessage> { List<BlockPos> lis; public SyncMessage() { } public SyncMessage(EntityPlayer player) { ItemFinder.lis = new ArrayList<BlockPos>(); int range = ItemFinder.range; for (int i = -range; i <= range; i++) for (int j = -range; j <= range; j++) for (int k = -range; k <= range; k++) { BlockPos pos = new BlockPos(i + player.posX, j + player.posY, k + player.posZ); if (player.worldObj.getTileEntity(pos) instanceof IInventory) { IInventory inv = (IInventory) player.worldObj .getTileEntity(pos); for (int ii = 0; ii < inv.getSizeInventory(); ii++) { if (inv.getStackInSlot(ii) != null && inv.getStackInSlot(ii).isItemEqual( player.getHeldItem())) { ItemFinder.lis.add(pos); break; } } } } lis = new ArrayList<BlockPos>(ItemFinder.lis); } @Override public IMessage onMessage(final SyncMessage message, MessageContext ctx) { IThreadListener mainThread = Minecraft.getMinecraft(); mainThread.addScheduledTask(new Runnable() { @Override public void run() { ItemFinder.lis = new ArrayList<BlockPos>(message.lis); } }); return null; } @Override public void fromBytes(ByteBuf buf) { this.lis = new Gson().fromJson(ByteBufUtils.readUTF8String(buf), new TypeToken<List<BlockPos>>() { }.getType()); } @Override public void toBytes(ByteBuf buf) { ByteBufUtils.writeUTF8String(buf, new Gson().toJson(this.lis)); } } @failender thanks, I'm gonna try this
February 3, 20169 yr You are using a static field, immediate red flag. Do not share state between client & server thread unless you absolutely, seriously 120% know what you are doing with multithreading. And even then I would be suspicious just you wait!
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.