Skip to content

Commit

Permalink
Rewritten protocol hacking for 1.21.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
iceBear67 committed Dec 8, 2024
1 parent 3059c70 commit 04b3a18
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 218 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
}

maven {
url = uri('https://repo.dmulloy2.net/repository/public/')
url = uri('https://repo.codemc.io/repository/maven-releases/')
}

maven {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/dev/tylerm/khs/Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -50,7 +52,6 @@
public class Main extends JavaPlugin implements Listener {

private static Main instance;

private Database database;
private Board board;
private Disguiser disguiser;
Expand All @@ -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");
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -250,6 +253,7 @@ public void onDisable() {
}

Bukkit.getServer().getMessenger().unregisterOutgoingPluginChannel(this);
PacketEvents.getAPI().terminate();
}

private void onTick() {
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/dev/tylerm/khs/game/events/Glow.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}

}
43 changes: 17 additions & 26 deletions src/main/java/dev/tylerm/khs/game/listener/DisguiseHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,18 +19,15 @@
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;
import java.util.List;

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)
Expand All @@ -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;
Expand Down
58 changes: 28 additions & 30 deletions src/main/java/dev/tylerm/khs/game/util/Disguise.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/dev/tylerm/khs/util/packet/AbstractPacket.java

This file was deleted.

24 changes: 0 additions & 24 deletions src/main/java/dev/tylerm/khs/util/packet/BlockChangePacket.java

This file was deleted.

Loading

0 comments on commit 04b3a18

Please sign in to comment.