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

View File

@ -5,13 +5,23 @@ import net.minecraft.server.PlayerConnection;
public interface PacketHandler { 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) { default boolean handleSentPacketCancellable(PlayerConnection connection, Packet<?> packet) {
return true; 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.eSpigot;
import com.elevatemc.spigot.exception.CryptException; import com.elevatemc.spigot.exception.CryptException;
import com.elevatemc.spigot.exception.ExploitException; import com.elevatemc.spigot.exception.ExploitException;
import com.elevatemc.spigot.handler.PacketHandler;
import com.velocitypowered.natives.compression.VelocityCompressor; // Paper import com.velocitypowered.natives.compression.VelocityCompressor; // Paper
import com.velocitypowered.natives.util.Natives; // Paper import com.velocitypowered.natives.util.Natives; // Paper
import com.google.common.collect.Queues; import com.google.common.collect.Queues;
@ -157,7 +158,6 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
} }
this.close(chatmessage); this.close(chatmessage);
// if (MinecraftServer.getServer().isDebugging()) throwable.printStackTrace(); // Spigot
} }
protected void a(ChannelHandlerContext channelhandlercontext, Packet packet) throws Exception { protected void a(ChannelHandlerContext channelhandlercontext, Packet packet) throws Exception {
@ -166,8 +166,21 @@ public class NetworkManager extends SimpleChannelInboundHandler<Packet<?>> {
packet.a(this.m); packet.a(this.m);
if (this.m instanceof PlayerConnection) { 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 { 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

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