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

Commit

Permalink
Rebase world border module
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 committed Jun 6, 2016
1 parent 5086e0a commit 3a54f30
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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());
}
}

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

}
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;
}

}

0 comments on commit 3a54f30

Please sign in to comment.