From ab170426cbddfbff855d24ba7531e78cdf14baa3 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 15 Nov 2020 21:54:06 +0100 Subject: [PATCH] Fix CommandTPSLimiter --- BauSystem_Main/pom.xml | 7 ++ .../bausystem/commands/CommandTPSLimiter.java | 97 +++++++++++++------ 2 files changed, 74 insertions(+), 30 deletions(-) diff --git a/BauSystem_Main/pom.xml b/BauSystem_Main/pom.xml index 3aa54e7..62e4e51 100644 --- a/BauSystem_Main/pom.xml +++ b/BauSystem_Main/pom.xml @@ -79,5 +79,12 @@ system ${main.basedir}/lib/WorldEdit-1.15.jar + + steamwar + ProtocolLib + 1.0 + system + ${main.basedir}/lib/ProtocolLib.jar + diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java index 8365aa7..0d91d83 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTPSLimiter.java @@ -19,6 +19,12 @@ package de.steamwar.bausystem.commands; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.reflect.StructureModifier; +import com.comphenix.protocol.wrappers.WrappedWatchableObject; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.world.Welt; @@ -34,11 +40,14 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftTNTPrimed; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.TNTPrimed; import org.bukkit.scheduler.BukkitTask; +import java.util.List; + public class CommandTPSLimiter implements CommandExecutor { private static int currentTPSLimit = 20; @@ -49,43 +58,71 @@ public class CommandTPSLimiter implements CommandExecutor { private BukkitTask tpsLimiter = null; public CommandTPSLimiter() { - Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { - if (currentTPSLimit == 20) { - return; + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.ENTITY_METADATA) { + @Override + public void onPacketSending(PacketEvent event) { + // if (tpsLimiterNotActive()) return; + if (event.getPacket().getEntityModifier(world).read(0) == null) return; + if (!(event.getPacket().getEntityModifier(world).read(0) instanceof CraftTNTPrimed)) return; + System.out.println("META: " + event.getPacket().getIntegers().read(0)); + List dataWatcherObjectList = event.getPacket().getWatchableCollectionModifier().read(0); + getByIndex(dataWatcherObjectList, 5).setValue(true); + getByIndex(dataWatcherObjectList, 7).setValue((int) ((20.0 / currentTPSLimit) * 80)); + System.out.println(dataWatcherObjectList); + event.getPacket().getWatchableCollectionModifier().write(0, dataWatcherObjectList); } - world.getEntities().stream().filter(entity -> entity instanceof TNTPrimed).forEach(entity -> { - /*DataWatcher dataWatcher = new DataWatcher(entity); - System.out.println("Let " + entity.getName() + " sneak " + sneaking); - dataWatcher.register(new DataWatcherObject<>(0, DataWatcherRegistry.a), sneaking ? (byte)0x02 : (byte)0x40); - PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(entity.getId(), dataWatcher, false); + }); + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.REL_ENTITY_MOVE) { + @Override + public void onPacketSending(PacketEvent event) { + if (tpsLimiterNotActive()) return; + if (event.getPacket().getEntityModifier(world).read(0) == null) return; + if (!(event.getPacket().getEntityModifier(world).read(0) instanceof CraftTNTPrimed)) return; + System.out.println(event.getPacket().getIntegers().read(0)); + StructureModifier structureModifier = event.getPacket().getIntegers(); + // structureModifier.write(1, 0); + // structureModifier.write(2, 0); + // structureModifier.write(3, 0); - for(Player player : Bukkit.getOnlinePlayers()){ - ((CraftPlayer)player).getHandle().playerConnection.sendPacket(packet); - }*/ + Entity entity = event.getPacket().getEntityModifier(world).read(0); + System.out.println(entity.getLocation()); + net.minecraft.server.v1_15_R1.Entity serverEntity = ((CraftEntity) entity).getHandle(); - net.minecraft.server.v1_15_R1.Entity serverEntitiy = ((CraftEntity) entity).getHandle(); + PacketPlayOutEntityTeleport packetPlayOutEntityTeleport = new PacketPlayOutEntityTeleport(serverEntity); + sendPlayers(packetPlayOutEntityTeleport); - PacketPlayOutEntityVelocity packet1 = new PacketPlayOutEntityVelocity(entity.getEntityId(), new Vec3D(0, 0, 0)); + PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(serverEntity.getId(), serverEntity.getDataWatcher(), true); + sendPlayers(packetPlayOutEntityMetadata); - PacketPlayOutEntityTeleport packet2 = new PacketPlayOutEntityTeleport(serverEntitiy); + Vec3D vec3D = serverEntity.getMot(); + PacketPlayOutEntityVelocity packetPlayOutEntityVelocity = new PacketPlayOutEntityVelocity(serverEntity.getId(), new Vec3D(vec3D.x / (20 - currentTPSLimit), vec3D.y / (20 - currentTPSLimit), vec3D.z / (20 - currentTPSLimit))); + sendPlayers(packetPlayOutEntityVelocity); + } + }); + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.ENTITY_VELOCITY) { + @Override + public void onPacketSending(PacketEvent event) { - DataWatcher dataWatcher = new DataWatcher(serverEntitiy); - dataWatcher.register(new DataWatcherObject<>(5, DataWatcherRegistry.i), true); - dataWatcher.register(new DataWatcherObject<>(7, DataWatcherRegistry.b), 80); - PacketPlayOutEntityMetadata packet3 = new PacketPlayOutEntityMetadata(serverEntitiy.getId(), dataWatcher, false); + } + }); + } - Bukkit.getOnlinePlayers().forEach(player -> { - PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection; - connection.sendPacket(packet1); - connection.sendPacket(packet2); - connection.sendPacket(packet3); - }); - }); - world.getEntities().stream().filter(entity -> entity instanceof TNTPrimed).forEach(entity -> { - Location location = entity.getLocation(); - world.spawnParticle(Particle.BARRIER, location.getX(), location.getY() + 0.49, location.getZ(), 1); - }); - }, 0, 1); + private boolean tpsLimiterNotActive() { + return currentTPSLimit == 20; + } + + private void sendPlayers(Packet packet) { + Bukkit.getOnlinePlayers().forEach(player -> { + PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection; + connection.sendPacket(packet); + }); + } + + private WrappedWatchableObject getByIndex(List wrappedWatchableObjects, int index) { + for (WrappedWatchableObject wrappedWatchableObject : wrappedWatchableObjects) { + if (wrappedWatchableObject.getIndex() == index) return wrappedWatchableObject; + } + return null; } private boolean permissionCheck(Player player) {