From 81d389846db9f578fd7538a7caf31bbeb19f1b6c Mon Sep 17 00:00:00 2001 From: Max Kalus Date: Wed, 29 Feb 2012 23:08:56 +0100 Subject: [PATCH] Implemented a command white lister that only allows certain commands during the game. --- .../de/beimax/simplespleef/SimpleSpleef.java | 2 + .../command/SimpleSpleefCommandWhitelist.java | 104 ++++++++++++++++++ src/main/resources/config.yml | 10 ++ src/main/resources/lang_de.yml | 1 + src/main/resources/lang_en.yml | 1 + 5 files changed, 118 insertions(+) create mode 100644 src/main/java/de/beimax/simplespleef/command/SimpleSpleefCommandWhitelist.java diff --git a/src/main/java/de/beimax/simplespleef/SimpleSpleef.java b/src/main/java/de/beimax/simplespleef/SimpleSpleef.java index 135683d..f3c12a3 100644 --- a/src/main/java/de/beimax/simplespleef/SimpleSpleef.java +++ b/src/main/java/de/beimax/simplespleef/SimpleSpleef.java @@ -39,6 +39,7 @@ import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import de.beimax.simplespleef.command.SimpleSpleefCommandExecutor; +import de.beimax.simplespleef.command.SimpleSpleefCommandWhitelist; import de.beimax.simplespleef.command.SimpleSpleefSignCommandExecutor; import de.beimax.simplespleef.game.GameHandler; import de.beimax.simplespleef.gamehelpers.OriginalPositionKeeper; @@ -315,6 +316,7 @@ protected void registerEvents() { pm.registerEvents(gameHandler, this); pm.registerEvents(new UpdateChecker(), this); // check updates pm.registerEvents(new SimpleSpleefSignCommandExecutor(), this); // check sign actions + pm.registerEvents(new SimpleSpleefCommandWhitelist(), this); // check command whitelist } /** diff --git a/src/main/java/de/beimax/simplespleef/command/SimpleSpleefCommandWhitelist.java b/src/main/java/de/beimax/simplespleef/command/SimpleSpleefCommandWhitelist.java new file mode 100644 index 0000000..2b525dc --- /dev/null +++ b/src/main/java/de/beimax/simplespleef/command/SimpleSpleefCommandWhitelist.java @@ -0,0 +1,104 @@ +/** + * This file is part of the SimpleSpleef bukkit plugin. + * Copyright (C) 2011 Maximilian Kalus + * See http://dev.bukkit.org/server-mods/simple-spleef/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + **/ + +package de.beimax.simplespleef.command; + +import java.util.LinkedList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.bukkit.ChatColor; +import org.bukkit.configuration.file.FileConfiguration; +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.PlayerCommandPreprocessEvent; + +import de.beimax.simplespleef.SimpleSpleef; + +/** + * @author mkalus + * + */ +public class SimpleSpleefCommandWhitelist implements Listener { + /** + * list of precompiled command patterns + */ + private List patterns; + + /** + * Constructor + */ + public SimpleSpleefCommandWhitelist() { + compileCommandPatterns(); // compile patterns + } + + /** + * compile the command patterns + */ + public void compileCommandPatterns() { + FileConfiguration config = SimpleSpleef.getPlugin().getConfig(); + + // compile command patterns, if settings are set to do so + if (config.getBoolean("checkCommands.activateWhitelist", true) && config.isList("checkCommands.whitelist")) { + // renew pattern list + patterns = new LinkedList(); + + // compile + for (String pattern : config.getStringList("checkCommands.whitelist")) { + patterns.add(Pattern.compile(pattern)); + } + } else patterns = null; + } + + /** + * Listener: Command preprocessor for the whitelister + * @param event + */ + @EventHandler(priority=EventPriority.HIGHEST) + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { + // some prechecks + if (patterns == null || event == null || event.isCancelled()) return; + // Configuration active for checking commands? + if (!SimpleSpleef.getPlugin().getConfig().getBoolean("checkCommands.activateWhitelist", true)) return; + + Player player = event.getPlayer(); + + // sanity check + if(player == null) return; + + // is player taking part in a game + if (SimpleSpleef.getGameHandler().checkPlayerInGame(player) == null) return; // no + + // ok, we checked all stuff, now check command itself + for (Pattern pattern : patterns) { + // match command + Matcher m = pattern.matcher(event.getMessage()); + if(m.find()) { // found a matching pattern! + return; // command was whitelisted, everything ok + } + } + + // command was not found - cancel command and tell player + player.sendMessage(ChatColor.DARK_RED + SimpleSpleef.ll("errors.commandNotWhitelisted")); + event.setCancelled(true); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d2638fc..bea0457 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -369,3 +369,13 @@ arenas: x: 0.0 y: 0.0 z: 0.0 +# Command whitelist section +checkCommands: +# Activate command white list during games. If you set this on, only commands in the whitelist will be accepted for spleefers during games. +# ALL other commands will be blocked! + activateWhitelist: true +# Whitelist list. List command patterns that are whitelisted. This uses regular expressions - e.g. ^ means start of the string. +# For a short tutorial, see http://www.vogella.de/articles/JavaRegularExpressions/article.html + whitelist: + - '^/spl' + - '^/spla ' \ No newline at end of file diff --git a/src/main/resources/lang_de.yml b/src/main/resources/lang_de.yml index 71ef4fa..cee1ca8 100644 --- a/src/main/resources/lang_de.yml +++ b/src/main/resources/lang_de.yml @@ -73,6 +73,7 @@ errors: statisticsDisabled: 'Statistiken sind ausgeschaltet.' statisticsNoPlayer: 'Spieler [PLAYER] wurde in den Spleef-Statistiken nicht gefunden.' statisticsEmpty: 'Keine Statistiken gefunden.' + commandNotWhitelisted: 'Du darfst dieses Kommando nicht während eines Spiels verwenden.' broadcasts: announce: '[PLAYER] hat ein neues Spleef Spiel in der Arena [ARENA] angekündigt. Kommt alle und spielt mit!' join: '[PLAYER] nimmt am Spleef in der Arena [ARENA] teil.' diff --git a/src/main/resources/lang_en.yml b/src/main/resources/lang_en.yml index 6fab3d3..b1e65fd 100644 --- a/src/main/resources/lang_en.yml +++ b/src/main/resources/lang_en.yml @@ -73,6 +73,7 @@ errors: statisticsDisabled: 'Statistics have been disabled.' statisticsNoPlayer: 'Player [PLAYER] not found in spleef statistics.' statisticsEmpty: 'Could not find any statics entries.' + commandNotWhitelisted: 'You are not allowed to use this command during a game.' broadcasts: announce: '[PLAYER] has announced a new game in arena [ARENA]. Come and join the Spleef!' join: '[PLAYER] has joined a game in arena [ARENA].'