Skip to content

Commit

Permalink
Localized messages work!
Browse files Browse the repository at this point in the history
  • Loading branch information
Badbird5907 committed Jun 16, 2022
1 parent 547b7cf commit 3e6c20a
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import net.octopvp.commander.annotation.Sender;
import net.octopvp.commander.bukkit.impl.BukkitCommandSenderImpl;
import net.octopvp.commander.bukkit.impl.BukkitCommandWrapper;
import net.octopvp.commander.bukkit.impl.BukkitHelpService;
import net.octopvp.commander.command.CommandContext;
import net.octopvp.commander.command.CommandInfo;
import net.octopvp.commander.command.ParameterInfo;
import net.octopvp.commander.exception.CommandException;
import net.octopvp.commander.help.HelpService;
import net.octopvp.commander.lang.LocalizedCommandException;
import net.octopvp.commander.platform.CommanderPlatform;
import net.octopvp.commander.sender.CoreCommandSender;
import org.bukkit.ChatColor;
Expand All @@ -26,7 +26,6 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class BukkitPlatform implements CommanderPlatform {
private final Plugin plugin;
Expand Down Expand Up @@ -65,6 +64,9 @@ public void handleCommandException(CommandContext ctx, CommandException e) {

@Override
public void handleCommandException(CommandInfo info, CoreCommandSender sender, CommandException e) {
if (e instanceof LocalizedCommandException) {
sender.sendMessage(ChatColor.RED + info.getCommander().getResponseHandler().getMessage(e));
}
sender.sendMessage(ChatColor.RED + e.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.octopvp.commander.config.CommanderConfig;
import net.octopvp.commander.exception.*;
import net.octopvp.commander.lang.DefaultResponseHandler;
import net.octopvp.commander.lang.LocalizedCommandException;
import net.octopvp.commander.lang.ResponseHandler;
import net.octopvp.commander.platform.CommanderPlatform;
import net.octopvp.commander.provider.Provider;
Expand Down Expand Up @@ -388,12 +389,14 @@ public void executeCommand(CoreCommandSender sender, String label, String[] args
postProcessor.accept(context, result);
}
} catch (CommandException e) {
LocalizedCommandException.checkResponseHandlerNull(e, getResponseHandler());
platform.handleCommandException(context, e);
} catch (Exception e) {
System.err.println("An error occurred while executing command \"" + label + "\"");
e.printStackTrace();
}
} catch (CommandException e) {
LocalizedCommandException.checkResponseHandlerNull(e, getResponseHandler());
platform.handleCommandException(commandInfo, sender, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.octopvp.commander.exception.CommandException;
import net.octopvp.commander.exception.CommandParseException;
import net.octopvp.commander.exception.InvalidArgsException;
import net.octopvp.commander.lang.LocalizedCommandException;
import net.octopvp.commander.provider.Provider;
import net.octopvp.commander.util.Primitives;
import net.octopvp.commander.validator.Validator;
Expand Down Expand Up @@ -104,6 +105,7 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
}
return arguments;
} catch (CommandException e) {
LocalizedCommandException.checkResponseHandlerNull(e, ctx.getCommandInfo().getCommander().getResponseHandler());
ctx.getCommandInfo().getCommander().getPlatform().handleCommandException(ctx, e);
}
return null;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import lombok.Getter;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.*;

@Getter
public class DefaultResponseHandler implements ResponseHandler {
private final Locale locale;

private final ResourceBundle bundle;
private final List<ResourceBundle> bundles = new ArrayList<>();

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

public DefaultResponseHandler(Locale locale) {
this.locale = locale;
this.bundle = ResourceBundle.getBundle("commander", locale);
bundles.add(ResourceBundle.getBundle("commander", locale));
}

@Override
Expand All @@ -27,11 +28,37 @@ public String getMessage(Exception e) {

@Override
public String getMessage(String key, Object... placeholders) {
return String.format(bundle.getString(key), placeholders);
String msg = null;
if (overrides.containsKey(key)) {
msg = overrides.get(key);
} else {
//make sure that the bundles added last are checked first, so that the overrides take precedence
for (int i = bundles.size() - 1; i >= 0; i--) {
ResourceBundle bundle = bundles.get(i);
if (bundle.containsKey(key)) {
msg = bundle.getString(key);
break;
}
}
}
if (msg == null) {
msg = key;
}
return String.format(msg, placeholders);
}

@Override
public Locale getLocale() {
return locale;
}

@Override
public void addBundle(ResourceBundle bundle) {
bundles.add(bundle);
}

@Override
public void overrideKey(String key, String value) {
overrides.put(key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@
@Setter
public class LocalizedCommandException extends CommandException {
private final String key;
private final ResponseHandler responseHandler;
private ResponseHandler responseHandler;

public LocalizedCommandException(String key, ResponseHandler responseHandler) {
this.key = key;
this.responseHandler = responseHandler;
}

public LocalizedCommandException(String key) {
this(key, null);
}

public static void checkResponseHandlerNull(Exception e, ResponseHandler responseHandler) {
if (e instanceof LocalizedCommandException) {
LocalizedCommandException le = (LocalizedCommandException) e;
if (le.getResponseHandler() == null) {
le.setResponseHandler(responseHandler);
}
}
}

@Override
public String getLocalizedMessage() {
return responseHandler.getMessage(this);
if (responseHandler != null)
return responseHandler.getMessage(this);
return super.getLocalizedMessage();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package net.octopvp.commander.lang;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public interface ResponseHandler {
String getMessage(Exception e);

String getMessage(String key, Object... placeholders);

Locale getLocale();

void addBundle(ResourceBundle bundle);

List<ResourceBundle> getBundles();

void overrideKey(String key, String value);

Map<String, String> getOverrides();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,46 @@
import net.octopvp.commander.Commander;
import net.octopvp.commander.CommanderImpl;
import net.octopvp.commander.annotation.Command;
import net.octopvp.commander.annotation.Cooldown;
import net.octopvp.commander.exception.TestException;
import net.octopvp.commander.lang.LocalizedCommandException;
import org.junit.jupiter.api.Test;

import java.util.Locale;
import java.util.ResourceBundle;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class LocaleTest {
private Commander commander;

private boolean passed = false;

@Test
public void test() {
commander = new CommanderImpl(new TestPlatform())
commander = new CommanderImpl(new TestPlatform() {
@Override
public void handleLocale(Exception e, Commander commander) {
String s = commander.getResponseHandler().getMessage(e);
System.out.println("Localized: " + s);
if (s.equals("Hello World!")) {
passed = true;
}
}
})
.init()
.register(this);
commander.getResponseHandler().addBundle(ResourceBundle.getBundle("test", Locale.ENGLISH));
commander.executeCommand(new CommandSender(), "test", null);
assertTrue(passed);
}

@Command(name = "test")
@Cooldown(1)
public void testCommand() {
throw new TestException(commander.getResponseHandler());
throw new TestException("message.test");
}

public class TestException extends LocalizedCommandException {
public TestException(String key) {
super(key);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.octopvp.commander.test;

import net.octopvp.commander.Commander;
import net.octopvp.commander.command.CommandContext;
import net.octopvp.commander.command.CommandInfo;
import net.octopvp.commander.exception.CommandException;
import net.octopvp.commander.help.HelpService;
import net.octopvp.commander.lang.LocalizedCommandException;
import net.octopvp.commander.platform.CommanderPlatform;
import net.octopvp.commander.sender.CoreCommandSender;

Expand All @@ -21,12 +23,18 @@ public void handleError(String error, CoreCommandSender sender) {

@Override
public void handleCommandException(CommandContext ctx, CommandException e) {
e.printStackTrace();
if (e instanceof LocalizedCommandException) handleLocale(e, ctx.getCommandInfo().getCommander());
else e.printStackTrace();
}

@Override
public void handleCommandException(CommandInfo info, CoreCommandSender sender, CommandException e) {
e.printStackTrace();
if (e instanceof LocalizedCommandException) handleLocale(e, info.getCommander());
else e.printStackTrace();
}

public void handleLocale(Exception e, Commander commander) {
System.err.println(commander.getResponseHandler().getMessage(e));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions Commander-Core/src/test/resources/test_en.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
message.test=Hello World!

0 comments on commit 3e6c20a

Please sign in to comment.