Makes output from dial and visualizer translatable
This commit is contained in:
@@ -5,8 +5,10 @@ import net.TheDgtl.Stargate.manager.PermissionManager;
|
||||
import net.TheDgtl.Stargate.network.Network;
|
||||
import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.RealPortal;
|
||||
import net.knarcraft.stargatecommand.StargateCommand;
|
||||
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
|
||||
import net.knarcraft.stargatecommand.manager.IconManager;
|
||||
import net.knarcraft.stargatecommand.manager.OverrideManager;
|
||||
import net.knarcraft.stargatecommand.property.Icon;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
import net.knarcraft.stargatecommand.util.PortalFinderHelper;
|
||||
import org.bukkit.command.Command;
|
||||
@@ -15,12 +17,15 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTranslatedErrorMessage;
|
||||
import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTranslatedInfoMessage;
|
||||
|
||||
/**
|
||||
* This command represents the dial command for dialing any available Stargate
|
||||
*/
|
||||
public class CommandDial implements CommandExecutor {
|
||||
|
||||
private final char spaceReplacement = StargateCommand.getSpaceReplacementCharacter();
|
||||
private final String spaceReplacement = IconManager.getIconString(Icon.SPACE_REPLACEMENT);
|
||||
private final StargateAPI stargateAPI;
|
||||
private final RegistryAPI registryAPI;
|
||||
|
||||
@@ -38,50 +43,50 @@ public class CommandDial implements CommandExecutor {
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMessage("This command can only be used by players");
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY));
|
||||
return true;
|
||||
}
|
||||
if (!player.hasPermission(StargateCommandCommand.DIAL.getPermissionNode())) {
|
||||
player.sendMessage("Permission Denied");
|
||||
player.sendMessage(getTranslatedErrorMessage(TranslatableMessage.PERMISSION_DENIED));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
player.sendMessage("You need to provide a network name and a portal name to dial");
|
||||
player.sendMessage(getTranslatedErrorMessage(TranslatableMessage.COMMAND_DIAL_ARGUMENTS));
|
||||
return true;
|
||||
}
|
||||
|
||||
PermissionManager permissionManager = stargateAPI.getPermissionManager(player);
|
||||
String networkName = args[0].replace(spaceReplacement, ' ');
|
||||
String portalName = args[1].replace(spaceReplacement, ' ');
|
||||
String networkName = args[0].replace(spaceReplacement, " ");
|
||||
String portalName = args[1].replace(spaceReplacement, " ");
|
||||
|
||||
//Validate that the network and portal exists, and that the player can access the portal
|
||||
Network network = registryAPI.getNetwork(networkName, false);
|
||||
if (network == null) {
|
||||
commandSender.sendMessage("Invalid network selected");
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_NETWORK_GIVEN));
|
||||
return true;
|
||||
}
|
||||
|
||||
RealPortal targetPortal = (RealPortal) network.getPortal(portalName);
|
||||
if (targetPortal == null) {
|
||||
commandSender.sendMessage("Invalid portal selected");
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_PORTAL_GIVEN));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!permissionManager.hasAccessPermission(targetPortal)) {
|
||||
commandSender.sendMessage("You don't have access to the selected portal");
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.PORTAL_NO_ACCESS));
|
||||
return true;
|
||||
}
|
||||
|
||||
//Find any Stargate block in the player's line of sight
|
||||
RealPortal originPortal = (RealPortal) PortalFinderHelper.findPortalByRaytrace(registryAPI, player, 10);
|
||||
if (originPortal == null) {
|
||||
player.sendMessage("You need to look at a portal to dial");
|
||||
player.sendMessage(getTranslatedErrorMessage(TranslatableMessage.NO_PORTAL_IN_SIGHT));
|
||||
return true;
|
||||
}
|
||||
originPortal.overrideDestination(targetPortal);
|
||||
OverrideManager.storeOverriddenDestination(originPortal);
|
||||
originPortal.open(player);
|
||||
|
||||
player.sendMessage("Your Stargate has been prepared");
|
||||
player.sendMessage(getTranslatedInfoMessage(TranslatableMessage.DIAL_SUCCESSFUL));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,19 +5,25 @@ import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.FixedPortal;
|
||||
import net.TheDgtl.Stargate.network.portal.Portal;
|
||||
import net.TheDgtl.Stargate.network.portal.PortalFlag;
|
||||
import net.knarcraft.stargatecommand.StargateCommand;
|
||||
import net.knarcraft.stargatecommand.formatting.StringFormatter;
|
||||
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
|
||||
import net.knarcraft.stargatecommand.formatting.Translator;
|
||||
import net.knarcraft.stargatecommand.manager.IconManager;
|
||||
import net.knarcraft.stargatecommand.property.Icon;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static net.knarcraft.stargatecommand.formatting.StringFormatter.getTranslatedErrorMessage;
|
||||
|
||||
/**
|
||||
* This command represents the command for visualizing a Stargate network
|
||||
*/
|
||||
public class CommandVisualizer implements CommandExecutor {
|
||||
|
||||
private final char spaceReplacement = StargateCommand.getSpaceReplacementCharacter();
|
||||
private final String spaceReplacement = IconManager.getIconString(Icon.SPACE_REPLACEMENT);
|
||||
private final RegistryAPI registryAPI;
|
||||
|
||||
/**
|
||||
@@ -33,57 +39,60 @@ public class CommandVisualizer implements CommandExecutor {
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
if (!commandSender.hasPermission(StargateCommandCommand.VISUALIZER.getPermissionNode())) {
|
||||
commandSender.sendMessage("Permission Denied");
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.PERMISSION_DENIED));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length < 1) {
|
||||
commandSender.sendMessage("A network must be provided");
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.COMMAND_VISUALIZER_ARGUMENTS));
|
||||
return true;
|
||||
}
|
||||
|
||||
Network network = registryAPI.getNetwork(args[0].replace(spaceReplacement, ' '), false);
|
||||
Network network = registryAPI.getNetwork(args[0].replace(spaceReplacement, " "), false);
|
||||
if (network == null) {
|
||||
commandSender.sendMessage("You must provide a valid network to visualize");
|
||||
commandSender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.INVALID_NETWORK_GIVEN));
|
||||
return true;
|
||||
}
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
stringBuilder.append("Symbol explanation: ").append("\n");
|
||||
stringBuilder.append("⇒ = hidden, ⇄ = not hidden").append("\n");
|
||||
stringBuilder.append("⬛ = always open, ⬜ = not always open").append("\n");
|
||||
stringBuilder.append("↯ = random destination, ↠ = non-random destination").append("\n");
|
||||
stringBuilder.append("-> = fixed portal going to the specified portal").append("\n").append('|').append("\n");
|
||||
stringBuilder.append("All portals in network ").append(network.getName()).append(":");
|
||||
stringBuilder.append(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_VISUALIZER_FORMAT));
|
||||
|
||||
//Print info about all portals in the network
|
||||
for (Portal portal : network.getAllPortals()) {
|
||||
stringBuilder.append("\n");
|
||||
StringBuilder iconBuilder = new StringBuilder();
|
||||
if (portal.hasFlag(PortalFlag.HIDDEN)) {
|
||||
stringBuilder.append('⇒');
|
||||
iconBuilder.append(Icon.HIDDEN.getPlaceholder());
|
||||
} else {
|
||||
stringBuilder.append('⇄');
|
||||
iconBuilder.append(Icon.NOT_HIDDEN.getPlaceholder());
|
||||
}
|
||||
if (portal.hasFlag(PortalFlag.ALWAYS_ON)) {
|
||||
stringBuilder.append('⬛');
|
||||
iconBuilder.append(Icon.ALWAYS_ON.getPlaceholder());
|
||||
} else {
|
||||
stringBuilder.append('⬜');
|
||||
iconBuilder.append(Icon.NOT_ALWAYS_ON.getPlaceholder());
|
||||
}
|
||||
if (portal.hasFlag(PortalFlag.RANDOM)) {
|
||||
stringBuilder.append('↯');
|
||||
iconBuilder.append(Icon.RANDOM.getPlaceholder());
|
||||
} else {
|
||||
stringBuilder.append('↠');
|
||||
iconBuilder.append(Icon.NOT_RANDOM.getPlaceholder());
|
||||
}
|
||||
String fixedString = "";
|
||||
//TODO: Look for the fixed flag instead of FixedPortal once it's fixed
|
||||
stringBuilder.append(" ").append(portal.getName());
|
||||
if (portal instanceof FixedPortal) {
|
||||
stringBuilder.append(" -> ");
|
||||
stringBuilder.append(portal.getDestinationName());
|
||||
fixedString = StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(
|
||||
TranslatableMessage.COMMAND_VISUALIZER_FIXED_FORMAT), "{portal}",
|
||||
portal.getDestinationName());
|
||||
}
|
||||
|
||||
stringBuilder.append(StringFormatter.replacePlaceholders(
|
||||
Translator.getTranslatedMessage(TranslatableMessage.COMMAND_VISUALIZER_PORTAL_FORMAT),
|
||||
new String[]{"{icons}", "{portal}", "{fixed}"}, new String[]{iconBuilder.toString(),
|
||||
portal.getName(), fixedString}));
|
||||
|
||||
}
|
||||
|
||||
commandSender.sendMessage(stringBuilder.toString());
|
||||
commandSender.sendMessage(IconManager.replaceIconsInString(
|
||||
StringFormatter.replacePlaceholder(stringBuilder.toString(), "{network}", network.getName())));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ import net.TheDgtl.Stargate.network.Network;
|
||||
import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.Portal;
|
||||
import net.TheDgtl.Stargate.network.portal.RealPortal;
|
||||
import net.knarcraft.stargatecommand.StargateCommand;
|
||||
import net.knarcraft.stargatecommand.manager.IconManager;
|
||||
import net.knarcraft.stargatecommand.property.Icon;
|
||||
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -29,7 +30,7 @@ import static net.knarcraft.stargatecommand.util.TabCompleterHelper.filterMatchi
|
||||
*/
|
||||
public class DialTabCompleter implements TabCompleter {
|
||||
|
||||
private final char spaceReplacement = StargateCommand.getSpaceReplacementCharacter();
|
||||
private final String spaceReplacement = IconManager.getIconString(Icon.SPACE_REPLACEMENT);
|
||||
private final StargateAPI stargateAPI;
|
||||
|
||||
/**
|
||||
@@ -65,14 +66,14 @@ public class DialTabCompleter implements TabCompleter {
|
||||
if (args.length > 2) {
|
||||
return new ArrayList<>();
|
||||
} else if (args.length > 1) {
|
||||
Network network = registryAPI.getNetwork(args[0].replace(spaceReplacement, ' '), false);
|
||||
Network network = registryAPI.getNetwork(args[0].replace(spaceReplacement, " "), false);
|
||||
if (network != null && availablePortals.containsKey(network)) {
|
||||
return filterMatching(availablePortals.get(network), args[1].replace(spaceReplacement, ' '));
|
||||
return filterMatching(availablePortals.get(network), args[1].replace(spaceReplacement, " "));
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
} else {
|
||||
return filterMatching(availableNetworks, args[0].replace(spaceReplacement, ' '));
|
||||
return filterMatching(availableNetworks, args[0].replace(spaceReplacement, " "));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,12 +96,12 @@ public class DialTabCompleter implements TabCompleter {
|
||||
if (!availablePortals.containsKey(network)) {
|
||||
availablePortals.put(network, new LinkedList<>());
|
||||
}
|
||||
availablePortals.get(network).add(portal.getName().replace(' ', spaceReplacement));
|
||||
availablePortals.get(network).add(portal.getName().replace(" ", spaceReplacement));
|
||||
}
|
||||
}
|
||||
}
|
||||
//Add only the network names with portals available to the player
|
||||
availablePortals.keySet().forEach((item) -> availableNetworks.add(item.getName().replace(' ',
|
||||
availablePortals.keySet().forEach((item) -> availableNetworks.add(item.getName().replace(" ",
|
||||
spaceReplacement)));
|
||||
}
|
||||
|
||||
|
||||
@@ -55,4 +55,59 @@ public enum TranslatableMessage {
|
||||
*/
|
||||
CONFIG_OPTION_DESCRIPTION,
|
||||
|
||||
/**
|
||||
* The message to display when a command is used from the console, but requires a player
|
||||
*/
|
||||
COMMAND_PLAYER_ONLY,
|
||||
|
||||
/**
|
||||
* The message to display for explaining expected arguments for the dial command
|
||||
*/
|
||||
COMMAND_DIAL_ARGUMENTS,
|
||||
|
||||
/**
|
||||
* The message to display when the user provides an invalid network name
|
||||
*/
|
||||
INVALID_NETWORK_GIVEN,
|
||||
|
||||
/**
|
||||
* The message to display when the user provides an invalid portal name
|
||||
*/
|
||||
INVALID_PORTAL_GIVEN,
|
||||
|
||||
/**
|
||||
* The message to display when the user provides a portal they cannot access
|
||||
*/
|
||||
PORTAL_NO_ACCESS,
|
||||
|
||||
/**
|
||||
* The message to display when a command cannot find a portal in the player's line of sight
|
||||
*/
|
||||
NO_PORTAL_IN_SIGHT,
|
||||
|
||||
/**
|
||||
* The message to display when a Stargate has been successfully dialed
|
||||
*/
|
||||
DIAL_SUCCESSFUL,
|
||||
|
||||
/**
|
||||
* The message to display for explaining expected arguments for the visualizer command
|
||||
*/
|
||||
COMMAND_VISUALIZER_ARGUMENTS,
|
||||
|
||||
/**
|
||||
* The format used to display the visualization of a Stargate network
|
||||
*/
|
||||
COMMAND_VISUALIZER_FORMAT,
|
||||
|
||||
/**
|
||||
* The format used to display one portal int the visualized network
|
||||
*/
|
||||
COMMAND_VISUALIZER_PORTAL_FORMAT,
|
||||
|
||||
/**
|
||||
* The format used to display info about a portal's fixed destination
|
||||
*/
|
||||
COMMAND_VISUALIZER_FIXED_FORMAT,
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package net.knarcraft.stargatecommand.manager;
|
||||
|
||||
import net.knarcraft.stargatecommand.formatting.StringFormatter;
|
||||
import net.knarcraft.stargatecommand.property.Icon;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This manager keeps track of the different icons used as part of output
|
||||
*/
|
||||
public final class IconManager {
|
||||
|
||||
private final static Map<Icon, String> managedIcons = new HashMap<>();
|
||||
|
||||
private IconManager() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the String used to display the given icon
|
||||
*
|
||||
* @param icon <p>The icon to get the icon string for</p>
|
||||
* @return <p>The string used to display the icon</p>
|
||||
*/
|
||||
public static String getIconString(Icon icon) {
|
||||
if (managedIcons.containsKey(icon)) {
|
||||
return managedIcons.get(icon);
|
||||
} else {
|
||||
return icon.getDefaultIconString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces all icon placeholders in the given string
|
||||
*
|
||||
* @param input <p>The input to replace placeholders for</p>
|
||||
* @return <p>The input with placeholders replaced</p>
|
||||
*/
|
||||
public static String replaceIconsInString(String input) {
|
||||
for (Icon icon : Icon.values()) {
|
||||
input = StringFormatter.replacePlaceholder(input, icon.getPlaceholder(), getIconString(icon));
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the given icon string to the given icon
|
||||
*
|
||||
* @param icon <p>The icon to register an icon string for</p>
|
||||
* @param string <p>The icon string to register</p>
|
||||
*/
|
||||
public static void registerIconString(Icon icon, String string) {
|
||||
if (icon == null || string == null) {
|
||||
managedIcons.remove(icon);
|
||||
} else {
|
||||
managedIcons.put(icon, string);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package net.knarcraft.stargatecommand.property;
|
||||
|
||||
/**
|
||||
* A representation of configurable icons used in this plugin
|
||||
*/
|
||||
public enum Icon {
|
||||
|
||||
/**
|
||||
* The icon used to mark a Stargate as hidden
|
||||
*/
|
||||
HIDDEN("⇒", "{icon_h}"),
|
||||
|
||||
/**
|
||||
* The icon used to mark a Stargate as not hidden
|
||||
*/
|
||||
NOT_HIDDEN("⇄", "{icon_nh}"),
|
||||
|
||||
/**
|
||||
* The icon used to mark a Stargate as always on/open
|
||||
*/
|
||||
ALWAYS_ON("⬛", "{icon_a}"),
|
||||
|
||||
/**
|
||||
* The icon used to mark a Stargate as not always on/open
|
||||
*/
|
||||
NOT_ALWAYS_ON("⬜", "{icon_na}"),
|
||||
|
||||
/**
|
||||
* The icon used to mark a Stargate as random
|
||||
*/
|
||||
RANDOM("↯", "{icon_r}"),
|
||||
|
||||
/**
|
||||
* The icon used to mark a Stargate as not random
|
||||
*/
|
||||
NOT_RANDOM("↠", "{icon_nr}"),
|
||||
|
||||
/**
|
||||
* The icon used as a rightwards arrow between two items
|
||||
*/
|
||||
ARROW_RIGHT("->", "{icon_arrow_right}"),
|
||||
|
||||
/**
|
||||
* The icon used to replace the space character for any names with spaces
|
||||
*/
|
||||
SPACE_REPLACEMENT("⚊", "{icon_space}");
|
||||
|
||||
private final String defaultIcon;
|
||||
private final String placeholder;
|
||||
|
||||
/**
|
||||
* Instantiates a new icon
|
||||
*
|
||||
* @param defaultIcon <p>The default value used unless another is specified</p>
|
||||
*/
|
||||
Icon(String defaultIcon, String placeholder) {
|
||||
this.defaultIcon = defaultIcon;
|
||||
this.placeholder = placeholder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default icon string used for this icon
|
||||
*
|
||||
* @return <p>The default string used for this icon</p>
|
||||
*/
|
||||
public String getDefaultIconString() {
|
||||
return this.defaultIcon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the placeholder this icon should replace
|
||||
*
|
||||
* @return <p>The placeholder this icon should replace</p>
|
||||
*/
|
||||
public String getPlaceholder() {
|
||||
return this.placeholder;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,4 +8,15 @@ en:
|
||||
INVALID_NUMBER_GIVEN: "Invalid number given"
|
||||
CONFIG_OPTION_CURRENT_VALUE: "&aCurrent value: &6{value}"
|
||||
CONFIG_VALUES_HEADER: "&aStargate &6Config values:"
|
||||
CONFIG_OPTION_DESCRIPTION: "&6{name}&f - &a{description}&8 (Default: &7{value}&8)"
|
||||
CONFIG_OPTION_DESCRIPTION: "&6{name}&r - &a{description}&8 (Default: &7{value}&8)"
|
||||
COMMAND_PLAYER_ONLY: "This command can only be used by players"
|
||||
COMMAND_DIAL_ARGUMENTS: "You need to provide a network name and a portal name to dial"
|
||||
INVALID_NETWORK_GIVEN: "Invalid network specified in input"
|
||||
INVALID_PORTAL_GIVEN: "Invalid portal specified in input"
|
||||
PORTAL_NO_ACCESS: "You don't have access to the selected portal"
|
||||
NO_PORTAL_IN_SIGHT: "You need to look at a portal to use this command"
|
||||
DIAL_SUCCESSFUL: "Your Stargate has been prepared"
|
||||
COMMAND_VISUALIZER_ARGUMENTS: "A network must be provided to visualize"
|
||||
COMMAND_VISUALIZER_FORMAT: "Symbol explanation: \n&6{icon_h}&r = hidden, &6{icon_nh}&r = not hidden\n&6{icon_a}&r = always open, &6{icon_na}&r = not always open\n&6{icon_r}&r = random destination, &6{icon_nr}&r = non-random destination\n&6{icon_arrow_right}&r = fixed portal going to the specified portal\n|\nAll portals in network &a{network}&r:"
|
||||
COMMAND_VISUALIZER_PORTAL_FORMAT: "&6{icons} &a{portal}&r{fixed}"
|
||||
COMMAND_VISUALIZER_FIXED_FORMAT: " {icon_arrow_right} &a{portal}"
|
||||
Reference in New Issue
Block a user