Add cooldown for empty/full shop owner notifications

This commit is contained in:
Phoenix616 2019-05-27 17:11:09 +01:00
parent cef571223e
commit b0523b2199
2 changed files with 28 additions and 1 deletions

View File

@ -164,6 +164,8 @@ public class Properties {
public static boolean SHOW_MESSAGE_OUT_OF_STOCK = true; public static boolean SHOW_MESSAGE_OUT_OF_STOCK = true;
@ConfigurationComment("Do you want to show \"Full shop\" messages?") @ConfigurationComment("Do you want to show \"Full shop\" messages?")
public static boolean SHOW_MESSAGE_FULL_SHOP = true; 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 @PrecededBySpace
@ConfigurationComment("Can players hide the \"Out of stock\" messages with /cstoggle?") @ConfigurationComment("Can players hide the \"Out of stock\" messages with /cstoggle?")

View File

@ -7,14 +7,19 @@ import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Database.Account; import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.PreTransactionEvent; 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.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack; 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.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_STOCK_IN_YOUR_SHOP;
import static com.Acrobot.ChestShop.Configuration.Messages.NOT_ENOUGH_SPACE_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 * @author Acrobot
*/ */
public class ErrorMessageSender implements Listener { public class ErrorMessageSender implements Listener {
private static Table<UUID, String, Long> 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) @EventHandler(priority = EventPriority.MONITOR)
public static void onMessage(PreTransactionEvent event) { public static void onMessage(PreTransactionEvent event) {
if (!event.isCancelled()) { if (!event.isCancelled()) {
@ -108,10 +123,20 @@ public class ErrorMessageSender implements Listener {
Player player = Bukkit.getPlayer(ownerAccount.getUuid()); Player player = Bukkit.getPlayer(ownerAccount.getUuid());
if (player != null) { if (player != null) {
message = message.replace("%material", "%item"); 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)) { if (Properties.SHOWITEM_MESSAGE && MaterialUtil.Show.sendMessage(player, message, stock)) {
return; return;
} }
player.sendMessage(message.replace("%item", MaterialUtil.getItemList(stock))); player.sendMessage(replacedMessage);
} }
} }
} }