From dffbb7f06ae03290b2f193a44e45f76437860aaf Mon Sep 17 00:00:00 2001 From: KingFaris10 Date: Tue, 20 May 2014 22:13:25 +0100 Subject: [PATCH] Added /cstoggle to disable spam messages. --- .../java/com/Acrobot/ChestShop/ChestShop.java | 10 ++-- .../Acrobot/ChestShop/Commands/Toggle.java | 57 +++++++++++++++++++ .../ChestShop/Configuration/Messages.java | 4 ++ .../Listeners/Player/PlayerLeave.java | 7 +++ .../TransactionMessageSender.java | 5 +- .../com/Acrobot/ChestShop/Permission.java | 4 +- src/main/resources/plugin.yml | 6 ++ 7 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java create mode 100644 src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerLeave.java diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index 810f7f3..8daaa22 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -3,6 +3,7 @@ package com.Acrobot.ChestShop; import com.Acrobot.Breeze.Configuration.Configuration; import com.Acrobot.ChestShop.Commands.Give; import com.Acrobot.ChestShop.Commands.ItemInfo; +import com.Acrobot.ChestShop.Commands.Toggle; import com.Acrobot.ChestShop.Commands.Version; import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; @@ -19,10 +20,7 @@ import com.Acrobot.ChestShop.Listeners.Item.ItemMoveListener; import com.Acrobot.ChestShop.Listeners.ItemInfoListener; import com.Acrobot.ChestShop.Listeners.Modules.DiscountModule; import com.Acrobot.ChestShop.Listeners.Modules.PriceRestrictionModule; -import com.Acrobot.ChestShop.Listeners.Player.PlayerConnect; -import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract; -import com.Acrobot.ChestShop.Listeners.Player.PlayerInventory; -import com.Acrobot.ChestShop.Listeners.Player.PlayerTeleport; +import com.Acrobot.ChestShop.Listeners.Player.*; import com.Acrobot.ChestShop.Listeners.PostShopCreation.CreationFeeGetter; import com.Acrobot.ChestShop.Listeners.PostShopCreation.MessageSender; import com.Acrobot.ChestShop.Listeners.PostShopCreation.ShopCreationLogger; @@ -133,6 +131,7 @@ public class ChestShop extends JavaPlugin { getCommand("iteminfo").setExecutor(new ItemInfo()); getCommand("csVersion").setExecutor(new Version()); getCommand("csGive").setExecutor(new Give()); + getCommand("cstoggle").setExecutor(new Toggle()); startStatistics(); startUpdater(); @@ -240,6 +239,8 @@ public class ChestShop extends JavaPlugin { public void onDisable() { getServer().getScheduler().cancelTasks(this); + Toggle.clearToggledPlayers(); + if (handler != null) { handler.close(); getLogger().removeHandler(handler); @@ -266,6 +267,7 @@ public class ChestShop extends JavaPlugin { registerEvent(new PlayerConnect()); registerEvent(new PlayerInteract()); registerEvent(new PlayerInventory()); + registerEvent(new PlayerLeave()); registerEvent(new PlayerTeleport()); registerEvent(new ItemInfoListener()); diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java b/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java new file mode 100644 index 0000000..559c59d --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Commands/Toggle.java @@ -0,0 +1,57 @@ +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.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; + +/** + * @author KingFaris10 + */ +public class Toggle implements CommandExecutor { + private static final List toggledPlayers = new ArrayList(); + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (sender instanceof Player) { + Player player = (Player) sender; + if (Permission.has(player, Permission.NOTIFY_TOGGLE)) { + if (setIgnoring(player, !toggledPlayers.contains(player.getName()))) player.sendMessage(Messages.TOGGLE_MESSAGES_OFF); + else player.sendMessage(Messages.TOGGLE_MESSAGES_ON); + } else { + player.sendMessage(Messages.ACCESS_DENIED); + } + return true; + } else { + return false; + } + } + + public static void clearToggledPlayers() { + toggledPlayers.clear(); + } + + public static boolean isIgnoring(OfflinePlayer player) { + return player != null && toggledPlayers.contains(player.getName()); + } + + public static boolean setIgnoring(Player player, boolean ignoring) { + Validate.notNull(player); // Make sure the player instance is not null. I believe this should be here instead of (object != null) because if the player is null, it shows there is an error with the code. + if (ignoring) { + if (!toggledPlayers.contains(player.getName())) + toggledPlayers.add(player.getName()); + } else { + if (toggledPlayers.contains(player.getName())) + toggledPlayers.remove(player.getName()); + } + return ignoring; + } + +} diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java index 876567e..2a36b05 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java @@ -67,6 +67,10 @@ public class Messages { @PrecededBySpace public static String CANNOT_CREATE_SHOP_HERE = "You can't create shop here!"; + @PrecededBySpace + public static String TOGGLE_MESSAGES_OFF = "You will no longer receive messages from your shop(s)."; + public static String TOGGLE_MESSAGES_ON = "You will now receive messages from your shop(s).."; + public static String prefix(String message) { return prefix + message; } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerLeave.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerLeave.java new file mode 100644 index 0000000..0e3a8d9 --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerLeave.java @@ -0,0 +1,7 @@ +package com.Acrobot.ChestShop.Listeners.Player; + +/** + * @author KingFaris10 + */ +public class PlayerLeave { +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java index 6bf7d47..1166750 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java @@ -2,6 +2,7 @@ package com.Acrobot.ChestShop.Listeners.PostTransaction; import com.Acrobot.Breeze.Utils.InventoryUtil; import com.Acrobot.Breeze.Utils.MaterialUtil; +import com.Acrobot.ChestShop.Commands.Toggle; import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Economy.Economy; @@ -45,7 +46,7 @@ public class TransactionMessageSender implements Listener { player.sendMessage(message); } - if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER) { + if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwner())) { String message = formatMessage(Messages.SOMEBODY_BOUGHT_FROM_YOUR_SHOP, itemName, price); message = message.replace("%buyer", player.getName()); @@ -68,7 +69,7 @@ public class TransactionMessageSender implements Listener { player.sendMessage(message); } - if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER) { + if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwner())) { String message = formatMessage(Messages.SOMEBODY_SOLD_TO_YOUR_SHOP, itemName, price); message = message.replace("%seller", player.getName()); diff --git a/src/main/java/com/Acrobot/ChestShop/Permission.java b/src/main/java/com/Acrobot/ChestShop/Permission.java index 742cce4..68deee4 100644 --- a/src/main/java/com/Acrobot/ChestShop/Permission.java +++ b/src/main/java/com/Acrobot/ChestShop/Permission.java @@ -24,7 +24,9 @@ public enum Permission { GROUP("ChestShop.group."), NOFEE("ChestShop.nofee"), - DISCOUNT("ChestShop.discount."); + DISCOUNT("ChestShop.discount."), + + NOTIFY_TOGGLE("ChestShop.toggle"); private final String permission; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 58de53d..cedb131 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -31,6 +31,9 @@ commands: aliases: [chestshop] description: Shows the ChestShop's version usage: / + cstoggle: + description: Toggle messages to the owner of a shop + usage: / permissions: ChestShop.*: @@ -67,6 +70,9 @@ permissions: description: Allows user to sell to a shop ChestShop.nofee: description: User doesn't have to pay the shop creation fee. + ChestShop.toggle: + description: Allows user to toggle messages. + default: true ChestShop.admin: description: Allows user to modify/destroy other stores and create an Admin Shops default: op