diff --git a/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java b/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java index 94f6131..54cc303 100644 --- a/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java +++ b/SpigotCore_18/src/de/steamwar/core/ProtocolWrapper18.java @@ -22,8 +22,10 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; import com.mojang.authlib.GameProfile; import com.mojang.datafixers.util.Pair; +import org.bukkit.GameMode; import java.util.Collections; +import java.util.EnumMap; import java.util.List; public class ProtocolWrapper18 implements ProtocolWrapper { @@ -34,9 +36,26 @@ public class ProtocolWrapper18 implements ProtocolWrapper { equipmentStack.set(packet, Collections.singletonList(new Pair<>(slot, stack))); } - private static final Reflection.ConstructorInvoker playerInfoDataConstructor = Reflection.getConstructor(playerInfoDataClass, GameProfile.class, int.class, enumGamemode, iChatBaseComponent); + private static final Class playerInfoPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutPlayerInfo"); + private static final Class playerInfoActionClass = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutPlayerInfo$EnumPlayerInfoAction"); + private static final Reflection.FieldAccessor playerInfoAction = Reflection.getField(playerInfoPacket, playerInfoActionClass, 0); + private static final Reflection.FieldAccessor playerInfoData = Reflection.getField(playerInfoPacket, List.class, 0); + private static final EnumMap actions = new EnumMap<>(PlayerInfoAction.class); + static { + Object[] nativeActions = playerInfoActionClass.getEnumConstants(); + actions.put(PlayerInfoAction.ADD, nativeActions[0]); + actions.put(PlayerInfoAction.GAMEMODE, nativeActions[1]); + actions.put(PlayerInfoAction.REMOVE, nativeActions[4]); + } + private static final Class iChatBaseComponent = Reflection.getClass("{nms.network.chat}.IChatBaseComponent"); + private static final Reflection.ConstructorInvoker playerInfoDataConstructor = Reflection.getConstructor("{nms.network.protocol.game}.PacketPlayOutPlayerInfo$PlayerInfoData", GameProfile.class, int.class, enumGamemode, iChatBaseComponent); + @Override - public Object playerInfoDataConstructor(Object packet, GameProfile profile, Object mode) { - return playerInfoDataConstructor.invoke(profile, 0, mode, null); + @SuppressWarnings("deprecation") + public Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode) { + Object packet = Reflection.newInstance(playerInfoPacket); + playerInfoAction.set(packet, actions.get(action)); + playerInfoData.set(packet, Collections.singletonList(playerInfoDataConstructor.invoke(profile, 0, ProtocolWrapper.getGameModeById.invoke(null, mode.getValue()), null))); + return packet; } } diff --git a/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java b/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java index 86c23fb..b4b0f8f 100644 --- a/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java +++ b/SpigotCore_19/src/de/steamwar/core/ProtocolWrapper19.java @@ -22,8 +22,13 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; import com.mojang.authlib.GameProfile; import com.mojang.datafixers.util.Pair; +import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket; +import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket; +import net.minecraft.world.level.EnumGamemode; +import org.bukkit.GameMode; import java.util.Collections; +import java.util.EnumSet; import java.util.List; public class ProtocolWrapper19 implements ProtocolWrapper { @@ -34,9 +39,19 @@ public class ProtocolWrapper19 implements ProtocolWrapper { equipmentStack.set(packet, Collections.singletonList(new Pair<>(slot, stack))); } - private static final Reflection.ConstructorInvoker playerInfoDataConstructor = Reflection.getConstructor(playerInfoDataClass, GameProfile.class, int.class, enumGamemode, iChatBaseComponent, Reflection.getClass("net.minecraft.world.entity.player.ProfilePublicKey$a")); + private static final Reflection.ConstructorInvoker removePacketConstructor = Reflection.getConstructor(ClientboundPlayerInfoRemovePacket.class, List.class); + private static final Reflection.FieldAccessor updateActions = Reflection.getField(ClientboundPlayerInfoUpdatePacket.class, EnumSet.class, 0); + private static final Reflection.FieldAccessor updatePlayers = Reflection.getField(ClientboundPlayerInfoUpdatePacket.class, List.class, 0); + @Override - public Object playerInfoDataConstructor(Object packet, GameProfile profile, Object mode) { - return playerInfoDataConstructor.invoke(profile, 0, mode, null, null); + @SuppressWarnings("deprecation") + public Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode) { + if(action == PlayerInfoAction.REMOVE) + return removePacketConstructor.invoke(Collections.singletonList(profile.getId())); + + Object packet = Reflection.newInstance(ClientboundPlayerInfoUpdatePacket.class); + updateActions.set(packet, action == PlayerInfoAction.ADD ? EnumSet.of(ClientboundPlayerInfoUpdatePacket.a.a, ClientboundPlayerInfoUpdatePacket.a.c) : EnumSet.of(ClientboundPlayerInfoUpdatePacket.a.c)); + updatePlayers.set(packet, Collections.singletonList(new ClientboundPlayerInfoUpdatePacket.b(profile.getId(), profile, false, 0, EnumGamemode.a(mode.getValue()), null, null))); + return packet; } } diff --git a/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java b/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java index 98aa0a2..9f0b234 100644 --- a/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java +++ b/SpigotCore_8/src/de/steamwar/core/ProtocolWrapper8.java @@ -21,6 +21,11 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; import com.mojang.authlib.GameProfile; +import org.bukkit.GameMode; + +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; public class ProtocolWrapper8 implements ProtocolWrapper { @@ -41,9 +46,26 @@ public class ProtocolWrapper8 implements ProtocolWrapper { equipmentStack.set(packet, stack); } - private static final Reflection.ConstructorInvoker playerInfoDataConstructor = Reflection.getConstructor(playerInfoDataClass, playerInfoPacket, GameProfile.class, int.class, enumGamemode, iChatBaseComponent); + private static final Class playerInfoPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutPlayerInfo"); + private static final Class playerInfoActionClass = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutPlayerInfo$EnumPlayerInfoAction"); + private static final Reflection.FieldAccessor playerInfoAction = Reflection.getField(playerInfoPacket, playerInfoActionClass, 0); + private static final Reflection.FieldAccessor playerInfoData = Reflection.getField(playerInfoPacket, List.class, 0); + private static final EnumMap actions = new EnumMap<>(PlayerInfoAction.class); + static { + Object[] nativeActions = playerInfoActionClass.getEnumConstants(); + actions.put(PlayerInfoAction.ADD, nativeActions[0]); + actions.put(PlayerInfoAction.GAMEMODE, nativeActions[1]); + actions.put(PlayerInfoAction.REMOVE, nativeActions[4]); + } + private static final Class iChatBaseComponent = Reflection.getClass("{nms.network.chat}.IChatBaseComponent"); + private static final Reflection.ConstructorInvoker playerInfoDataConstructor = Reflection.getConstructor("{nms.network.protocol.game}.PacketPlayOutPlayerInfo$PlayerInfoData", playerInfoPacket, GameProfile.class, int.class, enumGamemode, iChatBaseComponent); + @Override - public Object playerInfoDataConstructor(Object packet, GameProfile profile, Object mode) { - return playerInfoDataConstructor.invoke(packet, profile, 0, mode, null); + @SuppressWarnings("deprecation") + public Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode) { + Object packet = Reflection.newInstance(playerInfoPacket); + playerInfoAction.set(packet, actions.get(action)); + playerInfoData.set(packet, Collections.singletonList(playerInfoDataConstructor.invoke(packet, profile, 0, ProtocolWrapper.getGameModeById.invoke(null, mode.getValue()), null))); + return packet; } } diff --git a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java index ca02e58..6d140ca 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/ProtocolWrapper.java @@ -21,6 +21,7 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; import com.mojang.authlib.GameProfile; +import org.bukkit.GameMode; public interface ProtocolWrapper { @@ -28,10 +29,9 @@ public interface ProtocolWrapper { Class spawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutSpawnEntity"); Class spawnLivingPacket = Core.getVersion() > 18 ? ProtocolWrapper.spawnPacket : Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutSpawnEntityLiving"); Class equipmentPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityEquipment"); - Class playerInfoPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutPlayerInfo"); - Class playerInfoDataClass = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutPlayerInfo$PlayerInfoData"); + Class enumGamemode = Reflection.getClass(Core.getVersion() > 9 ? "{nms.world.level}.EnumGamemode" : "{nms}.WorldSettings$EnumGamemode"); - Class iChatBaseComponent = Reflection.getClass("{nms.network.chat}.IChatBaseComponent"); + Reflection.MethodInvoker getGameModeById = Reflection.getTypedMethod(enumGamemode, null, enumGamemode, int.class); // 0: hand, 1: offhand, 2: feet, 3: legs, 4: chest, 5: head Object[] itemSlots = Core.getVersion() > 8 ? Reflection.getClass("{nms.world.entity}.EnumItemSlot").getEnumConstants() : new Integer[]{0, 0, 1, 2, 3, 4}; @@ -40,6 +40,11 @@ public interface ProtocolWrapper { void setEquipmentPacketStack(Object packet, Object slot, Object stack); - Object playerInfoDataConstructor(Object packet, GameProfile profile, Object mode); + Object playerInfoPacketConstructor(PlayerInfoAction action, GameProfile profile, GameMode mode); + enum PlayerInfoAction { + ADD, + GAMEMODE, + REMOVE + } } diff --git a/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java b/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java index fa6d513..013642d 100644 --- a/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java +++ b/SpigotCore_Main/src/de/steamwar/entity/RPlayer.java @@ -25,12 +25,11 @@ import de.steamwar.core.BountifulWrapper; import de.steamwar.core.Core; import de.steamwar.core.FlatteningWrapper; import de.steamwar.core.ProtocolWrapper; +import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; -import java.util.Collections; -import java.util.List; import java.util.Map; import java.util.UUID; import java.util.function.Consumer; @@ -83,7 +82,7 @@ public class RPlayer extends REntity { @Override void spawn(Consumer packetSink) { - packetSink.accept(getPlayerInfoPacket(addPlayer)); + packetSink.accept(ProtocolWrapper.impl.playerInfoPacketConstructor(ProtocolWrapper.PlayerInfoAction.ADD, new GameProfile(uuid, name), GameMode.CREATIVE)); packetSink.accept(getNamedSpawnPacket()); packetSink.accept(getDataWatcherPacket(skinPartsDataWatcher, (byte) 0x7F)); @@ -98,20 +97,7 @@ public class RPlayer extends REntity { void despawn(Consumer packetSink) { //team.removeEntry(name); super.despawn(packetSink); - packetSink.accept(getPlayerInfoPacket(removePlayer)); - } - - private static final Class playerInfoActionClass = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutPlayerInfo$EnumPlayerInfoAction"); - private static final Object addPlayer = playerInfoActionClass.getEnumConstants()[0]; - private static final Reflection.FieldAccessor playerInfoAction = Reflection.getField(ProtocolWrapper.playerInfoPacket, playerInfoActionClass, 0); - private static final Object removePlayer = playerInfoActionClass.getEnumConstants()[4]; - private static final Reflection.FieldAccessor playerInfoData = Reflection.getField(ProtocolWrapper.playerInfoPacket, List.class, 0); - private static final Object creative = ProtocolWrapper.enumGamemode.getEnumConstants()[Core.getVersion() > 15 ? 1 : 2]; - private Object getPlayerInfoPacket(Object action){ - Object packet = Reflection.newInstance(ProtocolWrapper.playerInfoPacket); - playerInfoAction.set(packet, action); - playerInfoData.set(packet, Collections.singletonList(ProtocolWrapper.impl.playerInfoDataConstructor(packet, new GameProfile(uuid, name), creative))); - return packet; + packetSink.accept(ProtocolWrapper.impl.playerInfoPacketConstructor(ProtocolWrapper.PlayerInfoAction.REMOVE, new GameProfile(uuid, name), GameMode.CREATIVE)); } private static final Class namedSpawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutNamedEntitySpawn");