From 2b4e1a1947dc6b5fb08222815fb74578e35bce5c Mon Sep 17 00:00:00 2001 From: eueln Date: Fri, 20 Feb 2015 16:57:18 -0600 Subject: [PATCH] Gracefully handle the absence of WorldBorder Additionally move `isOutsideWorldBorder` to FLocation, because it doesn't belong in Board/MemoryBoard. --- .../java/com/massivecraft/factions/Board.java | 3 -- .../com/massivecraft/factions/FLocation.java | 36 ++++++++++++++++--- .../factions/zcore/persist/MemoryBoard.java | 19 ---------- .../factions/zcore/persist/MemoryFPlayer.java | 2 +- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/Board.java b/src/main/java/com/massivecraft/factions/Board.java index 5780dfcd..5f565bf9 100644 --- a/src/main/java/com/massivecraft/factions/Board.java +++ b/src/main/java/com/massivecraft/factions/Board.java @@ -45,9 +45,6 @@ public abstract class Board { // Is this coord connected to any coord claimed by the specified faction? public abstract boolean isConnectedLocation(FLocation flocation, Faction faction); - // Is this location outside the world border? - public abstract boolean isOutsideWorldBorder(FLocation flocation, int buffer); - public abstract boolean hasFactionWithin(FLocation flocation, Faction faction, int radius); //----------------------------------------------// diff --git a/src/main/java/com/massivecraft/factions/FLocation.java b/src/main/java/com/massivecraft/factions/FLocation.java index 9560cca8..790ddac8 100644 --- a/src/main/java/com/massivecraft/factions/FLocation.java +++ b/src/main/java/com/massivecraft/factions/FLocation.java @@ -1,10 +1,7 @@ package com.massivecraft.factions; import com.massivecraft.factions.util.MiscUtil; -import org.bukkit.Bukkit; -import org.bukkit.Chunk; -import org.bukkit.Location; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -15,10 +12,21 @@ import java.util.Set; public class FLocation implements Serializable { private static final long serialVersionUID = -8292915234027387983L; + private static final boolean worldBorderSupport; private String worldName = "world"; private int x = 0; private int z = 0; + static { + boolean worldBorderClassPresent = false; + try { + Class.forName("org.bukkit.WorldBorder"); + worldBorderClassPresent = true; + } catch (ClassNotFoundException ignored) {} + + worldBorderSupport = worldBorderClassPresent; + } + //----------------------------------------------// // Constructors //----------------------------------------------// @@ -158,6 +166,26 @@ public class FLocation implements Serializable { return loc.getWorld().getName().equalsIgnoreCase(getWorldName()) && chunk.getX() == x && chunk.getZ() == z; } + /** + * Checks if the chunk represented by this FLocation is outside the world border + * + * @param buffer the number of chunks from the border that will be treated as "outside" + * @return whether this location is outside of the border + */ + public boolean isOutsideWorldBorder(int buffer) { + if (!worldBorderSupport) { + return false; + } + + WorldBorder border = getWorld().getWorldBorder(); + Chunk chunk = border.getCenter().getChunk(); + + int lim = FLocation.chunkToRegion((int) border.getSize()) - buffer; + int diffX = Math.abs(chunk.getX() - x); + int diffZ = Math.abs(chunk.getZ() - z); + return diffX > lim || diffZ > lim; + } + //----------------------------------------------// // Some Geometry //----------------------------------------------// diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java index 85ad4971..e9aa9cd1 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java @@ -4,11 +4,7 @@ import com.massivecraft.factions.*; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.util.AsciiCompass; import com.massivecraft.factions.util.LazyLocation; - import org.bukkit.ChatColor; -import org.bukkit.Chunk; -import org.bukkit.World; -import org.bukkit.WorldBorder; import java.util.ArrayList; import java.util.HashMap; @@ -128,21 +124,6 @@ public abstract class MemoryBoard extends Board { } return false; } - - /** - * Checks if a claim chunk is outside the world border - * @param flocation claim chunk - * @return if claim chunk is outside world border - */ - public boolean isOutsideWorldBorder(FLocation flocation, int buffer) { - World world = flocation.getWorld(); - WorldBorder border = world.getWorldBorder(); - Chunk chunk = border.getCenter().getChunk(); - int lim = FLocation.chunkToRegion((int) border.getSize()) - buffer; - int diffX = (int) Math.abs(chunk.getX() - flocation.getX()); - int diffZ = (int) Math.abs(chunk.getZ() - flocation.getZ()); - return diffX > lim || diffZ > lim; - } /** * Checks if the faction at the flocation is not wilderness and different than given faction. diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java index b7f7298b..40f69858 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -698,7 +698,7 @@ public abstract class MemoryFPlayer implements FPlayer { } } else if (factionBuffer > 0 && Board.getInstance().hasFactionWithin(flocation, myFaction, factionBuffer)) { error = P.p.txt.parse(TL.CLAIM_TOOCLOSETOOTHERFACTION.format(factionBuffer)); - } else if (Board.getInstance().isOutsideWorldBorder(flocation, worldBuffer)) { + } else if (flocation.isOutsideWorldBorder(worldBuffer)) { if(worldBuffer > 0) { error = P.p.txt.parse(TL.CLAIM_OUTSIDEBORDERBUFFER.format(worldBuffer)); } else {