Jump to content

[1.15.2] What Should I Give the SimpleChannel.registerMessages() Parameters?


OuiOuiCroissant

Recommended Posts

I just learned how to do packets for the first time by following the forge documentation, but it only got me so far. I don't understand what SimpleChannel.registerMessages() should take as parameters.

In a lot of examples and tutorials they give it static void methods from the message class, but it won't accept those in my code. It "is not applicable for the arguments". I see people implementing IMessage into their packet class, but there is no IMessage interface! Has it been replaced with MSG? Do I have to create it?

 

If you know a lot about packets, can you tell me what I'm doing wrong?

 

My Packet Handler:

package com.siegemod.util;

import com.siegemod.SiegeMod;
import com.siegemod.packets.SetKingPacket;

import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;

public class SiegePacketHandler {

	private static final String PROTOCOL_VERSION = "1";
	public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(new ResourceLocation(SiegeMod.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals,
			PROTOCOL_VERSION::equals);

	private static int id;

	public static void registerPackets() {
		INSTANCE.registerMessage(id++, SetKingPacket.class, SetKingPacket::encode, SetKingPacket::decode, SetKingPacket::handle);
	}
}

/*
// Sending to one player
INSTANCE.send(PacketDistributor.PLAYER.with(playerMP),new MyMessage());

// Send to all players tracking this chunk
INSTANCE.send(PacketDistributor.TRACKING_CHUNK.with(chunk),new MyMessage());

// Sending to all connected players
INSTANCE.send(PacketDistributor.ALL.noArg(),new MyMessage());
*/

 

My Message:

package com.siegemod.packets;

import java.nio.ByteBuffer;

import org.lwjgl.system.windows.MSG;

import com.google.common.base.Supplier;

import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

public class SetKingPacket extends MSG {

	private String kingUUID;

	public SetKingPacket(ByteBuffer buffer) {
		super(buffer);
	}

	public static void encode(SetKingPacket msg, PacketBuffer buf) {
		buf.writeString(msg.kingUUID);
	}

	public static void decode(PacketBuffer buf, SetKingPacket msg) {
		msg.kingUUID = buf.readString();
	}

