aadnk / ProtocolLib

Provides read and write access to the Minecraft protocol with Bukkit.
GNU General Public License v2.0
288 stars 92 forks source link

Register custom packet with modded client #195

Open KinderrKill opened 2 years ago

KinderrKill commented 2 years ago

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 !