From 0a90cabf191b060db8e4b44964ac9669b3f90925 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Feb 2023 21:27:37 +0100 Subject: [PATCH] Expand AntiNocom --- .../de/steamwar/core/events/AntiNocom.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java b/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java index aea52b2..7cdf31f 100644 --- a/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java +++ b/SpigotCore_Main/src/de/steamwar/core/events/AntiNocom.java @@ -28,10 +28,45 @@ import de.steamwar.techhider.TechHider; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import java.util.function.Function; + public class AntiNocom { public AntiNocom() { TinyProtocol.instance.addFilter(blockDig, this::onDig); + + if(Core.getVersion() > 8) { + registerUseItem(); + } + } + + private void registerUseItem() { + Class useItem = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInUseItem"); + + Function getPosition; + if(Core.getVersion() > 12) { + Class movingObjectPositionBlock = Reflection.getClass("{nms.world.phys}.MovingObjectPositionBlock"); + Reflection.FieldAccessor useItemPosition = Reflection.getField(useItem, movingObjectPositionBlock, 0); + Reflection.FieldAccessor movingBlockPosition = Reflection.getField(movingObjectPositionBlock, TechHider.blockPosition, 0); + + getPosition = (packet) -> movingBlockPosition.get(useItemPosition.get(packet)); + } else { + getPosition = Reflection.getField(blockDig, TechHider.blockPosition, 0)::get; + } + + TinyProtocol.instance.addFilter(useItem, (player, packet) -> { + Object pos = getPosition.apply(packet); + int x = TechHider.blockPositionX.get(pos); + int z = TechHider.blockPositionZ.get(pos); + + if(!player.getWorld().isChunkLoaded(ProtocolUtils.posToChunk(x), ProtocolUtils.posToChunk(z))) { + Bukkit.getScheduler().runTask(Core.getInstance(), () -> player.kickPlayer(null)); + SWException.log(player.getName() + " kicked for using item on unloaded block (potential NoCom-DOS attack)", x + " " + z); + return null; + } + + return packet; + }); } private static final Class blockDig = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInBlockDig");