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;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Permission;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
* @author KingFaris10
*/
public class Toggle implements CommandExecutor {
private static final List<String> toggledPlayers = new ArrayList<String>();
private static final Set<UUID> toggledPlayers = new HashSet<>();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -30,7 +31,7 @@ public class Toggle implements CommandExecutor {
return false;
}
if (setIgnoring(player, !toggledPlayers.contains(player.getName()))) {
if (setIgnoring(player, !isIgnoring(player))) {
player.sendMessage(Messages.prefix(Messages.TOGGLE_MESSAGES_OFF));
} else {
player.sendMessage(Messages.prefix(Messages.TOGGLE_MESSAGES_ON));
@ -44,24 +45,28 @@ public class Toggle implements CommandExecutor {
}
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) {
return toggledPlayers.contains(playerName);
return isIgnoring(Bukkit.getOfflinePlayer(playerName));
}
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
if (ignoring) {
if (!toggledPlayers.contains(player.getName())) {
toggledPlayers.add(player.getName());
}
toggledPlayers.add(player.getUniqueId());
} else {
if (toggledPlayers.contains(player.getName())) {
toggledPlayers.remove(player.getName());
}
toggledPlayers.remove(player.getUniqueId());
}
return ignoring;

View File

@ -27,15 +27,13 @@ public class TransactionMessageSender implements Listener {
}
protected static void sendBuyMessage(TransactionEvent event) {
String ownerName = event.getOwnerAccount().getName();
Player player = event.getClient();
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());
if (owner != null) {
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) {
String ownerName = event.getOwnerAccount().getName();
Player player = event.getClient();
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());
if (owner != null) {
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;
break;
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();
String messageNotEnoughSpace = Messages.prefix(NOT_ENOUGH_SPACE_IN_YOUR_SHOP)
.replace("%price", Economy.formatBalance(event.getExactPrice()))
@ -83,7 +83,7 @@ public class ErrorMessageSender implements Listener {
message = Messages.NOT_ENOUGH_ITEMS_TO_SELL;
break;
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();
String messageOutOfStock = Messages.prefix(NOT_ENOUGH_STOCK_IN_YOUR_SHOP)
.replace("%price", Economy.formatBalance(event.getExactPrice()))