From 6fc78f64f367aa32caf5093df93b14411a8d97ed Mon Sep 17 00:00:00 2001 From: Xefir Date: Sun, 13 May 2012 17:47:43 +0200 Subject: [PATCH] Added Macros prevention --- .../bukkit/simplyperms/SimplyPrevents.java | 1 + .../simplyperms/preventions/chat/Macros.java | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/net/crystalyx/bukkit/simplyperms/preventions/chat/Macros.java diff --git a/src/net/crystalyx/bukkit/simplyperms/SimplyPrevents.java b/src/net/crystalyx/bukkit/simplyperms/SimplyPrevents.java index a55a1d4..67b7e21 100755 --- a/src/net/crystalyx/bukkit/simplyperms/SimplyPrevents.java +++ b/src/net/crystalyx/bukkit/simplyperms/SimplyPrevents.java @@ -30,6 +30,7 @@ public abstract class SimplyPrevents implements Listener { "chat.Chat", "chat.CapsLock", "chat.Flood", + "chat.Macros", "craft.Brew", "craft.Chest", "craft.Dispenser", diff --git a/src/net/crystalyx/bukkit/simplyperms/preventions/chat/Macros.java b/src/net/crystalyx/bukkit/simplyperms/preventions/chat/Macros.java new file mode 100644 index 0000000..7b5c524 --- /dev/null +++ b/src/net/crystalyx/bukkit/simplyperms/preventions/chat/Macros.java @@ -0,0 +1,45 @@ +package net.crystalyx.bukkit.simplyperms.preventions.chat; + +import java.util.HashMap; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +import net.crystalyx.bukkit.simplyperms.SimplyPlugin; +import net.crystalyx.bukkit.simplyperms.SimplyPrevents; + +public class Macros extends SimplyPrevents { + + private HashMap commandsTimestamps = new HashMap(); + + public Macros(SimplyPlugin plugin) { + super(plugin); + } + + @EventHandler(priority = EventPriority.LOW) + public void chat(PlayerCommandPreprocessEvent event) { + Player player = event.getPlayer(); + if (isChatLocked(player)) { + prevent(event, player, "macros,spam"); + } else { + setChatLock(player); + } + } + + private void setChatLock(Player player) { + commandsTimestamps.put(player, Long.valueOf(System.currentTimeMillis() + 2000)); + } + + private boolean isChatLocked(Player player) { + Long nextPossible = commandsTimestamps.get(player); + if (nextPossible == null) { + return false; + } + + long currentTime = System.currentTimeMillis(); + return nextPossible.longValue() >= currentTime; + } + +}