From 3a54f3077c5d4de8be6a5cc3f16936f9437140d4 Mon Sep 17 00:00:00 2001 From: Pablete1234 Date: Sat, 4 Jun 2016 03:11:30 +0200 Subject: [PATCH] Rebase world border module --- .../worldBorder/WorldBorderModule.java | 52 +++------- .../worldBorder/WorldBorderModuleBuilder.java | 99 +++++-------------- .../in/twizmwaz/cardinal/util/Strings.java | 5 + 3 files changed, 43 insertions(+), 113 deletions(-) diff --git a/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModule.java b/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModule.java index ee2514bd3..72f8aa246 100644 --- a/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModule.java +++ b/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModule.java @@ -1,21 +1,13 @@ package in.twizmwaz.cardinal.module.modules.worldBorder; -import in.twizmwaz.cardinal.Cardinal; import in.twizmwaz.cardinal.GameHandler; -import in.twizmwaz.cardinal.event.MatchEndEvent; -import in.twizmwaz.cardinal.event.MatchStartEvent; -import in.twizmwaz.cardinal.module.Module; import in.twizmwaz.cardinal.module.TaskedModule; import in.twizmwaz.cardinal.module.modules.filter.FilterModule; import in.twizmwaz.cardinal.module.modules.filter.FilterState; import in.twizmwaz.cardinal.module.modules.matchTimer.MatchTimer; -import org.bukkit.Bukkit; import org.bukkit.WorldBorder; -import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; -import java.io.File; - public class WorldBorderModule implements TaskedModule { public void unload() { @@ -73,41 +65,21 @@ public WorldBorderModule(double x, double z, double size, FilterModule when, int public int getWarningTime() { return warningTime; } public void run() { - if (!alreadyCreated) { - boolean canCreatedBasedOnTime = false; - if (getAfter() != 0) { - if (MatchTimer.getTimeInSeconds() >= getAfter()) { - canCreatedBasedOnTime = true; - } + if (!alreadyCreated && (when != null ? getWhen().evaluate().equals(FilterState.ALLOW) : MatchTimer.getTimeInSeconds() >= getAfter())) { + alreadyCreated = true; + border = GameHandler.getGameHandler().getMatchWorld().getWorldBorder(); + border.setCenter(getX(), getZ()); + + if (getDuration() > 0) { + border.setSize(getSize(), getDuration()); } else { - if (MatchTimer.getTimeInSeconds() == 0) { - canCreatedBasedOnTime = true; - } + border.setSize(getSize()); } - boolean canCreatedBasedOnFilter = false; - if (getWhen() != null) { - if (getWhen().evaluate().equals(FilterState.ALLOW)) { - canCreatedBasedOnFilter = true; - } - } else canCreatedBasedOnFilter = true; - - if (canCreatedBasedOnTime && canCreatedBasedOnFilter) { - alreadyCreated = true; - border = GameHandler.getGameHandler().getMatchWorld().getWorldBorder(); - border.setCenter(getX(), getZ()); - - if (getDuration() != 0) { - border.setSize(getSize(), getDuration()); - } else { - border.setSize(getSize()); - } - - border.setDamageAmount(getDamage()); - border.setDamageBuffer(getBuffer()); - border.setWarningDistance(getWarningDistance()); - border.setWarningTime(getWarningTime()); - } + border.setDamageAmount(getDamage()); + border.setDamageBuffer(getBuffer()); + border.setWarningDistance(getWarningDistance()); + border.setWarningTime(getWarningTime()); } } diff --git a/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModuleBuilder.java b/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModuleBuilder.java index 3e63d9a10..362996912 100644 --- a/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModuleBuilder.java +++ b/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModuleBuilder.java @@ -5,93 +5,46 @@ import in.twizmwaz.cardinal.module.ModuleCollection; import in.twizmwaz.cardinal.module.modules.filter.FilterModule; import in.twizmwaz.cardinal.module.modules.filter.FilterModuleBuilder; -import in.twizmwaz.cardinal.util.DomUtils; -import in.twizmwaz.cardinal.util.NumUtils; -import in.twizmwaz.cardinal.util.ParseUtils; -import in.twizmwaz.cardinal.util.StringUtils; +import in.twizmwaz.cardinal.util.Numbers; +import in.twizmwaz.cardinal.util.Parser; +import in.twizmwaz.cardinal.util.Strings; import org.jdom2.Element; -import java.util.logging.Filter; - public class WorldBorderModuleBuilder implements ModuleBuilder { @Override - public ModuleCollection load(Match match) { + public ModuleCollection load(Match match) { ModuleCollection results = new ModuleCollection<>(); for (Element borders : match.getDocument().getRootElement().getChildren("world-borders")) { for (Element border : borders.getChildren("world-border")) { - double x = 0; - double z = 0; - if (border.getAttributeValue("center") != null) { - String center = border.getAttributeValue("center"); - x = NumUtils.parseDouble(center.split(",")[0].trim()); - z = NumUtils.parseDouble(center.split(",")[1].trim()); - } else { - String center = border.getParentElement().getAttributeValue("center"); - x = NumUtils.parseDouble(center.split(",")[0].trim()); - z = NumUtils.parseDouble(center.split(",")[1].trim()); - } - - double size = 0; - if (border.getAttributeValue("size") != null) { - size = NumUtils.parseDouble(border.getAttributeValue("size").trim()); - } else { - size = NumUtils.parseDouble(border.getParentElement().getAttributeValue("size").trim()); - } - - FilterModule when = null; - if (border.getChildren("when") != null) { - for (Element filter : border.getChildren("when")) { - when = FilterModuleBuilder.getFilter(filter); - } - } - - int after = 0; - if (border.getAttributeValue("after") != null) { - after = StringUtils.timeStringToSeconds(border.getAttributeValue("after")); - } else if (border.getParentElement().getAttributeValue("after") != null) { - after = StringUtils.timeStringToSeconds(border.getParentElement().getAttributeValue("after")); - } - - long duration = 0; - if (border.getAttributeValue("duration") != null) { - duration = StringUtils.timeStringToSeconds(border.getAttributeValue("duration")); - } else if (border.getParentElement().getAttributeValue("duration") != null) { - duration = StringUtils.timeStringToSeconds(border.getParentElement().getAttributeValue("duration")); + results.add(parseWorldBorder(border, borders)); + } + for (Element borders2 : borders.getChildren("world-borders")) { + for (Element border : borders.getChildren("world-border")) { + results.add(parseWorldBorder(border, borders2, borders)); } + } + } + return results; + } - double damage = 0.2; - if (border.getAttributeValue("damage") != null) { - damage = NumUtils.parseDouble(border.getAttributeValue("damage")); - } else if (border.getParentElement().getAttributeValue("damage") != null) { - damage = NumUtils.parseDouble(border.getParentElement().getAttributeValue("damage")); - } + public WorldBorderModule parseWorldBorder(Element... elements) { + String center = Strings.fallback(Parser.getOrderedAttribute("center", elements), "0,0"); + double x = Numbers.parseDouble(center.split(",")[0].trim(), 0), + z = Numbers.parseDouble(center.split(",")[1].trim(), 0); - double buffer = 5; - if (border.getAttributeValue("buffer") != null) { - buffer = NumUtils.parseDouble(border.getAttributeValue("buffer")); - } else if (border.getParentElement().getAttributeValue("buffer") != null) { - buffer = NumUtils.parseDouble(border.getParentElement().getAttributeValue("buffer")); - } + double size = Numbers.parseDouble(Parser.getOrderedAttribute("size", elements), 0); - int warningDistance = 5; - if (border.getAttributeValue("warning-distance") != null) { - warningDistance = NumUtils.parseInt(border.getAttributeValue("warning-distance")); - } else if (border.getParentElement().getAttributeValue("warning-distance") != null) { - warningDistance = NumUtils.parseInt(border.getParentElement().getAttributeValue("warning-distance")); - } + FilterModule when = FilterModuleBuilder.getAttributeOrChild("when", elements); - int warningTime = 0; - if (border.getAttributeValue("warning-time") != null) { - warningTime = StringUtils.timeStringToSeconds(border.getAttributeValue("warning-time")); - } else if (border.getParentElement().getAttributeValue("warning-time") != null) { - warningTime = StringUtils.timeStringToSeconds(border.getParentElement().getAttributeValue("warning-time")); - } + int after = Strings.timeStringToSeconds(Strings.fallback(Parser.getOrderedAttribute("after", elements), "0s")); + long duration = Strings.timeStringToSeconds(Strings.fallback(Parser.getOrderedAttribute("duration", elements), "0s")); + double damage = Numbers.parseDouble(Parser.getOrderedAttribute("damage", elements), 0.2); + double buffer = Numbers.parseDouble(Parser.getOrderedAttribute("buffer", elements), 5); + int warningDistance = Numbers.parseInt(Parser.getOrderedAttribute("warning-distance", elements), 5); + int warningTime = Strings.timeStringToSeconds(Strings.fallback(Parser.getOrderedAttribute("warning-time", elements), "15s"));; - results.add(new WorldBorderModule(x, z, size, when, after, duration, damage, buffer, warningDistance, warningTime)); - } - } - return results; + return new WorldBorderModule(x, z, size, when, after, duration, damage, buffer, warningDistance, warningTime); } } diff --git a/src/main/java/in/twizmwaz/cardinal/util/Strings.java b/src/main/java/in/twizmwaz/cardinal/util/Strings.java index e4df631f5..5159b1ee9 100644 --- a/src/main/java/in/twizmwaz/cardinal/util/Strings.java +++ b/src/main/java/in/twizmwaz/cardinal/util/Strings.java @@ -162,4 +162,9 @@ public static String getCurrentChatColor(String str, int index) { } return color; } + + public static String fallback(String string, String fallback) { + return string == null ? fallback : string; + } + }