Jump to content

[1.10.2] I've having an issue with the Packets. [SOLVED]


derf6060

Recommended Posts

What I'm trying to is push information about my internal FluidTank in my tank's TileEntity from server to the client using forges Packet api, but for some reason its calling the setTank method on the server side rather than the client side. Here is some snippets of code from my source. I'll also post a link to the repo as well.

 

PacketHandler:

// Tank Fluid Update
INSTANCE.registerMessage(PacketTankFluidUpdate.Handler.class, PacketTankFluidUpdate.class, nextID(), Side.CLIENT);

PacketTankFluidUpdate and PacketTankFluidUpdate.Handler:

public class PacketTankFluidUpdate implements IMessage {

private int dimension;
private BlockPos pos;
// Tank 
private FluidTank tank = new FluidTank();

public PacketTankFluidUpdate() {}

public PacketTankFluidUpdate(World world, BlockPos pos, FluidTank tank) {
	this.dimension = world.provider.getDimension();
	this.pos = pos;
	this.tank = tank;
}

@Override
public void fromBytes(ByteBuf buf) {
	PacketBuffer buff = new PacketBuffer(buf);
	this.dimension = buff.readInt();
	this.pos = buff.readBlockPos();
	try {
		this.tank.readFromNBT(buff.readNBTTagCompoundFromBuffer());
	} catch (IOException e) {
		e.printStackTrace();
	}
}

@Override
public void toBytes(ByteBuf buf) {
	PacketBuffer buff = new PacketBuffer(buf);
	buff.writeInt(this.dimension);
	buff.writeBlockPos(this.pos);
	NBTTagCompound comp = new NBTTagCompound();
	tank.writeToNBT(comp);
	buff.writeNBTTagCompoundToBuffer(comp);
}

public void onMessageFromServer(MessageContext ctx) {
	World world = DimensionManager.getWorld(this.dimension);
	if(world != null) {
		TileEntity entity = world.getTileEntity(pos);

		if(entity instanceof TileEntityTank) {
			TileEntityTank tank = (TileEntityTank)entity;
			tank.setTank(this.tank);
		}
	}
}

public static class Handler implements IMessageHandler<PacketTankFluidUpdate, IMessage> {

	@Override
	public IMessage onMessage(PacketTankFluidUpdate message, MessageContext ctx) {

		if(ctx.side == Side.CLIENT) {
			message.onMessageFromServer(ctx);
		}

		return null;
	}

}


}

TileEntityTank:

private void sendToClient() {
PacketHandler.INSTANCE.sendToAll(new PacketTankFluidUpdate(this.worldObj, this.pos, this.tank));
}

 

Repo Link: https://github.com/aod6060/BTAWC

 

I'm thinking it could be me doing it backwards but I could be wrong.

Link to comment
Share on other sites

Thanks for the fast reply diesieben07. Pretty much DimesionalManager only runs server . Ok I'm going noting that when I'm sending info to the client.

 

Hmm, also about the warning from the document does that mean I can't send info from server (Server Side TileEntity of some kind) to the client (change Client Side TileEntity and GuiContainer reads from Client Side TileEntity) from the packet api?

Link to comment
Share on other sites

Yeah that's the warning box that I read. I know that you can check and see what Side I'm on. I'm using the context objects side field and checking if I'm on the client side or not. I my problem was using the DimesionManager which was getting an instance of the WorldServer object. Sorry for the confusing question diesieben07.

Link to comment
Share on other sites

Yeah I forgot this is what I changed in the source code if anyone is having the same issue.

 

public class PacketTankFluidUpdate implements IMessage, Runnable {

private int dimension;
private BlockPos pos;
// Tank 
private FluidTank tank = new FluidTank();

public PacketTankFluidUpdate() {}

public PacketTankFluidUpdate(World world, BlockPos pos, FluidTank tank) {
	this.dimension = world.provider.getDimension();
	this.pos = pos;
	this.tank = tank;
}

@Override
public void fromBytes(ByteBuf buf) {
	PacketBuffer buff = new PacketBuffer(buf);
	this.dimension = buff.readInt();
	this.pos = buff.readBlockPos();
	try {
		this.tank.readFromNBT(buff.readNBTTagCompoundFromBuffer());
	} catch (IOException e) {
		e.printStackTrace();
	}
}

@Override
public void toBytes(ByteBuf buf) {
	PacketBuffer buff = new PacketBuffer(buf);
	buff.writeInt(this.dimension);
	buff.writeBlockPos(this.pos);
	NBTTagCompound comp = new NBTTagCompound();
	tank.writeToNBT(comp);
	buff.writeNBTTagCompoundToBuffer(comp);
}

public static class Handler implements IMessageHandler<PacketTankFluidUpdate, IMessage> {

