From bae41e807d0008f725c0f6f7222b74f3d63ec713 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 13 Dec 2021 15:49:06 +0100 Subject: [PATCH] Add FreezeUtils Add TickCommand Signed-off-by: yoyosource --- .../features/tpslimit/FreezeUtils.java | 72 +++++++++++++++ .../features/tpslimit/TPSLimitCommand.java | 16 +++- .../features/tpslimit/TPSLimitUtils.java | 1 - .../features/tpslimit/TPSWarpUtils.java | 2 +- .../features/tpslimit/TickCommand.java | 92 +++++++++++++++++++ 5 files changed, 177 insertions(+), 6 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/FreezeUtils.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TickCommand.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/FreezeUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/FreezeUtils.java new file mode 100644 index 00000000..75703f76 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/FreezeUtils.java @@ -0,0 +1,72 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.tpslimit; + +import lombok.experimental.UtilityClass; +import net.minecraft.server.v1_15_R1.WorldServer; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; +import yapion.utils.ReflectionsUtils; + +import java.lang.reflect.Field; + +@UtilityClass +public class FreezeUtils { + + private static final Field field; + public static final boolean freezeEnabled; + + static { + field = ReflectionsUtils.getField(WorldServer.class, "freezed"); + if (field != null) field.setAccessible(true); + freezeEnabled = field != null; + } + + public static void freeze(World world) { + setFreeze(world, true); + } + + public static void unfreeze(World world) { + setFreeze(world, false); + } + + public static boolean frozen(World world) { + if (freezeEnabled) { + try { + return (boolean) field.get(((CraftWorld) world).getHandle()); + } catch (IllegalAccessException e) { + // Ignored; + } + } + return false; + } + + private void setFreeze(World world, boolean state) { + if (freezeEnabled) { + try { + System.out.println(state + " " + frozen(world)); + field.set(((CraftWorld) world).getHandle(), state); + System.out.println(state + " " + frozen(world)); + } catch (IllegalAccessException e) { + // Ignored; + } + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java index c7316bd1..c4ac11ed 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java @@ -30,11 +30,8 @@ import de.steamwar.command.SWCommandUtils; import de.steamwar.command.TypeMapper; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; -import net.minecraft.server.v1_15_R1.WorldServer; import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; import org.bukkit.entity.Player; -import yapion.utils.ReflectionsUtils; import java.util.ArrayList; import java.util.Arrays; @@ -53,6 +50,9 @@ public class TPSLimitCommand extends SWCommand implements Enable { tabCompletions.add(i + ""); } } + if (FreezeUtils.freezeEnabled) { + tabCompletions.add("0"); + } } @Override @@ -77,10 +77,18 @@ public class TPSLimitCommand extends SWCommand implements Enable { @Register public void valueCommand(Player p, /*@DoubleRange(min = 0.5, max = 60)*/ double tpsLimitDouble) { if (!permissionCheck(p)) return; + if (FreezeUtils.freezeEnabled && tpsLimitDouble == 0) { + FreezeUtils.freeze(p.getWorld()); + TPSLimitUtils.currentTPSLimit = 20; + TPSLimitUtils.tpsLimiter(); + sendNewTPSLimitMessage(); + return; + } if (tpsLimitDouble < 0.5 || tpsLimitDouble > (TPSWarpUtils.isWarpAllowed() ? 60 : 20)) { sendInvalidArgumentMessage(p); return; } + FreezeUtils.unfreeze(p.getWorld()); TPSLimitUtils.currentTPSLimit = tpsLimitDouble; TPSLimitUtils.tpsLimiter(); sendNewTPSLimitMessage(); @@ -112,6 +120,6 @@ public class TPSLimitCommand extends SWCommand implements Enable { } private void sendInvalidArgumentMessage(Player player) { - player.sendMessage(BauSystem.PREFIX + ColorConfig.DISABLE + "Nur Zahlen zwischen 0,5 und " + (TPSWarpUtils.isWarpAllowed() ? 60 : 20) + ", und 'default' erlaubt."); + player.sendMessage(BauSystem.PREFIX + ColorConfig.DISABLE + "Nur Zahlen zwischen 0,5 und " + (TPSWarpUtils.isWarpAllowed() ? 60 : 20) + ", und 'default'" + (FreezeUtils.freezeEnabled ? " und '0'" : "") + " erlaubt."); } } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java index a7952d08..7d4d6e9b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitUtils.java @@ -86,5 +86,4 @@ public class TPSLimitUtils { currentTPSLimit = d; tpsLimiter(); } - } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java index 997ae238..28fbbc02 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java @@ -44,7 +44,7 @@ public class TPSWarpUtils { public static void setTPS(double tps) { double d = 50 - (50 / (tps / 20.0)); - nanoDOffset = Math.max(0, Math.min((long) (d * 1000000), 37500000)); + nanoDOffset = Math.max(0, Math.min((long) (d * 1000000), 375000000)); if (Core.getVersion() != 15) { return; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TickCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TickCommand.java new file mode 100644 index 00000000..2f7fb020 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TickCommand.java @@ -0,0 +1,92 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.tpslimit; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.command.SWCommand; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.concurrent.atomic.AtomicInteger; + +@Linked(LinkageType.COMMAND) +public class TickCommand extends SWCommand { + + private AtomicInteger ticksLeft = null; + private Runnable disableTask = null; + + public TickCommand() { + super("tick"); + if (!FreezeUtils.freezeEnabled) { + unregister(); + return; + } + + new BukkitRunnable() { + @Override + public void run() { + if (ticksLeft == null) { + return; + } + + if (ticksLeft.getAndDecrement() < 0) { + disableTask.run(); + disableTask = null; + ticksLeft = null; + } + } + }.runTaskTimer(BauSystem.getInstance(), 0L, 1L); + } + + @Register({"step"}) + public void stepCommand(Player p, @OptionalValue("1") int ticks) { + if (ticksLeft != null) { + return; + } + FreezeUtils.unfreeze(p.getWorld()); + ticksLeft = new AtomicInteger(ticks); + disableTask = () -> { + FreezeUtils.freeze(p.getWorld()); + }; + } + + @Register({"warp"}) + public void warpCommand(Player p, int ticks) { + if (ticksLeft != null) { + return; + } + boolean frozen = FreezeUtils.frozen(p.getWorld()); + double currentTPSLimit = TPSLimitUtils.currentTPSLimit; + TPSLimitUtils.currentTPSLimit = 120; + TPSLimitUtils.tpsLimiter(); + + FreezeUtils.unfreeze(p.getWorld()); + ticksLeft = new AtomicInteger(ticks); + disableTask = () -> { + if (frozen) { + FreezeUtils.freeze(p.getWorld()); + } + TPSLimitUtils.currentTPSLimit = currentTPSLimit; + TPSLimitUtils.tpsLimiter(); + }; + } +}