Skip to content

Commit

Permalink
All new border management.
Browse files Browse the repository at this point in the history
 - BorderManager used.
 - The border can easily be set during the game with /uh border set <diameter>.
 - A warning can be send to all player outside a future border with /uh border warning <futureDiameter>.
 - An op (perm uh.border) can list the player outside a given border with /uh border check <diameter>.
  • Loading branch information
AmauryCarrade committed Jul 28, 2014
1 parent 0116509 commit fde2954
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 29 deletions.
169 changes: 169 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHBorderManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package me.azenet.UHPlugin;

import java.util.HashSet;

import me.azenet.UHPlugin.task.BorderWarningTask;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class UHBorderManager {

private UHPlugin p = null;
private Integer currentBorderDiameter = null;

private Integer warningSize = 0;
private BukkitRunnable warningTask = null;

public UHBorderManager(UHPlugin p) {
this.p = p;
this.currentBorderDiameter = p.getConfig().getInt("map.size");
}

public boolean isInsideBorder(Location location, int diameter) {
if(!location.getWorld().equals(Bukkit.getWorlds().get(0))) { // The nether is not limited.
return true;
}

Integer halfMapSize = (int) Math.floor(diameter/2);
Integer x = location.getBlockX();
Integer z = location.getBlockZ();

Location spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitXInf = spawn.add(-halfMapSize, 0, 0).getBlockX();

spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitXSup = spawn.add(halfMapSize, 0, 0).getBlockX();

spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitZInf = spawn.add(0, 0, -halfMapSize).getBlockZ();

spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitZSup = spawn.add(0, 0, halfMapSize).getBlockZ();

if (x < limitXInf || x > limitXSup || z < limitZInf || z > limitZSup) {
return false;
}
else {
return true;
}
}

public boolean isInsideBorder(Location location) {
return this.isInsideBorder(location, getCurrentBorderDiameter());
}

/**
* Return the distance from the location to the border, if the location is outside this border.
* If it is inside, returns 0.
*
* @param location
* @param diameter
* @return
*/
public int getDistanceToBorder(Location location, int diameter) {
if(!location.getWorld().equals(Bukkit.getWorlds().get(0))) { // The nether is not limited.
return 0;
}
if(isInsideBorder(location, diameter)) {
return 0;
}

Integer halfMapSize = (int) Math.floor(diameter/2);
Integer x = location.getBlockX();
Integer z = location.getBlockZ();

Location spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitXInf = spawn.add(-halfMapSize, 0, 0).getBlockX();

spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitXSup = spawn.add(halfMapSize, 0, 0).getBlockX();

spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitZInf = spawn.add(0, 0, -halfMapSize).getBlockZ();

spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
Integer limitZSup = spawn.add(0, 0, halfMapSize).getBlockZ();

if((x > limitXSup || x < limitXInf) && z < limitZSup && z > limitZInf) { // east or west of the border
return Math.abs(x - limitXSup);
}
else if((z > limitZSup || z < limitZInf) && x < limitXSup && x > limitXInf) { // north or south of the border
return Math.abs(z - limitZSup);
}
else if(x > limitXSup && z < limitZInf) { // N-E
return (int) location.distance(new Location(location.getWorld(), limitXSup, location.getBlockY(), limitZInf));
}
else if(x > limitXSup && z > limitZSup) { // S-E
return (int) location.distance(new Location(location.getWorld(), limitXSup, location.getBlockY(), limitZSup));
}
else if(x < limitXInf && z > limitZSup) { // S-O
return (int) location.distance(new Location(location.getWorld(), limitXInf, location.getBlockY(), limitZSup));
}
else if(x < limitXInf && z < limitZInf) { // N-O
return (int) location.distance(new Location(location.getWorld(), limitXInf, location.getBlockY(), limitZInf));
}
else {
return 0; // Should never happen.
}
}



public HashSet<Player> getPlayersOutside(int diameter) {
HashSet<Player> playersOutside = new HashSet<Player>();

for(final String playerS : p.getGameManager().getAlivePlayers()) {
Player player = Bukkit.getPlayer(playerS);
if(player == null) { // offline
continue;
}
if(!isInsideBorder(player.getLocation(), diameter)) {
playersOutside.add(player);
}
}

return playersOutside;
}

public HashSet<Player> getPlayersOutside() {
return this.getPlayersOutside(getCurrentBorderDiameter());
}

public int getWarningSize() {
return this.warningSize;
}

public void setWarningSize(int diameter) {
cancelWarning();

this.warningSize = diameter;

warningTask = new BorderWarningTask(p);
warningTask.runTaskTimer(p, 20L, 20L * p.getConfig().getInt("map.border.warningInterval", 90));
}

public void cancelWarning() {
if(warningTask != null) {
try {
warningTask.cancel();
} catch(IllegalStateException e) {

}
}
}

public int getCurrentBorderDiameter() {
return this.currentBorderDiameter;
}

public void setCurrentBorderDiameter(int diameter) {
cancelWarning();
this.currentBorderDiameter = diameter;

p.getWorldBorderIntegration().setupBorders(); // Update the WB border if needed
}

}
4 changes: 4 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHGameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ private ArrayList<UHTeam> getAliveTeams() {
}
return aliveTeams;
}

