SteamWar/BauSystem
Archiviert
13
0

Fix CommandTPSLimiter

Dieser Commit ist enthalten in:
jojo 2020-11-16 13:39:29 +01:00
Ursprung 36d6c6bedb
Commit 9a082dbdba

Datei anzeigen

@ -41,9 +41,12 @@ import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftTNTPrimed; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftTNTPrimed;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
public class CommandTPSLimiter implements CommandExecutor { public class CommandTPSLimiter implements CommandExecutor {
@ -58,6 +61,7 @@ public class CommandTPSLimiter implements CommandExecutor {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.ENTITY_METADATA) { ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.ENTITY_METADATA) {
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
if (true) return;
// if (tpsLimiterNotActive()) return; // if (tpsLimiterNotActive()) return;
if (event.getPacket().getEntityModifier(world).read(0) == null) return; if (event.getPacket().getEntityModifier(world).read(0) == null) return;
if (!(event.getPacket().getEntityModifier(world).read(0) instanceof CraftTNTPrimed)) return; if (!(event.getPacket().getEntityModifier(world).read(0) instanceof CraftTNTPrimed)) return;
@ -72,6 +76,7 @@ public class CommandTPSLimiter implements CommandExecutor {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.REL_ENTITY_MOVE) { ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.REL_ENTITY_MOVE) {
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
if (true) return;
if (tpsLimiterNotActive()) return; if (tpsLimiterNotActive()) return;
if (event.getPacket().getEntityModifier(world).read(0) == null) return; if (event.getPacket().getEntityModifier(world).read(0) == null) return;
if (!(event.getPacket().getEntityModifier(world).read(0) instanceof CraftTNTPrimed)) return; if (!(event.getPacket().getEntityModifier(world).read(0) instanceof CraftTNTPrimed)) return;
@ -92,14 +97,48 @@ public class CommandTPSLimiter implements CommandExecutor {
sendPlayers(packetPlayOutEntityMetadata); sendPlayers(packetPlayOutEntityMetadata);
Vec3D vec3D = serverEntity.getMot(); Vec3D vec3D = serverEntity.getMot();
PacketPlayOutEntityVelocity packetPlayOutEntityVelocity = new PacketPlayOutEntityVelocity(serverEntity.getId(), new Vec3D(vec3D.x / (20.0 / currentTPSLimit), vec3D.y / (20.0 / currentTPSLimit), vec3D.z / (20.0 / currentTPSLimit))); /*PacketPlayOutEntityVelocity packetPlayOutEntityVelocity = new PacketPlayOutEntityVelocity(serverEntity.getId(), new Vec3D(vec3D.x / (20.0 / currentTPSLimit), vec3D.y / (20.0 / currentTPSLimit), vec3D.z / (20.0 / currentTPSLimit)));
sendPlayers(packetPlayOutEntityVelocity); sendPlayers(packetPlayOutEntityVelocity);*/
} }
}); });
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.ENTITY_VELOCITY) { ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.ENTITY_VELOCITY) {
private Set<Entity> entitySet = new HashSet<>();
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
if (true) return;
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("ENTITY_VELOCITY: " + event.getPacket().getIntegers().read(0));
System.out.println(event.getPacket().getIntegers().getValues());
// event.setCancelled(true);
Entity entity = event.getPacket().getEntityModifier(world).read(0);
if (entitySet.contains(entity)) {
entitySet.remove(entity);
} else {
entitySet.add(entity);
event.setCancelled(true);
net.minecraft.server.v1_15_R1.Entity serverEntity = ((CraftEntity) entity).getHandle();
Vec3D vec3D = serverEntity.getMot();
PacketPlayOutEntityVelocity packetPlayOutEntityVelocity = new PacketPlayOutEntityVelocity(serverEntity.getId(), new Vec3D(vec3D.x / (20.0 / currentTPSLimit), vec3D.y / (20.0 / currentTPSLimit), vec3D.z / (20.0 / currentTPSLimit)));
sendPlayers(packetPlayOutEntityVelocity);
}
// PacketContainer packetContainer = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_VELOCITY);
// StructureModifier<Integer> structureModifier = packetContainer.getIntegers();
// structureModifier.write(0, entity.getEntityId());
// structureModifier.write(1, (int) (serverEntity.getMot().x / (20.0 / currentTPSLimit) * 8000));
// structureModifier.write(2, (int) (serverEntity.getMot().y / (20.0 / currentTPSLimit) * 8000));
// structureModifier.write(3, (int) (serverEntity.getMot().z / (20.0 / currentTPSLimit) * 8000));
// event.setPacket(packetContainer);
// System.out.println(event.getPacket().getIntegers());
} }
}); });
} }
@ -115,6 +154,15 @@ public class CommandTPSLimiter implements CommandExecutor {
}); });
} }
private void sendPlayers(Set<? extends Packet<?>> packets) {
Bukkit.getOnlinePlayers().forEach(player -> {
PlayerConnection connection = ((CraftPlayer)player).getHandle().playerConnection;
for (Packet<?> p : packets) {
connection.sendPacket(p);
}
});
}
private WrappedWatchableObject getByIndex(List<WrappedWatchableObject> wrappedWatchableObjects, int index) { private WrappedWatchableObject getByIndex(List<WrappedWatchableObject> wrappedWatchableObjects, int index) {
for (WrappedWatchableObject wrappedWatchableObject : wrappedWatchableObjects) { for (WrappedWatchableObject wrappedWatchableObject : wrappedWatchableObjects) {
if (wrappedWatchableObject.getIndex() == index) return wrappedWatchableObject; if (wrappedWatchableObject.getIndex() == index) return wrappedWatchableObject;
@ -183,13 +231,24 @@ public class CommandTPSLimiter implements CommandExecutor {
} else { } else {
if (tpsLimiter != null) return; if (tpsLimiter != null) return;
tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
sendTntMetaData();
for (int i = 0; i < (20 / currentTPSLimit); i++) {
sleepUntilNextTick();
sendTntData();
}
}, 0, 1);
}
}
private void sleepUntilNextTick() {
lastTime = currentTime; lastTime = currentTime;
currentTime = System.nanoTime(); currentTime = System.nanoTime();
long timeDelta = (currentTime - lastTime) / 1000000; long timeDelta = (currentTime - lastTime) / 1000000;
long neededDelta = 1000 / currentTPSLimit; long neededDelta = 50;
if (neededDelta - timeDelta < 1) { if (neededDelta - timeDelta < 0) {
return; return;
} }
@ -199,16 +258,33 @@ public class CommandTPSLimiter implements CommandExecutor {
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
}, 0, 1);
} }
private void sendTntData() {
Vec3D noMotion = new Vec3D(0, 0, 0);
Set<Packet<?>> packets = new HashSet<>();
world.getEntitiesByClasses(TNTPrimed.class).forEach(entity -> {
net.minecraft.server.v1_15_R1.Entity serverEntity = ((CraftEntity) entity).getHandle();
packets.add(new PacketPlayOutEntityTeleport(serverEntity));
packets.add(new PacketPlayOutEntityVelocity(serverEntity.getId(), noMotion));
});
sendPlayers(packets);
}
private void sendTntMetaData() {
world.getEntitiesByClasses(TNTPrimed.class).forEach(entity -> {
net.minecraft.server.v1_15_R1.Entity serverEntity = ((CraftEntity) entity).getHandle();
PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(serverEntity.getId(), serverEntity.getDataWatcher(), true);
sendPlayers(packetPlayOutEntityMetadata);
});
} }
public static int getCurrentTPSLimit() { public static int getCurrentTPSLimit() {
return currentTPSLimit; return currentTPSLimit;
} }
private void setMotion(Entity entity) {
}
} }