Skip to content

Commit

Permalink
finished localization!
Browse files Browse the repository at this point in the history
  • Loading branch information
Badbird5907 committed Jun 19, 2022
1 parent 3e6c20a commit 85adf6b
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
import net.octopvp.commander.bukkit.providers.CommandSenderProviders;
import net.octopvp.commander.bukkit.providers.OfflinePlayerProvider;
import net.octopvp.commander.bukkit.providers.PlayerProvider;
import net.octopvp.commander.exception.CommandParseException;
import net.octopvp.commander.lang.LocalizedCommandException;
import net.octopvp.commander.sender.CoreCommandSender;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.ResourceBundle;

public class BukkitCommander {
public static BukkitHelpService HELP_SERVICE = BukkitHelpService.INSTANCE;
public static Commander getCommander(Plugin plugin) {
return new CommanderImpl(new BukkitPlatform(plugin))
Commander impl = new CommanderImpl(new BukkitPlatform(plugin))
.init()
.registerProvider(Player.class,new PlayerProvider())
.registerProvider(OfflinePlayer.class,new OfflinePlayerProvider())
.registerProvider(Player.class, new PlayerProvider())
.registerProvider(OfflinePlayer.class, new OfflinePlayerProvider())
.registerProvider(CommandSender.class, new CommandSenderProviders.CommandSenderProvider())
.registerProvider(BukkitCommandSender.class, new CommandSenderProviders.BukkitCoreCommandSenderProvider())
.registerProvider(BukkitCommandSenderImpl.class, new CommandSenderProviders.BukkitCoreCommandSenderImplProvider())
Expand All @@ -32,20 +34,21 @@ public static Commander getCommander(Plugin plugin) {
if (ctx.getCommandInfo().isAnnotationPresent(PlayerOnly.class)) {
BukkitCommandSender sender = (BukkitCommandSender) ctx.getCommandSender();
if (!sender.isPlayer()) {
throw new CommandParseException("You must be a player to use this command.");
throw new LocalizedCommandException("must-be.player");
}
}else if (ctx.getCommandInfo().isAnnotationPresent(ConsoleOnly.class)) {
BukkitCommandSender sender = (BukkitCommandSender) ctx.getCommandSender();
if (!sender.isConsole()) {
throw new CommandParseException("You must be a console to use this command.");
throw new LocalizedCommandException("must-be.console");
}
}else if (ctx.getCommandInfo().isAnnotationPresent(OpOnly.class)) {
} else if (ctx.getCommandInfo().isAnnotationPresent(OpOnly.class)) {
BukkitCommandSender sender = (BukkitCommandSender) ctx.getCommandSender();
if (!sender.isOp()) {
throw new CommandParseException("You must be a console to use this command.");
throw new LocalizedCommandException("must-be.op");
}
}
})
;
});
impl.getResponseHandler().addBundle(ResourceBundle.getBundle("bukkit", impl.getResponseHandler().getLocale()));
return impl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.RequiredArgsConstructor;
import net.octopvp.commander.bukkit.BukkitCommandSender;
import net.octopvp.commander.exception.CommandException;
import net.octopvp.commander.lang.LocalizedCommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -35,7 +35,7 @@ public boolean isOp() {

@Override
public Player getPlayer() {
if (!(isPlayer())) throw new CommandException("You must be a player to do this!");
if (!(isPlayer())) throw new LocalizedCommandException("must-be.player");
return (Player) commandSender;
}

Expand Down
3 changes: 3 additions & 0 deletions Commander-Bukkit/src/main/resources/bukkit_en.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
must-be.player=You must be a player to use this command.
must-be.console=You must be console to use this command.
must-be.op=You must be an operator to use this command.
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,14 @@

public class CommanderImpl implements Commander {
private final CommanderPlatform platform;
private CommanderConfig config;

private final Map<Class<?>, Provider<?>> argumentProviders = new HashMap<>();

private final Map<Class<?>, Supplier<?>> dependencies = new HashMap<>();

private final Map<String, CommandInfo> commandMap = new HashMap<>();

private final List<Consumer<CommandContext>> preProcessors = new ArrayList<>();
private final List<BiConsumer<CommandContext, Object>> postProcessors = new ArrayList<>();

private final Map<Class<?>, Validator<Object>> validators = new HashMap<>();

private final ResponseHandler responseHandler;
private CommanderConfig config;

public CommanderImpl(CommanderPlatform platform) {
this(platform, new CommanderConfig());
Expand Down Expand Up @@ -112,7 +106,8 @@ public Commander init() {
minMax.append("max: ").append(max);
}
minMax.append(")");
throw new ValidateException("Value " + value.doubleValue() + " is not in valid range! " + minMax);
//throw new ValidateException("Value " + value.doubleValue() + " is not in valid range! " + minMax);
throw new ValidateException("validate.exception", value.doubleValue(), minMax.toString());
}
});
return this;
Expand Down Expand Up @@ -331,7 +326,7 @@ public void executeCommand(CoreCommandSender sender, String label, String[] args
commandInfo = parent.getSubCommand("");
isRootLevel = commandInfo != null;
if (!isRootLevel) {
throw new CommandNotFoundException("Could not find subcommand \"" + sub.toLowerCase() + "\" for command \"" + parent.getName() + "\"");
throw new CommandNotFoundException("subcommand.not-found", sub.toLowerCase(), parent.getName());
}
}
if (!isRootLevel) {
Expand All @@ -340,7 +335,7 @@ public void executeCommand(CoreCommandSender sender, String label, String[] args
args = newArgs;
}
} else if (commandInfo == null) {
throw new CommandNotFoundException("Could not find command handler for \"" + label.toLowerCase() + "\"");
throw new CommandNotFoundException("handler.not-found", label.toLowerCase());
}

