Makes the SignHelper helper class into the proper PortalSignDrawer which each Portal now has one instance of

This commit is contained in:
2021-10-20 01:33:36 +02:00
parent 1d4b988ca4
commit 635d08b1b3
4 changed files with 65 additions and 48 deletions

View File

@ -21,8 +21,6 @@ import java.util.Map;
import java.util.Random;
import java.util.UUID;
import static net.knarcraft.stargate.utility.SignHelper.drawSign;
/**
* This class represents a portal in space which points to one or several portals
*/
@ -30,6 +28,7 @@ public class Portal {
// Gate location block info
private final PortalLocation location;
private final PortalSignDrawer signDrawer;
// Block references
private final Gate gate;
@ -79,6 +78,14 @@ public class Portal {
this.ownerUUID = ownerUUID;
this.ownerName = ownerName;
this.options = new PortalOptions(options, destination.length() > 0);
this.signDrawer = new PortalSignDrawer(this);
}
/**
* Re-draws the sign on this portal
*/
public void drawSign() {
this.signDrawer.drawSign();
}
/**
@ -153,7 +160,7 @@ public class Portal {
@SuppressWarnings("unused")
public void setName(String name) {
this.name = filterName(name);
drawSign(this);
this.drawSign();
}
/**
@ -379,7 +386,7 @@ public class Portal {
destination.open(openFor, false);
destination.setDestination(this);
if (destination.isVerified()) {
drawSign(destination);
destination.drawSign();
}
}
}
@ -550,7 +557,7 @@ public class Portal {
}
destination = event.getDestination();
destinations = event.getDestinations();
drawSign(this);
this.drawSign();
return true;
}
@ -571,7 +578,7 @@ public class Portal {
destinations.clear();
destination = "";
activePlayer = null;
drawSign(this);
this.drawSign();
}
/**
@ -623,7 +630,7 @@ public class Portal {
cycleDestination(direction);
}
openTime = System.currentTimeMillis() / 1000;
drawSign(this);
this.drawSign();
}
/**

View File

@ -9,7 +9,6 @@ import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.EconomyHelper;
import net.knarcraft.stargate.utility.PermissionHelper;
import net.knarcraft.stargate.utility.SignHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
@ -164,7 +163,7 @@ public class PortalHandler {
}
//Update the portal's sign
if (origin.getOptions().isFixed()) {
SignHelper.drawSign(origin);
origin.drawSign();
}
//Close portal without destination
if (origin.getOptions().isAlwaysOn()) {
@ -585,7 +584,7 @@ public class PortalHandler {
* @param destinationName <p>The name of the destination portal</p>
*/
private static void updateNewPortal(Portal portal, String destinationName) {
SignHelper.drawSign(portal);
portal.drawSign();
//Open an always on portal
if (portal.getOptions().isRandom() || portal.getOptions().isBungee()) {
portal.open(true);
@ -593,7 +592,7 @@ public class PortalHandler {
Portal destinationPortal = getByName(destinationName, portal.getNetwork());
if (destinationPortal != null) {
portal.open(true);
SignHelper.drawSign(destinationPortal);
destinationPortal.drawSign();
}
} else {
//Update the block type for the portal's opening to the closed block
@ -618,7 +617,7 @@ public class PortalHandler {
}
//Update sign of fixed gates pointing at this gate
if (origin.getOptions().isFixed()) {
SignHelper.drawSign(origin);
origin.drawSign();
}
//Open any always on portal pointing at this portal
if (origin.getOptions().isAlwaysOn()) {
@ -941,7 +940,7 @@ public class PortalHandler {
//Re-draw the signs in case a bug in the config prevented the portal from loading and has been fixed since
for (Portal portal : allPortals) {
SignHelper.drawSign(portal);
portal.drawSign();
}
return true;
} catch (Exception e) {

View File

@ -0,0 +1,193 @@
package net.knarcraft.stargate.portal;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.PermissionHelper;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
/**
* The portal sign drawer draws the sing of a given portal
*/
public class PortalSignDrawer {
private final Portal portal;
/**
* Instantiates a new portal sign drawer
*
* @param portal <p>The portal whose sign this portal sign drawer is responsible for drawing</p>
*/
public PortalSignDrawer(Portal portal) {
this.portal = portal;
}
/**
* Draws the sign of the portal this sign drawer is responsible for
*/
public void drawSign() {
Block signBlock = portal.getSignLocation().getBlock();
BlockState state = signBlock.getState();
if (!(state instanceof Sign sign)) {
Stargate.logger.warning(Stargate.getString("prefix") + "Sign block is not a Sign object");
Stargate.debug("Portal::drawSign", "Block: " + signBlock.getType() + " @ " +
signBlock.getLocation());
return;
}
drawSign(sign);
}
/**
* Draws the sign of the portal this sign drawer is responsible for
*
* @param sign <p>The sign re-draw</p>
*/
public void drawSign(Sign sign) {
//Clear sign
for (int index = 0; index <= 3; index++) {
sign.setLine(index, "");
}
Stargate.setLine(sign, 0, ChatColor.WHITE + "-" + Stargate.signColor + portal.getName() +
ChatColor.WHITE + "-");
if (!portal.isActive()) {
//Default sign text
drawInactiveSign(sign);
} else {
if (portal.getOptions().isBungee()) {
//Bungee sign
drawBungeeSign(sign);
} else if (portal.getOptions().isFixed()) {
//Sign pointing at one other portal
drawFixedSign(sign);
} else {
//Networking stuff
drawNetworkSign(sign);
}
}
sign.update();
}
/**
* Draws a sign with choose-able network locations
*
* @param sign <p>The sign to re-draw</p>
*/
private void drawNetworkSign(Sign sign) {
int maxIndex = portal.getDestinations().size() - 1;
int signLineIndex = 0;
int destinationIndex = portal.getDestinations().indexOf(portal.getDestinationName());
boolean freeGatesGreen = EconomyHandler.useEconomy() && EconomyHandler.freeGatesGreen;
//Last, and not only entry. Draw the entry two back
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)) {
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 <p>Whether to display free gates in a green color</p>
* @param sign <p>The sign to draw on</p>
* @param signLineIndex <p>The line to draw on</p>
*/
private void drawNetworkSignChosenLine(boolean freeGatesGreen, Sign sign, int signLineIndex) {
if (freeGatesGreen) {
Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork());
boolean green = PermissionHelper.isFree(portal.getActivePlayer(), portal, destination);
Stargate.setLine(sign, signLineIndex, (green ? ChatColor.DARK_GREEN : "") + ">" +
portal.getDestinationName() + (green ? ChatColor.DARK_GREEN : "") + "<");
} else {
Stargate.setLine(sign, signLineIndex, Stargate.signColor + " >" + portal.getDestinationName() +
Stargate.signColor + "< ");
}
}
/**
* Draws one network destination on one sign line
*
* @param freeGatesGreen <p>Whether to display free gates in a green color</p>
* @param sign <p>The sign to draw on</p>
* @param signLineIndex <p>The line to draw on</p>
* @param destinationIndex <p>The index of the destination to draw</p>
*/
private void drawNetworkSignLine(boolean freeGatesGreen, Sign sign, int signLineIndex, int destinationIndex) {
if (freeGatesGreen) {
Portal destination = PortalHandler.getByName(portal.getDestinations().get(destinationIndex), portal.getNetwork());
boolean green = PermissionHelper.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 <p>The sign to re-draw</p>
*/
private void drawBungeeSign(Sign sign) {
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 <p>The sign to re-draw</p>
*/
private void drawInactiveSign(Sign sign) {
Stargate.setLine(sign, 1, Stargate.getString("signRightClick"));
Stargate.setLine(sign, 2, Stargate.getString("signToUse"));
if (!portal.getOptions().isNoNetwork()) {
Stargate.setLine(sign, 3, "(" + portal.getNetwork() + ")");
} else {
Stargate.setLine(sign, 3, "");
}
}
/**
* Draws a sign pointing to a fixed location
*
* @param sign <p>The sign to re-draw</p>
*/
private void drawFixedSign(Sign sign) {
if (portal.getOptions().isRandom()) {
Stargate.setLine(sign, 1, "> " + Stargate.getString("signRandom") + " <");
} else {
Stargate.setLine(sign, 1, ">" + portal.getDestinationName() + "<");
}
if (portal.getOptions().isNoNetwork()) {
Stargate.setLine(sign, 2, "");
} else {
Stargate.setLine(sign, 2, "(" + portal.getNetwork() + ")");
}
Portal destination = PortalHandler.getByName(portal.getDestinationName(), portal.getNetwork());
if (destination == null && !portal.getOptions().isRandom()) {
Stargate.setLine(sign, 3, Stargate.getString("signDisconnected"));
} else {
Stargate.setLine(sign, 3, "");
}
}
}