Added more stuff to the PacketHandler

This commit is contained in:
ImHacking 2022-07-03 12:16:06 -04:00
parent 4dbe6b1853
commit c648da98b7
4 changed files with 42 additions and 8 deletions

View File

@ -19,6 +19,8 @@ import java.util.concurrent.*;
public class eSpigot {
private final Set<PacketHandler> packetHandlers = new HashSet<>();
private final Set<PacketHandler> cancellablePacketHandlers = new HashSet<>();
private final Set<MovementHandler> movementHandlers = new HashSet<>();
public static final ScheduledExecutorService EXECUTOR_SERVICE =
Executors.newSingleThreadScheduledExecutor();
@ -49,6 +51,7 @@ public class eSpigot {
public void addPacketHandler(PacketHandler handler) {
Bukkit.getLogger().info("Adding packet handler: " + handler.getClass().getPackage().getName() + "." + handler.getClass().getSimpleName());
if (handler.handlesCancellable()) this.cancellablePacketHandlers.add(handler);
this.packetHandlers.add(handler);
}
@ -61,6 +64,10 @@ public class eSpigot {
return this.movementHandlers;
}
public Set<PacketHandler> getCancellablePacketHandlers() {
return this.cancellablePacketHandlers;
}
public Set<PacketHandler> getPacketHandlers() {
return this.packetHandlers;
}

View File

@ -5,13 +5,23 @@ import net.minecraft.server.PlayerConnection;
public interface PacketHandler {
default void handleReceivedPacket(PlayerConnection connection, Packet<?> packet) {}
default void handleReceivedPacket(PlayerConnection connection, Packet<?> packet) {
}
default void handleSentPacket(PlayerConnection connection, Packet<?> packet) {}
default void handleSentPacket(PlayerConnection connection, Packet<?> packet) {
}
default boolean handleSentPacketCancellable(PlayerConnection connection, Packet<?> packet) {
return true;
}
default boolean handleReceivedPacketCancellable(PlayerConnection connection, Packet<?> packet) {
return true;
}
default boolean handlesCancellable() {
return false;
}
}

View File

@ -3,6 +3,7 @@ package net.minecraft.server;
import com.elevatemc.spigot.eSpigot;
import com.elevatemc.spigot.exception.CryptException;
import com.elevatemc.spigot.exception.ExploitException;
import com.elevatemc.spigot.handler.PacketHandler;
import com.velocitypowered.natives.compression.VelocityCompressor; // Paper
import com.velocitypowered.natives.util.Natives; // Paper
import com.google.common.collect.Queues;
@ -157,7 +158,6 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
}
this.close(chatmessage);
// if (MinecraftServer.getServer().isDebugging()) throwable.printStackTrace(); // Spigot
}
protected void a(ChannelHandlerContext channelhandlercontext, Packet packet) throws Exception {
@ -166,8 +166,21 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
packet.a(this.m);
if (this.m instanceof PlayerConnection) {
//ImHacking Start
for (PacketHandler handler : eSpigot.getInstance().getCancellablePacketHandlers()) {
try {
if (!handler.handleReceivedPacketCancellable((PlayerConnection) this.m, packet)) {
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//ImHacking End
try {
eSpigot.getInstance().getPacketHandlers().forEach(packetHandler -> packetHandler.handleReceivedPacket((PlayerConnection) this.m, packet));
for (PacketHandler handler : eSpigot.getInstance().getPacketHandlers()) {
handler.handleReceivedPacket((PlayerConnection) this.m, packet);
}
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -967,6 +967,7 @@ public class PlayerConnection implements PacketListenerPlayIn, IUpdatePlayerList
// CraftBukkit end
try {
// ImHacking - Start
// Loop through cancellable handlers first
for (PacketHandler handler : eSpigot.getInstance().getPacketHandlers()) {
try {
@ -977,13 +978,16 @@ public class PlayerConnection implements PacketListenerPlayIn, IUpdatePlayerList
e.printStackTrace();
}
}
// Loop through normal handlers
for (PacketHandler handler : eSpigot.getInstance().getPacketHandlers()) {
handler.handleSentPacket(this, packet);
try {
handler.handleSentPacket(this, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
this.networkManager.handle(packet);
// ImHacking - End
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.a(throwable, "Sending packet");
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Packet being sent");