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
2 geänderte Dateien mit 19 neuen und 123 gelöschten Zeilen
Nur Änderungen aus Commit 9b803facd5 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

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

Datei anzeigen

@ -19,12 +19,6 @@
package de.steamwar.bausystem.commands; 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.BauSystem;
import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.Welt; import de.steamwar.bausystem.world.Welt;
@ -38,14 +32,11 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity; 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.CraftPlayer;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftTNTPrimed;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.TNTPrimed;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
public class CommandTPSLimiter implements CommandExecutor { public class CommandTPSLimiter implements CommandExecutor {
@ -58,93 +49,7 @@ public class CommandTPSLimiter implements CommandExecutor {
private BukkitTask tpsLimiter = null; private BukkitTask tpsLimiter = null;
public CommandTPSLimiter() { public CommandTPSLimiter() {
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;
System.out.println("ENTITY_METADATA: " + 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);
}
});
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;
System.out.println("REL_ENTITY_MOVE: " + event.getPacket().getIntegers().read(0));
StructureModifier<Integer> structureModifier = event.getPacket().getIntegers();
// structureModifier.write(1, 0);
// structureModifier.write(2, 0);
// structureModifier.write(3, 0);
Entity entity = event.getPacket().getEntityModifier(world).read(0);
// System.out.println(entity.getLocation());
net.minecraft.server.v1_15_R1.Entity serverEntity = ((CraftEntity) entity).getHandle();
PacketPlayOutEntityTeleport packetPlayOutEntityTeleport = new PacketPlayOutEntityTeleport(serverEntity);
sendPlayers(packetPlayOutEntityTeleport);
PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(serverEntity.getId(), serverEntity.getDataWatcher(), true);
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);*/
}
});
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());
}
});
}
private boolean tpsLimiterNotActive() {
return currentTPSLimit == 20;
} }
private void sendPlayers(Packet<?> packet) { private void sendPlayers(Packet<?> packet) {
@ -163,13 +68,6 @@ public class CommandTPSLimiter implements CommandExecutor {
}); });
} }
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) { private boolean permissionCheck(Player player) {
if (Welt.noPermission(player, Permission.world)) { if (Welt.noPermission(player, Permission.world)) {
player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den TPS-Limiter nutzen"); player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den TPS-Limiter nutzen");
@ -233,9 +131,16 @@ public class CommandTPSLimiter implements CommandExecutor {
tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { tpsLimiter = Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
sendTntMetaData(); sendTntMetaData();
Vec3D noMotion = new Vec3D(0, 0, 0);
Set<PacketPlayOutEntityVelocity> velocityPackets = new HashSet<>();
world.getEntitiesByClasses(TNTPrimed.class).forEach(entity -> {
velocityPackets.add(new PacketPlayOutEntityVelocity(entity.getEntityId(), noMotion));
});
for (int i = 0; i < (20 / currentTPSLimit); i++) { for (int i = 0; i < (20 / currentTPSLimit); i++) {
sleepUntilNextTick(); sleepUntilNextTick();
sendTntData(); sendTntData();
sendPlayers(velocityPackets);
} }
}, 0, 1); }, 0, 1);
} }
@ -260,20 +165,6 @@ public class CommandTPSLimiter implements CommandExecutor {
} }
} }
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() { private void sendTntMetaData() {
world.getEntitiesByClasses(TNTPrimed.class).forEach(entity -> { world.getEntitiesByClasses(TNTPrimed.class).forEach(entity -> {
net.minecraft.server.v1_15_R1.Entity serverEntity = ((CraftEntity) entity).getHandle(); net.minecraft.server.v1_15_R1.Entity serverEntity = ((CraftEntity) entity).getHandle();
@ -283,6 +174,18 @@ public class CommandTPSLimiter implements CommandExecutor {
}); });
} }
private void sendTntData() {
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);
}
public static int getCurrentTPSLimit() { public static int getCurrentTPSLimit() {
return currentTPSLimit; return currentTPSLimit;
} }