Add FreezeUtils
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Add TickCommand

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-12-13 15:49:06 +01:00
Ursprung 5ae5fe3de3
Commit bae41e807d
5 geänderte Dateien mit 177 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}
}
}

Datei anzeigen

@ -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.");
}
}

Datei anzeigen

@ -86,5 +86,4 @@ public class TPSLimitUtils {
currentTPSLimit = d;
tpsLimiter();
}
}

Datei anzeigen

@ -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;
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
};
}
}