Jump to content

[1.8.9] Problem with sending BlockPos


MrRiegel

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.