Jump to content

Custom clientbound packet handling [1.19] [SOLVED]


RInventor7

Recommended Posts

Hi! I'm trying to send some custom network packets from server to client. The packets are sent but I have some problems handling them on client. To avoid arbitrary chuck generation I'm trying to use level#hasChunckAt(pos). It's deprected. Is this the correct thing to use or should I use something else? And it doesn't execute the code in client because I can get the ServerLevel but I need ClientLevel? How can I get it working? Thanks.

import java.util.function.Supplier;

import com.rinventor.transportmod.core.base.PTMDbg;

import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

public class InformationScreenSyncMessage {

	public final BlockPos pos;
	public final String data;

    public InformationScreenSyncMessage(BlockPos pos, String data) {
    	this.pos = pos;
    	this.data = data;
    }

    public InformationScreenSyncMessage(FriendlyByteBuf buffer) {
    	this(buffer.readBlockPos(), buffer.readUtf());
    }

    public void encode(FriendlyByteBuf buffer) {
    	buffer.writeBlockPos(this.pos);
    	buffer.writeUtf(this.data);
    }

    public static void handle(InformationScreenSyncMessage msg, Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() ->
            // Make sure it's only executed on the physical client
            DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> InformationScreenClient.handlePacket(msg, ctx))
        );
        ctx.get().setPacketHandled(true);
    }
    
    public class InformationScreenClient
    {
    	@SuppressWarnings("deprecation")
	public static void handlePacket(InformationScreenSyncMessage msg, Supplier<NetworkEvent.Context> ctx) {
    	    Level level = ctx.get().getSender().getLevel();
    	    BlockPos pos = msg.pos;
    	    if (level.hasChunkAt(pos)) {
    	    	if (level.getBlockEntity(pos) instanceof InformationScreenBlockEntity be) {
    			be.data = msg.data;
    		}
    	    }
    	}
    	
    }

}

 

Edited by RInventor7
Link to comment
Share on other sites

level = Minecraft.getInstance().level

level.isLoaded(BlockPos)

Mojang have moved to "sections" instead of chunks.

It won't generate chunks on the client, it will crash if you access unloaded chunks.

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

  • RInventor7 changed the title to Custom clientbound packet handling [1.19] [SOLVED]

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.