Only fetches name from UUID if necessary
Also fixes some minor warnings
This commit is contained in:
		@@ -54,7 +54,7 @@ public class Portal {
 | 
			
		||||
        this.network = portalStrings.network();
 | 
			
		||||
        this.name = portalStrings.name();
 | 
			
		||||
        this.portalOwner = portalOwner;
 | 
			
		||||
        this.options = new PortalOptions(options, portalStrings.destination().length() > 0);
 | 
			
		||||
        this.options = new PortalOptions(options, !portalStrings.destination().isEmpty());
 | 
			
		||||
        this.signDrawer = new PortalSignDrawer(this);
 | 
			
		||||
        this.portalOpener = new PortalOpener(this, portalStrings.destination());
 | 
			
		||||
        this.structure = new PortalStructure(this, gate, button);
 | 
			
		||||
 
 | 
			
		||||
@@ -74,7 +74,7 @@ public class PortalActivator {
 | 
			
		||||
        if (portal.getOptions().isRandom()) {
 | 
			
		||||
            //Find possible destinations
 | 
			
		||||
            List<String> destinations = PortalHandler.getDestinations(portal, player, portalNetwork);
 | 
			
		||||
            if (destinations.size() == 0) {
 | 
			
		||||
            if (destinations.isEmpty()) {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            //Get one random destination
 | 
			
		||||
@@ -217,7 +217,7 @@ public class PortalActivator {
 | 
			
		||||
     * @return <p>Whether this portal activator's portal is active</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isActive() {
 | 
			
		||||
        return portal.getOptions().isFixed() || (destinations.size() > 0);
 | 
			
		||||
        return portal.getOptions().isFixed() || (!destinations.isEmpty());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -255,7 +255,7 @@ public class PortalActivator {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //If no destinations are available, just tell the player and quit
 | 
			
		||||
        if (destinations.size() == 0) {
 | 
			
		||||
        if (destinations.isEmpty()) {
 | 
			
		||||
            if (!portal.getOptions().isSilent()) {
 | 
			
		||||
                Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.NO_DESTINATION));
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -171,7 +171,7 @@ public class PortalCreator {
 | 
			
		||||
        String route = "PortalCreator::getNetworkName";
 | 
			
		||||
 | 
			
		||||
        //Use default network if a proper alternative is not set
 | 
			
		||||
        if (portalStrings.network().length() < 1 || portalStrings.network().length() > getMaxNameNetworkLength()) {
 | 
			
		||||
        if (portalStrings.network().isEmpty() || portalStrings.network().length() > getMaxNameNetworkLength()) {
 | 
			
		||||
            network = Stargate.getDefaultNetwork();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -207,7 +207,7 @@ public class PortalCreator {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Check if the user can create portals to this world.
 | 
			
		||||
        if (!bungee && destinationName.length() > 0) {
 | 
			
		||||
        if (!bungee && !destinationName.isEmpty()) {
 | 
			
		||||
            Portal destinationPortal = PortalHandler.getByName(destinationName, network);
 | 
			
		||||
            if (destinationPortal != null && destinationPortal.getWorld() != null) {
 | 
			
		||||
                String world = destinationPortal.getWorld().getName();
 | 
			
		||||
@@ -294,7 +294,7 @@ public class PortalCreator {
 | 
			
		||||
        String route = "PortalCreator::checkIfNewPortalIsValid";
 | 
			
		||||
 | 
			
		||||
        //Check if the portal name can fit on the sign with padding (>name<)
 | 
			
		||||
        if (portal.getCleanName().length() < 1 || portal.getCleanName().length() > getMaxNameNetworkLength()) {
 | 
			
		||||
        if (portal.getCleanName().isEmpty() || portal.getCleanName().length() > getMaxNameNetworkLength()) {
 | 
			
		||||
            Stargate.debug(route, String.format("Name length error. %s is too long.",
 | 
			
		||||
                    portal.getCleanName()));
 | 
			
		||||
            Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.CREATION_NAME_LENGTH));
 | 
			
		||||
 
 | 
			
		||||
@@ -251,7 +251,7 @@ public class PortalHandler {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Can not create a non-fixed always-on portal
 | 
			
		||||
        if (portalOptions.get(PortalOption.ALWAYS_ON) && destinationName.length() == 0) {
 | 
			
		||||
        if (portalOptions.get(PortalOption.ALWAYS_ON) && destinationName.isEmpty()) {
 | 
			
		||||
            portalOptions.put(PortalOption.ALWAYS_ON, false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -255,7 +255,7 @@ public class PortalRegistry {
 | 
			
		||||
     * @param portal <p>The portal to register</p>
 | 
			
		||||
     */
 | 
			
		||||
    public static void registerPortal(@NotNull Portal portal) {
 | 
			
		||||
        portal.getOptions().setFixed(portal.getDestinationName().length() > 0 || portal.getOptions().isRandom() ||
 | 
			
		||||
        portal.getOptions().setFixed(!portal.getDestinationName().isEmpty() || portal.getOptions().isRandom() ||
 | 
			
		||||
                portal.getOptions().isBungee());
 | 
			
		||||
 | 
			
		||||
        String portalName = portal.getCleanName();
 | 
			
		||||
 
 | 
			
		||||
@@ -6,8 +6,6 @@ import org.bukkit.OfflinePlayer;
 | 
			
		||||
import org.jetbrains.annotations.NotNull;
 | 
			
		||||
import org.jetbrains.annotations.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -15,7 +13,6 @@ import java.util.UUID;
 | 
			
		||||
 */
 | 
			
		||||
public class PortalOwner {
 | 
			
		||||
 | 
			
		||||
    private static final Map<String, OfflinePlayer> fetchedPlayers = new HashMap<>();
 | 
			
		||||
    private UUID ownerUUID;
 | 
			
		||||
    private String ownerName;
 | 
			
		||||
 | 
			
		||||
@@ -25,13 +22,7 @@ public class PortalOwner {
 | 
			
		||||
     * @param ownerIdentifier <p>A UUID, or a username for legacy support</p>
 | 
			
		||||
     */
 | 
			
		||||
    public PortalOwner(@NotNull String ownerIdentifier) {
 | 
			
		||||
        if (fetchedPlayers.containsKey(ownerIdentifier)) {
 | 
			
		||||
            OfflinePlayer player = fetchedPlayers.get(ownerIdentifier);
 | 
			
		||||
            this.ownerUUID = player.getUniqueId();
 | 
			
		||||
            this.ownerName = player.getName();
 | 
			
		||||
        } else {
 | 
			
		||||
            parseIdentifier(ownerIdentifier);
 | 
			
		||||
        }
 | 
			
		||||
        parseIdentifier(ownerIdentifier);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -77,6 +68,12 @@ public class PortalOwner {
 | 
			
		||||
     */
 | 
			
		||||
    @NotNull
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        if (this.ownerUUID != null && this.ownerName == null) {
 | 
			
		||||
            this.ownerName = fetchName();
 | 
			
		||||
            if (this.ownerName == null) {
 | 
			
		||||
                this.ownerName = "UNKNOWN!";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return ownerName;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -108,14 +105,11 @@ public class PortalOwner {
 | 
			
		||||
     */
 | 
			
		||||
    private void parseIdentifier(@NotNull String ownerIdentifier) {
 | 
			
		||||
        UUID ownerUUID = null;
 | 
			
		||||
        String ownerName;
 | 
			
		||||
        String ownerName = null;
 | 
			
		||||
        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);
 | 
			
		||||
                fetchedPlayers.put(ownerIdentifier, offlineOwner);
 | 
			
		||||
                ownerName = offlineOwner.getName();
 | 
			
		||||
            } catch (IllegalArgumentException exception) {
 | 
			
		||||
                //Invalid as UUID and username, so just keep it as owner name and hope the server owner fixes it
 | 
			
		||||
                ownerName = ownerIdentifier;
 | 
			
		||||
@@ -129,4 +123,14 @@ public class PortalOwner {
 | 
			
		||||
        this.ownerUUID = ownerUUID;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the name of a player
 | 
			
		||||
     *
 | 
			
		||||
     * @return <p>The player to get the name of</p>
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    private String fetchName() {
 | 
			
		||||
        return Bukkit.getServer().getOfflinePlayer(ownerUUID).getName();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user