	@Override
	public IMessage onMessage(PacketTankFluidUpdate message, MessageContext ctx) {

		if(ctx.side == Side.CLIENT) {
			Minecraft.getMinecraft().addScheduledTask(message);
		}

		return null;
	}

}

@Override
public void run() {
	World world = Minecraft.getMinecraft().theWorld;

	if(world != null) {
		TileEntity entity = world.getTileEntity(pos);

		if(entity instanceof TileEntityTank) {
			TileEntityTank tank = (TileEntityTank)entity;
			tank.setTank(this.tank);
		}
	}
}
}

Link to comment
Share on other sites

Your code is still broken because you are still not following the warning box.

This code will randomly fail with weird exceptions or unexpected behavior, because you are interacting with non-threadsafe code from a thread it is not designed to be interact with from.

 

What do you mean? I'm following the warning and I'm using the scheduled task in the static Handler class. I haven't had any issues on my end with. If you did testing with it can you please post the stack trace of it so I can look at it?

 


	@Override
	public IMessage onMessage(PacketTankFluidUpdate message, MessageContext ctx) {

		if(ctx.side == Side.CLIENT) {
			Minecraft.getMinecraft().addScheduledTask(message);
		}

		return null;
	}

 

Hi

 

This example project might be of use

https://github.com/TheGreyGhost/MinecraftByExample

see example mbe60 which shows how to send messages back and forth between client and server

 

Also this page explaining it in a bit more detail

http://greyminecraftcoder.blogspot.com.au/2015/01/thread-safety-with-network-messages.html

 

-TGG

 

Thanks for the code dude. I took a look and it will help me generalize packets later on.

Link to comment
Share on other sites

Its cool dude. I agree with you that its probably not the best way to go about it. Its simply a hot fix for the moment. I'm trying to make a more general routine that would make the data inside the message abstract so the packets can be more ambiguous. Lets say reduce the amount of Packet implementations down to 2 different variants. One packet that will be used to client to server communication and the other is server to client communication. The data portion is were the Runnable stuff will happen. I'm just trying to find a way to decouple data from the message. I'll probably post the system at a later date.

Link to comment
Share on other sites

That is not as good of an idea as you might think, because it will for sure increase data traffic.

Thats probably true. I'm really just doing this for my sense of curiosity. I just want to see if I can do or not. From the results I have at the moment it can be done. :)

 

I agree that it will increase traffic. I'm going to try and identity bottle necks that will cause a ton of network traffic. For example my six sided tank I'm working on has 2 packet that are sent from both the its TileEntity and GuiContainer. Ones for updating the Server Side FluidTank with the Client Side Fluid Tank. What I would do is find the areas were the Server Side Tank needs an update. Those packets will need to be sent when the tank is fill or draining from one of the sides of the block or if a user files and empties a bucket.

 

For the other Packet which aids with configuring the sides of the block to fill and drain the tank from a side of the block. The only time when this happens if the user press a button in the gui to change the configuration. 

 