public HashSet<String> getAlivePlayers() {
return this.alivePlayers;
}

public UHScoreboardManager getScoreboardManager() {
return scoreboardManager;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class UHPlugin extends JavaPlugin {
private UHTeamManager teamManager = null;
private UHGameManager gameManager = null;
private UHPluginCommand commandManager = null;
private UHBorderManager borderManager = null;

private UHWorldBorderIntegration wbintegration = null;
private UHSpectatorPlusIntegration spintegration = null;
Expand All @@ -51,6 +52,7 @@ public void onEnable() {

teamManager = new UHTeamManager(this);
gameManager = new UHGameManager(this);
borderManager = new UHBorderManager(this);

wbintegration = new UHWorldBorderIntegration(this);
spintegration = new UHSpectatorPlusIntegration(this);
Expand Down Expand Up @@ -219,6 +221,10 @@ public UHPluginCommand getCommandManager() {
return commandManager;
}

public UHBorderManager getBorderManager() {
return borderManager;
}

public UHWorldBorderIntegration getWorldBorderIntegration() {
return wbintegration;
}
Expand Down
137 changes: 131 additions & 6 deletions src/main/java/me/azenet/UHPlugin/UHPluginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ public class UHPluginCommand implements CommandExecutor {

private UHPlugin p = null;

private ChatColor ce = ChatColor.RED; // error
private ChatColor ci = ChatColor.WHITE; // info
private ChatColor cc = ChatColor.GOLD; // command
private ChatColor cs = ChatColor.GREEN; // success message
private ChatColor cst = ChatColor.GRAY; // status

private ArrayList<String> commands = new ArrayList<String>();
private ArrayList<String> teamCommands = new ArrayList<String>();
private ArrayList<String> specCommands = new ArrayList<String>();
private ArrayList<String> borderCommands = new ArrayList<String>();

private I18n i = null;

Expand All @@ -42,6 +37,7 @@ public UHPluginCommand(UHPlugin p) {
commands.add("team");
commands.add("addspawn");
commands.add("generatewalls");
commands.add("border");
commands.add("heal");
commands.add("healall");
commands.add("resurrect");
Expand All @@ -58,6 +54,11 @@ public UHPluginCommand(UHPlugin p) {
specCommands.add("add");
specCommands.add("remove");
specCommands.add("list");

borderCommands.add("current");
borderCommands.add("set");
borderCommands.add("warning");
borderCommands.add("check");
}

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -134,6 +135,7 @@ private void help(CommandSender sender, boolean error) {
sender.sendMessage(i.t("cmd.helpAddspawnXZ"));
sender.sendMessage(i.t("cmd.helpSpec"));
sender.sendMessage(i.t("cmd.helpWall"));
sender.sendMessage(i.t("cmd.helpBorder"));

sender.sendMessage(i.t("cmd.titleBugCmd"));
sender.sendMessage(i.t("cmd.helpHeal"));
Expand Down Expand Up @@ -710,6 +712,125 @@ else if(subcommand.equalsIgnoreCase("list")) {
}


/**
* This command manages borders (gets current, checks if players are out, sets a new size, warnings players
* about the futur size).
*
* @param sender
* @param command
* @param label
* @param args
*/
@SuppressWarnings("unused")
private void doBorder(CommandSender sender, Command command, String label, String[] args) {
if(args.length == 1) { // /uh border
sender.sendMessage(i.t("cmd.borderHelpAvailable"));
sender.sendMessage(i.t("cmd.borderHelpCurrent"));
sender.sendMessage(i.t("cmd.borderHelpSet"));
sender.sendMessage(i.t("cmd.borderHelpWarning"));
sender.sendMessage(i.t("cmd.borderHelpWarningCancel"));
sender.sendMessage(i.t("cmd.borderHelpCheck"));
}
else {
String subcommand = args[1];

if(subcommand.equalsIgnoreCase("current")) { // /uh border current
sender.sendMessage(i.t("borders.current.message", String.valueOf(p.getBorderManager().getCurrentBorderDiameter())));
}

else if(subcommand.equalsIgnoreCase("set")) { // /uh border set
if(args.length == 2) { // /uh border set
sender.sendMessage(i.t("borders.syntaxError"));
}
else if(args.length == 3) { // /uh border set <?>
try {
Integer newDiameter = Integer.valueOf(args[2]);

if(p.getBorderManager().getPlayersOutside(newDiameter).size() != 0) { // Some players are outside
sender.sendMessage(i.t("borders.set.playersOutsideCanceled"));
sender.sendMessage(i.t("borders.set.playersOutsideCanceledCmd", args[2]));
if(!p.getWorldBorderIntegration().isWBIntegrationEnabled()) {
sender.sendMessage(i.t("borders.set.playersOutsideCanceledWarnWorldBorder"));
}
}
else {
p.getBorderManager().setCurrentBorderDiameter(newDiameter);
p.getServer().broadcastMessage(i.t("borders.set.broadcast", args[2]));
}

} catch(NumberFormatException e) {
sender.sendMessage(i.t("borders.NaN", args[2]));
}
}
else if(args.length == 4 && args[3].equalsIgnoreCase("force")) { // /uh border set <?> force
try {
Integer newDiameter = Integer.valueOf(args[2]);

p.getBorderManager().setCurrentBorderDiameter(newDiameter);
p.getServer().broadcastMessage(i.t("borders.set.broadcast", args[2]));

} catch(NumberFormatException e) {
sender.sendMessage(i.t("borders.NaN", args[2]));
}
}
}

else if(subcommand.equalsIgnoreCase("warning")) { // /uh border warning
if(args.length == 2) { // /uh border warning
sender.sendMessage(i.t("borders.syntaxError"));
}
else if(args[2].equalsIgnoreCase("cancel")) { // /uh border warning cancel
p.getBorderManager().cancelWarning();
sender.sendMessage(i.t("borders.warning.canceled"));
}
else { // /uh border warning <?>
try {
Integer warnDiameter = Integer.valueOf(args[2]);
p.getBorderManager().setWarningSize(warnDiameter);
sender.sendMessage(i.t("borders.warning.set", p.getConfig().getString("map.border.warningInterval", "90")));

} catch(NumberFormatException e) {
sender.sendMessage(i.t("borders.NaN", args[2]));
}
}
}
else if(subcommand.equalsIgnoreCase("check")) {
if(args.length == 2) { // /uh border check
sender.sendMessage(i.t("borders.syntaxError"));
}
else { // /uh border check <?>
try {
Integer checkDiameter = Integer.valueOf(args[2]);
HashSet<Player> playersOutside = p.getBorderManager().getPlayersOutside(checkDiameter);

if(playersOutside.size() == 0) {
sender.sendMessage(i.t("borders.check.allPlayersInside"));
}
else {
sender.sendMessage(i.t("borders.check.countPlayersOutside", String.valueOf(playersOutside.size())));
for(Player player : p.getBorderManager().getPlayersOutside(checkDiameter)) {
int distance = p.getBorderManager().getDistanceToBorder(player.getLocation(), checkDiameter);
if(distance > 150) {
sender.sendMessage(i.t("borders.check.itemPlayerFar", player.getName()));
}
else if(distance > 25) {
sender.sendMessage(i.t("borders.check.itemPlayerClose", player.getName()));
}
else {
sender.sendMessage(i.t("borders.check.itemPlayerVeryClose", player.getName()));
}
}
}

} catch(NumberFormatException e) {
sender.sendMessage(i.t("borders.NaN", args[2]));
}
}
}
}

}



public ArrayList<String> getCommands() {
Expand All @@ -723,4 +844,8 @@ public ArrayList<String> getTeamCommands() {
public ArrayList<String> getSpecCommands() {
return specCommands;
}

public ArrayList<String> getBorderCommands() {
return borderCommands;
}
}
Loading

0 comments on commit fde2954

Please sign in to comment.