Added Macros prevention

This commit is contained in:
Michel Roux 2012-05-13 17:47:43 +02:00
parent 46bc9963fa
commit 6fc78f64f3
2 changed files with 46 additions and 0 deletions

View File

@ -30,6 +30,7 @@ public abstract class SimplyPrevents implements Listener {
"chat.Chat",
"chat.CapsLock",
"chat.Flood",
"chat.Macros",
"craft.Brew",
"craft.Chest",
"craft.Dispenser",

View File

@ -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<Player, Long> commandsTimestamps = new HashMap<Player, Long>();
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;
}
}