Improved code a bit for errors

This commit is contained in:
Risto Lahtela 2020-09-21 15:02:19 +03:00
parent 8c7f4f44af
commit eff5faba44
8 changed files with 112 additions and 33 deletions

View File

@ -43,17 +43,21 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
this.command = command; this.command = command;
} }
private CMDSender getSender(CommandSender sender) {
if (sender instanceof Player) {
return new BukkitPlayerCMDSender((Player) sender);
} else {
return new BukkitCMDSender(sender);
}
}
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
runnableFactory.create("", new AbsRunnable() { runnableFactory.create("", new AbsRunnable() {
@Override @Override
public void run() { public void run() {
try { try {
if (sender instanceof Player) { command.getExecutor().accept(getSender(sender), new Arguments(args));
command.getExecutor().accept(new BukkitPlayerCMDSender((Player) sender), new Arguments(args));
} else {
command.getExecutor().accept(new BukkitCMDSender(sender), new Arguments(args));
}
} catch (Exception e) { } catch (Exception e) {
errorLogger.log(L.ERROR, e, ErrorContext.builder() errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(sender.getClass()) .related(sender.getClass())
@ -68,11 +72,7 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
try { try {
if (sender instanceof Player) { return command.getArgumentResolver().apply(getSender(sender), new Arguments(args));
return command.getArgumentResolver().apply(new BukkitPlayerCMDSender((Player) sender), new Arguments(args));
} else {
return command.getArgumentResolver().apply(new BukkitCMDSender(sender), new Arguments(args));
}
} catch (Exception e) { } catch (Exception e) {
errorLogger.log(L.ERROR, e, ErrorContext.builder() errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(sender.getClass()) .related(sender.getClass())

View File

@ -96,7 +96,7 @@ public class PlanBungee extends BungeePlugin implements PlanPlugin {
return; return;
} }
for (String name : command.getAliases()) { for (String name : command.getAliases()) {
getProxy().getPluginManager().registerCommand(this, new BungeeCommand(runnableFactory, command, name)); getProxy().getPluginManager().registerCommand(this, new BungeeCommand(runnableFactory, system.getErrorLogger(), command, name));
} }
} }

View File

@ -17,7 +17,11 @@
package com.djrapitops.plan.command.use; package com.djrapitops.plan.command.use;
import com.djrapitops.plan.commands.use.Arguments; import com.djrapitops.plan.commands.use.Arguments;
import com.djrapitops.plan.commands.use.CMDSender;
import com.djrapitops.plan.commands.use.Subcommand; import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory; import com.djrapitops.plugin.task.RunnableFactory;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
@ -25,31 +29,47 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor; import net.md_5.bungee.api.plugin.TabExecutor;
import java.util.Arrays;
import java.util.Collections;
public class BungeeCommand extends Command implements TabExecutor { public class BungeeCommand extends Command implements TabExecutor {
private final RunnableFactory runnableFactory; private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
private final Subcommand command; private final Subcommand command;
public BungeeCommand( public BungeeCommand(
RunnableFactory runnableFactory, RunnableFactory runnableFactory,
Subcommand command, ErrorLogger errorLogger, Subcommand command,
String name String name
) { ) {
super(name); super(name);
this.runnableFactory = runnableFactory; this.runnableFactory = runnableFactory;
this.errorLogger = errorLogger;
this.command = command; this.command = command;
} }
private CMDSender getSender(CommandSender sender) {
if (sender instanceof ProxiedPlayer) {
return new BungeePlayerCMDSender((ProxiedPlayer) sender);
} else {
return new BungeeCMDSender(sender);
}
}
@Override @Override
public void execute(CommandSender sender, String[] args) { public void execute(CommandSender sender, String[] args) {
runnableFactory.create("", new AbsRunnable() { runnableFactory.create("", new AbsRunnable() {
@Override @Override
public void run() { public void run() {
if (sender instanceof ProxiedPlayer) { try {
command.getExecutor().accept(new BungeePlayerCMDSender((ProxiedPlayer) sender), new Arguments(args)); command.getExecutor().accept(getSender(sender), new Arguments(args));
} else { } catch (Exception e) {
command.getExecutor().accept(new BungeeCMDSender(sender), new Arguments(args)); errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(sender.getClass())
.related(Arrays.toString(args))
.build());
} }
} }
}).runTaskAsynchronously(); }).runTaskAsynchronously();
@ -57,10 +77,15 @@ public class BungeeCommand extends Command implements TabExecutor {
@Override @Override
public Iterable<String> onTabComplete(CommandSender sender, String[] args) { public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
if (sender instanceof ProxiedPlayer) { try {
return command.getArgumentResolver().apply(new BungeePlayerCMDSender((ProxiedPlayer) sender), new Arguments(args)); return command.getArgumentResolver().apply(getSender(sender), new Arguments(args));
} else { } catch (Exception e) {
return command.getArgumentResolver().apply(new BungeeCMDSender(sender), new Arguments(args)); errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(sender.getClass())
.related("tab completion")
.related(Arrays.toString(args))
.build());
return Collections.emptyList();
} }
} }
} }

View File

@ -26,11 +26,14 @@ import com.djrapitops.plan.gathering.ServerShutdownSave;
import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.PluginLang; import com.djrapitops.plan.settings.locale.lang.PluginLang;
import com.djrapitops.plan.settings.theme.PlanColorScheme; import com.djrapitops.plan.settings.theme.PlanColorScheme;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plugin.NukkitPlugin; import com.djrapitops.plugin.NukkitPlugin;
import com.djrapitops.plugin.benchmarking.Benchmark; import com.djrapitops.plugin.benchmarking.Benchmark;
import com.djrapitops.plugin.command.ColorScheme; import com.djrapitops.plugin.command.ColorScheme;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.AbsRunnable;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -121,7 +124,14 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin {
runnableFactory.create("", new AbsRunnable() { runnableFactory.create("", new AbsRunnable() {
@Override @Override
public void run() { public void run() {
command.getExecutor().accept(sender, new Arguments(args)); try {
command.getExecutor().accept(sender, new Arguments(args));
} catch (Exception e) {
system.getErrorLogger().log(L.ERROR, e, ErrorContext.builder()
.related(sender.getClass())
.related(label + " " + Arrays.toString(args))
.build());
}
} }
}).runTaskAsynchronously(); }).runTaskAsynchronously();
return true; return true;

View File

@ -167,7 +167,7 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
commandManager.removeMapping(registered); commandManager.removeMapping(registered);
} }
Optional<CommandMapping> register = commandManager.register(this, new SpongeCommand(runnableFactory, command), name); Optional<CommandMapping> register = commandManager.register(this, new SpongeCommand(runnableFactory, system.getErrorLogger(), command), name);
register.ifPresent(commandMapping -> commands.put(name, commandMapping)); register.ifPresent(commandMapping -> commands.put(name, commandMapping));
} }
} }

View File

@ -16,10 +16,12 @@
*/ */
package com.djrapitops.plan.commands.use; package com.djrapitops.plan.commands.use;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory; import com.djrapitops.plugin.task.RunnableFactory;
import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult; import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.source.RconSource; import org.spongepowered.api.command.source.RconSource;
@ -29,28 +31,38 @@ import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World; import org.spongepowered.api.world.World;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
public class SpongeCommand implements CommandCallable { public class SpongeCommand implements CommandCallable {
private final RunnableFactory runnableFactory; private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
private final Subcommand command; private final Subcommand command;
public SpongeCommand( public SpongeCommand(
RunnableFactory runnableFactory, RunnableFactory runnableFactory,
Subcommand command ErrorLogger errorLogger, Subcommand command
) { ) {
this.runnableFactory = runnableFactory; this.runnableFactory = runnableFactory;
this.errorLogger = errorLogger;
this.command = command; this.command = command;
} }
@Override @Override
public CommandResult process(CommandSource source, String arguments) throws CommandException { public CommandResult process(CommandSource source, String arguments) {
runnableFactory.create("", new AbsRunnable() { runnableFactory.create("", new AbsRunnable() {
@Override @Override
public void run() { public void run() {
command.getExecutor().accept(getSender(source), new Arguments(arguments)); try {
command.getExecutor().accept(getSender(source), new Arguments(arguments));
} catch (Exception e) {
errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(source.getClass())
.related(arguments)
.build());
}
} }
}).runTaskAsynchronously(); }).runTaskAsynchronously();
return CommandResult.success(); return CommandResult.success();
@ -65,9 +77,18 @@ public class SpongeCommand implements CommandCallable {
} }
@Override @Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException { public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) {
return command.getArgumentResolver() try {
.apply(getSender(source), new Arguments(arguments)); return command.getArgumentResolver()
.apply(getSender(source), new Arguments(arguments));
} catch (Exception e) {
errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(source.getClass())
.related("tab completion")
.related(arguments)
.build());
return Collections.emptyList();
}
} }
@Override @Override

View File

@ -119,7 +119,7 @@ public class PlanVelocity extends VelocityPlugin implements PlanPlugin {
return; return;
} }
getProxy().getCommandManager().register( getProxy().getCommandManager().register(
new VelocityCommand(runnableFactory, command), new VelocityCommand(runnableFactory, system.getErrorLogger(), command),
command.getAliases().toArray(new String[0]) command.getAliases().toArray(new String[0])
); );
} }

View File

@ -16,21 +16,28 @@
*/ */
package com.djrapitops.plan.commands.use; package com.djrapitops.plan.commands.use;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory; import com.djrapitops.plugin.task.RunnableFactory;
import com.velocitypowered.api.command.Command; import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class VelocityCommand implements Command { public class VelocityCommand implements Command {
private final RunnableFactory runnableFactory; private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
private final Subcommand command; private final Subcommand command;
public VelocityCommand(RunnableFactory runnableFactory, Subcommand command) { public VelocityCommand(RunnableFactory runnableFactory, ErrorLogger errorLogger, Subcommand command) {
this.runnableFactory = runnableFactory; this.runnableFactory = runnableFactory;
this.errorLogger = errorLogger;
this.command = command; this.command = command;
} }
@ -39,7 +46,14 @@ public class VelocityCommand implements Command {
runnableFactory.create("", new AbsRunnable() { runnableFactory.create("", new AbsRunnable() {
@Override @Override
public void run() { public void run() {
command.getExecutor().accept(getSender(source), new Arguments(args)); try {
command.getExecutor().accept(getSender(source), new Arguments(args));
} catch (Exception e) {
errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(source.getClass())
.related(Arrays.toString(args))
.build());
}
} }
}).runTaskAsynchronously(); }).runTaskAsynchronously();
} }
@ -54,6 +68,15 @@ public class VelocityCommand implements Command {
@Override @Override
public List<String> suggest(CommandSource source, String[] currentArgs) { public List<String> suggest(CommandSource source, String[] currentArgs) {
return command.getArgumentResolver().apply(getSender(source), new Arguments(currentArgs)); try {
return command.getArgumentResolver().apply(getSender(source), new Arguments(currentArgs));
} catch (Exception e) {
errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(source.getClass())
.related("tab completion")
.related(Arrays.toString(currentArgs))
.build());
return Collections.emptyList();
}
} }
} }