Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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

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

  • 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

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.