Only fetches name from UUID if necessary

Also fixes some minor warnings
This commit is contained in:
2024-04-21 16:13:57 +02:00
parent 484d4f4cf1
commit 6287a5e492
7 changed files with 29 additions and 25 deletions

View File

@@ -344,7 +344,7 @@ Other special characters include the following:
- Spaces/blank characters (` `) will not be checked, and as such, represent any block. - Spaces/blank characters (` `) will not be checked, and as such, represent any block.
- Periods (`.`) represent the portal's *iris* (i.e. the part that opens and closes) - Periods (`.`) represent the portal's *iris* (i.e. the part that opens and closes)
- An asterix (`*`) represents the portals's "exit point" (i.e. the block the player will teleport in front of). - An asterix (`*`) represents the portals' "exit point" (i.e. the block the player will teleport in front of).
##### Underwater Portals ##### Underwater Portals
@@ -377,7 +377,7 @@ Gates are not limited to the shape of a standard nether portal -- they can be th
In this case, a simple 5x5 square has been used as a gate. In this case, a simple 5x5 square has been used as a gate.
Gates are also not limited to [materials](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html) (such Gates are also not limited to [materials](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html) (such
as `OBSIDIAN`); they may also use [tags](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Tag.html)) (such as `OBSIDIAN`); they may also use [tags](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Tag.html) (such
as `#WOOL`).<br> as `#WOOL`).<br>
Note that all tags must be prefaced with a hashtag (`#`), as in `#WOOL`. Note that all tags must be prefaced with a hashtag (`#`), as in `#WOOL`.

View File

@@ -54,7 +54,7 @@ public class Portal {
this.network = portalStrings.network(); this.network = portalStrings.network();
this.name = portalStrings.name(); this.name = portalStrings.name();
this.portalOwner = portalOwner; 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.signDrawer = new PortalSignDrawer(this);
this.portalOpener = new PortalOpener(this, portalStrings.destination()); this.portalOpener = new PortalOpener(this, portalStrings.destination());
this.structure = new PortalStructure(this, gate, button); this.structure = new PortalStructure(this, gate, button);

View File

@@ -74,7 +74,7 @@ public class PortalActivator {
if (portal.getOptions().isRandom()) { if (portal.getOptions().isRandom()) {
//Find possible destinations //Find possible destinations
List<String> destinations = PortalHandler.getDestinations(portal, player, portalNetwork); List<String> destinations = PortalHandler.getDestinations(portal, player, portalNetwork);
if (destinations.size() == 0) { if (destinations.isEmpty()) {
return null; return null;
} }
//Get one random destination //Get one random destination
@@ -217,7 +217,7 @@ public class PortalActivator {
* @return <p>Whether this portal activator's portal is active</p> * @return <p>Whether this portal activator's portal is active</p>
*/ */
public boolean isActive() { 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 no destinations are available, just tell the player and quit
if (destinations.size() == 0) { if (destinations.isEmpty()) {
if (!portal.getOptions().isSilent()) { if (!portal.getOptions().isSilent()) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.NO_DESTINATION)); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.NO_DESTINATION));
} }

View File

@@ -171,7 +171,7 @@ public class PortalCreator {
String route = "PortalCreator::getNetworkName"; String route = "PortalCreator::getNetworkName";
//Use default network if a proper alternative is not set //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(); network = Stargate.getDefaultNetwork();
} }
@@ -207,7 +207,7 @@ public class PortalCreator {
} }
//Check if the user can create portals to this world. //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); Portal destinationPortal = PortalHandler.getByName(destinationName, network);
if (destinationPortal != null && destinationPortal.getWorld() != null) { if (destinationPortal != null && destinationPortal.getWorld() != null) {
String world = destinationPortal.getWorld().getName(); String world = destinationPortal.getWorld().getName();
@@ -294,7 +294,7 @@ public class PortalCreator {
String route = "PortalCreator::checkIfNewPortalIsValid"; String route = "PortalCreator::checkIfNewPortalIsValid";
//Check if the portal name can fit on the sign with padding (>name<) //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.", Stargate.debug(route, String.format("Name length error. %s is too long.",
portal.getCleanName())); portal.getCleanName()));
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.CREATION_NAME_LENGTH)); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString(Message.CREATION_NAME_LENGTH));

View File

@@ -251,7 +251,7 @@ public class PortalHandler {
} }
//Can not create a non-fixed always-on portal //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); portalOptions.put(PortalOption.ALWAYS_ON, false);
} }

View File

@@ -255,7 +255,7 @@ public class PortalRegistry {
* @param portal <p>The portal to register</p> * @param portal <p>The portal to register</p>
*/ */
public static void registerPortal(@NotNull Portal portal) { 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()); portal.getOptions().isBungee());
String portalName = portal.getCleanName(); String portalName = portal.getCleanName();

View File

@@ -6,8 +6,6 @@ import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
/** /**
@@ -15,7 +13,6 @@ import java.util.UUID;
*/ */
public class PortalOwner { public class PortalOwner {
private static final Map<String, OfflinePlayer> fetchedPlayers = new HashMap<>();
private UUID ownerUUID; private UUID ownerUUID;
private String ownerName; private String ownerName;
@@ -25,13 +22,7 @@ public class PortalOwner {
* @param ownerIdentifier <p>A UUID, or a username for legacy support</p> * @param ownerIdentifier <p>A UUID, or a username for legacy support</p>
*/ */
public PortalOwner(@NotNull String ownerIdentifier) { public PortalOwner(@NotNull String ownerIdentifier) {
if (fetchedPlayers.containsKey(ownerIdentifier)) { parseIdentifier(ownerIdentifier);
OfflinePlayer player = fetchedPlayers.get(ownerIdentifier);
this.ownerUUID = player.getUniqueId();
this.ownerName = player.getName();
} else {
parseIdentifier(ownerIdentifier);
}
} }
/** /**
@@ -77,6 +68,12 @@ public class PortalOwner {
*/ */
@NotNull @NotNull
public String getName() { public String getName() {
if (this.ownerUUID != null && this.ownerName == null) {
this.ownerName = fetchName();
if (this.ownerName == null) {
this.ownerName = "UNKNOWN!";
}
}
return ownerName; return ownerName;
} }
@@ -108,14 +105,11 @@ public class PortalOwner {
*/ */
private void parseIdentifier(@NotNull String ownerIdentifier) { private void parseIdentifier(@NotNull String ownerIdentifier) {
UUID ownerUUID = null; UUID ownerUUID = null;
String ownerName; String ownerName = null;
if (ownerIdentifier.length() > 16) { if (ownerIdentifier.length() > 16) {
//If more than 16 characters, the string cannot be a username, so it's probably a UUID //If more than 16 characters, the string cannot be a username, so it's probably a UUID
try { try {
ownerUUID = UUID.fromString(ownerIdentifier); ownerUUID = UUID.fromString(ownerIdentifier);
OfflinePlayer offlineOwner = Bukkit.getServer().getOfflinePlayer(ownerUUID);
fetchedPlayers.put(ownerIdentifier, offlineOwner);
ownerName = offlineOwner.getName();
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
//Invalid as UUID and username, so just keep it as owner name and hope the server owner fixes it //Invalid as UUID and username, so just keep it as owner name and hope the server owner fixes it
ownerName = ownerIdentifier; ownerName = ownerIdentifier;
@@ -129,4 +123,14 @@ public class PortalOwner {
this.ownerUUID = ownerUUID; 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();
}
} }