Plus I'm just playing around with Minecraft modding. My mod is experimental anyway. If the mod was a bigger mod that requires a ton of maintenance or appeal to a ton of Minecraft users then I would change my tone about it. I focus more on the polish factor and optimize code so the to keep UPS issues down. Enough of this silly wall of text. Take it easy man.

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • why when I launch the forge version of minecraft in full screen and turn on screen recording, only the first static image of the launch is recorded and the gameplay itself is not recorded
    • As a software developer from Sakha Republic, Russia. and like many in my field, I am quite tech-savvy. However, I recently became a victim of a cryptocurrency scam that cost me more than 10 million in my currency. The experience was not only financially devastating but also emotionally draining. I reported the crime to the authorities, hoping for a swift resolution, but I was frustrated and lacked results. It felt like I was in a maze with no exit. Feeling stuck and desperate, I turned to fellow developers for advice. One of them recommended a company called Muyern Trust Hacker. Initially, I was hesitant; I had heard mixed reviews about recovery services and I was concerned about falling victim to another scam. However, after speaking with their team, I was impressed by their professionalism and the clarity of their recovery methods. The team at Muyern Trust Hacker was dedicated and responsive, providing regular updates on their progress. It was refreshing to work with professionals who genuinely cared about my situation and were committed to helping me recover my lost funds. To my astonishment, they managed to recover most of my lost funds. The experience taught me valuable lessons about the risks of cryptocurrency investments and the importance of vigilance in the digital space.
    • Hello! I am the leader of this rp community for a Minecraft rp server and I would like to lend out an invitation to you all to join! We are on a rp server meaning we are going to do some rp and have characters, lore, stories and more! If you want to join just click the discord link below and I’ll help guide you through the process in joining the server and joining us on the server which should be easy. (NOTE: server requires you have to be 16 years old or older) (Java only!) Discord: https://discord.gg/V9uxjKQ7dB
    • these are the errors at startup [06nov2024 19:13:37.566] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--launchTarget, forgeserver, --fml.forgeVersion, 47.3.11, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [06nov2024 19:13:37.570] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 23.0.1 by Oracle Corporation; OS Windows 11 arch amd64 version 10.0 [06nov2024 19:13:39.173] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: ImmediateWindowProvider not loading because launch target is forgeserver [06nov2024 19:13:39.178] [main/INFO] [mixin-transmog/]: Mixin Transmogrifier is definitely up to no good... [06nov2024 19:13:39.398] [main/INFO] [mixin-transmog/]: Redefining 5 mixin classes [06nov2024 19:13:39.412] [main/INFO] [mixin-transmog/]: crimes against java were committed [06nov2024 19:13:39.413] [main/INFO] [com.teampotato.redirector.Redirector/]: Redirector CoreMod initialized successfully! [06nov2024 19:13:39.424] [main/INFO] [mixin-transmog/]: Original mixin transformation service successfully crobbed by mixin-transmogrifier! [06nov2024 19:13:39.453] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.5 Source=union:/C:/Users/zappi/OneDrive/Desktop/server%20forge/mods/Connector-1.0.0-beta.18+1.20.1-full.jar%23386%23390!/ Service=ModLauncher Env=SERVER [06nov2024 19:13:40.424] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\zappi\OneDrive\Desktop\server forge\libraries\net\minecraftforge\fmlcore\1.20.1-47.3.11\fmlcore-1.20.1-47.3.11.jar is missing mods.toml file [06nov2024 19:13:40.427] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\zappi\OneDrive\Desktop\server forge\libraries\net\minecraftforge\javafmllanguage\1.20.1-47.3.11\javafmllanguage-1.20.1-47.3.11.jar is missing mods.toml file [06nov2024 19:13:40.430] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\zappi\OneDrive\Desktop\server forge\libraries\net\minecraftforge\lowcodelanguage\1.20.1-47.3.11\lowcodelanguage-1.20.1-47.3.11.jar is missing mods.toml file [06nov2024 19:13:40.433] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\zappi\OneDrive\Desktop\server forge\libraries\net\minecraftforge\mclanguage\1.20.1-47.3.11\mclanguage-1.20.1-47.3.11.jar is missing mods.toml file [06nov2024 19:13:41.061] [main/WARN] [net.minecraftforge.jarjar.selection.JarSelector/]: Attempted to select two dependency jars from JarJar which have the same identification: Mod File:  and Mod File: . Using Mod File:  [06nov2024 19:13:41.062] [main/WARN] [net.minecraftforge.jarjar.selection.JarSelector/]: Attempted to select two dependency jars from JarJar which have the same identification: Mod File:  and Mod File: . Using Mod File:  [06nov2024 19:13:41.063] [main/WARN] [net.minecraftforge.jarjar.selection.JarSelector/]: Attempted to select a dependency jar for JarJar which was passed in as source: curios. Using Mod File: C:\Users\zappi\OneDrive\Desktop\server forge\mods\curios-forge-5.10.0+1.20.1.jar [06nov2024 19:13:41.063] [main/WARN] [net.minecraftforge.jarjar.selection.JarSelector/]: Attempted to select a dependency jar for JarJar which was passed in as source: terrablender. Using Mod File: C:\Users\zappi\OneDrive\Desktop\server forge\mods\TerraBlender-forge-1.20.1-3.0.1.7.jar [06nov2024 19:13:41.063] [main/WARN] [net.minecraftforge.jarjar.selection.JarSelector/]: Attempted to select a dependency jar for JarJar which was passed in as source: aeroblender. Using Mod File: C:\Users\zappi\OneDrive\Desktop\server forge\mods\aeroblender-1.20.1-1.0.1-neoforge.jar [06nov2024 19:13:41.063] [main/INFO] [net.minecraftforge.fml.loading.moddiscovery.JarInJarDependencyLocator/]: Found 97 dependencies adding them to mods collection [06nov2024 19:13:42.637] [main/INFO] [dev.su5ed.sinytra.connector.locator.DependencyResolver/]: Dependency resolution found 11 candidates to load [06nov2024 19:13:43.954] [main/INFO] [Configured Defaults/]: Applying default files...
  • Topics

×
×
  • Create New...

Important Information

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