diff --git a/src/main/java/net/knarcraft/stargate/portal/Portal.java b/src/main/java/net/knarcraft/stargate/portal/Portal.java index 0e6e875..36650e0 100644 --- a/src/main/java/net/knarcraft/stargate/portal/Portal.java +++ b/src/main/java/net/knarcraft/stargate/portal/Portal.java @@ -2,7 +2,6 @@ package net.knarcraft.stargate.portal; import net.knarcraft.stargate.BlockLocation; import net.knarcraft.stargate.BloxPopulator; -import net.knarcraft.stargate.utility.EconomyHandler; import net.knarcraft.stargate.RelativeBlockVector; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.event.StargateActivateEvent; @@ -11,8 +10,8 @@ import net.knarcraft.stargate.event.StargateDeactivateEvent; import net.knarcraft.stargate.event.StargateOpenEvent; import net.knarcraft.stargate.event.StargatePortalEvent; import net.knarcraft.stargate.utility.EntityHelper; +import net.knarcraft.stargate.utility.SignHelper; import org.bukkit.Axis; -import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -268,6 +267,10 @@ public class Portal { drawSign(); } + public List getDestinations() { + return new ArrayList<>(this.destinations); + } + public Portal getDestination(Player player) { if (isRandom()) { destinations = PortalHandler.getDestinations(this, player, getNetwork()); @@ -792,7 +795,17 @@ public class Portal { cycleDestination(player, 1); } - public void cycleDestination(Player player, int dir) { + /** + * Cycles destination for a network gate + * + * @param player

The player cycling destinations

+ * @param direction

The direction of the cycle (+1 for next, -1 for previous)

+ */ + public void cycleDestination(Player player, int direction) { + if (direction != 1 && direction != -1) { + throw new IllegalArgumentException("The destination direction must be 1 or -1."); + } + boolean activate = false; if (!isActive() || getActivePlayer() != player) { // If the event is cancelled, return @@ -811,7 +824,7 @@ public class Portal { if (!Stargate.destMemory || !activate || lastDestination.isEmpty()) { int index = destinations.indexOf(destination); - index += dir; + index += direction; if (index >= destinations.size()) index = 0; else if (index < 0) @@ -835,146 +848,7 @@ public class Portal { } Sign sign = (Sign) state; - - //Clear sign - for (int index = 0; index <= 3; index++) { - sign.setLine(index, ""); - } - Stargate.setLine(sign, 0, "-" + name + "-"); - - if (!isActive()) { - //Default sign text - drawInactiveSign(sign); - } else { - if (isBungee()) { - //Bungee sign - drawBungeeSign(sign); - } else if (isFixed()) { - //Sign pointing at one other portal - drawFixedSign(sign); - } else { - //Networking stuff - drawNetworkSign(sign); - } - } - - sign.update(); - } - - /** - * Draws a sign with chooseable network locations - * - * @param sign

The sign to draw on

- */ - private void drawNetworkSign(Sign sign) { - int maxIndex = destinations.size() - 1; - int signLineIndex = 0; - int destinationIndex = destinations.indexOf(destination); - boolean freeGatesGreen = EconomyHandler.useEconomy() && EconomyHandler.freeGatesGreen; - - //Last entry, and not only entry. Draw the entry two previously - if ((destinationIndex == maxIndex) && (maxIndex > 1)) { - drawNetworkSignLine(freeGatesGreen, sign, ++signLineIndex, destinationIndex - 2); - } - //Not first entry. Draw the previous entry - if (destinationIndex > 0) { - drawNetworkSignLine(freeGatesGreen, sign, ++signLineIndex, destinationIndex - 1); - } - //Draw the chosen entry (line 2 or 3) - drawNetworkSignChosenLine(freeGatesGreen, sign, ++signLineIndex); - //Has another entry and space on the sign - if ((maxIndex >= destinationIndex + 1) && (++signLineIndex <= 3)) { - drawNetworkSignLine(freeGatesGreen, sign, signLineIndex, destinationIndex + 1); - } - //Has another entry and space on the sign - if ((maxIndex >= destinationIndex + 2) && (++signLineIndex <= 3)) { - drawNetworkSignLine(freeGatesGreen, sign, signLineIndex, destinationIndex + 2); - } - } - - /** - * Draws the chosen destination on one sign line - * - * @param freeGatesGreen

Whether to display free gates in a green color

- * @param sign

The sign to draw on

- * @param signLineIndex

The line to draw on

- */ - private void drawNetworkSignChosenLine(boolean freeGatesGreen, Sign sign, int signLineIndex) { - if (freeGatesGreen) { - Portal destination = PortalHandler.getByName(this.destination, network); - boolean green = Stargate.isFree(activePlayer, this, destination); - Stargate.setLine(sign, signLineIndex, (green ? ChatColor.DARK_GREEN : "") + ">" + this.destination + "<"); - } else { - Stargate.setLine(sign, signLineIndex, " >" + destination + "< "); - } - } - - /** - * Draws one network destination on one sign line - * - * @param freeGatesGreen

Whether to display free gates in a green color

- * @param sign

The sign to draw on

- * @param signLineIndex

The line to draw on

- * @param destinationIndex

The index of the destination to draw

- */ - private void drawNetworkSignLine(boolean freeGatesGreen, Sign sign, int signLineIndex, int destinationIndex) { - if (freeGatesGreen) { - Portal destination = PortalHandler.getByName(destinations.get(destinationIndex), network); - boolean green = Stargate.isFree(activePlayer, this, destination); - Stargate.setLine(sign, signLineIndex, (green ? ChatColor.DARK_GREEN : "") + destinations.get(destinationIndex)); - } else { - Stargate.setLine(sign, signLineIndex, destinations.get(destinationIndex)); - } - } - - /** - * Draws a bungee sign - * - * @param sign

The sign to draw on

- */ - private void drawBungeeSign(Sign sign) { - Stargate.setLine(sign, 1, Stargate.getString("bungeeSign")); - Stargate.setLine(sign, 2, ">" + destination + "<"); - Stargate.setLine(sign, 3, "[" + network + "]"); - } - - /** - * Draws an inactive sign - * - * @param sign

The sign to draw on

- */ - private void drawInactiveSign(Sign sign) { - Stargate.setLine(sign, 1, Stargate.getString("signRightClick")); - Stargate.setLine(sign, 2, Stargate.getString("signToUse")); - if (!isNoNetwork()) { - Stargate.setLine(sign, 3, "(" + network + ")"); - } else { - Stargate.setLine(sign, 3, ""); - } - } - - /** - * Draws a sign pointing to a fixed location - * - * @param sign

The sign to draw on

- */ - private void drawFixedSign(Sign sign) { - if (isRandom()) { - Stargate.setLine(sign, 1, "> " + Stargate.getString("signRandom") + " <"); - } else { - Stargate.setLine(sign, 1, ">" + destination + "<"); - } - if (isNoNetwork()) { - Stargate.setLine(sign, 2, ""); - } else { - Stargate.setLine(sign, 2, "(" + network + ")"); - } - Portal destination = PortalHandler.getByName(this.destination, network); - if (destination == null && !isRandom()) { - Stargate.setLine(sign, 3, Stargate.getString("signDisconnected")); - } else { - Stargate.setLine(sign, 3, ""); - } + SignHelper.drawSign(sign, this); } /** diff --git a/src/main/java/net/knarcraft/stargate/utility/SignHelper.java b/src/main/java/net/knarcraft/stargate/utility/SignHelper.java new file mode 100644 index 0000000..e902770 --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/utility/SignHelper.java @@ -0,0 +1,160 @@ +package net.knarcraft.stargate.utility; + +import net.knarcraft.stargate.Stargate; +import net.knarcraft.stargate.portal.Portal; +import net.knarcraft.stargate.portal.PortalHandler; +import org.bukkit.ChatColor; +import org.bukkit.block.Sign; + +/** + * This class helps drawing the sign on a portal as it's a bit too complicated to be contained within the portal class + */ +public final class SignHelper { + + /** + * Draws the sign on this portal + */ + public static void drawSign(Sign sign, Portal portal) { + //Clear sign + for (int index = 0; index <= 3; index++) { + sign.setLine(index, ""); + } + Stargate.setLine(sign, 0, "-" + portal.getName() + "-"); + + if (!portal.isActive()) { + //Default sign text + drawInactiveSign(sign, portal); + } else { + if (portal.isBungee()) { + //Bungee sign + drawBungeeSign(sign, portal); + } else if (portal.isFixed()) { + //Sign pointing at one other portal + drawFixedSign(sign, portal); + } else { + //Networking stuff + drawNetworkSign(sign, portal); + } + } + + sign.update(); + } + + /** + * Draws a sign with chooseable network locations + * + * @param sign

The sign to draw on

+ */ + private static void drawNetworkSign(Sign sign, Portal portal) { + int maxIndex = portal.getDestinations().size() - 1; + int signLineIndex = 0; + int destinationIndex = portal.getDestinations().indexOf(portal.getDestinationName()); + boolean freeGatesGreen = EconomyHandler.useEconomy() && EconomyHandler.freeGatesGreen; + + //Last entry, and not only entry. Draw the entry two previously + if ((destinationIndex == maxIndex) && (maxIndex > 1)) { + drawNetworkSignLine(freeGatesGreen, sign, ++signLineIndex, destinationIndex - 2, portal); + } + //Not first entry. Draw the previous entry + if (destinationIndex > 0) { + drawNetworkSignLine(freeGatesGreen, sign, ++signLineIndex, destinationIndex - 1, portal); + } + //Draw the chosen entry (line 2 or 3) + drawNetworkSignChosenLine(freeGatesGreen, sign, ++signLineIndex, portal); + //Has another entry and space on the sign + if ((maxIndex >= destinationIndex + 1) && (++signLineIndex <= 3)) { + drawNetworkSignLine(freeGatesGreen, sign, signLineIndex, destinationIndex + 1, portal); + } + //Has another entry and space on the sign + if ((maxIndex >= destinationIndex + 2) && (++signLineIndex <= 3)) { + drawNetworkSignLine(freeGatesGreen, sign, signLineIndex, destinationIndex + 2, portal); + } + } + + /** + * Draws the chosen destination on one sign line + * + * @param freeGatesGreen

Whether to display free gates in a green color

+ * @param sign

The sign to draw on

+ * @param signLineIndex

The line to draw on

+ */ + private static void drawNetworkSignChosenLine(boolean freeGatesGreen, Sign sign, int signLineIndex, Portal portal) { + if (freeGatesGreen) { + Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork()); + boolean green = Stargate.isFree(portal.getActivePlayer(), portal, destination); + Stargate.setLine(sign, signLineIndex, (green ? ChatColor.DARK_GREEN : "") + ">" + portal.getDestinationName() + "<"); + } else { + Stargate.setLine(sign, signLineIndex, " >" + portal.getDestinationName() + "< "); + } + } + + /** + * Draws one network destination on one sign line + * + * @param freeGatesGreen

Whether to display free gates in a green color

+ * @param sign

The sign to draw on

+ * @param signLineIndex

The line to draw on

+ * @param destinationIndex

The index of the destination to draw

+ */ + private static void drawNetworkSignLine(boolean freeGatesGreen, Sign sign, int signLineIndex, int destinationIndex, + Portal portal) { + if (freeGatesGreen) { + Portal destination = PortalHandler.getByName(portal.getDestinations().get(destinationIndex), portal.getNetwork()); + boolean green = Stargate.isFree(portal.getActivePlayer(), portal, destination); + Stargate.setLine(sign, signLineIndex, (green ? ChatColor.DARK_GREEN : "") + portal.getDestinations().get(destinationIndex)); + } else { + Stargate.setLine(sign, signLineIndex, portal.getDestinations().get(destinationIndex)); + } + } + + /** + * Draws a bungee sign + * + * @param sign

The sign to draw on

+ */ + private static void drawBungeeSign(Sign sign, Portal portal) { + Stargate.setLine(sign, 1, Stargate.getString("bungeeSign")); + Stargate.setLine(sign, 2, ">" + portal.getDestinationName() + "<"); + Stargate.setLine(sign, 3, "[" + portal.getNetwork() + "]"); + } + + /** + * Draws an inactive sign + * + * @param sign

The sign to draw on

+ */ + private static void drawInactiveSign(Sign sign, Portal portal) { + Stargate.setLine(sign, 1, Stargate.getString("signRightClick")); + Stargate.setLine(sign, 2, Stargate.getString("signToUse")); + if (!portal.isNoNetwork()) { + Stargate.setLine(sign, 3, "(" + portal.getNetwork() + ")"); + } else { + Stargate.setLine(sign, 3, ""); + } + } + + /** + * Draws a sign pointing to a fixed location + * + * @param sign

The sign to draw on

+ */ + private static void drawFixedSign(Sign sign, Portal portal) { + if (portal.isRandom()) { + Stargate.setLine(sign, 1, "> " + Stargate.getString("signRandom") + " <"); + } else { + Stargate.setLine(sign, 1, ">" + portal.getDestinationName() + "<"); + } + if (portal.isNoNetwork()) { + Stargate.setLine(sign, 2, ""); + } else { + Stargate.setLine(sign, 2, "(" + portal.getNetwork() + ")"); + } + Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork()); + if (destination == null && !portal.isRandom()) { + Stargate.setLine(sign, 3, Stargate.getString("signDisconnected")); + } else { + Stargate.setLine(sign, 3, ""); + } + } + +}