From b7ad176e51d37076923b8837709fed2ec320578e Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 27 May 2021 22:52:42 +0200 Subject: [PATCH] Fixing code duplication Signed-off-by: Chaoscaot --- .../features/loadtimer/Loadtimer.java | 42 +++++++------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/Loadtimer.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/Loadtimer.java index 35353caf..a5a430fc 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/Loadtimer.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/Loadtimer.java @@ -80,39 +80,27 @@ public class Loadtimer implements Listener { String sec = new DecimalFormat("#.#").format(timeSec); AtomicInteger tnt = new AtomicInteger(); tntPlaced.forEach((player, integer) -> tnt.addAndGet(integer)); - Bukkit.getOnlinePlayers().forEach(player -> { - if (Region.getRegion(player.getLocation()).equals(region)) { - BossBar bar = getOrDefault(player); - bar.setTitle(BauSystem.MESSAGE.parse("LOADTIMER_BOSSBAR", player, timeSinceStart, timeSinceHalf, sec, tnt)); - bar.setProgress((timeSinceStart % 20d) / 20); - bar.setColor(BarColor.GREEN); - bar.setStyle(BarStyle.SEGMENTED_20); - } - }); + sendBarToRegion(BarStyle.SEGMENTED_20, "LOADTIMER_BOSSBAR", BarColor.GREEN, (timeSinceStart % 20d) / 20, timeSinceStart, timeSinceHalf, sec, tnt); } else if (stage == Stage.WAITING) { - Bukkit.getOnlinePlayers().forEach(player -> { - if (Region.getRegion(player.getLocation()).equals(region)) { - BossBar bar = getOrDefault(player); - bar.setStyle(BarStyle.SOLID); - bar.setTitle(BauSystem.MESSAGE.parse("LOADTIMER_WAITING", player)); - bar.setColor(BarColor.GREEN); - bar.setProgress(1); - } - }); + sendBarToRegion(BarStyle.SOLID, "LOADTIMER_WAITING", BarColor.GREEN, 1); } else if (stage == Stage.ACTIVATED || stage == Stage.IGNITION) { - Bukkit.getOnlinePlayers().forEach(player -> { - if (Region.getRegion(player.getLocation()).equals(region)) { - BossBar bar = getOrDefault(player); - bar.setStyle(BarStyle.SOLID); - bar.setTitle(BauSystem.MESSAGE.parse(stage == Stage.ACTIVATED ? "LOADTIMER_ACTIVATED" : "LOADTIMER_IGNITION", player)); - bar.setColor(BarColor.YELLOW); - bar.setProgress(1); - } - }); + sendBarToRegion(BarStyle.SOLID, stage == Stage.ACTIVATED ? "LOADTIMER_ACTIVATED" : "LOADTIMER_IGNITION", BarColor.YELLOW, 1); } }, 1, 1); } + private void sendBarToRegion(BarStyle style, String message, BarColor color, double progress, Object... objects) { + Bukkit.getOnlinePlayers().forEach(player -> { + if (Region.getRegion(player.getLocation()).equals(region)) { + BossBar bar = getOrDefault(player); + bar.setStyle(style); + bar.setTitle(BauSystem.MESSAGE.parse(message, player, objects)); + bar.setColor(color); + bar.setProgress(progress); + } + }); + } + public static boolean hasTimer(Region r) { return timers.containsKey(r); }