String[] argsCopy = new String[args.length];
Expand Down Expand Up @@ -410,7 +405,7 @@ private Map<String, String> extractFlags(final List<String> args, final Paramete
if (arg.startsWith(config.getFlagPrefix())) {
String flag = arg.substring(config.getFlagPrefix().length());
if (flags.containsKey(flag)) {
throw new CommandParseException("Flag " + flag + " is defined multiple times.");
throw new CommandParseException("flags.multiple", flag);
}
if (paramsList.stream().noneMatch(p -> p.isFlag() && p.getFlags().contains(flag)))
continue;
Expand All @@ -420,7 +415,7 @@ private Map<String, String> extractFlags(final List<String> args, final Paramete
iterator.remove();
flags.put(flag, value);
} else {
throw new CommandParseException("Flag " + flag + " requires a value.");
throw new CommandParseException("flags.requires-value", flag);
}
}
}
Expand All @@ -434,14 +429,14 @@ private Map<String, Boolean> extractSwitches(final List<String> args, final Para
while (iterator.hasNext()) {
String arg = iterator.next();
if (arg.startsWith(config.getSwitchPrefix())) {
String flag = arg.substring(config.getSwitchPrefix().length());
if (switches.containsKey(flag)) {
throw new CommandParseException("Switch " + flag + " is defined multiple times.");
String commandSwitch = arg.substring(config.getSwitchPrefix().length());
if (switches.containsKey(commandSwitch)) {
throw new CommandParseException("switches.multiple", commandSwitch);
}
if (paramsList.stream().noneMatch(p -> p.isSwitch() && p.getSwitches().contains(flag)))
if (paramsList.stream().noneMatch(p -> p.isSwitch() && p.getSwitches().contains(commandSwitch)))
continue;
iterator.remove();
switches.put(flag, true);
switches.put(commandSwitch, true);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
ParameterInfo parameter = ctx.getCommandInfo().getParameters()[i];
if (parameter.isFlag()) {
if (cArgs.getFlags() == null) {
throw new CommandParseException("Flags are null!");
throw new CommandParseException("flags.null");
}
List<String> paramFlags = parameter.getFlags();
for (String paramFlag : paramFlags) {
Expand All @@ -47,7 +47,7 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
}
if (parameter.isSwitch()) {
if (cArgs.getSwitches() == null) {
throw new CommandParseException("Switches are null!");
throw new CommandParseException("switches.null");
}
List<String> switches = parameter.getSwitches();
Boolean b = cArgs.getSwitches().entrySet().stream().filter(e -> switches.contains(e.getKey())).map(Map.Entry::getValue).findFirst().orElse(null);
Expand All @@ -60,15 +60,15 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
Class<?> classType = parameter.getParameter().getType();
Supplier<?> supplier = cArgs.getCommander().getDependencies().get(classType);
if (supplier == null) {
throw new CommandParseException("Dependency not found for " + classType.getName()); //maybe let the user control this?
throw new CommandParseException("dependency.not-found", classType.getName()); //maybe let the user control this?
}
arguments[i] = supplier.get();
continue;
}
Provider<?> provider = parameter.getProvider();

if (provider == null) {
throw new CommandParseException("No provider found for " + parameter.getParameter().getType().getName());
throw new CommandParseException("provider.not-found", parameter.getParameter().getType().getName());
}
Object obj;
try {
Expand All @@ -82,12 +82,12 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
throw new InvalidArgsException(ctx.getCommandInfo());
}
if (provider.failOnExceptionIgnoreOptional()) {
throw new CommandParseException("Failed to parse argument " + parameter.getName(), e);
throw new CommandParseException(parameter.getName(), e);
}
if (parameter.isOptional()) {
obj = null;
} else if (provider.failOnException())
throw new CommandParseException("Failed to parse argument " + parameter.getName(), e);
throw new CommandParseException(parameter.getName(), e);
else {
obj = null;
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public static List<String> combineArgs(List<String> in) {
}
}
if (currentArgIsQuoted) {
throw new CommandParseException("Unclosed quote!");
throw new CommandParseException("quote.unclosed");
}
if (sb.length() > 0) {
out.add(sb.toString().trim());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package net.octopvp.commander.exception;

public class CommandException extends RuntimeException{
public CommandException(String message){
public class CommandException extends RuntimeException {
public CommandException(String message) {
super(message);
}
public CommandException(String message, Throwable cause){

public CommandException(String message, Throwable cause) {
super(message, cause);
}
public CommandException(Throwable cause){

public CommandException(Throwable cause) {
super(cause);
}
public CommandException() {}

public CommandException() {
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.octopvp.commander.exception;

public class CommandNotFoundException extends CommandException {
public CommandNotFoundException(String message) {
super(message);
import net.octopvp.commander.lang.LocalizedCommandException;

public class CommandNotFoundException extends LocalizedCommandException {
public CommandNotFoundException(String key, Object... placeholders) {
super(key, placeholders);
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package net.octopvp.commander.exception;

public class CommandParseException extends CommandException{
public CommandParseException(String message) {
super(message);
}
import net.octopvp.commander.lang.LocalizedCommandException;

public class CommandParseException extends LocalizedCommandException {
public CommandParseException() {
super("parse.fail");
}

public CommandParseException(Exception cause) {
super("parse.fail", cause);
}
public CommandParseException(String message, Throwable cause) {
super(message, cause);

public CommandParseException(Object... placeholders) {
super("parse.fail", placeholders);
}
public CommandParseException(Throwable cause) {
super(cause.getMessage());

public CommandParseException(String key) {
super(key);
}

public CommandParseException(String key, Object... placeholders) {
super(key, placeholders);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package net.octopvp.commander.exception;

import net.octopvp.commander.lang.LocalizedCommandException;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class CooldownException extends CommandException {
public class CooldownException extends LocalizedCommandException {
private static final DecimalFormat TWO_DECIMAL_FORMAT = new DecimalFormat("0.00");

public CooldownException(double timeLeft) {
super("You must wait " + TWO_DECIMAL_FORMAT.format(timeLeft) + " seconds before using this command again.");
super("cooldown", TWO_DECIMAL_FORMAT.format(timeLeft));
//super("You must wait " + TWO_DECIMAL_FORMAT.format(timeLeft) + " seconds before using this command again.");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package net.octopvp.commander.exception;

import net.octopvp.commander.command.CommandInfo;
import net.octopvp.commander.lang.LocalizedCommandException;

public class InvalidArgsException extends CommandException {
public class InvalidArgsException extends LocalizedCommandException {
public InvalidArgsException(CommandInfo info) {
super("Usage: " + info.getFullUsage());
super("args.invalid", info.getFullUsage());
}

public InvalidArgsException(String usage) {
super("Usage: " + usage);
super("args.invalid", usage);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.octopvp.commander.exception;

public class NoPermissionException extends CommandException {
public NoPermissionException() {
super("You do not have permission to perform this command.");
}
import net.octopvp.commander.lang.LocalizedCommandException;

public NoPermissionException(String message) {
super(message);
public class NoPermissionException extends LocalizedCommandException {
public NoPermissionException() {
super("no-permission");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.octopvp.commander.exception;

public class ValidateException extends CommandException {
public ValidateException(String message) {
super(message);
import net.octopvp.commander.lang.LocalizedCommandException;

public class ValidateException extends LocalizedCommandException {
public ValidateException(String key, Object... placeholders) {
super(key, placeholders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public DefaultResponseHandler(Locale locale) {
}

@Override
public String getMessage(Exception e) {
public String getMessage(Exception e, Object... placeholders) {
if (e instanceof LocalizedCommandException) {
LocalizedCommandException le = (LocalizedCommandException) e;
return getMessage(le.getKey());
return getMessage(le.getKey(), placeholders);
}
return e.getMessage();
}
Expand Down
Loading

0 comments on commit 85adf6b

Please sign in to comment.