SteamWar/BauSystem
Archiviert
13
0

Add /tpslimit Command #113

Manuell gemergt
YoyoNow hat 13 Commits von TPSLimiter nach master 2020-11-16 14:32:47 +01:00 zusammengeführt
Nur Änderungen aus Commit 9a082dbdba werden angezeigt - Alle Commits anzeigen

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.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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) {
@Override
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;
@ -72,6 +76,7 @@ public class CommandTPSLimiter implements CommandExecutor {
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.REL_ENTITY_MOVE) {
@Override
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;
@ -92,14 +97,48 @@ public class CommandTPSLimiter implements CommandExecutor {
sendPlayers(packetPlayOutEntityMetadata);
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);
/*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);*/
}
});
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getPlugin(), PacketType.Play.Server.ENTITY_VELOCITY) {
private Set<Entity> entitySet = new HashSet<>();
@Override
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) {
for (WrappedWatchableObject wrappedWatchableObject : wrappedWatchableObjects) {
if (wrappedWatchableObject.getIndex() == index) return wrappedWatchableObject;
@ -183,32 +231,60 @@ public class CommandTPSLimiter implements CommandExecutor {
} else {
if (tpsLimiter != null) return;
tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
lastTime = currentTime;
currentTime = System.nanoTime();
sendTntMetaData();
long timeDelta = (currentTime - lastTime) / 1000000;
long neededDelta = 1000 / currentTPSLimit;
if (neededDelta - timeDelta < 1) {
return;
}
try {
Thread.sleep(neededDelta - timeDelta);
currentTime = System.nanoTime();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
for (int i = 0; i < (20 / currentTPSLimit); i++) {
sleepUntilNextTick();
sendTntData();
}
}, 0, 1);
}
}
private void sleepUntilNextTick() {
lastTime = currentTime;
currentTime = System.nanoTime();
long timeDelta = (currentTime - lastTime) / 1000000;
long neededDelta = 50;
if (neededDelta - timeDelta < 0) {
return;
}
try {
Thread.sleep(neededDelta - timeDelta);
currentTime = System.nanoTime();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
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() {
return currentTPSLimit;
}
private void setMotion(Entity entity) {
}
}