Add support for channels and groups for (chat) commands
#93 Fixed private command classes erroring
This commit is contained in:
parent
b77871bb8c
commit
a3160c4040
5 changed files with 24 additions and 118 deletions
|
@ -5,6 +5,7 @@ import buttondevteam.lib.architecture.ButtonPlugin;
|
|||
import buttondevteam.lib.chat.ChatMessage;
|
||||
import buttondevteam.lib.chat.Command2MCSender;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import buttondevteam.lib.player.ChromaGamerBase;
|
||||
import buttondevteam.lib.player.TBMCPlayer;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
import lombok.val;
|
||||
|
@ -58,7 +59,13 @@ public class PlayerListener implements Listener {
|
|||
|
||||
private void handlePreprocess(CommandSender sender, String message, Cancellable event) {
|
||||
if (event.isCancelled()) return;
|
||||
val ev = new TBMCCommandPreprocessEvent(sender, message);
|
||||
/*val cg = Optional.ofNullable(ChromaGamerBase.getFromSender(sender));
|
||||
val ch = cg.map(ChromaGamerBase::channel).map(ChannelPlayerData::get);
|
||||
val rtr = ch.map(c -> c.getRTR(sender)).orElseGet(() -> new Channel.RecipientTestResult("Failed to get user"));
|
||||
val ev = new TBMCCommandPreprocessEvent(sender, ch.orElse(Channel.GlobalChat), message, rtr.score, rtr.groupID);*/
|
||||
val cg = ChromaGamerBase.getFromSender(sender);
|
||||
if (cg == null) throw new RuntimeException("Couldn't get user from sender for " + sender.getName() + "!");
|
||||
val ev = new TBMCCommandPreprocessEvent(sender, cg.channel().get(), message, sender);
|
||||
Bukkit.getPluginManager().callEvent(ev);
|
||||
if (ev.isCancelled())
|
||||
event.setCancelled(true); //Cancel the original event
|
||||
|
@ -68,7 +75,7 @@ public class PlayerListener implements Listener {
|
|||
public void onTBMCPreprocess(TBMCCommandPreprocessEvent event) {
|
||||
if (event.isCancelled()) return;
|
||||
try {
|
||||
event.setCancelled(ButtonPlugin.getCommand2MC().handleCommand(new Command2MCSender(event.getSender()), event.getMessage()));
|
||||
event.setCancelled(ButtonPlugin.getCommand2MC().handleCommand(new Command2MCSender(event.getSender(), event.getChannel(), event.getPermCheck()), event.getMessage()));
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("Command processing failed for sender '" + event.getSender() + "' and message '" + event.getMessage() + "'", e);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package buttondevteam.lib;
|
||||
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.Cancellable;
|
||||
|
@ -14,20 +16,18 @@ import org.bukkit.event.HandlerList;
|
|||
* @author NorbiPeti
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class TBMCCommandPreprocessEvent extends Event implements Cancellable {
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
private final CommandSender sender;
|
||||
private final Channel channel;
|
||||
@Setter
|
||||
private String message;
|
||||
private final String message;
|
||||
private final CommandSender permCheck;
|
||||
@Setter
|
||||
private boolean cancelled;
|
||||
|
||||
public TBMCCommandPreprocessEvent(CommandSender sender, String message) {
|
||||
this.sender = sender;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
|
|
|
@ -254,6 +254,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
|
|||
}
|
||||
Runnable lol = () -> {
|
||||
try {
|
||||
sd.method.setAccessible(true); //It may be part of a private class
|
||||
val ret = sd.method.invoke(sd.command, params.toArray()); //I FORGOT TO TURN IT INTO AN ARRAY (for a long time)
|
||||
if (ret instanceof Boolean) {
|
||||
if (!(boolean) ret) //Show usage
|
||||
|
|
|
@ -157,111 +157,6 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
|
|||
.map(comp -> component.getClass().getSimpleName().equals(comp.getClass().getSimpleName())).orElse(false));
|
||||
}
|
||||
|
||||
/*@EventHandler
|
||||
private void handleTabComplete(TabCompleteEvent event) {
|
||||
String commandline = event.getBuffer();
|
||||
CommandSender sender = event.getSender();
|
||||
for (int i = commandline.length(); i != -1; i = commandline.lastIndexOf(' ', i - 1)) {
|
||||
String subcommand = commandline.substring(0, i).toLowerCase();
|
||||
if (subcommand.length() == 0 || subcommand.charAt(0) != '/') subcommand = '/' + subcommand; //Console
|
||||
SubcommandData<ICommand2MC> sd = subcommands.get(subcommand); //O(1)
|
||||
if (sd == null) continue;
|
||||
Arrays.stream(sd.helpText).skip(1).map(ht -> new HashMap.SimpleEntry<>(ht, subcommands.get(ht))).filter(e -> e.getValue() != null)
|
||||
.filter(kv -> kv.getKey().startsWith(commandline))
|
||||
.filter(kv -> hasPermission(sender, kv.getValue().command, kv.getValue().method))
|
||||
.forEach(kv -> event.getCompletions().add((kv.getKey()).substring(kv.getKey().lastIndexOf(' ', commandline.length()) + 1)));
|
||||
if (sd.method == null || sd.command == null)
|
||||
return;
|
||||
/*if (!hasPermission(sender, sd.command, sd.method)) { - TODO: Arguments
|
||||
sender.sendMessage("§cYou don't have permission to use this command");
|
||||
return true;
|
||||
}
|
||||
val params = new ArrayList<Object>(sd.method.getParameterCount());
|
||||
int j = subcommand.length(), pj;
|
||||
Class<?>[] parameterTypes = sd.method.getParameterTypes();
|
||||
if (parameterTypes.length == 0)
|
||||
throw new Exception("No sender parameter for method '" + sd.method + "'");
|
||||
val sendertype = parameterTypes[0];
|
||||
final ChromaGamerBase cg;
|
||||
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 (sender instanceof Command2MCSender
|
||||
&& sendertype.isAssignableFrom(((Command2MCSender) sender).getSender().getClass()))
|
||||
params.add(((Command2MCSender) sender).getSender());
|
||||
else if (ChromaGamerBase.class.isAssignableFrom(sendertype)
|
||||
&& 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 {
|
||||
sender.sendMessage("§cYou need to be a " + sendertype.getSimpleName() + " to use this command.");
|
||||
return true;
|
||||
}
|
||||
val paramArr = sd.method.getParameters();
|
||||
for (int i1 = 1; i1 < parameterTypes.length; i1++) {
|
||||
Class<?> cl = parameterTypes[i1];
|
||||
pj = j + 1; //Start index
|
||||
if (pj == commandline.length() + 1) { //No param given
|
||||
if (paramArr[i1].isAnnotationPresent(OptionalArg.class)) {
|
||||
if (cl.isPrimitive())
|
||||
params.add(Defaults.defaultValue(cl));
|
||||
else if (Number.class.isAssignableFrom(cl)
|
||||
|| Number.class.isAssignableFrom(cl))
|
||||
params.add(Defaults.defaultValue(Primitives.unwrap(cl)));
|
||||
else
|
||||
params.add(null);
|
||||
continue; //Fill the remaining params with nulls
|
||||
} else {
|
||||
sender.sendMessage(sd.helpText); //Required param missing
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (paramArr[i1].isVarArgs()) {
|
||||
par0ams.add(commandline.substring(j + 1).split(" +"));
|
||||
continue;
|
||||
}
|
||||
j = commandline.indexOf(' ', j + 1); //End index
|
||||
if (j == -1 || paramArr[i1].isAnnotationPresent(TextArg.class)) //Last parameter
|
||||
j = commandline.length();
|
||||
String param = commandline.substring(pj, j);
|
||||
if (cl == String.class) {
|
||||
params.add(param);
|
||||
continue;
|
||||
} else if (Number.class.isAssignableFrom(cl) || cl.isPrimitive()) {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
Number n = ThorpeUtils.convertNumber(NumberFormat.getInstance().parse(param), (Class<? extends Number>) cl);
|
||||
params.add(n);
|
||||
} catch (ParseException e) {
|
||||
sender.sendMessage("§c'" + param + "' is not a number.");
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
val conv = paramConverters.get(cl);
|
||||
if (conv == null)
|
||||
throw new Exception("No suitable converter found for parameter type '" + cl.getCanonicalName() + "' for command '" + sd.method.toString() + "'");
|
||||
val cparam = conv.converter.apply(param);
|
||||
if (cparam == null) {
|
||||
sender.sendMessage(conv.errormsg); //Param conversion failed - ex. plugin not found
|
||||
return true;
|
||||
}
|
||||
params.add(cparam);
|
||||
}
|
||||
try {
|
||||
val ret = sd.method.invoke(sd.command, params.toArray()); //I FORGOT TO TURN IT INTO AN ARRAY (for a long time)
|
||||
if (ret instanceof Boolean) {
|
||||
if (!(boolean) ret) //Show usage
|
||||
sender.sendMessage(sd.helpText);
|
||||
} else if (ret != null)
|
||||
throw new Exception("Wrong return type! Must return a boolean or void. Return value: " + ret);
|
||||
return true; //We found a method
|
||||
} catch (InvocationTargetException e) {
|
||||
TBMCCoreAPI.SendException("An error occurred in a command handler!", e.getCause());
|
||||
}*
|
||||
}
|
||||
}*/
|
||||
|
||||
private boolean shouldRegisterOfficially = true;
|
||||
|
||||
private void registerOfficially(ICommand2MC command, List<SubcommandData<ICommand2MC>> subcmds) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package buttondevteam.lib.chat;
|
||||
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
@ -7,6 +8,8 @@ import org.bukkit.command.CommandSender;
|
|||
@RequiredArgsConstructor
|
||||
public class Command2MCSender implements Command2Sender {
|
||||
private @Getter final CommandSender sender;
|
||||
private @Getter final Channel channel;
|
||||
private @Getter final CommandSender permCheck;
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
|
|
Loading…
Reference in a new issue