Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Worldborder module (rebased) #1023

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/in/twizmwaz/cardinal/module/ModuleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -189,7 +190,8 @@ private void addBuilders() {
NetBuilder.class,
StatsBuilder.class,
FallingBlocksBuilder.class,
SpectatorToolsBuilder.class
SpectatorToolsBuilder.class,
WorldBorderModuleBuilder.class
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

}
Original file line number Diff line number Diff line change
@@ -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<WorldBorderModule> load(Match match) {
ModuleCollection<WorldBorderModule> 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);
}

}
5 changes: 5 additions & 0 deletions src/main/java/in/twizmwaz/cardinal/util/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}