Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register custom packet with modded client #195

Open
KinderrKill opened this issue Nov 28, 2021 · 0 comments
Open

Register custom packet with modded client #195

KinderrKill opened this issue Nov 28, 2021 · 0 comments

Comments

@KinderrKill
Copy link

KinderrKill commented Nov 28, 2021

Hello ! First of all thanks for all the work you've done with ProtocolLib since so many years.

I use ProtocolLib 3.5.0 for my old project and i want to made a public API for allow modder to simply create and register new packets for modded client.

It work well with 3.5.0 and 1.8.8, but with the 4.7.0 version i can"t understand and use my code. (I'm still in 1.8.8)

Here what i have now

`public void registerPacket(Class<? extends CocPacket> packetClass, int packetId, Sender sender) {

	final PacketType packetType = new PacketType(Protocol.PLAY, sender, packetId, MinecraftVersion.BOUNTIFUL_UPDATE, packetClass.getSimpleName());
	packetToType.put(packetClass, packetType);

	final EnumProtocol protocol = EnumProtocol.PLAY;
	final EnumProtocolDirection direction = packetType.isClient() ? EnumProtocolDirection.SERVERBOUND : EnumProtocolDirection.CLIENTBOUND;

	try {
		Map<EnumProtocolDirection, BiMap<Integer, Class<? extends Packet<?>>>> theMap = (Map<EnumProtocolDirection, BiMap<Integer, Class<? extends Packet<?>>>>) FieldUtils.readField(protocol, "j", true);

		BiMap<Integer, Class<? extends Packet<?>>> biMap = theMap.get(direction);
		biMap.put(packetId, (Class<? extends Packet<?>>) packetClass);
		theMap.put(direction, biMap);

	} catch(IllegalAccessException e) {
		e.printStackTrace();
	}

	Map<Class<?>, EnumProtocol> map = (Map<Class<?>, EnumProtocol>) Accessors.getFieldAccessor(EnumProtocol.class, Map.class, true).get(protocol);
	map.put(packetClass, protocol);
}

public void sendCustomPacket(Player player, SPacket packet) {
	PacketContainer container = new PacketContainer(packetToType.get(packet.getClass()), packet);

	try {
		ProtocolLibrary.getProtocolManager().sendServerPacket(player, container);
	} catch(InvocationTargetException e) {
		e.printStackTrace();
	}
}`

With 4.7.0 i juste change the packetType declaration and that the error, now i have when i want to use an custom packet :
Could not find packet for type null

When i print the packetType, yeah, it's null, but with variable inside, for exemple
Packet : CPacketPosition || PacketType : null[PLAY, CLIENT, 127, classNames: [net.minecraft.network.play.client.CPacketPosition] (unregistered)]

Packet : SPacketPosition || PacketType : null[PLAY, SERVER, 175, classNames: [net.minecraft.network.play.server.SPacketPosition] (unregistered)]

I search on internet but it's realy rare to find users using ProtocolLib for custom packet, but it's so simple with you're lib.

Thanks in advance for the help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant