Made the chat plugin less essential and fixed /u help <cmd>

- CommandCaller moved to ButtonCore
- Now only cancelling the Bukkit chat event if the custom event ran
without an error
This commit is contained in:
Norbi Peti 2016-12-09 18:10:30 +01:00
parent 24d04f04c8
commit 9c06e22ca7
5 changed files with 10 additions and 119 deletions

View file

@ -48,7 +48,6 @@
<artifactSet>
<includes>
<include>net.sourceforge.htmlcleaner:htmlcleaner</include>
<include>org.reflections:reflections</include>
<include>org.javassist:javassist</include>
</includes>
</artifactSet>

View file

@ -14,7 +14,6 @@ import org.bukkit.scoreboard.Scoreboard;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import buttondevteam.chat.commands.CommandCaller;
import buttondevteam.chat.commands.YeehawCommand;
import buttondevteam.chat.listener.PlayerListener;
import buttondevteam.lib.TBMCCoreAPI;
@ -105,7 +104,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
};
t = new Thread(r);
t.start();
Bukkit.getScheduler().runTaskLater(this, () -> CommandCaller.RegisterCommands(), 0);
}
public Boolean stop = false;

View file

@ -1,110 +0,0 @@
package buttondevteam.chat.commands;
import java.util.Arrays;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.chat.PluginMain;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
public class CommandCaller implements CommandExecutor {
private static final String REGISTER_ERROR_MSG = "An error occured while registering commands";
private CommandCaller() {
}
private static CommandCaller instance;
public static void RegisterCommands() {
if (instance == null)
instance = new CommandCaller();
for (Entry<String, TBMCCommandBase> entry : TBMCChatAPI.GetCommands().entrySet()) {
TBMCCommandBase c = entry.getValue();
if (c == null) {
TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Null command found at " + entry.getKey() + "!"));
continue;
}
if (c.GetCommandPath() == null) {
TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Command " + entry.getKey() + " has no command path!"));
continue;
}
if (c.getPlugin() == null) {
TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Command " + entry.getKey() + " has no plugin!"));
continue;
}
int i;
String topcmd;
if ((i = (topcmd = c.GetCommandPath()).indexOf(' ')) != -1) // Get top-level command
topcmd = c.GetCommandPath().substring(0, i);
{
PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(topcmd);
if (pc == null)
TBMCCoreAPI.SendException(REGISTER_ERROR_MSG, new Exception("Top level command " + topcmd
+ " not registered in plugin.yml for plugin: " + c.getPlugin().getName()));
else
pc.setExecutor(instance);
}
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
String path = command.getName().toLowerCase();
for (String arg : args)
path += " " + arg;
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
int argc = 0;
while (cmd == null && path.contains(" ")) {
path = path.substring(0, path.lastIndexOf(' '));
argc++;
cmd = TBMCChatAPI.GetCommands().get(path);
}
if (cmd == null) {
String[] subcmds = TBMCChatAPI.GetSubCommands(path, sender);
if (subcmds.length > 0)
sender.sendMessage(subcmds);
else {
final String errormsg = "§cYou don't have access to any of this command's subcommands.";
sender.sendMessage(errormsg);
if (!(sender instanceof ConsoleCommandSender))
Bukkit.getConsoleSender().sendMessage(errormsg);
}
return true;
}
if (cmd.GetModOnly() && !PluginMain.permission.has(sender, "tbmc.admin")) {
sender.sendMessage("§cYou need to be a mod to use this command.");
return true;
}
if (cmd.GetPlayerOnly() && !(sender instanceof Player)) {
sender.sendMessage("§cOnly ingame players can use this command.");
return true;
}
final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args;
try {
if (!cmd.OnCommand(sender, alias, cmdargs)) {
if (cmd.GetHelpText(alias) == null) {
sender.sendMessage("Wrong usage, but there's no help text! Error is being reported to devs.");
throw new NullPointerException("GetHelpText is null for comand /" + cmd.GetCommandPath());
} else
sender.sendMessage(cmd.GetHelpText(alias));
}
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to execute command /" + cmd.GetCommandPath() + " with arguments "
+ Arrays.toString(cmdargs), e);
}
return true;
}
}

View file

@ -58,12 +58,16 @@ public final class HelpCommand extends UCommandBase {
} else {
String path = args[0];
for (int i = 1; i < args.length; i++)
path += "/" + args[i];
path += " " + args[i];
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
if (cmd == null)
sender.sendMessage(new String[] { "§cError: Command not found: " + path.replace('/', ' '),
"Usage example: /u accept --> /u help u accept" });
else
if (cmd == null) {
String[] subcmds = TBMCChatAPI.GetSubCommands(path, sender);
if (subcmds.length > 0)
sender.sendMessage(subcmds);
else
sender.sendMessage(new String[] { "§cError: Command not found or you don't have permission for it: " + path,
"Usage example: /u accept --> /u help u accept" });
} else
sender.sendMessage(cmd.GetHelpText(args[0]));
}
return true;

View file

@ -66,10 +66,10 @@ public class PlayerListener implements Listener {
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (event.isCancelled())
return;
event.setCancelled(true);
TBMCChatAPI.SendChatMessage(
TBMCPlayer.getPlayer(event.getPlayer()).asPluginPlayer(ChatPlayer.class).CurrentChannel,
event.getPlayer(), event.getMessage());
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGHEST)