Add ability to configure server economy account UUID

This adds better support for fake server economy accounts by not
requiring that a player with the name already has an account but allow
automatic creation of the account with the fake UUID provided in the
config.

Old configs that only define the name will still work though.
This commit is contained in:
Phoenix616 2020-04-27 21:00:46 +01:00
parent e57bf94923
commit 744b02a447
2 changed files with 46 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
/**
@ -75,6 +76,23 @@ public class Properties {
return object;
}
});
Configuration.registerParser("UUID", new ValueParser() {
@Override
public String parseToYAML(Object object) {
if (object instanceof UUID) {
return object.toString();
}
return super.parseToYAML(object);
}
@Override
public <T> Object parseToJava(Class<T> type, Object object) {
if (object instanceof String) {
return UUID.fromString((String) object);
}
return object;
}
});
}
@ConfigurationComment("Should the plugin log some messages that are useful for debugging?")
@ -136,9 +154,12 @@ public class Properties {
@ConfigurationComment("First line of your Admin Shop's sign should look like this:")
public static String ADMIN_SHOP_NAME = "Admin Shop";
@ConfigurationComment("The economy account which Admin Shops should use and to which all taxes will go")
@ConfigurationComment("The name of the economy account which Admin Shops should use and to which all taxes will go")
public static String SERVER_ECONOMY_ACCOUNT = "";
@ConfigurationComment("The uuid of the economy account for the Admin Shop. Useful for fake accounts as normally only accounts of players work")
public static UUID SERVER_ECONOMY_ACCOUNT_UUID = new UUID(0, 0);
@ConfigurationComment("Percent of the price that should go to the server's account. (100 = 100 percent)")
public static int TAX_AMOUNT = 0;

View File

@ -55,13 +55,25 @@ 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 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);
return getOrCreateAccount(player.getUniqueId(), player.getName());
}
Account account = getAccount(player.getUniqueId());
/**
* Get or create an account for a player
*
* @param id The UUID of the player to get or create the account for
* @param name The name of the player to get or create the account fo
* @return The account info
* @throws IllegalArgumentException when id or name are null
*/
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(uuidVersion > -1 && id.version() == uuidVersion, "Invalid OfflinePlayer! " + id + " is not of server version " + uuidVersion);
Account account = getAccount(id);
if (account == null) {
account = storeUsername(new PlayerDTO(player.getUniqueId(), player.getName()));
account = storeUsername(new PlayerDTO(id, name));
}
return account;
}
@ -375,10 +387,13 @@ public class NameManager implements Listener {
if (!Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
serverEconomyAccount = getAccount(Properties.SERVER_ECONOMY_ACCOUNT);
if (serverEconomyAccount == null || serverEconomyAccount.getUuid() == null) {
serverEconomyAccount = null;
ChestShop.getBukkitLogger().log(Level.WARNING, "Server economy account setting '" + Properties.SERVER_ECONOMY_ACCOUNT + "' doesn't seem to be the name of a known player! Please log in at least once in order for the server economy account to work.");
}
}
if (serverEconomyAccount == null && !Properties.SERVER_ECONOMY_ACCOUNT.isEmpty() && !Properties.SERVER_ECONOMY_ACCOUNT_UUID.equals(new UUID(0, 0))) {
serverEconomyAccount = getOrCreateAccount(Properties.SERVER_ECONOMY_ACCOUNT_UUID, Properties.SERVER_ECONOMY_ACCOUNT);
}
if (serverEconomyAccount == null || serverEconomyAccount.getUuid() == null) {
serverEconomyAccount = null;
ChestShop.getBukkitLogger().log(Level.WARNING, "Server economy account setting '" + Properties.SERVER_ECONOMY_ACCOUNT + "' doesn't seem to be the name of a known player! Please log in at least once in order for the server economy account to work.");
}
} catch (SQLException e) {
e.printStackTrace();