SteamWar/BauSystem2.0
Archiviert
12
0

Fix TPSLimit 0
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2023-04-14 18:43:59 +02:00
Ursprung cfd625c6f0
Commit e60ae56b67

Datei anzeigen

@ -20,13 +20,27 @@
package de.steamwar.bausystem.features.tpslimit;
import com.comphenix.tinyprotocol.Reflection;
import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.core.BountifulWrapper;
import de.steamwar.core.ChatWrapper;
import lombok.Getter;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import yapion.utils.ReflectionsUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@UtilityClass
public class FreezeUtils {
@ -64,10 +78,84 @@ public class FreezeUtils {
if (freezeEnabled) {
try {
field.set(getWorldHandle.invoke(world), state);
cacheEntityPackets(state);
frozen = state;
} catch (IllegalAccessException e) {
// Ignored;
}
}
}
private List<Object> packets = new ArrayList<>();
private Set<Entity> entities = new HashSet<>();
private BukkitTask task = null;
private Class<?> vec3dClass = Reflection.getClass("{nms.world.phys}.Vec3D");
private Reflection.FieldAccessor<Object> zeroVec3d = (Reflection.FieldAccessor<Object>) Reflection.getField(vec3dClass, vec3dClass, 0);
private Object ZERO_VEC3D = zeroVec3d.get(null);
private Class<?> velocityPacketClass = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityVelocity");
private Reflection.ConstructorInvoker velocityPacketConstructor = Reflection.getConstructor(velocityPacketClass, int.class, vec3dClass);
private Class<?> teleportPacketClass = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutEntityTeleport");
private Class<?> entityClass = Reflection.getClass("{nms.world.entity}.Entity");
private Reflection.ConstructorInvoker teleportPacketConstructor = Reflection.getConstructor(teleportPacketClass, entityClass);
private Class<?> craftEntityClass = Reflection.getClass("{obc}.entity.CraftEntity");
private Reflection.MethodInvoker getHandle = Reflection.getMethod(craftEntityClass, "getHandle");
private Object noGravityDataWatcher = BountifulWrapper.impl.getDataWatcherObject(5, Boolean.class);
private Object fuseDataWatcher = BountifulWrapper.impl.getDataWatcherObject(8, Integer.class);
private void cacheEntityPackets(boolean state) {
if (state) {
createPackets();
if (task == null) {
task = new BukkitRunnable() {
@Override
public void run() {
createPackets();
for (Player player : Bukkit.getOnlinePlayers()) {
for (Object packet : packets) {
TinyProtocol.instance.sendPacket(player, packet);
}
}
}
}.runTaskTimer(BauSystem.getInstance(), 1, 1);
}
} else {
packets.clear();
entities.clear();
if (task != null) {
task.cancel();
task = null;
}
}
}
private void createPackets() {
List<Entity> entities = Bukkit.getWorlds().get(0).getEntities().stream()
.filter(e -> !(e instanceof Player))
.filter(e -> !FreezeUtils.entities.contains(e))
.collect(Collectors.toList());
for (Entity entity : entities) {
packets.add(teleportPacketConstructor.invoke(getHandle.invoke(entity)));
}
for (Entity entity : entities) {
packets.add(velocityPacketConstructor.invoke(entity.getEntityId(), ZERO_VEC3D));
}
for (Entity entity : entities) {
packets.add(ChatWrapper.impl.getDataWatcherPacket(entity.getEntityId(), noGravityDataWatcher, true));
}
for (Entity entity : entities) {
if (!(entity instanceof TNTPrimed)) continue;
TNTPrimed tnt = (TNTPrimed) entity;
packets.add(ChatWrapper.impl.getDataWatcherPacket(entity.getEntityId(), fuseDataWatcher, tnt.getFuseTicks()));
}
FreezeUtils.entities.addAll(entities);
}
}