This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a command white lister that only allows certain commands
during the game.
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/java/de/beimax/simplespleef/command/SimpleSpleefCommandWhitelist.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
**/ | ||
|
||
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<Pattern> 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<Pattern>(); | ||
|
||
// 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters