Make querying of accounts by (short) name an event too

This commit is contained in:
Phoenix616 2019-05-06 23:49:16 +01:00
parent dcd1dd9057
commit b300798a62
9 changed files with 89 additions and 13 deletions

View File

@ -276,6 +276,8 @@ public class ChestShop extends JavaPlugin {
private void registerEvents() {
registerEvent(new com.Acrobot.ChestShop.Plugins.ChestShop()); //Chest protection
registerEvent(new NameManager());
registerPreShopCreationEvents();
registerPreTransactionEvents();
registerPostShopCreationEvents();

View File

@ -33,8 +33,12 @@ public class Account {
}
public Account(String name, UUID uuid) {
this(name, NameUtil.stripUsername(name), uuid);
}
public Account(String name, String shortName, UUID uuid) {
this.name = name;
this.shortName = NameUtil.stripUsername(name);
this.shortName = shortName;
this.uuid = uuid;
}

View File

@ -0,0 +1,39 @@
package com.Acrobot.ChestShop.Events;
import com.Acrobot.ChestShop.Database.Account;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Represents a query for an account by using the name (e.g. from the shop sign)
*/
public class AccountQueryEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final String name;
private Account account = null;
public AccountQueryEvent(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -5,6 +5,7 @@ import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.Economy.AccountCheckEvent;
import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
@ -13,7 +14,6 @@ import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Plugins.ChestShop;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -155,7 +155,9 @@ public class PlayerInteract implements Listener {
String prices = sign.getLine(PRICE_LINE);
String material = sign.getLine(ITEM_LINE);
Account account = NameManager.getLastAccountFromShortName(name);
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account == null) {
player.sendMessage(Messages.prefix(Messages.PLAYER_NOT_FOUND));
return null;

View File

@ -1,8 +1,10 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -30,7 +32,9 @@ public class NameChecker implements Listener {
event.setOutcome(UNKNOWN_PLAYER);
}
} else {
Account account = NameManager.getAccountFromShortName(name);
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account == null) {
event.setOutcome(UNKNOWN_PLAYER);
}

View File

@ -5,11 +5,13 @@ import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -39,7 +41,9 @@ public class ShopRefundListener implements Listener {
return;
}
Account account = NameManager.getAccountFromShortName(event.getSign().getLine(NAME_LINE));
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(event.getSign().getLine(NAME_LINE));
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account == null) {
return;
}

View File

@ -3,11 +3,12 @@ package com.Acrobot.ChestShop;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.Protection.ProtectBlockEvent;
import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
@ -78,7 +79,9 @@ public class Security {
continue;
}
Account account = NameManager.getAccountFromShortName(sign.getLine(ChestShopSign.NAME_LINE));
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(sign.getLine(ChestShopSign.NAME_LINE));
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account != null && !account.getUuid().equals(player.getUniqueId())) {
return true;
}

View File

@ -5,9 +5,11 @@ import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
@ -123,7 +125,9 @@ public class ChestShopSign {
String name = sign.getLine(NAME_LINE);
if (name == null || name.isEmpty()) return false;
Account account = NameManager.getAccountFromShortName(name);
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account == null) {
return player.getName().equalsIgnoreCase(name);
}

View File

@ -7,6 +7,7 @@ import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Database.DaoCreator;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.j256.ormlite.dao.Dao;
@ -15,6 +16,8 @@ import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.sql.SQLException;
import java.util.Date;
@ -30,7 +33,7 @@ import static com.Acrobot.ChestShop.Permission.OTHER_NAME;
* @author Andrzej Pomirski (Acrobot)
*/
@SuppressWarnings("UnusedAssignment") // I deliberately set the variables to null while initializing
public class NameManager {
public class NameManager implements Listener {
private static Dao<Account, String> accounts;
private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
@ -116,6 +119,13 @@ public class NameManager {
}
}
@EventHandler
public static void onAccountQuery(AccountQueryEvent event) {
if (event.getAccount() == null) {
event.setAccount(getLastAccountFromShortName(event.getName()));
}
}
/**
* 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.
@ -123,6 +133,7 @@ public class NameManager {
* @param shortName The name of the player to get the account info
* @return The account info or <tt>null</tt> if none was found
* @throws IllegalArgumentException if the username is empty
* @deprecated Use the {@link AccountQueryEvent} instead!
*/
public static Account getAccountFromShortName(String shortName) {
return getAccountFromShortName(shortName, true);
@ -170,8 +181,10 @@ public class NameManager {
*
* @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 not a shortened name and longer than 15 chars
* @throws IllegalArgumentException if the username is empty
* @deprecated Use the {@link AccountQueryEvent} instead!
*/
@Deprecated
public static Account getLastAccountFromShortName(String shortName) {
Account account = getAccountFromShortName(shortName); // first get the account associated with the short name
if (account != null) {
@ -239,7 +252,9 @@ public class NameManager {
*/
@Deprecated
public static String getFullUsername(String shortName) {
Account account = getLastAccountFromShortName(shortName);
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(shortName);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account != null) {
return account.getName();
}
@ -276,8 +291,7 @@ public class NameManager {
}
if (latestAccount == null) {
latestAccount = new Account(player.getName(), player.getUniqueId());
latestAccount.setShortName(getNewShortenedName(player));
latestAccount = new Account(player.getName(), getNewShortenedName(player), player.getUniqueId());
}
latestAccount.setLastSeen(new Date());