Use the standard Bukkit command handling system.

Doing so will remove the possiblility for dynamic command alias assignment but makes factions compatible with all other
plugins doing stuff like blocking commands from being used (AntiGuest, NoCheatPlus, War etc) and plugins that log command
useage (Hawkeye etc).
This commit is contained in:
Olof Larsson
2013-01-03 08:23:46 +01:00
parent 31faf605dc
commit 16c69d67cd
10 changed files with 49 additions and 253 deletions

View File

@@ -27,7 +27,6 @@ public abstract class MCommand<T extends MPlugin>
// The different names this commands will react to
public List<String> aliases;
public boolean allowNoSlashAccess;
// Information on the args
public List<String> requiredArgs;
@@ -73,8 +72,6 @@ public abstract class MCommand<T extends MPlugin>
this.permission = null;
this.allowNoSlashAccess = false;
this.subCommands = new ArrayList<MCommand<?>>();
this.aliases = new ArrayList<String>();

View File

@@ -7,7 +7,6 @@ import java.util.Map.Entry;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
@@ -36,15 +35,9 @@ public abstract class MPlugin extends JavaPlugin
protected boolean loadSuccessful = false;
public boolean getAutoSave() {return this.autoSave;}
public void setAutoSave(boolean val) {this.autoSave = val;}
public String refCommand = "";
// Listeners
private MPluginSecretPlayerListener mPluginSecretPlayerListener;
private MPluginSecretServerListener mPluginSecretServerListener;
// Our stored base commands
private List<MCommand<?>> baseCommands = new ArrayList<MCommand<?>>();
public List<MCommand<?>> getBaseCommands() { return this.baseCommands; }
public MPluginSecretPlayerListener mPluginSecretPlayerListener;
// -------------------------------------------- //
// ENABLE
@@ -70,22 +63,8 @@ public abstract class MPlugin extends JavaPlugin
this.txt = new TextUtil();
initTXT();
// attempt to get first command defined in plugin.yml as reference command, if any commands are defined in there
// reference command will be used to prevent "unknown command" console messages
try
{
Map<String, Map<String, Object>> refCmd = this.getDescription().getCommands();
if (refCmd != null && !refCmd.isEmpty())
this.refCommand = (String)(refCmd.keySet().toArray()[0]);
}
catch (ClassCastException ex) {}
// Create and register listeners
this.mPluginSecretPlayerListener = new MPluginSecretPlayerListener(this);
this.mPluginSecretServerListener = new MPluginSecretServerListener(this);
getServer().getPluginManager().registerEvents(this.mPluginSecretPlayerListener, this);
getServer().getPluginManager().registerEvents(this.mPluginSecretServerListener, this);
// Register recurring tasks
long saveTicks = 20 * 60 * 30; // Approximately every 30 min
@@ -174,72 +153,6 @@ public abstract class MPlugin extends JavaPlugin
}
}
// -------------------------------------------- //
// COMMAND HANDLING
// -------------------------------------------- //
// can be overridden by P method, to provide option
public boolean logPlayerCommands()
{
return true;
}
public boolean handleCommand(CommandSender sender, String commandString, boolean testOnly)
{
return handleCommand(sender, commandString, testOnly, false);
}
public boolean handleCommand(final CommandSender sender, String commandString, boolean testOnly, boolean async)
{
boolean noSlash = true;
if (commandString.startsWith("/"))
{
noSlash = false;
commandString = commandString.substring(1);
}
for (final MCommand<?> command : this.getBaseCommands())
{
if (noSlash && ! command.allowNoSlashAccess) continue;
for (String alias : command.aliases)
{
// disallow double-space after alias, so specific commands can be prevented (preventing "f home" won't prevent "f home")
if (commandString.startsWith(alias+" ")) return false;
if (commandString.startsWith(alias+" ") || commandString.equals(alias))
{
final List<String> args = new ArrayList<String>(Arrays.asList(commandString.split("\\s+")));
args.remove(0);
if (testOnly) return true;
if (async)
{
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
{
@Override
public void run()
{
command.execute(sender, args);
}
});
}
else
command.execute(sender, args);
return true;
}
}
}
return false;
}
public boolean handleCommand(CommandSender sender, String commandString)
{
return this.handleCommand(sender, commandString, false);
}
// -------------------------------------------- //
// HOOKS
// -------------------------------------------- //

View File

@@ -4,8 +4,6 @@ import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import com.massivecraft.factions.zcore.persist.EM;
@@ -15,36 +13,11 @@ import com.massivecraft.factions.zcore.persist.PlayerEntityCollection;
public class MPluginSecretPlayerListener implements Listener
{
private MPlugin p;
public MPlugin p;
public MPluginSecretPlayerListener(MPlugin p)
{
this.p = p;
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
if (event.isCancelled()) return;
if (p.handleCommand(event.getPlayer(), event.getMessage()))
{
if (p.logPlayerCommands())
Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage());
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerChat(AsyncPlayerChatEvent event)
{
if (event.isCancelled()) return;
if (p.handleCommand(event.getPlayer(), event.getMessage(), false, true))
{
if (p.logPlayerCommands())
Bukkit.getLogger().info("[PLAYER_COMMAND] "+event.getPlayer().getName()+": "+event.getMessage());
event.setCancelled(true);
}
Bukkit.getPluginManager().registerEvents(this, this.p);
}
@EventHandler(priority = EventPriority.LOWEST)

View File

@@ -1,28 +0,0 @@
package com.massivecraft.factions.zcore;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerCommandEvent;
public class MPluginSecretServerListener implements Listener
{
private MPlugin p;
public MPluginSecretServerListener(MPlugin p)
{
this.p = p;
}
@EventHandler(priority = EventPriority.LOWEST)
public void onServerCommand(ServerCommandEvent event)
{
if (event.getCommand().length() == 0) return;
if (p.handleCommand(event.getSender(), event.getCommand()))
{
event.setCommand(p.refCommand);
}
}
}