Jump to content

Wait for all packages to arrive


ErfinderLabyrinth

Recommended Posts

Hi, I created a SimpleChannel and sent a few messages about it. The problem is that when I'm on the multiplayer (external server), I don't receive the message. My guess is that I read the message before the message is received. But how can I receive the message, I did not come to a solution

SimpleChannelMessage:

package net.lager.system.client;

import java.util.function.Supplier;

import net.lager.system.register.ConnectionHandlerRegister;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.thread.SidedThreadGroups;
import net.minecraftforge.fml.network.NetworkEvent;

public class SimpleChannelMessage {
	public String msg;
	private ResourceLocation rl;
	public BlockPos bpos;
	
	public SimpleChannelMessage(ResourceLocation rl, String msg) {
		this.rl = rl;
		this.msg = msg;
	}
	public SimpleChannelMessage(ResourceLocation rl, String msg, BlockPos bpos) {
		this.rl = rl;
		this.msg = msg;
		this.bpos = bpos;
	}
	public static void encode(SimpleChannelMessage msg, PacketBuffer buffer) {
		java.lang.System.out.println("encode");
		buffer.writeBytes(("." + msg.rl.getNamespace() + ":" + msg.rl.getPath() + " " + msg.msg).getBytes());
		buffer.writeBlockPos(msg.bpos);
		java.lang.System.out.println(msg.rl.getNamespace() + ":" + msg.rl.getPath() + " " + msg.msg);
	}
	public static SimpleChannelMessage decode(PacketBuffer buffer) {
		java.lang.System.out.println("decode");
		buffer.capacity(17000);
		buffer.writerIndex(16000);
		String[] list = new String(buffer.readByteArray()).split(" ");
		String rls = list[0];
		java.lang.System.out.println(new String(buffer.readByteArray(16000)));
		ResourceLocation rl = new ResourceLocation(rls.split(":")[0], rls.split(":")[1]);
		String msg = list[1];
		BlockPos bpos = buffer.readBlockPos();
		if (bpos == null) {
			return new SimpleChannelMessage(rl, msg);
		} else {
			return new SimpleChannelMessage(rl, msg, bpos);
		}
	}
	public static void handle(SimpleChannelMessage msg, Supplier<NetworkEvent.Context> ctx) {
		java.lang.System.out.println("handle");
		ctx.get().enqueueWork(() -> {
	        DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {if (Thread.currentThread().getThreadGroup() == SidedThreadGroups.SERVER) {
	        	SimpleChannelMessage.handleServer(msg, ctx);
			} else if(Thread.currentThread().getThreadGroup() == SidedThreadGroups.CLIENT) {
				SimpleChannelMessage.handleClient(msg, ctx);
			}});
	        DistExecutor.unsafeRunWhenOn(Dist.DEDICATED_SERVER, () -> () -> SimpleChannelMessage.handleServer(msg, ctx));
	    });
	    ctx.get().setPacketHandled(true);
	}
	public static void handleServer(SimpleChannelMessage msg, Supplier<NetworkEvent.Context> ctx) {
		java.lang.System.out.println("handle server");
		ctx.get().enqueueWork(() -> {
			ServerPlayerEntity sender = ctx.get().getSender();
			if(ConnectionHandlerRegister.runServer(msg, msg.rl, sender)) {
				ctx.get().setPacketHandled(true);
			}
		});
	}
	public static void handleClient(SimpleChannelMessage msg, Supplier<NetworkEvent.Context> ctx) {
		java.lang.System.out.println("handle client");
		ctx.get().enqueueWork(() -> {
			if(ConnectionHandlerRegister.runClient(msg, msg.rl)) {
				ctx.get().setPacketHandled(true);
			}
		});
	}
}

 

Link to comment
Share on other sites

Quote

DistExecutor.unsafeRunWhenOn(Dist.DEDICATED_SERVER, () -> () -> SimpleChannelMessage.handleServer(msg, ctx));

This means this code won't be executed in single player mode.

Why do you need this? The server logical side is always available, unlike the client.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

3 hours ago, ErfinderLabyrinth said:
ctx.get().enqueueWork(() -> {
	        DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {if (Thread.currentThread().getThreadGroup() == SidedThreadGroups.SERVER) {
	        	SimpleChannelMessage.handleServer(msg, ctx);
			} else if(Thread.currentThread().getThreadGroup() == SidedThreadGroups.CLIENT) {
				SimpleChannelMessage.handleClient(msg, ctx);
			}});
	        DistExecutor.unsafeRunWhenOn(Dist.DEDICATED_SERVER, () -> () -> SimpleChannelMessage.handleServer(msg, ctx));
	    });

Dieser ganze Code überprüft, ob die Nachricht vom Client zum Server oder vom Server zum Client geschickt worden ist

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.



×
×
  • Create New...

Important Information

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