From 04b3a185f6307c1304328974a39f323f2945c81e Mon Sep 17 00:00:00 2001 From: iceBear67 Date: Sun, 8 Dec 2024 18:56:01 +0800 Subject: [PATCH] Rewritten protocol hacking for 1.21.2+ --- build.gradle | 9 +-- gradle/libs.versions.toml | 4 +- src/main/java/dev/tylerm/khs/Main.java | 8 ++- .../java/dev/tylerm/khs/game/events/Glow.java | 24 ++++---- .../khs/game/listener/DisguiseHandler.java | 43 ++++++-------- .../dev/tylerm/khs/game/util/Disguise.java | 58 +++++++++--------- .../khs/util/packet/AbstractPacket.java | 29 --------- .../khs/util/packet/BlockChangePacket.java | 24 -------- .../khs/util/packet/EntityMetadataPacket.java | 59 ------------------- .../khs/util/packet/EntityTeleportPacket.java | 29 --------- 10 files changed, 69 insertions(+), 218 deletions(-) delete mode 100644 src/main/java/dev/tylerm/khs/util/packet/AbstractPacket.java delete mode 100644 src/main/java/dev/tylerm/khs/util/packet/BlockChangePacket.java delete mode 100644 src/main/java/dev/tylerm/khs/util/packet/EntityMetadataPacket.java delete mode 100644 src/main/java/dev/tylerm/khs/util/packet/EntityTeleportPacket.java diff --git a/build.gradle b/build.gradle index 53f8a98..1a85675 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ repositories { } maven { - url = uri('https://repo.dmulloy2.net/repository/public/') + url = uri('https://repo.codemc.io/repository/maven-releases/') } maven { @@ -36,13 +36,14 @@ dependencies { compileOnly libs.org.xerial.sqlite.jdbc compileOnly libs.org.mariadb.jdbc.mariadb.java.client compileOnly libs.org.spigotmc.spigot.api - compileOnly libs.com.comphenix.protocol.protocollib + api libs.retrooper.packetevents compileOnly libs.me.clip.placeholderapi } shadowJar { - relocate 'com.cryptomorin.xseries', 'dev.tylerm.depend.xseries' - relocate 'com.zaxxer.hikari', 'dev.tylerm.depend.hikari' + relocate 'com.cryptomorin.xseries', 'khs.relocation.xseries' + relocate 'com.zaxxer.hikari', 'khs.relocation.hikari' + relocate 'com.github.retrooper', 'khs.relocation.retrooper' } publishing { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e0e9593..1ae4540 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,7 @@ # https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format [versions] -com-comphenix-protocol-protocollib = "5.1.0" +packetevents = "2.6.0" com-github-cryptomorin-xseries = "11.3.0" com-zaxxer-hikaricp = "4.0.3" me-clip-placeholderapi = "2.11.6" @@ -14,7 +14,7 @@ canvas = "1.7.0-SNAPSHOT" [libraries] -com-comphenix-protocol-protocollib = { module = "com.comphenix.protocol:ProtocolLib", version.ref = "com-comphenix-protocol-protocollib" } +retrooper-packetevents = {module = "com.github.retrooper:packetevents-spigot", version.ref="packetevents" } com-github-cryptomorin-xseries = { module = "com.github.cryptomorin:XSeries", version.ref = "com-github-cryptomorin-xseries" } com-zaxxer-hikaricp = { module = "com.zaxxer:HikariCP", version.ref = "com-zaxxer-hikaricp" } me-clip-placeholderapi = { module = "me.clip:placeholderapi", version.ref = "me-clip-placeholderapi" } diff --git a/src/main/java/dev/tylerm/khs/Main.java b/src/main/java/dev/tylerm/khs/Main.java index 7df9f6b..9011ee8 100644 --- a/src/main/java/dev/tylerm/khs/Main.java +++ b/src/main/java/dev/tylerm/khs/Main.java @@ -1,6 +1,7 @@ package dev.tylerm.khs; import com.cryptomorin.xseries.XItemStack; +import com.github.retrooper.packetevents.PacketEvents; import dev.tylerm.khs.command.*; import dev.tylerm.khs.command.map.Debug; import dev.tylerm.khs.command.map.GoTo; @@ -26,6 +27,7 @@ import dev.tylerm.khs.database.Database; import dev.tylerm.khs.command.util.CommandGroup; import dev.tylerm.khs.util.item.ItemStacks; +import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.command.Command; @@ -50,7 +52,6 @@ public class Main extends JavaPlugin implements Listener { private static Main instance; - private Database database; private Board board; private Disguiser disguiser; @@ -61,7 +62,8 @@ public class Main extends JavaPlugin implements Listener { public void onEnable() { - + PacketEvents.setAPI(SpigotPacketEventsBuilder.build(this)); + PacketEvents.getAPI().load(); long start = System.currentTimeMillis(); getLogger().info("Loading Kenshin's Hide and Seek"); @@ -98,6 +100,7 @@ public void onEnable() { this.disguiser = new Disguiser(); getLogger().info("Registering listeners..."); this.registerListeners(); + PacketEvents.getAPI().init(); getLogger().info("Registering commands..."); this.commandGroup = new CommandGroup("hs", @@ -250,6 +253,7 @@ public void onDisable() { } Bukkit.getServer().getMessenger().unregisterOutgoingPluginChannel(this); + PacketEvents.getAPI().terminate(); } private void onTick() { diff --git a/src/main/java/dev/tylerm/khs/game/events/Glow.java b/src/main/java/dev/tylerm/khs/game/events/Glow.java index 6015f26..ede275b 100644 --- a/src/main/java/dev/tylerm/khs/game/events/Glow.java +++ b/src/main/java/dev/tylerm/khs/game/events/Glow.java @@ -1,17 +1,17 @@ package dev.tylerm.khs.game.events; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; -import dev.tylerm.khs.util.packet.EntityMetadataPacket; +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.protocol.entity.data.EntityData; +import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata; import dev.tylerm.khs.Main; import org.bukkit.entity.Player; +import java.util.List; + import static dev.tylerm.khs.configuration.Config.*; public class Glow { - - private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); - private int glowTime; private boolean running; @@ -60,13 +60,11 @@ public boolean isRunning() { } public void setGlow(Player player, Player target, boolean glowing) { - - EntityMetadataPacket packet = new EntityMetadataPacket(); - packet.setEntity(target); - packet.setGlow(glowing); - packet.writeMetadata(); - packet.send(player); - + var packet = new WrapperPlayServerEntityMetadata( + target.getEntityId(), + List.of(new EntityData(0, EntityDataTypes.BYTE, glowing ? 0x40 : (byte)0)) + ); + PacketEvents.getAPI().getPlayerManager().sendPacket(player, packet); } } diff --git a/src/main/java/dev/tylerm/khs/game/listener/DisguiseHandler.java b/src/main/java/dev/tylerm/khs/game/listener/DisguiseHandler.java index 1a1212c..56684a7 100644 --- a/src/main/java/dev/tylerm/khs/game/listener/DisguiseHandler.java +++ b/src/main/java/dev/tylerm/khs/game/listener/DisguiseHandler.java @@ -1,17 +1,13 @@ package dev.tylerm.khs.game.listener; -import static com.comphenix.protocol.PacketType.Play.Client.*; - -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.wrappers.EnumWrappers; -import com.comphenix.protocol.wrappers.WrappedEnumEntityUseAction; +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.event.PacketListener; +import com.github.retrooper.packetevents.event.PacketListenerPriority; +import com.github.retrooper.packetevents.event.PacketReceiveEvent; +import com.github.retrooper.packetevents.protocol.packettype.PacketType; +import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity; import dev.tylerm.khs.Main; import dev.tylerm.khs.game.util.Disguise; -import dev.tylerm.khs.game.util.Status; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.attribute.Attribute; @@ -23,7 +19,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import java.util.ArrayList; @@ -31,10 +26,8 @@ public class DisguiseHandler implements Listener { - private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); - public DisguiseHandler() { - protocolManager.addPacketListener(createProtocol()); + PacketEvents.getAPI().getEventManager().registerListener(createProtocol(), PacketListenerPriority.NORMAL); } @EventHandler(priority = EventPriority.HIGHEST) @@ -49,24 +42,22 @@ public void onMove(PlayerMoveEvent event) { disguise.startSolidifying(); } - private PacketAdapter createProtocol() { - return new PacketAdapter(Main.getInstance(), USE_ENTITY) { - + private PacketListener createProtocol() { + return new PacketListener(){ @Override - public void onPacketReceiving(PacketEvent event) { - PacketContainer packet = event.getPacket(); - + public void onPacketReceive(PacketReceiveEvent event) { + if(event.getPacketType() != PacketType.Play.Client.INTERACT_ENTITY) return; + var packet = new WrapperPlayClientInteractEntity(event); + var action = packet.getAction(); // only left click attacks //EnumWrappers.EntityUseAction action = packet.getEntityUseActions().getValues().stream().findFirst().orElse(null); - var action = packet.getEnumEntityUseActions().getValues().stream().findFirst().orElse(null); - if (action == null || action.getAction() != EnumWrappers.EntityUseAction.ATTACK) return; + if(action != WrapperPlayClientInteractEntity.InteractAction.ATTACK) return; //noinspection ComparatorResultComparison - if (action.getAction().compareTo(EnumWrappers.EntityUseAction.INTERACT) == 2) { - return; - } + // iceBear: Idk what is this but it works anyfucks + if(action.compareTo(WrapperPlayClientInteractEntity.InteractAction.INTERACT) == 2) return; Player player = event.getPlayer(); - int id = packet.getIntegers().read(0); + int id = packet.getEntityId(); Disguise disguise = Main.getInstance().getDisguiser().getByEntityID(id); if (disguise == null) disguise = Main.getInstance().getDisguiser().getByHitBoxID(id); if (disguise == null) return; diff --git a/src/main/java/dev/tylerm/khs/game/util/Disguise.java b/src/main/java/dev/tylerm/khs/game/util/Disguise.java index f284341..792071a 100644 --- a/src/main/java/dev/tylerm/khs/game/util/Disguise.java +++ b/src/main/java/dev/tylerm/khs/game/util/Disguise.java @@ -2,24 +2,20 @@ import com.cryptomorin.xseries.XSound; import com.cryptomorin.xseries.messages.ActionBar; -import dev.tylerm.khs.configuration.Map; +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.util.Vector3d; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityTeleport; import dev.tylerm.khs.util.Helper; -import dev.tylerm.khs.util.packet.BlockChangePacket; -import dev.tylerm.khs.util.packet.EntityTeleportPacket; import dev.tylerm.khs.Main; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.block.Block; import org.bukkit.entity.*; -import org.bukkit.loot.LootTable; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Team; -import java.util.HashMap; - @SuppressWarnings("deprecation") public class Disguise { final Player hider; @@ -118,26 +114,32 @@ public void update() { private void onSolidifyChanged(boolean solid) { if (solid) { - var tp = new EntityTeleportPacket(); - tp.setEntity(getPlayer()); + var loc = Helper.obfuscateLocation(getPlayer().getLocation()); + var vec = new Vector3d(loc.getX(), -66.0, loc.getZ()); + var tp = new WrapperPlayServerEntityTeleport( + getPlayer().getEntityId(), + new com.github.retrooper.packetevents.protocol.world.Location( + vec, loc.getYaw(), loc.getPitch() + ), + true + ); // seekers may see a "phantom" when hiders come back to their block. // so obfuscating is a little bit necessary - var loc = Helper.obfuscateLocation(getPlayer().getLocation()); - tp.setX(loc.getX()); - tp.setY(-66); - tp.setZ(loc.getZ()); for (Player seeker : Main.getInstance().getBoard().getSeekers()) { - tp.send(seeker); + PacketEvents.getAPI().getPlayerManager().sendPacket(seeker, tp); } } else { - var tp = new EntityTeleportPacket(); - tp.setEntity(getPlayer()); var loc = getPlayer().getLocation(); - tp.setX(loc.getX()); - tp.setY(loc.getY()); - tp.setZ(loc.getZ()); + var tp = new WrapperPlayServerEntityTeleport( + getPlayer().getEntityId(), + new com.github.retrooper.packetevents.protocol.world.Location( + new Vector3d(loc.getX(), loc.getY(),loc.getZ()), + loc.getYaw(), loc.getPitch() + ), + getPlayer().isOnGround() + ); for (Player seeker : Main.getInstance().getBoard().getSeekers()) { - tp.send(seeker); + PacketEvents.getAPI().getPlayerManager().sendPacket(seeker, tp); } } } @@ -147,19 +149,16 @@ public void setSolidify(boolean value) { } private void sendBlockUpdate(Location location, Material material) { - BlockChangePacket packet = new BlockChangePacket(); - packet.setBlockPosition(location); - packet.setMaterial(material); + var data = material.createBlockData(); Bukkit.getOnlinePlayers().forEach(receiver -> { if (receiver.getName().equals(hider.getName())) return; - packet.send(receiver); + receiver.sendBlockChange(location, data); }); } private void teleportEntity(Entity entity, boolean center) { if (entity == null) return; - EntityTeleportPacket packet = new EntityTeleportPacket(); - packet.setEntity(entity); + WrapperPlayServerEntityTeleport packet; double x, y, z; if (center) { x = Math.round(hider.getLocation().getX() + .5) - .5; @@ -170,10 +169,9 @@ private void teleportEntity(Entity entity, boolean center) { y = hider.getLocation().getY(); z = hider.getLocation().getZ(); } - packet.setX(x); - packet.setY(y); - packet.setZ(z); - Bukkit.getOnlinePlayers().forEach(packet::send); + var pos = entity.getLocation(); + packet = new WrapperPlayServerEntityTeleport(entity.getEntityId(),new Vector3d(x,y,z),pos.getYaw(),pos.getPitch(),entity.isOnGround()); + Bukkit.getOnlinePlayers().forEach(p -> PacketEvents.getAPI().getPlayerManager().sendPacket(p, packet)); } private void toggleEntityVisibility(Entity entity, boolean show) { diff --git a/src/main/java/dev/tylerm/khs/util/packet/AbstractPacket.java b/src/main/java/dev/tylerm/khs/util/packet/AbstractPacket.java deleted file mode 100644 index e6113ac..0000000 --- a/src/main/java/dev/tylerm/khs/util/packet/AbstractPacket.java +++ /dev/null @@ -1,29 +0,0 @@ -package dev.tylerm.khs.util.packet; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; -import com.comphenix.protocol.events.PacketContainer; -import org.bukkit.entity.Player; - -import java.lang.reflect.InvocationTargetException; - -public class AbstractPacket { - - private static final ProtocolManager protocolManager; - static { - protocolManager = ProtocolLibrary.getProtocolManager(); - } - - protected final PacketContainer packet; - - protected AbstractPacket(PacketType type){ - packet = protocolManager.createPacket(type); - packet.getModifier().writeDefaults(); - } - - public void send(Player player){ - protocolManager.sendServerPacket(player, packet); - } - -} diff --git a/src/main/java/dev/tylerm/khs/util/packet/BlockChangePacket.java b/src/main/java/dev/tylerm/khs/util/packet/BlockChangePacket.java deleted file mode 100644 index 0603832..0000000 --- a/src/main/java/dev/tylerm/khs/util/packet/BlockChangePacket.java +++ /dev/null @@ -1,24 +0,0 @@ -package dev.tylerm.khs.util.packet; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.wrappers.BlockPosition; -import com.comphenix.protocol.wrappers.WrappedBlockData; -import org.bukkit.Location; -import org.bukkit.Material; -import org.jetbrains.annotations.NotNull; - -public class BlockChangePacket extends AbstractPacket { - - public BlockChangePacket(){ - super(PacketType.Play.Server.BLOCK_CHANGE); - } - - public void setBlockPosition(@NotNull Location location){ - super.packet.getBlockPositionModifier().write(0, new BlockPosition(location.toVector())); - } - - public void setMaterial(Material material){ - super.packet.getBlockData().write(0, WrappedBlockData.createData(material)); - } - -} diff --git a/src/main/java/dev/tylerm/khs/util/packet/EntityMetadataPacket.java b/src/main/java/dev/tylerm/khs/util/packet/EntityMetadataPacket.java deleted file mode 100644 index ddafcd3..0000000 --- a/src/main/java/dev/tylerm/khs/util/packet/EntityMetadataPacket.java +++ /dev/null @@ -1,59 +0,0 @@ -package dev.tylerm.khs.util.packet; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.wrappers.WrappedDataValue; -import com.comphenix.protocol.wrappers.WrappedDataWatcher; -import com.comphenix.protocol.wrappers.WrappedWatchableObject; - -import dev.tylerm.khs.Main; - -import org.bukkit.entity.Entity; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; - -public class EntityMetadataPacket extends AbstractPacket { - - private final WrappedDataWatcher watcher; - private final WrappedDataWatcher.Serializer serializer; - - public EntityMetadataPacket() { - super(PacketType.Play.Server.ENTITY_METADATA); - watcher = new WrappedDataWatcher(); - serializer = WrappedDataWatcher.Registry.get(Byte.class); - } - - public void setEntity(@NotNull Entity target) { - super.packet.getIntegers().write(0, target.getEntityId()); - watcher.setEntity(target); - } - - public void setGlow(boolean glowing) { - if (glowing) { - watcher.setObject(0, serializer, (byte) (0x40)); - } else { - watcher.setObject(0, serializer, (byte) (0x0)); - } - } - - public void writeMetadata() { - final List wrappedDataValueList = new ArrayList<>(); - - for (final WrappedWatchableObject entry : watcher.getWatchableObjects()) { - if (entry == null) continue; - - final WrappedDataWatcher.WrappedDataWatcherObject watcherObject = entry.getWatcherObject(); - wrappedDataValueList.add( - new WrappedDataValue( - watcherObject.getIndex(), - watcherObject.getSerializer(), - entry.getRawValue() - ) - ); - } - - packet.getDataValueCollectionModifier().write(0, wrappedDataValueList); - } - -} diff --git a/src/main/java/dev/tylerm/khs/util/packet/EntityTeleportPacket.java b/src/main/java/dev/tylerm/khs/util/packet/EntityTeleportPacket.java deleted file mode 100644 index cd29e28..0000000 --- a/src/main/java/dev/tylerm/khs/util/packet/EntityTeleportPacket.java +++ /dev/null @@ -1,29 +0,0 @@ -package dev.tylerm.khs.util.packet; - -import com.comphenix.protocol.PacketType; -import org.bukkit.entity.Entity; -import org.jetbrains.annotations.NotNull; - -public class EntityTeleportPacket extends AbstractPacket { - - public EntityTeleportPacket(){ - super(PacketType.Play.Server.ENTITY_TELEPORT); - } - - public void setEntity(@NotNull Entity entity){ - super.packet.getIntegers().write(0, entity.getEntityId()); - } - - public void setX(double x){ - super.packet.getDoubles().write(0, x); - } - - public void setY(double y){ - super.packet.getDoubles().write(1, y); - } - - public void setZ(double z){ - super.packet.getDoubles().write(2, z); - } - -}