Only run cmds sync if they were sync

Needed for DC command handler
This commit is contained in:
Norbi Peti 2019-08-26 13:13:42 +02:00
parent 0967095f59
commit bb10f9fb0f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56

View file

@ -131,9 +131,10 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
String subcommand = commandline.substring(0, i).toLowerCase(); String subcommand = commandline.substring(0, i).toLowerCase();
SubcommandData<TC> sd = subcommands.get(subcommand); //O(1) SubcommandData<TC> sd = subcommands.get(subcommand); //O(1)
if (sd == null) continue; if (sd == null) continue;
boolean sync = Bukkit.isPrimaryThread();
Bukkit.getScheduler().runTaskAsynchronously(MainPlugin.Instance, () -> { Bukkit.getScheduler().runTaskAsynchronously(MainPlugin.Instance, () -> {
try { try {
handleCommandAsync(sender, commandline, sd, subcommand); handleCommandAsync(sender, commandline, sd, subcommand, sync);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Command execution failed for sender " + sender + " and message " + commandline, e); TBMCCoreAPI.SendException("Command execution failed for sender " + sender + " and message " + commandline, e);
} }
@ -144,7 +145,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
} }
//Needed because permission checking may load the (perhaps offline) sender's file which is disallowed on the main thread //Needed because permission checking may load the (perhaps offline) sender's file which is disallowed on the main thread
public void handleCommandAsync(TP sender, String commandline, SubcommandData<TC> sd, String subcommand) throws Exception { public void handleCommandAsync(TP sender, String commandline, SubcommandData<TC> sd, String subcommand, boolean sync) throws Exception {
if (sd.method == null || sd.command == null) { //Main command not registered, but we have subcommands if (sd.method == null || sd.command == null) { //Main command not registered, but we have subcommands
sender.sendMessage(sd.helpText); sender.sendMessage(sd.helpText);
return; return;
@ -225,7 +226,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
} }
params.add(cparam); params.add(cparam);
} }
Bukkit.getScheduler().runTask(MainPlugin.Instance, () -> { Runnable lol = () -> {
try { try {
val ret = sd.method.invoke(sd.command, params.toArray()); //I FORGOT TO TURN IT INTO AN ARRAY (for a long time) 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 (ret instanceof Boolean) {
@ -238,7 +239,11 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Command handling failed for sender " + sender + " and subcommand " + subcommand, e); TBMCCoreAPI.SendException("Command handling failed for sender " + sender + " and subcommand " + subcommand, e);
} }
}); };
if (sync)
Bukkit.getScheduler().runTask(MainPlugin.Instance, lol);
else
lol.run();
} //TODO: Add to the help } //TODO: Add to the help
public abstract void registerCommand(TC command); public abstract void registerCommand(TC command);