Fix command casting issue

This commit is contained in:
sandtechnology committed 2021-06-16 00:54:25 +08:00
1 parent ea7bcaedb2
commit 787224b8e7
2 files changed
+12 -11

No files matched your search

@@ -29,6 +29,7 @@ import org.jetbrains.annotations.Nullable;
import org.maxgamer.quickshop.util.MsgUtil;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.function.Function;
@@ -68,12 +69,17 @@ public class CommandContainer {
return executorType;
}
public <T extends CommandSender> T cast(Object type) {
return (T) type;
}
public void bakeExecutorType() {
for (Method declaredMethod : getExecutor().getClass().getDeclaredMethods()) {
if (!"onCommand".equals(declaredMethod.getName()) && !"onTabComplete".equals(declaredMethod.getName())) {
continue;
}
if (declaredMethod.getParameterCount() != 3) {
if (declaredMethod.getParameterCount() != 3 || declaredMethod.getModifiers() != Modifier.PUBLIC) {
continue;
}
executorType = declaredMethod.getParameterTypes()[0];
@@ -408,11 +408,11 @@ public class CommandManager implements TabCompleter, CommandExecutor {
}
Util.debugLog("Execute container: " + container.getPrefix() + " - " + cmdArg[0]);
((CommandHandler<CommandSender>) container.getExecutor()).onCommand(sender, commandLabel, passThroughArgs);
container.getExecutor().onCommand(container.cast(sender), commandLabel, passThroughArgs);
return true;
}
Util.debugLog("All checks failed, print helps");
((CommandHandler<CommandSender>) rootContainer.getExecutor()).onCommand(sender, commandLabel, passThroughArgs);
rootContainer.getExecutor().onCommand(rootContainer.cast(sender), commandLabel, passThroughArgs);
}
return true;
}
@@ -462,13 +462,8 @@ public class CommandManager implements TabCompleter, CommandExecutor {
private boolean isAdapt(CommandContainer container, CommandSender sender) {
try {
sender.getClass().cast(container.getExecutorType());
//container.getExecutorType().cast(sender);
return true;
} catch (ClassCastException exception) {
return false;
}
return container.getExecutorType().isInstance(sender);
}
@Override
@@ -507,7 +502,7 @@ public class CommandManager implements TabCompleter, CommandExecutor {
return Collections.emptyList();
}
Util.debugLog("Tab-complete container: " + container.getPrefix());
return ((CommandHandler<CommandSender>) container.getExecutor()).onTabComplete(sender, commandLabel, passThroughArgs);
return container.getExecutor().onTabComplete(container.cast(sender), commandLabel, passThroughArgs);
}
return Collections.emptyList();