Supporting any sender for the commands
This commit is contained in:
parent
e32805c1fc
commit
e3df62af7e
6 changed files with 41 additions and 13 deletions
|
@ -4,6 +4,7 @@ import buttondevteam.lib.TBMCCommandPreprocessEvent;
|
|||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||
import buttondevteam.lib.architecture.ButtonPlugin;
|
||||
import buttondevteam.lib.chat.Command2MCSender;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
import lombok.val;
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -64,7 +65,7 @@ public class PlayerListener implements Listener {
|
|||
public void onTBMCPreprocess(TBMCCommandPreprocessEvent event) {
|
||||
if (event.isCancelled()) return;
|
||||
try {
|
||||
event.setCancelled(ButtonPlugin.getCommand2MC().handleCommand(event.getSender(), event.getMessage()));
|
||||
event.setCancelled(ButtonPlugin.getCommand2MC().handleCommand(new Command2MCSender(event.getSender()), event.getMessage()));
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("Command processing failed for sender '" + event.getSender() + "' and message '" + event.getMessage() + "'", e);
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ import java.util.stream.Collectors;
|
|||
* The method name is the subcommand, use underlines (_) to add further subcommands.
|
||||
* The args may be null if the conversion failed and it's optional.
|
||||
*/
|
||||
public abstract class Command2<TC extends ICommand2> {
|
||||
public abstract class Command2<TC extends ICommand2, TP extends Command2Sender> {
|
||||
protected Command2() {
|
||||
}
|
||||
|
||||
|
@ -80,9 +80,9 @@ public abstract class Command2<TC extends ICommand2> {
|
|||
map.put(cl, new ParamConverter<>(converter, errormsg));
|
||||
}
|
||||
|
||||
public abstract boolean handleCommand(CommandSender sender, String commandLine) throws Exception;
|
||||
public abstract boolean handleCommand(TP sender, String commandLine) throws Exception;
|
||||
|
||||
protected boolean handleCommand(CommandSender sender, String commandline,
|
||||
protected boolean handleCommand(TP sender, String commandline,
|
||||
HashMap<String, SubcommandData<TC>> subcommands,
|
||||
HashMap<Class<?>, ParamConverter<?>> paramConverters) throws Exception {
|
||||
for (int i = commandline.length(); i != -1; i = commandline.lastIndexOf(' ', i - 1)) {
|
||||
|
@ -107,7 +107,8 @@ public abstract class Command2<TC extends ICommand2> {
|
|||
if (sendertype.isAssignableFrom(sender.getClass()))
|
||||
params.add(sender); //The command either expects a CommandSender or it is a Player, or some other expected type
|
||||
else if (ChromaGamerBase.class.isAssignableFrom(sendertype)
|
||||
&& (cg = ChromaGamerBase.getFromSender(sender)) != null
|
||||
&& sender instanceof Command2MCSender
|
||||
&& (cg = ChromaGamerBase.getFromSender(((Command2MCSender) sender).getSender())) != null
|
||||
&& cg.getClass() == sendertype) //The command expects a user of our system
|
||||
params.add(cg);
|
||||
else {
|
||||
|
@ -224,5 +225,5 @@ public abstract class Command2<TC extends ICommand2> {
|
|||
return ht;
|
||||
}
|
||||
|
||||
public abstract boolean hasPermission(CommandSender sender, TC command);
|
||||
public abstract boolean hasPermission(TP sender, TC command);
|
||||
} //TODO: Test support of Player instead of CommandSender
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
package buttondevteam.lib.chat;
|
||||
|
||||
import buttondevteam.core.MainPlugin;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Command2MC extends Command2<ICommand2MC> {
|
||||
public class Command2MC extends Command2<ICommand2MC, Command2MCSender> {
|
||||
private HashMap<String, SubcommandData<ICommand2MC>> subcommands = new HashMap<>();
|
||||
private HashMap<Class<?>, ParamConverter<?>> paramConverters = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean handleCommand(CommandSender sender, String commandLine) throws Exception {
|
||||
public boolean handleCommand(Command2MCSender sender, String commandLine) throws Exception {
|
||||
return handleCommand(sender, commandLine, subcommands, paramConverters);
|
||||
}
|
||||
|
||||
|
@ -21,8 +20,8 @@ public class Command2MC extends Command2<ICommand2MC> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(CommandSender sender, ICommand2MC command) {
|
||||
return MainPlugin.permission.has(sender, "thorpe.command." + command.getCommandPath().replace(' ', '.'));
|
||||
public boolean hasPermission(Command2MCSender sender, ICommand2MC command) {
|
||||
return MainPlugin.permission.has(sender.getSender(), "thorpe.command." + command.getCommandPath().replace(' ', '.'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package buttondevteam.lib.chat;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class Command2MCSender implements Command2Sender {
|
||||
private @Getter final CommandSender sender;
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
sender.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String[] message) {
|
||||
sender.sendMessage(message);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package buttondevteam.lib.chat;
|
||||
|
||||
public interface Command2Sender { //We don't need the 'extras' of CommandSender on Discord
|
||||
void sendMessage(String message);
|
||||
|
||||
void sendMessage(String[] message);
|
||||
}
|
|
@ -31,9 +31,9 @@ public abstract class ICommand2 {
|
|||
|
||||
private final String path;
|
||||
@Getter
|
||||
private final Command2<?> manager; //TIL that if I use a raw type on a variable then none of the type args will work (including what's defined on a method, not on the type)
|
||||
private final Command2<?, ?> manager; //TIL that if I use a raw type on a variable then none of the type args will work (including what's defined on a method, not on the type)
|
||||
|
||||
public <T extends ICommand2> ICommand2(Command2<T> manager) {
|
||||
public <T extends ICommand2> ICommand2(Command2<T, ?> manager) {
|
||||
path = getcmdpath();
|
||||
this.manager = manager;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue