Improve /cstoggle by using UUIDs and HashSet instead of ArrayList

This commit is contained in:
Phoenix616 2019-09-14 23:49:40 +01:00
parent d51a512ab8
commit c028015d5e
3 changed files with 24 additions and 23 deletions

View File

@ -1,22 +1,23 @@
package com.Acrobot.ChestShop.Commands; package com.Acrobot.ChestShop.Commands;
import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Permission;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.HashSet;
import java.util.List; import java.util.Set;
import java.util.UUID;
/** /**
* @author KingFaris10 * @author KingFaris10
*/ */
public class Toggle implements CommandExecutor { public class Toggle implements CommandExecutor {
private static final List<String> toggledPlayers = new ArrayList<String>(); private static final Set<UUID> toggledPlayers = new HashSet<>();
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -30,7 +31,7 @@ public class Toggle implements CommandExecutor {
return false; return false;
} }
if (setIgnoring(player, !toggledPlayers.contains(player.getName()))) { if (setIgnoring(player, !isIgnoring(player))) {
player.sendMessage(Messages.prefix(Messages.TOGGLE_MESSAGES_OFF)); player.sendMessage(Messages.prefix(Messages.TOGGLE_MESSAGES_OFF));
} else { } else {
player.sendMessage(Messages.prefix(Messages.TOGGLE_MESSAGES_ON)); player.sendMessage(Messages.prefix(Messages.TOGGLE_MESSAGES_ON));
@ -44,24 +45,28 @@ public class Toggle implements CommandExecutor {
} }
public static boolean isIgnoring(OfflinePlayer player) { public static boolean isIgnoring(OfflinePlayer player) {
return player != null && isIgnoring(player.getName()); return player != null && isIgnoring(player.getUniqueId());
} }
public static boolean isIgnoring(UUID playerId) {
return toggledPlayers.contains(playerId);
}
/**
* @deprecated Use {@link #isIgnoring(UUID)}
*/
@Deprecated
public static boolean isIgnoring(String playerName) { public static boolean isIgnoring(String playerName) {
return toggledPlayers.contains(playerName); return isIgnoring(Bukkit.getOfflinePlayer(playerName));
} }
public static boolean setIgnoring(Player player, boolean ignoring) { public static boolean setIgnoring(Player player, boolean ignoring) {
Validate.notNull(player); // Make sure the player instance is not null, in case there are any errors in the code Validate.notNull(player); // Make sure the player instance is not null, in case there are any errors in the code
if (ignoring) { if (ignoring) {
if (!toggledPlayers.contains(player.getName())) { toggledPlayers.add(player.getUniqueId());
toggledPlayers.add(player.getName());
}
} else { } else {
if (toggledPlayers.contains(player.getName())) { toggledPlayers.remove(player.getUniqueId());
toggledPlayers.remove(player.getName());
}
} }
return ignoring; return ignoring;

View File

@ -27,15 +27,13 @@ public class TransactionMessageSender implements Listener {
} }
protected static void sendBuyMessage(TransactionEvent event) { protected static void sendBuyMessage(TransactionEvent event) {
String ownerName = event.getOwnerAccount().getName();
Player player = event.getClient(); Player player = event.getClient();
if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) { if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) {
sendMessage(player, Messages.YOU_BOUGHT_FROM_SHOP, event, "owner", ownerName); sendMessage(player, Messages.YOU_BOUGHT_FROM_SHOP, event, "owner", event.getOwnerAccount().getName());
} }
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwnerAccount().getName())) { if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) {
Player owner = Bukkit.getPlayer(event.getOwnerAccount().getUuid()); Player owner = Bukkit.getPlayer(event.getOwnerAccount().getUuid());
if (owner != null) { if (owner != null) {
sendMessage(owner, Messages.SOMEBODY_BOUGHT_FROM_YOUR_SHOP, event, "buyer", player.getName()); sendMessage(owner, Messages.SOMEBODY_BOUGHT_FROM_YOUR_SHOP, event, "buyer", player.getName());
@ -44,15 +42,13 @@ public class TransactionMessageSender implements Listener {
} }
protected static void sendSellMessage(TransactionEvent event) { protected static void sendSellMessage(TransactionEvent event) {
String ownerName = event.getOwnerAccount().getName();
Player player = event.getClient(); Player player = event.getClient();
if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) { if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) {
sendMessage(player, Messages.YOU_SOLD_TO_SHOP, event, "buyer", ownerName); sendMessage(player, Messages.YOU_SOLD_TO_SHOP, event, "buyer", event.getOwnerAccount().getName());
} }
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(ownerName)) { if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) {
Player owner = Bukkit.getPlayer(event.getOwnerAccount().getUuid()); Player owner = Bukkit.getPlayer(event.getOwnerAccount().getUuid());
if (owner != null) { if (owner != null) {
sendMessage(owner, Messages.SOMEBODY_SOLD_TO_YOUR_SHOP, event, "seller", player.getName()); sendMessage(owner, Messages.SOMEBODY_SOLD_TO_YOUR_SHOP, event, "seller", player.getName());

View File

@ -63,7 +63,7 @@ public class ErrorMessageSender implements Listener {
message = Messages.NOT_ENOUGH_MONEY_SHOP; message = Messages.NOT_ENOUGH_MONEY_SHOP;
break; break;
case NOT_ENOUGH_SPACE_IN_CHEST: case NOT_ENOUGH_SPACE_IN_CHEST:
if (Properties.SHOW_MESSAGE_FULL_SHOP && !Properties.CSTOGGLE_TOGGLES_FULL_SHOP || !Toggle.isIgnoring(event.getOwnerAccount().getName())) { if (Properties.SHOW_MESSAGE_FULL_SHOP && !Properties.CSTOGGLE_TOGGLES_FULL_SHOP || !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) {
Location loc = event.getSign().getLocation(); Location loc = event.getSign().getLocation();
String messageNotEnoughSpace = Messages.prefix(NOT_ENOUGH_SPACE_IN_YOUR_SHOP) String messageNotEnoughSpace = Messages.prefix(NOT_ENOUGH_SPACE_IN_YOUR_SHOP)
.replace("%price", Economy.formatBalance(event.getExactPrice())) .replace("%price", Economy.formatBalance(event.getExactPrice()))
@ -83,7 +83,7 @@ public class ErrorMessageSender implements Listener {
message = Messages.NOT_ENOUGH_ITEMS_TO_SELL; message = Messages.NOT_ENOUGH_ITEMS_TO_SELL;
break; break;
case NOT_ENOUGH_STOCK_IN_CHEST: case NOT_ENOUGH_STOCK_IN_CHEST:
if (Properties.SHOW_MESSAGE_OUT_OF_STOCK && !Properties.CSTOGGLE_TOGGLES_OUT_OF_STOCK || !Toggle.isIgnoring(event.getOwnerAccount().getName())) { if (Properties.SHOW_MESSAGE_OUT_OF_STOCK && !Properties.CSTOGGLE_TOGGLES_OUT_OF_STOCK || !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) {
Location loc = event.getSign().getLocation(); Location loc = event.getSign().getLocation();
String messageOutOfStock = Messages.prefix(NOT_ENOUGH_STOCK_IN_YOUR_SHOP) String messageOutOfStock = Messages.prefix(NOT_ENOUGH_STOCK_IN_YOUR_SHOP)
.replace("%price", Economy.formatBalance(event.getExactPrice())) .replace("%price", Economy.formatBalance(event.getExactPrice()))