Skip to content

Commit

Permalink
i18n: Commands E-O (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean authored and mastercoms committed Jan 7, 2019
1 parent 57c7b28 commit c993dd1
Show file tree
Hide file tree
Showing 25 changed files with 305 additions and 221 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -287,7 +287,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
<configuration>
<forceCreation>true</forceCreation>
<archive>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/glowstone/command/CommandTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NonNls;

public class CommandTarget {

Expand All @@ -27,6 +28,7 @@ public class CommandTarget {
*/
@Getter
private final SelectorType selector;
@NonNls
private final HashMap<String, SelectorValue> arguments;

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/glowstone/command/CommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
Expand Down Expand Up @@ -275,4 +276,13 @@ public static boolean isPhysical(CommandSender sender) {
return sender instanceof Entity || sender instanceof BlockCommandSender;
}

/**
* Returns the input unchanged if it already has a namespace prefix; otherwise, adds the
* {@link org.bukkit.NamespacedKey#MINECRAFT} prefix.
* @param input a namespaced-key name, or prefix of one, that may or may not be namespaced
* @return the input, namespaced
*/
public static String toNamespaced(String input) {
return input.indexOf(':') >= 0 ? input : NamespacedKey.MINECRAFT + ':' + input;
}
}
10 changes: 6 additions & 4 deletions src/main/java/net/glowstone/command/LocalizedEnumNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.glowstone.i18n.ConsoleMessages;
import net.glowstone.i18n.LocalizedStringImpl;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -48,8 +49,8 @@ public class LocalizedEnumNames<T> {
* auto-complete, separated by commas
* @param baseName the base name of the resource bundle
*/
public LocalizedEnumNames(IntFunction<? extends T> integerResolver, String unknownKey,
String commaSeparatedNamesKey, String baseName) {
public LocalizedEnumNames(IntFunction<? extends T> integerResolver, @NonNls String unknownKey,
@NonNls String commaSeparatedNamesKey, @NonNls String baseName) {
this.integerResolver = integerResolver;
this.unknownKey = unknownKey;
this.commaSeparatedNamesKey = commaSeparatedNamesKey;
Expand All @@ -60,7 +61,7 @@ public LocalizedEnumNames(IntFunction<? extends T> integerResolver, String unkno
}

private static <T> ImmutableSortedMap<String, T> resourceBundleToMap(Locale locale,
String baseName, IntFunction<? extends T> integerResolver) {
@NonNls String baseName, IntFunction<? extends T> integerResolver) {
Collator caseInsensitive = Collator.getInstance(locale);
caseInsensitive.setStrength(Collator.PRIMARY);
ImmutableSortedMap.Builder<String, T> nameToModeBuilder
Expand Down Expand Up @@ -156,7 +157,8 @@ public Entry(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
ResourceBundle strings = ResourceBundle.getBundle("strings", locale);
ResourceBundle strings
= ResourceBundle.getBundle("strings", locale); // NON-NLS
unknown = new LocalizedStringImpl(unknownKey, strings).get();
nameToModeMap = resourceBundleToMap(locale, baseName, integerResolver);
ImmutableMap.Builder<T, String> modeToNameBuilder = ImmutableMap.builder();
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/net/glowstone/command/minecraft/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NonNls;

public class ClearCommand extends GlowVanillaCommand {

@NonNls
private static final String VANILLA_ITEM_PREFIX = "minecraft:";

/**
* Creates the instance for this command.
*/
Expand Down Expand Up @@ -77,10 +73,7 @@ public boolean execute(CommandSender sender, String label, String[] args,
return false;
}
if (args.length >= 2) {
String itemName = args[1];
if (!itemName.startsWith(VANILLA_ITEM_PREFIX)) {
itemName = VANILLA_ITEM_PREFIX + itemName;
}
String itemName = CommandUtils.toNamespaced(args[1]);
Material type = ItemIds.getItem(itemName);
if (type == null) {
new LocalizedStringImpl("clear.no-such-item", resourceBundle)
Expand All @@ -93,8 +86,7 @@ public boolean execute(CommandSender sender, String label, String[] args,
try {
data = Integer.valueOf(dataString);
} catch (NumberFormatException ex) {
new LocalizedStringImpl("clear.nan", resourceBundle)
.sendInColor(ChatColor.RED, sender, dataString);
messages.getNotANumber().sendInColor(ChatColor.RED, sender, dataString);
return false;
}
if (data < -1) {
Expand All @@ -108,8 +100,7 @@ public boolean execute(CommandSender sender, String label, String[] args,
try {
amount = Integer.valueOf(amountString);
} catch (NumberFormatException ex) {
new LocalizedStringImpl("clear.nan", resourceBundle)
.sendInColor(ChatColor.RED, sender, amountString);
messages.getNotANumber().sendInColor(ChatColor.RED, sender, amountString);
return false;
}
if (amount < -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Objects;
import net.glowstone.GlowServer;
import net.glowstone.ServerProvider;
import net.glowstone.i18n.ConsoleMessages;
import net.glowstone.i18n.LocalizedStringImpl;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -40,7 +41,7 @@ public boolean execute(CommandSender sender, String label, String[] args,
if (ex != null) {
new LocalizedStringImpl("deop.failed", messages.getResourceBundle())
.sendInColor(ChatColor.RED, sender, name, ex.getMessage());
ex.printStackTrace();
ConsoleMessages.Error.Command.DEOP_FAILED.log(ex, name);
return;
}
if (player.isOp()) {
Expand Down
51 changes: 21 additions & 30 deletions src/main/java/net/glowstone/command/minecraft/EffectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,36 @@
import net.glowstone.command.CommandUtils;
import net.glowstone.constants.GlowPotionEffect;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.i18n.LocalizedStringImpl;
import net.glowstone.util.TickUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.VanillaCommand;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.StringUtil;

public class EffectCommand extends VanillaCommand {
public class EffectCommand extends GlowVanillaCommand {

private static final List<String> VANILLA_IDS = GlowPotionEffect.getVanillaIds();

/**
* Creates the instance for this command.
*/
public EffectCommand() {
super("effect",
"Gives a player an effect",
"/effect <player> clear "
+ "OR /effect <player> <effect> [seconds] [amplifier] [hideParticles]",
Collections.emptyList());
setPermission("minecraft.command.effect");
super("effect", Collections.emptyList());
setPermission("minecraft.command.effect"); // NON-NLS
}

@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if (!testPermission(sender)) {
public boolean execute(CommandSender sender, String label, String[] args,
CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}

if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "Usage:" + usageMessage);
sendUsageMessage(sender, commandMessages);
return false;
}

Expand All @@ -57,25 +54,27 @@ public boolean execute(CommandSender sender, String label, String[] args) {
} else {
GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
if (player == null) {
sender.sendMessage(ChatColor.RED + " Player '" + name + "' cannot be found");
commandMessages.getNoSuchPlayer().sendInColor(ChatColor.RED, sender, name);
return false;
} else {
players = Collections.singletonList(player);
}
}

if (args[1].equals("clear")) {
if (args[1].equals("clear")) { // NON-NLS
for (GlowPlayer player : players) {
for (PotionEffect potionEffect : player.getActivePotionEffects()) {
player.removePotionEffect(potionEffect.getType());
}
sender.sendMessage("Cleared potion effects from " + player.getName());
new LocalizedStringImpl("effect.cleared", commandMessages.getResourceBundle())
.send(sender, player.getName());
}
return true;
} else {
PotionEffectType effectType = GlowPotionEffect.parsePotionEffectId(args[1]);
if (effectType == null) {
sender.sendMessage(ChatColor.RED + "Potion effect " + args[1] + " is unknown");
new LocalizedStringImpl("effect.unknown", commandMessages.getResourceBundle())
.sendInColor(ChatColor.RED, sender, args[1]);
return false;
}

Expand All @@ -84,7 +83,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
try {
duration = TickUtil.secondsToTicks(Integer.parseInt(args[2]));
} catch (NumberFormatException exc) {
sender.sendMessage(ChatColor.RED + args[2] + " is not a valid integer");
commandMessages.getNotANumber().sendInColor(ChatColor.RED, sender, args[2]);
return false;
}
}
Expand All @@ -94,7 +93,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
try {
amplifier = Integer.parseInt(args[3]);
} catch (NumberFormatException exc) {
sender.sendMessage(ChatColor.RED + args[3] + " is not a valid integer");
commandMessages.getNotANumber().sendInColor(ChatColor.RED, sender, args[3]);
return false;
}
}
Expand All @@ -103,14 +102,13 @@ public boolean execute(CommandSender sender, String label, String[] args) {
if (args.length >= 5 && args[4] != null) {
hideParticles = Boolean.parseBoolean(args[4]);
}

LocalizedStringImpl doneMessage = new LocalizedStringImpl("effect.done",
commandMessages.getResourceBundle());
for (GlowPlayer player : players) {
player.addPotionEffect(
new PotionEffect(effectType, duration, amplifier, false, hideParticles));
sender.sendMessage(
"Given " + effectType.getName() + " (ID " + effectType.getId() + ") * "
+ amplifier + " to " + player.getName() + " for " + duration / 20
+ " seconds");
doneMessage.send(sender, effectType.getName(),
effectType.getId(), amplifier, player.getName(), duration / 20);
}
return true;
}
Expand All @@ -122,14 +120,7 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
if (args == null) {
return Collections.emptyList();
} else if (args.length == 2) {
String effectName = args[1];

if (!effectName.startsWith("minecraft:")) {
final int colonIndex = effectName.indexOf(':');
effectName =
"minecraft:" + effectName.substring(colonIndex == -1 ? 0 : (colonIndex + 1));
}

String effectName = CommandUtils.toNamespaced(args[1]);
return StringUtil
.copyPartialMatches(effectName, VANILLA_IDS, new ArrayList<>(VANILLA_IDS.size()));
}
Expand Down
42 changes: 20 additions & 22 deletions src/main/java/net/glowstone/command/minecraft/EnchantCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,40 @@
import net.glowstone.command.CommandUtils;
import net.glowstone.constants.GlowEnchantment;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.i18n.LocalizedStringImpl;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.VanillaCommand;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NonNls;

public class EnchantCommand extends VanillaCommand {
public class EnchantCommand extends GlowVanillaCommand {

@NonNls
private static final String PREFIX = NamespacedKey.MINECRAFT + ':';
private static List<String> VANILLA_IDS = GlowEnchantment.getVanillaIds();

/**
* Creates the instance for this command.
*/
public EnchantCommand() {
super("enchant",
"Adds an enchantment to the currently by a player held item",
"/enchant <player> <enchantment ID> [level]",
Collections.emptyList());
setPermission("minecraft.command.enchant");
super("enchant", Collections.emptyList());
setPermission("minecraft.command.enchant"); // NON-NLS
}

@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if (!testPermission(sender)) {
public boolean execute(CommandSender sender, String label, String[] args,
CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}

if (args.length < 3) {
sender.sendMessage(ChatColor.RED + "Usage:" + usageMessage);
sendUsageMessage(sender, commandMessages);
return false;
}

Expand All @@ -54,7 +56,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
} else {
GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
if (player == null) {
sender.sendMessage(ChatColor.RED + " Player '" + name + "' cannot be found");
commandMessages.getNoSuchPlayer().sendInColor(ChatColor.RED, sender, name);
return false;
} else {
players = Collections.singletonList(player).stream();
Expand All @@ -63,18 +65,20 @@ public boolean execute(CommandSender sender, String label, String[] args) {

Enchantment enchantment = GlowEnchantment.parseEnchantment(args[1]);
if (enchantment == null) {
sender.sendMessage(ChatColor.RED + "Enchantment " + args[1] + " is unknown");
new LocalizedStringImpl("enchant.unknown", commandMessages.getResourceBundle())
.sendInColor(ChatColor.RED, sender, args[1]);
return false;
}

int level;
try {
level = Integer.parseInt(args[2]);
} catch (NumberFormatException exc) {
sender.sendMessage(ChatColor.RED + args[2] + " is not a valid integer");
commandMessages.getNotANumber().sendInColor(ChatColor.RED, sender, args[2]);
return false;
}

LocalizedStringImpl successMessage
= new LocalizedStringImpl("enchant.done", commandMessages.getResourceBundle());
players
.filter(player -> player.getItemInHand() != null)
.filter(player -> player.getItemInHand().getData().getItemType() != Material.AIR)
Expand All @@ -83,7 +87,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
ItemStack itemInHand = player.getItemInHand();
itemInHand.addUnsafeEnchantment(enchantment, level);
player.setItemInHand(itemInHand);
sender.sendMessage("Enchanting succeeded");
successMessage.send(sender);
});
return true;
}
Expand All @@ -94,13 +98,7 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
if (args == null) {
return Collections.emptyList();
} else if (args.length == 2) {
String effectName = args[1];

if (!effectName.startsWith("minecraft:")) {
final int colonIndex = effectName.indexOf(':');
effectName =
"minecraft:" + effectName.substring(colonIndex == -1 ? 0 : (colonIndex + 1));
}
String effectName = CommandUtils.toNamespaced(args[1]);

return StringUtil
.copyPartialMatches(effectName, VANILLA_IDS, new ArrayList(VANILLA_IDS.size()));
Expand Down
Loading

0 comments on commit c993dd1

Please sign in to comment.