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

Commit

Permalink
Add WorldBorderModule
Browse files Browse the repository at this point in the history
  • Loading branch information
thomblr authored and Pablete1234 committed Jun 6, 2016
1 parent 0e0de0c commit 5086e0a
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 1 deletion.
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 @@ -82,6 +82,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 @@ -183,7 +184,8 @@ private void addBuilders() {
CraftingModuleBuilder.class,
PostBuilder.class,
FlagBuilder.class,
NetBuilder.class
NetBuilder.class,
WorldBorderModuleBuilder.class
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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() {
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) {
boolean canCreatedBasedOnTime = false;
if (getAfter() != 0) {
if (MatchTimer.getTimeInSeconds() >= getAfter()) {
canCreatedBasedOnTime = true;
}
} else {
if (MatchTimer.getTimeInSeconds() == 0) {
canCreatedBasedOnTime = true;
}
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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.DomUtils;
import in.twizmwaz.cardinal.util.NumUtils;
import in.twizmwaz.cardinal.util.ParseUtils;
import in.twizmwaz.cardinal.util.StringUtils;
import org.jdom2.Element;

import java.util.logging.Filter;

public class WorldBorderModuleBuilder implements ModuleBuilder {

@Override
public ModuleCollection 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"));
}

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

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

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

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

results.add(new WorldBorderModule(x, z, size, when, after, duration, damage, buffer, warningDistance, warningTime));
}
}
return results;
}

}

0 comments on commit 5086e0a

Please sign in to comment.