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

View File

@ -96,7 +96,7 @@ public class PlanBungee extends BungeePlugin implements PlanPlugin {
return;
}
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;
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.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.RunnableFactory;
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.TabExecutor;
import java.util.Arrays;
import java.util.Collections;
public class BungeeCommand extends Command implements TabExecutor {
private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
private final Subcommand command;
public BungeeCommand(
RunnableFactory runnableFactory,
Subcommand command,
ErrorLogger errorLogger, Subcommand command,
String name
) {
super(name);
this.runnableFactory = runnableFactory;
this.errorLogger = errorLogger;
this.command = command;
}
private CMDSender getSender(CommandSender sender) {
if (sender instanceof ProxiedPlayer) {
return new BungeePlayerCMDSender((ProxiedPlayer) sender);
} else {
return new BungeeCMDSender(sender);
}
}
@Override
public void execute(CommandSender sender, String[] args) {
runnableFactory.create("", new AbsRunnable() {
@Override
public void run() {
if (sender instanceof ProxiedPlayer) {
command.getExecutor().accept(new BungeePlayerCMDSender((ProxiedPlayer) sender), new Arguments(args));
} else {
command.getExecutor().accept(new BungeeCMDSender(sender), new Arguments(args));
try {
command.getExecutor().accept(getSender(sender), new Arguments(args));
} catch (Exception e) {
errorLogger.log(L.ERROR, e, ErrorContext.builder()
.related(sender.getClass())
.related(Arrays.toString(args))
.build());
}
}
}).runTaskAsynchronously();
@ -57,10 +77,15 @@ public class BungeeCommand extends Command implements TabExecutor {
@Override
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
if (sender instanceof ProxiedPlayer) {
return command.getArgumentResolver().apply(new BungeePlayerCMDSender((ProxiedPlayer) sender), new Arguments(args));
} else {
return command.getArgumentResolver().apply(new BungeeCMDSender(sender), new Arguments(args));
try {
return command.getArgumentResolver().apply(getSender(sender), new Arguments(args));
} catch (Exception e) {
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.lang.PluginLang;
import com.djrapitops.plan.settings.theme.PlanColorScheme;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plugin.NukkitPlugin;
import com.djrapitops.plugin.benchmarking.Benchmark;
import com.djrapitops.plugin.command.ColorScheme;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.task.AbsRunnable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
@ -121,7 +124,14 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin {
runnableFactory.create("", new AbsRunnable() {
@Override
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();
return true;

View File

@ -167,7 +167,7 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin {
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));
}
}

View File

@ -16,10 +16,12 @@
*/
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.RunnableFactory;
import org.spongepowered.api.command.CommandCallable;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.source.RconSource;
@ -29,28 +31,38 @@ import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class SpongeCommand implements CommandCallable {
private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
private final Subcommand command;
public SpongeCommand(
RunnableFactory runnableFactory,
Subcommand command
ErrorLogger errorLogger, Subcommand command
) {
this.runnableFactory = runnableFactory;
this.errorLogger = errorLogger;
this.command = command;
}
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
public CommandResult process(CommandSource source, String arguments) {
runnableFactory.create("", new AbsRunnable() {
@Override
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();
return CommandResult.success();
@ -65,9 +77,18 @@ public class SpongeCommand implements CommandCallable {
}
@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
return command.getArgumentResolver()
.apply(getSender(source), new Arguments(arguments));
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) {
try {
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

View File

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

View File

@ -16,21 +16,28 @@
*/
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.RunnableFactory;
import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class VelocityCommand implements Command {
private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
private final Subcommand command;
public VelocityCommand(RunnableFactory runnableFactory, Subcommand command) {
public VelocityCommand(RunnableFactory runnableFactory, ErrorLogger errorLogger, Subcommand command) {
this.runnableFactory = runnableFactory;
this.errorLogger = errorLogger;
this.command = command;
}
@ -39,7 +46,14 @@ public class VelocityCommand implements Command {
runnableFactory.create("", new AbsRunnable() {
@Override
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();
}
@ -54,6 +68,15 @@ public class VelocityCommand implements Command {
@Override
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();
}
}
}