Fix issues with user account storage on shop creation

This fixes a bug where shop owner accounts would not be created in certain cases e.g. when the owner had certain admin permissions or created the account for another player while having certain admin permissions.
This commit is contained in:
Phoenix616 2019-05-05 19:21:16 +01:00
parent 4769ae2cf7
commit 2e0c5a3cac
3 changed files with 28 additions and 4 deletions

View File

@ -166,7 +166,7 @@ public class PreTransactionEvent extends Event implements Cancellable {
*/
@Deprecated
public void setOwner(OfflinePlayer owner) {
this.ownerAccount = NameManager.getAccount(owner.getUniqueId());
this.ownerAccount = NameManager.getOrCreateAccount(owner);
}
/**

View File

@ -2,7 +2,6 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -24,12 +23,17 @@ public class NameChecker implements Listener {
Player player = event.getPlayer();
if (name.isEmpty() || !NameManager.canUseName(player, OTHER_NAME_CREATE, name)) {
Account account = NameManager.getAccount(player.getName());
Account account = NameManager.getOrCreateAccount(player);
if (account != null) {
event.setSignLine(NAME_LINE, account.getShortName());
} else {
event.setOutcome(UNKNOWN_PLAYER);
}
} else {
Account account = NameManager.getAccountFromShortName(name);
if (account == null) {
event.setOutcome(UNKNOWN_PLAYER);
}
}
}
}

View File

@ -42,6 +42,25 @@ public class NameManager {
private static Account serverEconomyAccount;
private static int uuidVersion = -1;
/**
* Get or create an account for a player
*
* @param player The Player to get or create the account for (only if the player has both an UUID and a name!)
* @return The account info
* @throws IllegalArgumentException when an invalid player object was passed
*/
public static Account getOrCreateAccount(OfflinePlayer player) {
Validate.notNull(player.getName(), "Name of player is null?");
Validate.notNull(player.getUniqueId(), "UUID of player is null?");
Validate.isTrue(player.getUniqueId().version() != uuidVersion, "Invalid OfflinePlayer! " + player.getUniqueId() + " is not of server version " + uuidVersion);
Account account = getAccount(player.getUniqueId());
if (account == null) {
account = storeUsername(new PlayerDTO(player.getUniqueId(), player.getName()));
}
return account;
}
/**
* Get account info from a UUID
*
@ -98,7 +117,8 @@ public class NameManager {
}
/**
* Get account info from a username that might be shortened
* Get account info from a username that might be shortened.
* If no account was found it will try to search all known players and create an account.
*
* @param shortName The name of the player to get the account info
* @return The account info or <tt>null</tt> if none was found