From d1afb344d9d7f4ca3bd5bba07654c12168e53742 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 30 Jan 2024 16:06:53 +0100 Subject: [PATCH] Fix SimulatorSaver --- .../data/redstone/RedstonePhase.java | 2 +- .../features/tpslimit/TPSSystem.java | 66 ++++++++++--------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java index f2b99d36..614be659 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java @@ -54,7 +54,7 @@ public final class RedstonePhase extends SimulatorPhase { block.setType(Material.REDSTONE_BLOCK); } }); - tickEnd.accept(tickOffset + lifetime, new SimulatorAction(0, 1) { + tickStart.accept(tickOffset + lifetime, new SimulatorAction(ORDER_LIMIT + 1, 1) { @Override public void accept(World world) { BlockState state = blockState.get(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSSystem.java index b801e324..aa25a433 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSSystem.java @@ -26,33 +26,31 @@ import de.steamwar.bausystem.linkage.specific.BauGuiItem; import de.steamwar.bausystem.region.GlobalRegion; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.utils.ScoreboardElement; +import de.steamwar.bausystem.utils.TickEndEvent; import de.steamwar.bausystem.utils.bossbar.BauSystemBossbar; import de.steamwar.bausystem.utils.bossbar.BossBarService; import de.steamwar.command.AbstractSWCommand; import de.steamwar.command.SWCommand; -import de.steamwar.command.TypeValidator; import de.steamwar.core.Core; import de.steamwar.core.TPSWarpUtils; import de.steamwar.core.TPSWatcher; import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; -import de.steamwar.linkage.api.Plain; import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.boss.BarColor; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; -import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.scheduler.BukkitTask; import java.util.Arrays; -import java.util.concurrent.atomic.AtomicInteger; @Linked -public class TPSSystem implements Plain { +public class TPSSystem implements Listener { @Getter private double currentTPSLimit = 20; @@ -87,12 +85,9 @@ public class TPSSystem implements Plain { instance = this; } - private BukkitTask stepper = null; - private void setTPS(double tps) { - if (stepper != null) { - stepper.cancel(); - stepper = null; + if (currentlyStepping) { + currentlyStepping = false; Bukkit.getOnlinePlayers().forEach(player -> { BossBarService.instance.remove(player, GlobalRegion.getInstance(), "TickStep"); }); @@ -120,27 +115,34 @@ public class TPSSystem implements Plain { }); } - private void setSkip(int steps, double tpsLimitToUse) { - double currentLimit = tpsLimitToUse == 20 ? 0 : currentTPSLimit; - setTPS(tpsLimitToUse); - stepper = new BukkitRunnable() { - AtomicInteger stepsLeft = new AtomicInteger(steps); + private boolean currentlyStepping = false; + private double currentLimit; + private int stepsTotal; + private int stepsLeft; - @Override - public void run() { - if (steps > 1) { - Bukkit.getOnlinePlayers().forEach(player -> { - BauSystemBossbar bossbar = BossBarService.instance.get(player, GlobalRegion.getInstance(), "TickStep"); - bossbar.setColor(BarColor.YELLOW); - bossbar.setTitle(BauSystem.MESSAGE.parse("TICK_BOSSBAR", player, (steps - stepsLeft.get()), steps)); - bossbar.setProgress((steps - stepsLeft.get()) / (double) steps); - }); - } - if (stepsLeft.decrementAndGet() < 0) { - setTPS(currentLimit); - } - } - }.runTaskTimer(BauSystem.getInstance(), 1, 1); + private void setSkip(int steps, double tpsLimitToUse) { + currentLimit = tpsLimitToUse == 20 ? 0 : currentTPSLimit; + setTPS(tpsLimitToUse); + stepsLeft = steps; + stepsTotal = steps; + currentlyStepping = true; + } + + @EventHandler + public void onTickEnd(TickEndEvent event) { + if (!currentlyStepping) return; + if (stepsTotal > 1) { + Bukkit.getOnlinePlayers().forEach(player -> { + BauSystemBossbar bossbar = BossBarService.instance.get(player, GlobalRegion.getInstance(), "TickStep"); + bossbar.setColor(BarColor.YELLOW); + bossbar.setTitle(BauSystem.MESSAGE.parse("TICK_BOSSBAR", player, (stepsTotal - stepsLeft), stepsTotal)); + bossbar.setProgress((stepsTotal - stepsLeft) / (double) stepsTotal); + }); + } + stepsLeft--; + if (stepsLeft <= 0) { + setTPS(currentLimit); + } } private class TPSBaseCommand extends SWCommand { @@ -321,7 +323,7 @@ public class TPSSystem implements Plain { @Override public String get(Region region, Player p) { - if (TPSSystem.getInstance().stepper != null) { + if (TPSSystem.getInstance().currentlyStepping) { long time = System.currentTimeMillis() % 1000; if (time < 250) { return "§e" + BauSystem.MESSAGE.parse("SCOREBOARD_TPS", p) + "§8: §7•••";