2014-04-06 19:51:30 +02:00
|
|
|
package com.Acrobot.ChestShop.UUIDs;
|
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.Encoding.Base62;
|
2014-04-06 19:51:47 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.NameUtil;
|
2018-03-12 21:01:03 +01:00
|
|
|
import com.Acrobot.Breeze.Collection.SimpleCache;
|
2020-06-19 22:39:23 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.NumberUtil;
|
2017-06-30 22:00:30 +02:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2014-04-12 16:52:21 +02:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2014-04-10 21:52:05 +02:00
|
|
|
import com.Acrobot.ChestShop.Database.Account;
|
2015-05-22 13:26:07 +02:00
|
|
|
import com.Acrobot.ChestShop.Database.DaoCreator;
|
2020-05-02 16:32:48 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.AccountAccessEvent;
|
2019-05-07 00:49:16 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
|
2014-04-12 13:57:39 +02:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2014-08-13 17:22:52 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
2022-06-13 23:52:26 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
2014-04-10 21:52:05 +02:00
|
|
|
import com.j256.ormlite.dao.Dao;
|
2016-09-20 21:45:18 +02:00
|
|
|
|
2019-07-29 19:50:26 +02:00
|
|
|
import com.j256.ormlite.stmt.SelectArg;
|
2014-04-12 16:52:21 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2016-07-07 18:15:43 +02:00
|
|
|
import org.bukkit.OfflinePlayer;
|
2014-04-06 19:51:47 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2019-05-07 00:49:16 +02:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
2014-04-06 19:51:47 +02:00
|
|
|
|
2014-04-10 21:52:05 +02:00
|
|
|
import java.sql.SQLException;
|
2017-07-01 18:14:41 +02:00
|
|
|
import java.util.Date;
|
2020-01-29 16:35:04 +01:00
|
|
|
import java.util.Locale;
|
2014-04-06 19:51:47 +02:00
|
|
|
import java.util.UUID;
|
2017-06-30 21:28:18 +02:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2016-07-07 18:15:43 +02:00
|
|
|
import java.util.logging.Level;
|
2014-04-06 19:51:47 +02:00
|
|
|
|
2014-04-06 19:51:30 +02:00
|
|
|
/**
|
2014-04-06 19:51:47 +02:00
|
|
|
* Lets you save/cache username and UUID relations
|
2014-04-11 17:23:04 +02:00
|
|
|
*
|
2014-04-06 19:51:30 +02:00
|
|
|
* @author Andrzej Pomirski (Acrobot)
|
|
|
|
*/
|
2016-07-07 18:15:43 +02:00
|
|
|
@SuppressWarnings("UnusedAssignment") // I deliberately set the variables to null while initializing
|
2019-05-07 00:49:16 +02:00
|
|
|
public class NameManager implements Listener {
|
2014-04-10 21:52:05 +02:00
|
|
|
private static Dao<Account, String> accounts;
|
|
|
|
|
2018-03-12 21:01:03 +01:00
|
|
|
private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
|
|
|
|
private static SimpleCache<UUID, Account> uuidToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
|
|
|
|
private static SimpleCache<String, Account> shortToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
|
2018-08-29 22:29:27 +02:00
|
|
|
private static SimpleCache<String, Boolean> invalidPlayers = new SimpleCache<>(Properties.CACHE_SIZE);
|
2015-03-11 18:02:30 +01:00
|
|
|
|
2017-10-29 23:50:24 +01:00
|
|
|
private static Account adminAccount;
|
|
|
|
private static Account serverEconomyAccount;
|
2018-08-29 22:29:27 +02:00
|
|
|
private static int uuidVersion = -1;
|
2017-10-29 23:50:24 +01:00
|
|
|
|
2020-06-19 22:39:23 +02:00
|
|
|
public static int getAccountCount() {
|
|
|
|
try {
|
|
|
|
return NumberUtil.toInt(accounts.queryBuilder().countOf() - 1);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 20:21:16 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2022-06-13 23:52:26 +02:00
|
|
|
Preconditions.checkNotNull(player.getName(), "Name of player " + player.getUniqueId() + " is null?");
|
2022-11-23 22:15:21 +01:00
|
|
|
Preconditions.checkArgument(player instanceof Player || !Properties.ENSURE_CORRECT_PLAYERID || uuidVersion < 0 || player.getUniqueId().version() == uuidVersion,
|
2021-01-28 15:26:23 +01:00
|
|
|
"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.");
|
2020-04-27 22:00:46 +02:00
|
|
|
return getOrCreateAccount(player.getUniqueId(), player.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2022-06-13 23:52:26 +02:00
|
|
|
Preconditions.checkNotNull(id, "UUID of player is null?");
|
|
|
|
Preconditions.checkNotNull(name, "Name of player " + id + " is null?");
|
2019-05-05 20:21:16 +02:00
|
|
|
|
2020-04-27 22:00:46 +02:00
|
|
|
Account account = getAccount(id);
|
2019-05-05 20:21:16 +02:00
|
|
|
if (account == null) {
|
2020-04-27 22:00:46 +02:00
|
|
|
account = storeUsername(new PlayerDTO(id, name));
|
2019-05-05 20:21:16 +02:00
|
|
|
}
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
/**
|
2017-07-01 18:14:41 +02:00
|
|
|
* Get account info from a UUID
|
2018-05-07 16:32:51 +02:00
|
|
|
*
|
|
|
|
* @param uuid The UUID of the player to get the account info
|
|
|
|
* @return The account info or <tt>null</tt> if none was found
|
2017-06-30 21:28:18 +02:00
|
|
|
*/
|
2017-07-01 18:14:41 +02:00
|
|
|
public static Account getAccount(UUID uuid) {
|
2014-04-10 21:52:05 +02:00
|
|
|
try {
|
2017-07-01 18:14:41 +02:00
|
|
|
return uuidToAccount.get(uuid, () -> {
|
|
|
|
try {
|
2019-07-29 19:50:26 +02:00
|
|
|
Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("uuid", new SelectArg(uuid)).queryForFirst();
|
2017-07-01 18:14:41 +02:00
|
|
|
if (account != null) {
|
2018-05-07 16:32:51 +02:00
|
|
|
account.setUuid(uuid); // HOW IS IT EVEN POSSIBLE THAT UUID IS NOT SET EVEN IF WE HAVE FOUND THE PLAYER?!
|
2017-07-01 18:14:41 +02:00
|
|
|
shortToAccount.put(account.getShortName(), account);
|
|
|
|
usernameToAccount.put(account.getName(), account);
|
|
|
|
return account;
|
2017-06-30 21:28:18 +02:00
|
|
|
}
|
2017-07-01 18:14:41 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while getting account for " + uuid + ":", e);
|
2017-06-30 21:28:18 +02:00
|
|
|
}
|
2017-07-01 18:14:41 +02:00
|
|
|
throw new Exception("Could not find account for " + uuid);
|
2017-06-30 21:28:18 +02:00
|
|
|
});
|
2017-07-01 18:14:41 +02:00
|
|
|
} catch (ExecutionException ignored) {
|
2014-04-10 21:52:05 +02:00
|
|
|
return null;
|
|
|
|
}
|
2014-04-06 19:51:47 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
/**
|
2017-07-01 18:14:41 +02:00
|
|
|
* Get account info from a non-shortened username
|
2018-05-07 16:32:51 +02:00
|
|
|
*
|
|
|
|
* @param fullName The full name of the player to get the account info
|
|
|
|
* @return The account info or <tt>null</tt> if none was found
|
2017-07-01 18:14:41 +02:00
|
|
|
* @throws IllegalArgumentException if the username is empty or null
|
2017-06-30 21:28:18 +02:00
|
|
|
*/
|
2017-07-01 18:14:41 +02:00
|
|
|
public static Account getAccount(String fullName) {
|
2022-06-13 23:52:26 +02:00
|
|
|
Preconditions.checkNotNull(fullName, "fullName cannot be null!");
|
|
|
|
Preconditions.checkArgument(!fullName.isEmpty(), "fullName cannot be empty!");
|
2014-04-10 21:52:05 +02:00
|
|
|
try {
|
2017-07-01 18:14:41 +02:00
|
|
|
return usernameToAccount.get(fullName, () -> {
|
|
|
|
try {
|
2019-07-29 19:50:26 +02:00
|
|
|
Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("name", new SelectArg(fullName)).queryForFirst();
|
2017-07-01 18:14:41 +02:00
|
|
|
if (account != null) {
|
2018-05-07 16:32:51 +02:00
|
|
|
account.setName(fullName); // HOW IS IT EVEN POSSIBLE THAT THE NAME IS NOT SET EVEN IF WE HAVE FOUND THE PLAYER?!
|
2017-07-01 18:14:41 +02:00
|
|
|
shortToAccount.put(account.getShortName(), account);
|
|
|
|
return account;
|
2017-06-30 21:28:18 +02:00
|
|
|
}
|
2017-07-01 18:14:41 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while getting account for " + fullName + ":", e);
|
2017-06-30 21:28:18 +02:00
|
|
|
}
|
2017-07-01 18:14:41 +02:00
|
|
|
throw new Exception("Could not find account for " + fullName);
|
2017-06-30 21:28:18 +02:00
|
|
|
});
|
2017-07-01 18:14:41 +02:00
|
|
|
} catch (ExecutionException ignored) {
|
|
|
|
return null;
|
2014-04-11 17:17:20 +02:00
|
|
|
}
|
2014-04-06 19:51:47 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 00:49:16 +02:00
|
|
|
@EventHandler
|
|
|
|
public static void onAccountQuery(AccountQueryEvent event) {
|
|
|
|
if (event.getAccount() == null) {
|
2021-02-24 15:18:15 +01:00
|
|
|
event.setAccount(getLastAccountFromName(event.getName(), event.searchOfflinePlayers()));
|
2019-05-07 00:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
/**
|
2019-05-05 20:21:16 +02:00
|
|
|
* Get account info from a username that might be shortened.
|
2018-05-07 16:32:51 +02:00
|
|
|
*
|
2017-07-01 18:14:41 +02:00
|
|
|
* @param shortName The name of the player to get the account info
|
2018-05-07 16:32:51 +02:00
|
|
|
* @return The account info or <tt>null</tt> if none was found
|
2017-07-09 18:30:26 +02:00
|
|
|
* @throws IllegalArgumentException if the username is empty
|
2019-05-07 00:49:16 +02:00
|
|
|
* @deprecated Use the {@link AccountQueryEvent} instead!
|
2017-06-30 21:28:18 +02:00
|
|
|
*/
|
2021-01-28 15:26:23 +01:00
|
|
|
@Deprecated
|
2017-07-01 18:14:41 +02:00
|
|
|
public static Account getAccountFromShortName(String shortName) {
|
2022-06-13 23:52:26 +02:00
|
|
|
Preconditions.checkNotNull(shortName, "shortName cannot be null!");
|
|
|
|
Preconditions.checkArgument(!shortName.isEmpty(), "shortName cannot be empty!");
|
2018-08-29 22:29:27 +02:00
|
|
|
Account account = null;
|
|
|
|
|
2021-02-24 15:18:15 +01:00
|
|
|
try {
|
|
|
|
account = shortToAccount.get(shortName, () -> {
|
|
|
|
try {
|
|
|
|
Account a = accounts.queryBuilder().where().eq("shortName", new SelectArg(shortName)).queryForFirst();
|
|
|
|
if (a != null) {
|
|
|
|
a.setShortName(shortName); // HOW IS IT EVEN POSSIBLE THAT THE NAME IS NOT SET EVEN IF WE HAVE FOUND THE PLAYER?!
|
|
|
|
return a;
|
2018-08-29 22:29:27 +02:00
|
|
|
}
|
2021-02-24 15:18:15 +01:00
|
|
|
} catch (SQLException e) {
|
|
|
|
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while getting account for " + shortName + ":", e);
|
|
|
|
}
|
|
|
|
throw new Exception("Could not find account for " + shortName);
|
|
|
|
});
|
|
|
|
} catch (ExecutionException ignored) {}
|
2018-08-29 22:29:27 +02:00
|
|
|
return account;
|
2017-07-01 18:14:41 +02:00
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2020-05-02 20:25:44 +02:00
|
|
|
/**
|
2021-02-24 15:18:15 +01:00
|
|
|
* Get the information from the last time a player logged in that previously used the (shortened) name
|
2020-05-02 20:25:44 +02:00
|
|
|
*
|
2021-02-24 15:18:15 +01:00
|
|
|
* @param name The name of the player to get the last account for
|
2020-05-02 20:25:44 +02:00
|
|
|
* @param searchOfflinePlayer Whether or not to search the offline players too
|
|
|
|
* @return The last account or <tt>null</tt> if none was found
|
|
|
|
* @throws IllegalArgumentException if the username is empty
|
|
|
|
*/
|
2021-02-24 15:18:15 +01:00
|
|
|
private static Account getLastAccountFromName(String name, boolean searchOfflinePlayer) {
|
|
|
|
Account account = getAccountFromShortName(name); // first get the account associated with the short name
|
|
|
|
if (account == null) {
|
|
|
|
account = getAccount(name);
|
|
|
|
}
|
|
|
|
if (account == null && searchOfflinePlayer && !invalidPlayers.contains(name.toLowerCase(Locale.ROOT))) {
|
|
|
|
// no account with that shortname was found, try to get an offline player with that name
|
|
|
|
OfflinePlayer offlinePlayer = ChestShop.getBukkitServer().getOfflinePlayer(name);
|
|
|
|
if (offlinePlayer != null && offlinePlayer.getName() != null && offlinePlayer.getUniqueId() != null
|
|
|
|
&& (!Properties.ENSURE_CORRECT_PLAYERID || offlinePlayer.getUniqueId().version() == uuidVersion)) {
|
|
|
|
account = storeUsername(new PlayerDTO(offlinePlayer.getUniqueId(), offlinePlayer.getName()));
|
|
|
|
} else {
|
|
|
|
invalidPlayers.put(name.toLowerCase(Locale.ROOT), true);
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 22:08:06 +01:00
|
|
|
if (account != null) {
|
|
|
|
return getAccount(account.getUuid()); // then get the last account that was online with that UUID
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-07-01 18:14:41 +02:00
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
/**
|
|
|
|
* Store the username of a player into the database and the username-uuid cache
|
2018-05-07 16:32:51 +02:00
|
|
|
*
|
|
|
|
* @param player The data transfer object of the player to store
|
2018-08-29 22:29:27 +02:00
|
|
|
* @return The stored/updated account. <tt>null</tt> if there was an error updating it
|
2017-06-30 21:28:18 +02:00
|
|
|
*/
|
2018-08-29 22:29:27 +02:00
|
|
|
public static Account storeUsername(final PlayerDTO player) {
|
2014-05-06 11:55:33 +02:00
|
|
|
final UUID uuid = player.getUniqueId();
|
2014-04-06 19:51:47 +02:00
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
Account latestAccount = null;
|
|
|
|
try {
|
2019-07-29 19:50:26 +02:00
|
|
|
latestAccount = accounts.queryBuilder().where().eq("uuid", new SelectArg(uuid)).and().eq("name", new SelectArg(player.getName())).queryForFirst();
|
2017-06-30 21:28:18 +02:00
|
|
|
} catch (SQLException e) {
|
2017-06-30 22:00:30 +02:00
|
|
|
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while searching for latest account of " + player.getName() + "/" + uuid + ":", e);
|
2017-06-30 21:28:18 +02:00
|
|
|
}
|
2015-07-05 21:56:42 +02:00
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
if (latestAccount == null) {
|
2019-05-07 00:49:16 +02:00
|
|
|
latestAccount = new Account(player.getName(), getNewShortenedName(player), player.getUniqueId());
|
2015-03-11 18:02:30 +01:00
|
|
|
}
|
2014-05-06 11:55:33 +02:00
|
|
|
|
2017-07-01 18:14:41 +02:00
|
|
|
latestAccount.setLastSeen(new Date());
|
|
|
|
try {
|
|
|
|
accounts.createOrUpdate(latestAccount);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while updating account " + latestAccount + ":", e);
|
2018-08-29 22:29:27 +02:00
|
|
|
return null;
|
2017-07-01 18:14:41 +02:00
|
|
|
}
|
2015-03-11 18:02:30 +01:00
|
|
|
|
2017-07-01 18:14:41 +02:00
|
|
|
usernameToAccount.put(latestAccount.getName(), latestAccount);
|
|
|
|
uuidToAccount.put(uuid, latestAccount);
|
|
|
|
shortToAccount.put(latestAccount.getShortName(), latestAccount);
|
2018-08-29 22:29:27 +02:00
|
|
|
|
|
|
|
return latestAccount;
|
2014-04-06 19:51:47 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 21:28:18 +02:00
|
|
|
/**
|
|
|
|
* Get a new unique shortened name that hasn't been used by another player yet
|
2018-05-07 16:32:51 +02:00
|
|
|
*
|
|
|
|
* @param player The player data to get the shortened name for
|
|
|
|
* @return A new shortened name that hasn't been used before and is a maximum of 15 chars long
|
2017-06-30 21:28:18 +02:00
|
|
|
*/
|
|
|
|
private static String getNewShortenedName(PlayerDTO player) {
|
|
|
|
String shortenedName = NameUtil.stripUsername(player.getName());
|
2014-07-03 18:49:12 +02:00
|
|
|
|
2021-02-24 15:18:15 +01:00
|
|
|
Account account = getAccountFromShortName(shortenedName);
|
2017-07-01 18:14:41 +02:00
|
|
|
if (account == null) {
|
2017-06-30 21:28:18 +02:00
|
|
|
return shortenedName;
|
2014-07-03 18:49:12 +02:00
|
|
|
}
|
2017-07-01 18:14:41 +02:00
|
|
|
for (int id = 0; account != null; id++) {
|
2017-06-30 21:28:18 +02:00
|
|
|
String baseId = Base62.encode(id);
|
|
|
|
shortenedName = NameUtil.stripUsername(player.getName(), 15 - 1 - baseId.length()) + ":" + baseId;
|
2021-02-24 15:18:15 +01:00
|
|
|
account = getAccountFromShortName(shortenedName);
|
2014-07-03 18:49:12 +02:00
|
|
|
}
|
2017-06-30 21:28:18 +02:00
|
|
|
|
|
|
|
return shortenedName;
|
2014-07-03 18:49:12 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
public static boolean canUseName(Player player, Permission base, String name) {
|
2015-03-21 16:11:33 +01:00
|
|
|
if (ChestShopSign.isAdminShop(name)) {
|
2022-07-06 01:54:44 +02:00
|
|
|
if (Permission.has(player, Permission.ADMIN_SHOP)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
ChestShop.logDebug(player.getName() + " cannot use the name " + name + " as it's an admin shop and they don't have the permission " + Permission.ADMIN_SHOP);
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-21 16:11:33 +01:00
|
|
|
}
|
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
if (Permission.otherName(player, base, name)) {
|
2017-07-01 18:14:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-02 20:25:44 +02:00
|
|
|
AccountQueryEvent queryEvent = new AccountQueryEvent(name);
|
|
|
|
ChestShop.callEvent(queryEvent);
|
|
|
|
Account account = queryEvent.getAccount();
|
2020-05-02 16:32:48 +02:00
|
|
|
if (account == null) {
|
2022-07-06 21:05:21 +02:00
|
|
|
// There is no account by the provided name, but it matches the player name
|
|
|
|
// Return true as they specified their own name and a new account should get created
|
|
|
|
if (player.getName().equalsIgnoreCase(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-06 01:54:44 +02:00
|
|
|
ChestShop.logDebug(player.getName() + " cannot use the name " + name + " for a shop as no account with that name exists");
|
2020-05-02 16:32:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!account.getName().equalsIgnoreCase(name) && Permission.otherName(player, base, account.getName())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
AccountAccessEvent event = new AccountAccessEvent(player, account);
|
|
|
|
ChestShop.callEvent(event);
|
|
|
|
return event.canAccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public static void onAccountAccessCheck(AccountAccessEvent event) {
|
|
|
|
if (!event.canAccess()) {
|
|
|
|
event.setAccess(event.getPlayer().getUniqueId().equals(event.getAccount().getUuid()));
|
2022-07-06 01:54:44 +02:00
|
|
|
if (!event.canAccess()) {
|
|
|
|
ChestShop.logDebug(event.getPlayer().getName() + "/" + event.getPlayer().getUniqueId()
|
|
|
|
+ " cannot access the account " + event.getAccount().getName() + "/" + event.getAccount().getUuid()
|
|
|
|
+ " as their UUID doesn't match!");
|
|
|
|
}
|
2020-05-02 16:32:48 +02:00
|
|
|
}
|
2014-04-12 13:57:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-14 20:46:59 +02:00
|
|
|
public static boolean isAdminShop(UUID uuid) {
|
2017-10-29 23:50:24 +01:00
|
|
|
return adminAccount != null && uuid.equals(adminAccount.getUuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isServerEconomyAccount(UUID uuid) {
|
|
|
|
return serverEconomyAccount != null && uuid.equals(serverEconomyAccount.getUuid());
|
2014-06-14 20:46:59 +02:00
|
|
|
}
|
|
|
|
|
2014-04-06 19:51:47 +02:00
|
|
|
public static void load() {
|
2020-11-16 14:09:07 +01:00
|
|
|
if (getUuidVersion() < 0) {
|
|
|
|
if (Bukkit.getOnlineMode()) {
|
|
|
|
setUuidVersion(4);
|
|
|
|
} else if (!Bukkit.getOnlinePlayers().isEmpty()) {
|
|
|
|
setUuidVersion(Bukkit.getOnlinePlayers().iterator().next().getUniqueId().version());
|
|
|
|
}
|
2019-08-20 14:15:41 +02:00
|
|
|
}
|
2014-04-06 19:51:47 +02:00
|
|
|
try {
|
2015-05-22 13:26:07 +02:00
|
|
|
accounts = DaoCreator.getDaoAndCreateTable(Account.class);
|
2014-04-12 16:52:21 +02:00
|
|
|
|
2017-10-29 23:50:24 +01:00
|
|
|
adminAccount = new Account(Properties.ADMIN_SHOP_NAME, Bukkit.getOfflinePlayer(Properties.ADMIN_SHOP_NAME).getUniqueId());
|
2014-07-28 20:51:04 +02:00
|
|
|
accounts.createOrUpdate(adminAccount);
|
2017-07-09 18:00:31 +02:00
|
|
|
|
|
|
|
if (!Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
|
2017-10-29 23:50:24 +01:00
|
|
|
serverEconomyAccount = getAccount(Properties.SERVER_ECONOMY_ACCOUNT);
|
2020-04-27 22:00:46 +02:00
|
|
|
}
|
|
|
|
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;
|
2020-07-04 19:18:59 +02:00
|
|
|
if (!Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
|
2020-09-08 13:20:19 +02:00
|
|
|
ChestShop.getBukkitLogger().log(Level.WARNING, "Server economy account setting '"
|
|
|
|
+ Properties.SERVER_ECONOMY_ACCOUNT
|
|
|
|
+ "' doesn't seem to be the name of a known player account!" +
|
|
|
|
" Please specify the SERVER_ECONOMY_ACCOUNT_UUID" +
|
|
|
|
" or log in at least once and create a player shop with that account" +
|
|
|
|
" in order for the server economy account to work.");
|
2020-07-04 19:18:59 +02:00
|
|
|
}
|
2017-07-09 18:00:31 +02:00
|
|
|
}
|
2014-04-10 21:52:05 +02:00
|
|
|
} catch (SQLException e) {
|
2023-03-01 17:49:59 +01:00
|
|
|
ChestShop.getBukkitLogger().log(Level.SEVERE, "Error while trying to setup accounts", e);
|
2014-04-06 19:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2017-11-15 21:13:07 +01:00
|
|
|
public static Account getServerEconomyAccount() {
|
|
|
|
return serverEconomyAccount;
|
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2018-08-29 22:29:27 +02:00
|
|
|
public static void setUuidVersion(int uuidVersion) {
|
|
|
|
NameManager.uuidVersion = uuidVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getUuidVersion() {
|
|
|
|
return uuidVersion;
|
|
|
|
}
|
2014-04-06 19:51:30 +02:00
|
|
|
}
|