From b0523b2199d67d3d3b1e34508ec36016358affc0 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 27 May 2019 17:11:09 +0100 Subject: [PATCH] Add cooldown for empty/full shop owner notifications --- .../ChestShop/Configuration/Properties.java | 2 ++ .../PreTransaction/ErrorMessageSender.java | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java index fd535e9..37d4e42 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java @@ -164,6 +164,8 @@ public class Properties { public static boolean SHOW_MESSAGE_OUT_OF_STOCK = true; @ConfigurationComment("Do you want to show \"Full shop\" messages?") public static boolean SHOW_MESSAGE_FULL_SHOP = true; + @ConfigurationComment("How many seconds do you want to wait before showing notifications for the same shop to the owner again?") + public static long NOTIFICATION_MESSAGE_COOLDOWN = 10; @PrecededBySpace @ConfigurationComment("Can players hide the \"Out of stock\" messages with /cstoggle?") diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java index 0d5bc2b..af7f27f 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java @@ -7,14 +7,19 @@ import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Database.Account; import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Events.PreTransactionEvent; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Table; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; +import java.util.UUID; + import static com.Acrobot.ChestShop.Configuration.Messages.CLIENT_DEPOSIT_FAILED; import static com.Acrobot.ChestShop.Configuration.Messages.NOT_ENOUGH_STOCK_IN_YOUR_SHOP; import static com.Acrobot.ChestShop.Configuration.Messages.NOT_ENOUGH_SPACE_IN_YOUR_SHOP; @@ -23,6 +28,16 @@ import static com.Acrobot.ChestShop.Configuration.Messages.NOT_ENOUGH_SPACE_IN_Y * @author Acrobot */ public class ErrorMessageSender implements Listener { + + private static Table notificationCooldowns = HashBasedTable.create(); + + @EventHandler(priority = EventPriority.MONITOR) + public static void onQuit(PlayerQuitEvent event) { + if (Properties.NOTIFICATION_MESSAGE_COOLDOWN > 0) { + notificationCooldowns.rowMap().remove(event.getPlayer().getUniqueId()); + } + } + @EventHandler(priority = EventPriority.MONITOR) public static void onMessage(PreTransactionEvent event) { if (!event.isCancelled()) { @@ -108,10 +123,20 @@ public class ErrorMessageSender implements Listener { Player player = Bukkit.getPlayer(ownerAccount.getUuid()); if (player != null) { message = message.replace("%material", "%item"); + String replacedMessage = message.replace("%item", MaterialUtil.getItemList(stock)); + + if (Properties.NOTIFICATION_MESSAGE_COOLDOWN > 0) { + Long last = notificationCooldowns.get(player.getUniqueId(), replacedMessage); + if (last != null && last + Properties.NOTIFICATION_MESSAGE_COOLDOWN * 1000 > System.currentTimeMillis()) { + return; + } + notificationCooldowns.put(player.getUniqueId(), replacedMessage, System.currentTimeMillis()); + } + if (Properties.SHOWITEM_MESSAGE && MaterialUtil.Show.sendMessage(player, message, stock)) { return; } - player.sendMessage(message.replace("%item", MaterialUtil.getItemList(stock))); + player.sendMessage(replacedMessage); } } }