SteamWar/BauSystem
Archiviert
13
0

Fix CommandTPSLimiter

Dieser Commit ist enthalten in:
jojo 2020-11-15 21:54:06 +01:00
Ursprung 9388e1abd5
Commit ab170426cb
2 geänderte Dateien mit 74 neuen und 30 gelöschten Zeilen

Datei anzeigen

@ -79,5 +79,12 @@
<scope>system</scope>
<systemPath>${main.basedir}/lib/WorldEdit-1.15.jar</systemPath>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>ProtocolLib</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${main.basedir}/lib/ProtocolLib.jar</systemPath>
</dependency>
</dependencies>
</project>

Datei anzeigen

@ -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<WrappedWatchableObject> 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<Integer> 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<WrappedWatchableObject> wrappedWatchableObjects, int index) {
for (WrappedWatchableObject wrappedWatchableObject : wrappedWatchableObjects) {
if (wrappedWatchableObject.getIndex() == index) return wrappedWatchableObject;
}
return null;
}
private boolean permissionCheck(Player player) {