Improve NameManager OfflinePlayer and UUID version handling (#383)

This also removes the deprecated methods which might interact with the
 changed methods in unintended ways from the NameManager and the
 PreTransactionEvent.
This commit is contained in:
Phoenix616 2021-01-28 15:26:23 +01:00
parent ba47b82ba1
commit 22d3f262f5
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
3 changed files with 6 additions and 126 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.acrobot.chestshop</groupId>
<artifactId>chestshop</artifactId>
<version>3.11.1-SNAPSHOT</version>
<version>3.12-SNAPSHOT</version>
<description>Chest-and-sign shop plugin for Bukkit</description>
<name>ChestShop</name>

View File

@ -149,26 +149,6 @@ public class PreTransactionEvent extends Event implements Cancellable {
this.ownerAccount = ownerAccount;
}
/**
* @return Shop's owner
* @deprecated Use {@link #getOwnerAccount}
*/
@Deprecated
public OfflinePlayer getOwner() {
return Bukkit.getOfflinePlayer(ownerAccount.getUuid());
}
/**
* Sets the shop's owner
*
* @param owner Shop owner
* @deprecated Use {@link #setOwnerAccount(Account)}
*/
@Deprecated
public void setOwner(OfflinePlayer owner) {
this.ownerAccount = NameManager.getOrCreateAccount(owner);
}
/**
* @return Owner's inventory
*/

View File

@ -65,6 +65,10 @@ public class NameManager implements Listener {
* @throws IllegalArgumentException when an invalid player object was passed
*/
public static Account getOrCreateAccount(OfflinePlayer player) {
Validate.notNull(player.getName(), "Name of player " + player.getUniqueId() + " is null?");
Validate.isTrue(!(player instanceof Player) || !Properties.ENSURE_CORRECT_PLAYERID || uuidVersion < 0 || player.getUniqueId().version() == uuidVersion,
"Invalid OfflinePlayer! " + player.getUniqueId() + " has version " + player.getUniqueId().version() + " and not server version " + uuidVersion + ". " +
"If you believe that is an error and your setup allows such UUIDs then set the ENSURE_CORRECT_PLAYERID config option to false.");
return getOrCreateAccount(player.getUniqueId(), player.getName());
}
@ -79,9 +83,6 @@ public class NameManager implements Listener {
public static Account getOrCreateAccount(UUID id, String name) {
Validate.notNull(id, "UUID of player is null?");
Validate.notNull(name, "Name of player " + id + " is null?");
Validate.isTrue(!Properties.ENSURE_CORRECT_PLAYERID || uuidVersion < 0 || id.version() == uuidVersion,
"Invalid OfflinePlayer! " + id + " has version " + id.version() + " and not server version " + uuidVersion + ". " +
"If you believe that is an error and your setup allows such UUIDs then set the ENSURE_CORRECT_PLAYERID config option to false.");
Account account = getAccount(id);
if (account == null) {
@ -161,6 +162,7 @@ public class NameManager implements Listener {
* @throws IllegalArgumentException if the username is empty
* @deprecated Use the {@link AccountQueryEvent} instead!
*/
@Deprecated
public static Account getAccountFromShortName(String shortName) {
return getAccountFromShortName(shortName, true);
}
@ -202,19 +204,6 @@ public class NameManager implements Listener {
return account;
}
/**
* Get the information from the last time a player logged in that previously used the shortened name
*
* @param shortName The name of the player to get the last account for
* @return The last account or <tt>null</tt> if none was found
* @throws IllegalArgumentException if the username is empty
* @deprecated Use the {@link AccountQueryEvent} instead!
*/
@Deprecated
public static Account getLastAccountFromShortName(String shortName) {
return getLastAccountFromShortName(shortName, true);
}
/**
* Get the information from the last time a player logged in that previously used the shortened name
*
@ -231,87 +220,6 @@ public class NameManager implements Listener {
return null;
}
/**
* Get the UUID from a player's (non-shortened) username
*
* @param username The player's username
* @return The UUID or <tt>null</tt> if the UUID can't be found or an error occurred
* @deprecated Use {@link NameManager#getAccount(String)}
*/
@Deprecated
public static UUID getUUID(String username) {
Validate.notEmpty(username, "username cannot be null or empty!");
Player player = Bukkit.getPlayer(username);
if (player != null) {
return player.getUniqueId();
}
Account account = getAccount(username);
if (account != null) {
return account.getUuid();
}
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
if (offlinePlayer != null && offlinePlayer.hasPlayedBefore() && offlinePlayer.getUniqueId() != null) {
return offlinePlayer.getUniqueId();
}
return null;
}
/**
* Get the username from a player's UUID
*
* @param uuid The UUID of the player
* @return The username that is stored or <tt>null</tt> if none was found
* @deprecated Use {@link NameManager#getAccount(UUID)}
*/
@Deprecated
public static String getUsername(UUID uuid) {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
return player.getName();
}
Account account = getAccount(uuid);
if (account != null) {
return account.getName();
}
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
if (offlinePlayer != null && offlinePlayer.hasPlayedBefore() && offlinePlayer.getName() != null) {
return offlinePlayer.getName();
}
return null;
}
/**
* Get the full username from another username that might be shortened
*
* @param shortName The name of the player to get the full username for
* @return The full username or <tt>null</tt> if none was found
* @throws IllegalArgumentException if the username is not a shortened name and longer than 15 chars
* @deprecated Use {@link NameManager#getAccountFromShortName(String)}
*/
@Deprecated
public static String getFullUsername(String shortName) {
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(shortName);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account != null) {
return account.getName();
}
return null;
}
/**
* Get the short username from a full username
*
* @param fullName The name of the player to get the short username for
* @return The short username or <tt>null</tt> if none was found
* @deprecated Use {@link NameManager#getAccount(String)}
*/
@Deprecated
public static String getShortUsername(String fullName) {
Account account = getAccount(fullName);
return account != null ? account.getShortName() : null;
}
/**
* Store the username of a player into the database and the username-uuid cache
*
@ -369,14 +277,6 @@ public class NameManager implements Listener {
return shortenedName;
}
/**
* @deprecated Use {@link #canUseName(Player, Permission, String)} to provide specific information about how the player wants to use the name
*/
@Deprecated
public static boolean canUseName(Player player, String name) {
return canUseName(player, OTHER_NAME, name);
}
public static boolean canUseName(Player player, Permission base, String name) {
if (ChestShopSign.isAdminShop(name)) {
return Permission.has(player, Permission.ADMIN_SHOP);