This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
/
SimpleSpleefCommandWhitelist.java
104 lines (89 loc) · 3.22 KB
/
SimpleSpleefCommandWhitelist.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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);
}
}