Jump to content

Recommended Posts

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

Posted

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

Posted

@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

Posted
  On 2/3/2016 at 11:03 PM, diesieben07 said:

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

×   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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

×
×
  • Create New...

Important Information

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