diff --git a/src/main/java/in/twizmwaz/cardinal/module/ModuleFactory.java b/src/main/java/in/twizmwaz/cardinal/module/ModuleFactory.java index c0f68053e..8579495fe 100644 --- a/src/main/java/in/twizmwaz/cardinal/module/ModuleFactory.java +++ b/src/main/java/in/twizmwaz/cardinal/module/ModuleFactory.java @@ -85,6 +85,7 @@ import in.twizmwaz.cardinal.module.modules.visibility.VisibilityBuilder; import in.twizmwaz.cardinal.module.modules.wildcard.WildCardBuilder; import in.twizmwaz.cardinal.module.modules.wools.WoolObjectiveBuilder; +import in.twizmwaz.cardinal.module.modules.worldBorder.WorldBorderModuleBuilder; import in.twizmwaz.cardinal.module.modules.worldFreeze.WorldFreezeBuilder; import org.bukkit.Bukkit; @@ -189,7 +190,8 @@ private void addBuilders() { NetBuilder.class, StatsBuilder.class, FallingBlocksBuilder.class, - SpectatorToolsBuilder.class + SpectatorToolsBuilder.class, + WorldBorderModuleBuilder.class )); } 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 new file mode 100644 index 000000000..72f8aa246 --- /dev/null +++ b/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModule.java @@ -0,0 +1,86 @@ +package in.twizmwaz.cardinal.module.modules.worldBorder; + +import in.twizmwaz.cardinal.GameHandler; +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.WorldBorder; +import org.bukkit.event.HandlerList; + +public class WorldBorderModule implements TaskedModule { + + public void unload() { + HandlerList.unregisterAll(this); + } + + private WorldBorder border; + private boolean alreadyCreated; + + private double x, z; + private double size; + private FilterModule when; + private int after; + private long duration; + private double damage; + private double buffer; + private int warningDistance; + private int warningTime; + + public WorldBorderModule(double x, double z, double size, FilterModule when, int after, long duration, double damage, double buffer, int warningDistance, int warningTime) { + this.x = x; + this.z = z; + this.size = size; + this.when = when; + this.after = after; + this.duration = duration; + this.damage = damage; + this.buffer = buffer; + this.warningDistance = warningDistance; + this.warningTime = warningTime; + + this.alreadyCreated = false; + } + + public WorldBorder getBorder() { return border; } + + public double getX() { return x; } + + public double getZ() { return z; } + + public double getSize() { return size; } + + public FilterModule getWhen() { return when; } + + public int getAfter() { return after; } + + public long getDuration() { return duration; } + + public double getDamage() { return damage; } + + public double getBuffer() { return buffer; } + + public int getWarningDistance() { return warningDistance; } + + public int getWarningTime() { return warningTime; } + + public void run() { + 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 { + border.setSize(getSize()); + } + + 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 new file mode 100644 index 000000000..362996912 --- /dev/null +++ b/src/main/java/in/twizmwaz/cardinal/module/modules/worldBorder/WorldBorderModuleBuilder.java @@ -0,0 +1,50 @@ +package in.twizmwaz.cardinal.module.modules.worldBorder; + +import in.twizmwaz.cardinal.match.Match; +import in.twizmwaz.cardinal.module.ModuleBuilder; +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.Numbers; +import in.twizmwaz.cardinal.util.Parser; +import in.twizmwaz.cardinal.util.Strings; +import org.jdom2.Element; + +public class WorldBorderModuleBuilder implements ModuleBuilder { + + @Override + 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")) { + 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; + } + + 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 size = Numbers.parseDouble(Parser.getOrderedAttribute("size", elements), 0); + + FilterModule when = FilterModuleBuilder.getAttributeOrChild("when", elements); + + 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"));; + + 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; + } + }