Moves the portal owner name and owner UUID to the PortalOwner class which makes the TwoTuple unnecessary
This commit is contained in:
@ -6,18 +6,15 @@ import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* This class represents a portal in space which points to one or several portals
|
||||
*/
|
||||
public class Portal {
|
||||
|
||||
// Gate information
|
||||
private final String name;
|
||||
private final String network;
|
||||
private final String ownerName;
|
||||
private final UUID ownerUUID;
|
||||
private final PortalOwner portalOwner;
|
||||
|
||||
private final PortalOptions options;
|
||||
private final PortalOpener portalOpener;
|
||||
@ -35,17 +32,15 @@ public class Portal {
|
||||
* @param name <p>The name of the portal defined on the sign's first line</p>
|
||||
* @param network <p>The network the portal belongs to, defined on the sign's third</p>
|
||||
* @param gate <p>The gate type to use for this portal</p>
|
||||
* @param ownerUUID <p>The UUID of the gate's owner</p>
|
||||
* @param ownerName <p>The name of the gate's owner</p>
|
||||
* @param portalOwner <p>The portal's owner</p>
|
||||
* @param options <p>A map containing all possible portal options, with true for the ones enabled</p>
|
||||
*/
|
||||
public Portal(PortalLocation portalLocation, BlockLocation button, String destination, String name, String network,
|
||||
Gate gate, UUID ownerUUID, String ownerName, Map<PortalOption, Boolean> options) {
|
||||
Gate gate, PortalOwner portalOwner, Map<PortalOption, Boolean> options) {
|
||||
this.location = portalLocation;
|
||||
this.network = network;
|
||||
this.name = name;
|
||||
this.ownerUUID = ownerUUID;
|
||||
this.ownerName = ownerName;
|
||||
this.portalOwner = portalOwner;
|
||||
this.options = new PortalOptions(options, destination.length() > 0);
|
||||
this.signDrawer = new PortalSignDrawer(this);
|
||||
this.portalOpener = new PortalOpener(this, destination);
|
||||
@ -180,25 +175,14 @@ public class Portal {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of this portal's owner
|
||||
* Gets this portal's owner
|
||||
*
|
||||
* <p>The owner is the player which created the portal.</p>
|
||||
*
|
||||
* @return <p>The name of this portal's owner</p>
|
||||
* @return <p>This portal's owner</p>
|
||||
*/
|
||||
public String getOwnerName() {
|
||||
return ownerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the UUID of this portal's owner
|
||||
*
|
||||
* <p>The owner is the player which created the portal.</p>
|
||||
*
|
||||
* @return <p>The UUID of this portal's owner</p>
|
||||
*/
|
||||
public UUID getOwnerUUID() {
|
||||
return ownerUUID;
|
||||
public PortalOwner getOwner() {
|
||||
return portalOwner;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -208,10 +192,10 @@ public class Portal {
|
||||
* @return <p>True if the player is the owner of this portal</p>
|
||||
*/
|
||||
public boolean isOwner(Player player) {
|
||||
if (this.ownerUUID != null) {
|
||||
return player.getUniqueId().compareTo(this.ownerUUID) == 0;
|
||||
if (this.portalOwner.getUUID() != null) {
|
||||
return player.getUniqueId().compareTo(this.portalOwner.getUUID()) == 0;
|
||||
} else {
|
||||
return player.getName().equalsIgnoreCase(this.ownerName);
|
||||
return player.getName().equalsIgnoreCase(this.portalOwner.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,8 +152,9 @@ public class PortalCreator {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.portal = new Portal(portalLocation, null, destinationName, portalName,
|
||||
network, gate, player.getUniqueId(), player.getName(), portalOptions);
|
||||
PortalOwner owner = new PortalOwner(player);
|
||||
this.portal = new Portal(portalLocation, null, destinationName, portalName, network, gate, owner,
|
||||
portalOptions);
|
||||
return validatePortal(denyMessage, event.getLines(), deny);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@ package net.knarcraft.stargate.portal;
|
||||
import net.knarcraft.stargate.Stargate;
|
||||
import net.knarcraft.stargate.container.BlockLocation;
|
||||
import net.knarcraft.stargate.container.RelativeBlockVector;
|
||||
import net.knarcraft.stargate.container.TwoTuple;
|
||||
import net.knarcraft.stargate.utility.PermissionHelper;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
@ -11,7 +10,6 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -101,16 +99,6 @@ public class PortalHandler {
|
||||
return destinations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Un-registers the given portal
|
||||
*
|
||||
* @param portal <p>The portal to un-register</p>
|
||||
* @param removeAll <p>Whether to remove the portal from the list of all portals</p>
|
||||
*/
|
||||
public static void unregisterPortal(Portal portal, boolean removeAll) {
|
||||
PortalRegistry.unregisterPortal(portal, removeAll);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a portal
|
||||
*
|
||||
@ -397,51 +385,59 @@ public class PortalHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens all always open portals
|
||||
* Opens all always-on portals
|
||||
*
|
||||
* @return <p>A TwoTuple where the first value is the number of always open portals and the second value is the total number of portals</p>
|
||||
* @return <p>The number of always open portals enabled</p>
|
||||
*/
|
||||
public static TwoTuple<Integer, Integer> openAlwaysOpenPortals() {
|
||||
int portalCount = 0;
|
||||
int openCount = 0;
|
||||
for (Iterator<Portal> iterator = PortalRegistry.getAllPortals().iterator(); iterator.hasNext(); ) {
|
||||
Portal portal = iterator.next();
|
||||
if (portal == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Verify portal integrity/register portal
|
||||
PortalStructure structure = portal.getStructure();
|
||||
if (!structure.wasVerified() && (!structure.isVerified() || !structure.checkIntegrity())) {
|
||||
destroyInvalidPortal(portal);
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
portalCount++;
|
||||
public static int openAlwaysOpenPortals() {
|
||||
int alwaysOpenCount = 0;
|
||||
|
||||
for (Portal portal : PortalRegistry.getAllPortals()) {
|
||||
//Open the gate if it's set as always open or if it's a bungee gate
|
||||
if (portal.getOptions().isFixed() && (Stargate.enableBungee && portal.getOptions().isBungee() ||
|
||||
portal.getPortalActivator().getDestination() != null && portal.getOptions().isAlwaysOn())) {
|
||||
portal.getPortalOpener().openPortal(true);
|
||||
openCount++;
|
||||
alwaysOpenCount++;
|
||||
}
|
||||
}
|
||||
return new TwoTuple<>(openCount, portalCount);
|
||||
return alwaysOpenCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys a portal which has failed its integrity test
|
||||
* Tries to verify all portals and un-registers non-verifiable portals
|
||||
*/
|
||||
public static void verifyAllPortals() {
|
||||
List<Portal> invalidPortals = new ArrayList<>();
|
||||
for (Portal portal : PortalRegistry.getAllPortals()) {
|
||||
//Try and verify the portal. Invalidate it if it cannot be validated
|
||||
PortalStructure structure = portal.getStructure();
|
||||
if (!structure.wasVerified() && (!structure.isVerified() || !structure.checkIntegrity())) {
|
||||
invalidPortals.add(portal);
|
||||
}
|
||||
}
|
||||
|
||||
//Un-register any invalid portals found
|
||||
for (Portal portal : invalidPortals) {
|
||||
unregisterInvalidPortal(portal);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Un-registers a portal which has failed its integrity tests
|
||||
*
|
||||
* @param portal <p>The portal of the star portal</p>
|
||||
*/
|
||||
private static void destroyInvalidPortal(Portal portal) {
|
||||
// DEBUG
|
||||
private static void unregisterInvalidPortal(Portal portal) {
|
||||
//Show debug information
|
||||
for (RelativeBlockVector control : portal.getGate().getLayout().getControls()) {
|
||||
if (!portal.getBlockAt(control).getBlock().getType().equals(portal.getGate().getControlBlock())) {
|
||||
Stargate.debug("loadAllPortals", "Control Block Type == " + portal.getBlockAt(control).getBlock().getType().name());
|
||||
Block block = portal.getBlockAt(control).getBlock();
|
||||
//Log control blocks not matching the gate layout
|
||||
if (!block.getType().equals(portal.getGate().getControlBlock())) {
|
||||
Stargate.debug("PortalHandler::destroyInvalidPortal", "Control Block Type == " +
|
||||
block.getType().name());
|
||||
}
|
||||
}
|
||||
PortalHandler.unregisterPortal(portal, false);
|
||||
PortalRegistry.unregisterPortal(portal, false);
|
||||
Stargate.logger.info(Stargate.getString("prefix") + "Destroying stargate at " + portal);
|
||||
}
|
||||
|
||||
|
102
src/main/java/net/knarcraft/stargate/portal/PortalOwner.java
Normal file
102
src/main/java/net/knarcraft/stargate/portal/PortalOwner.java
Normal file
@ -0,0 +1,102 @@
|
||||
package net.knarcraft.stargate.portal;
|
||||
|
||||
import net.knarcraft.stargate.Stargate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* The portal owner represents the owner of a portal
|
||||
*/
|
||||
public class PortalOwner {
|
||||
|
||||
private UUID ownerUUID;
|
||||
private String ownerName;
|
||||
|
||||
/**
|
||||
* Instantiates a new portal owner
|
||||
*
|
||||
* @param ownerIdentifier <p>A UUID, or a username for legacy support</p>
|
||||
*/
|
||||
public PortalOwner(String ownerIdentifier) {
|
||||
parseIdentifier(ownerIdentifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new portal owner
|
||||
*
|
||||
* @param player <p>The player which is the owner of the portal</p>
|
||||
*/
|
||||
public PortalOwner(Player player) {
|
||||
this.ownerUUID = player.getUniqueId();
|
||||
this.ownerName = player.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the UUID of this owner
|
||||
*
|
||||
* @return <p>The UUID of this owner, or null if a UUID is not available</p>
|
||||
*/
|
||||
public UUID getUUID() {
|
||||
return ownerUUID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of this owner
|
||||
*
|
||||
* @return <p>The name of this owner</p>
|
||||
*/
|
||||
public String getName() {
|
||||
return ownerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the one identifier used for saving the owner
|
||||
*
|
||||
* <p>If the UUID is available, a string representation of the UUID will be returned. If not, the owner's name will
|
||||
* be returned.</p>
|
||||
*
|
||||
* @return <p>The owner's identifier</p>
|
||||
*/
|
||||
public String getIdentifier() {
|
||||
if (ownerUUID != null) {
|
||||
return ownerUUID.toString();
|
||||
} else {
|
||||
return ownerName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the identifier of a portal's owner
|
||||
*
|
||||
* <p>The identifier should be a valid UUID, but can be a username of max 16 characters for legacy support. Strings
|
||||
* longer than 16 characters not parse-able as a UUID will silently fail by setting the owner name to the
|
||||
* identifier.</p>
|
||||
*
|
||||
* @param ownerIdentifier <p>The identifier for a portal's owner</p>
|
||||
*/
|
||||
private void parseIdentifier(String ownerIdentifier) {
|
||||
UUID ownerUUID = null;
|
||||
String ownerName;
|
||||
if (ownerIdentifier.length() > 16) {
|
||||
//If more than 16 characters, the string cannot be a username, so it's probably a UUID
|
||||
try {
|
||||
ownerUUID = UUID.fromString(ownerIdentifier);
|
||||
OfflinePlayer offlineOwner = Bukkit.getServer().getOfflinePlayer(ownerUUID);
|
||||
ownerName = offlineOwner.getName();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
//Invalid as UUID and username, so just keep it as owner name and hope the server owner fixes it
|
||||
ownerName = ownerIdentifier;
|
||||
Stargate.debug("loadAllPortals", "Invalid stargate owner string: " + ownerIdentifier);
|
||||
}
|
||||
} else {
|
||||
//Old username from the pre-UUID times. Just keep it as the owner name
|
||||
ownerName = ownerIdentifier;
|
||||
}
|
||||
this.ownerName = ownerName;
|
||||
this.ownerUUID = ownerUUID;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user