	public static void handle(SetKingPacket msg, Supplier<NetworkEvent.Context> ctx) {
		ctx.get().enqueueWork(() -> {
			// Work that needs to be threadsafe (most work)
			ServerPlayerEntity sender = ctx.get().getSender(); // the client that sent this packet
			// do stuff
		});
		ctx.get().setPacketHandled(true);
	}
}

 

Link to comment
Share on other sites

29 minutes ago, OuiOuiCroissant said:

public static void decode(PacketBuffer buf, SetKingPacket msg) {

This is wrong. You will never receive a packet instance as a parameter here because your job is to create it.

https://github.com/Draco18s/ReasonableRealism/blob/1.14.4/src/main/java/com/draco18s/industry/network/ToServerFilterClick.java#L38-L47

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

That makes sense. I'm still getting errors on the last 3 parameters of INSTANCE.registerMessage() in my packet handler. Is there a new way to register packets in 1.15.2?

Quote

The method registerMessage(int, Class<MSG>, BiConsumer<MSG,PacketBuffer>, Function<PacketBuffer,MSG>, BiConsumer<MSG,Supplier<NetworkEvent.Context>>) in the type SimpleChannel is not applicable for the arguments (int, Class<SetKingPacket>, SetKingPacket::encode, SetKingPacket::new, SetKingPacket.Handler::handle)

 

My Packet:

package com.siegemod.packets;

import com.google.common.base.Supplier;

import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

public class SetKingPacket {

	private final int data;

	public SetKingPacket(int dataIn) {
		this.data = dataIn;
	}

	public static void encode(SetKingPacket msg, PacketBuffer buf) {
		buf.writeInt(msg.data);
	}

	public static SetKingPacket decode(PacketBuffer buf) {
		int data = buf.readInt();

		return new SetKingPacket(data);
	}

	public static class Handler {

		public static void handle(SetKingPacket msg, Supplier<NetworkEvent.Context> ctx) {
			ctx.get().enqueueWork(() -> {
				// Work that needs to be threadsafe (most work)
				ServerPlayerEntity sender = ctx.get().getSender(); // the client that sent this packet
				// do stuff
			});
			ctx.get().setPacketHandled(true);
		}
	}
}

 

My Packet Handler:

package com.siegemod.packets;

import com.siegemod.SiegeMod;

import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;

public final class SiegePacketHandler {

	private static final String PROTOCOL_VERSION = "1";
	public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(new ResourceLocation(SiegeMod.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals,
			PROTOCOL_VERSION::equals);

	private static int id;

	public static void register() {
		INSTANCE.registerMessage(id++, SetKingPacket.class, SetKingPacket::encode, SetKingPacket::new, SetKingPacket.Handler::handle);
	}
}

/*
// Sending to one player
INSTANCE.send(PacketDistributor.PLAYER.with(playerMP),new MyMessage());

// Send to all players tracking this chunk
INSTANCE.send(PacketDistributor.TRACKING_CHUNK.with(chunk),new MyMessage());

// Sending to all connected players
INSTANCE.send(PacketDistributor.ALL.noArg(),new MyMessage());
*/

 

Link to comment
Share on other sites

Yeah, your registration line was fine, all you had to do was change the signature of decode.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

It's still giving me those same 3 errors on the underlined bits:

INSTANCE.registerMessage(id++, SetKingPacket.class, SetKingPacket::encode, SetKingPacket::decode, SetKingPacket.Handler::handle);

"The type SetKingPacket does not define encode(MSG, PacketBuffer) that is applicable here"

"The type of decode(PacketBuffer) from the type SetKingPacket is SetKingPacket, this is incompatible with the descriptor's return type: MSG"

"The type SetKingPacket.Handler does not define handle(MSG, Supplier<NetworkEvent.Context>) that is applicable here"

 

SetKingPacket.encode() is a static void method, but SimpleChannel.registerMessage() wants a BiConsumer<MSG,PacketBuffer> in that parameter. How would encode ever work?

 

Handler:

package com.siegemod.packets;

import com.siegemod.SiegeMod;

import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;

public class SiegePacketHandler {

	private static final String PROTOCOL_VERSION = "1";
	public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(new ResourceLocation(SiegeMod.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals,
			PROTOCOL_VERSION::equals);

	private static int id;

	public static void registerPackets() {
		INSTANCE.registerMessage(id++, SetKingPacket.class, SetKingPacket::encode, SetKingPacket::decode, SetKingPacket.Handler::handle);
	}
}

/*
// Sending to one player
INSTANCE.send(PacketDistributor.PLAYER.with(playerMP),new MyMessage());

// Send to all players tracking this chunk
INSTANCE.send(PacketDistributor.TRACKING_CHUNK.with(chunk),new MyMessage());

// Sending to all connected players
INSTANCE.send(PacketDistributor.ALL.noArg(),new MyMessage());
*/

Packet:

package com.siegemod.packets;

import com.google.common.base.Supplier;

import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

public class SetKingPacket {

	private final int data;

	public SetKingPacket(int dataIn) {
		this.data = dataIn;
	}

	public static void encode(SetKingPacket msg, PacketBuffer buf) {
		buf.writeInt(msg.data);
	}

	public static SetKingPacket decode(PacketBuffer buf) {
		int data = buf.readInt();

		return new SetKingPacket(data);
	}

	public static class Handler {

		public static void handle(SetKingPacket msg, Supplier<NetworkEvent.Context> ctx) {
			ctx.get().enqueueWork(() -> {
				// Work that needs to be threadsafe (most work)
				ServerPlayerEntity sender = ctx.get().getSender(); // the client that sent this packet
				// do stuff
			});
			ctx.get().setPacketHandled(true);
		}
	}
}

 

 

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.

Announcements



×
×
  • Create New...

Important Information

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