Uses KnarLib's Translator for customizable messages

This commit is contained in:
Kristian Knarvik 2023-07-08 03:21:28 +02:00
parent 2097911afa
commit d65f288dc1
9 changed files with 113 additions and 115 deletions

View File

@ -97,7 +97,7 @@
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId>
<version>1.1</version>
<version>1.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -1,9 +1,13 @@
package net.knarcraft.launchpad;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.launchpad.command.LaunchpadCommand;
import net.knarcraft.launchpad.command.LaunchpadTabCompleter;
import net.knarcraft.launchpad.command.ReloadCommand;
import net.knarcraft.launchpad.config.LaunchpadConfiguration;
import net.knarcraft.launchpad.config.LaunchpadMessage;
import net.knarcraft.launchpad.listener.LaunchpadBreakListener;
import net.knarcraft.launchpad.listener.LaunchpadModifyListener;
import net.knarcraft.launchpad.listener.LaunchpadUseListener;
@ -24,6 +28,7 @@ public final class Launchpad extends JavaPlugin {
private static Launchpad instance;
private LaunchpadConfiguration launchpadConfiguration;
private StringFormatter stringFormatter;
/**
* Gets an instance of this plugin
@ -61,11 +66,29 @@ public final class Launchpad extends JavaPlugin {
return launchpadConfiguration;
}
/**
* Gets the string formatter used to format strings
*
* @return <p>The string formatter</p>
*/
public StringFormatter getStringFormatter() {
return this.stringFormatter;
}
@Override
public void onEnable() {
Launchpad.instance = this;
getConfig().options().copyDefaults(true);
saveConfig();
Translator translator = new Translator();
translator.registerMessageCategory(LaunchpadMessage.ERROR_PLAYER_ONLY);
translator.setColorConversion(ColorConversion.RGB);
translator.loadLanguages(this.getDataFolder(), "en", "en");
this.stringFormatter = new StringFormatter(this.getDescription().getName(), translator);
this.stringFormatter.setColorConversion(ColorConversion.RGB);
this.stringFormatter.setNamePrefix("#FFE34C[");
this.stringFormatter.setNameSuffix("#FFE34C]");
// Register events
Bukkit.getPluginManager().registerEvents(new LaunchpadUseListener(), this);

View File

@ -1,6 +1,8 @@
package net.knarcraft.launchpad.command;
import net.knarcraft.launchpad.config.Message;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.LaunchpadMessage;
import net.knarcraft.launchpad.launchpad.ModificationAction;
import net.knarcraft.launchpad.launchpad.ModificationRequest;
import net.knarcraft.launchpad.launchpad.ModificationRequestHandler;
@ -19,8 +21,9 @@ public class LaunchpadCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
StringFormatter stringFormatter = Launchpad.getInstance().getStringFormatter();
if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage());
stringFormatter.displayErrorMessage(commandSender, LaunchpadMessage.ERROR_PLAYER_ONLY);
return false;
}
@ -46,7 +49,7 @@ public class LaunchpadCommand implements CommandExecutor {
case ABORT -> {
// Retrieving modification requests also removes them
ModificationRequestHandler.getRequests(player.getUniqueId());
commandSender.sendMessage(Message.SUCCESS_MODIFICATIONS_CLEARED.getMessage());
stringFormatter.displaySuccessMessage(commandSender, LaunchpadMessage.SUCCESS_MODIFICATIONS_CLEARED);
return true;
}
case VELOCITIES -> {
@ -57,7 +60,7 @@ public class LaunchpadCommand implements CommandExecutor {
}
}
commandSender.sendMessage(Message.SUCCESS_CLICK_BLOCK.getMessage());
stringFormatter.displaySuccessMessage(commandSender, LaunchpadMessage.SUCCESS_CLICK_BLOCK);
return true;
}

View File

@ -1,7 +1,7 @@
package net.knarcraft.launchpad.command;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.Message;
import net.knarcraft.launchpad.config.LaunchpadMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
@ -19,7 +19,8 @@ public class ReloadCommand implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
Launchpad.getInstance().reload();
commandSender.sendMessage(Message.SUCCESS_PLUGIN_RELOADED.getMessage());
Launchpad.getInstance().getStringFormatter().displaySuccessMessage(commandSender,
LaunchpadMessage.SUCCESS_PLUGIN_RELOADED);
return true;
}

View File

@ -0,0 +1,59 @@
package net.knarcraft.launchpad.config;
import net.knarcraft.knarlib.formatting.TranslatableMessage;
/**
* A message which ca be displayed to the user
*/
public enum LaunchpadMessage implements TranslatableMessage {
/* ************** *
* Error messages *
* ************** */
/**
* The message displayed if the console tries to execute a player-only command
*/
ERROR_PLAYER_ONLY,
/**
* The message displayed if the player tries to modify launchpad property for a block outside the whitelist
*/
ERROR_NOT_WHITELISTED,
/**
* The message displayed if an un-parse-able message is given by a user
*/
ERROR_MATERIAL_NOT_PARSE_ABLE,
/* **************** *
* Success messages *
* **************** */
/**
* The message displayed if the Launchpad plugin is reloaded
*/
SUCCESS_PLUGIN_RELOADED,
/**
* The message to display when a player uses /launchpad clear
*/
SUCCESS_MODIFICATIONS_CLEARED,
/**
* The message to display to prompt the player to click a launchpad
*/
SUCCESS_CLICK_BLOCK,
/**
* The message to display when a launchpad has been added or modified
*/
SUCCESS_MODIFIED_LAUNCHPAD,
;
@Override
public TranslatableMessage[] getAllMessages() {
return LaunchpadMessage.values();
}
}

View File

@ -1,99 +0,0 @@
package net.knarcraft.launchpad.config;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.util.ColorHelper;
import org.jetbrains.annotations.NotNull;
/**
* A message which ca be displayed to the user
*/
public enum Message {
/* ************** *
* Error messages *
* ************** */
/**
* The message displayed if the console tries to execute a player-only command
*/
ERROR_PLAYER_ONLY("&cThis command must be used by a player"),
/**
* The message displayed if the player tries to modify launchpad property for a block outside the whitelist
*/
ERROR_NOT_WHITELISTED("&cThe block could not be modified, as it's not whitelisted. If you want to " +
"abort changing a launchpad, use \"/launchpad abort\""),
/**
* The message displayed if an un-parse-able message is given by a user
*/
ERROR_MATERIAL_NOT_PARSE_ABLE("&cUnable to parse material: {material}"),
/* **************** *
* Success messages *
* **************** */
/**
* The message displayed if the Launchpad plugin is reloaded
*/
SUCCESS_PLUGIN_RELOADED("&aPlugin reloaded!"),
/**
* The message to display when a player uses /launchpad clear
*/
SUCCESS_MODIFICATIONS_CLEARED("&aCleared your launchpad modification queue"),
/**
* The message to display to prompt the player to click a launchpad
*/
SUCCESS_CLICK_BLOCK("&aClick the launchpad you want to create or modify"),
/**
* The message to display when a launchpad has been added or modified
*/
SUCCESS_MODIFIED_LAUNCHPAD("&aThe clicked block's launchpad properties have been modified"),
;
private final @NotNull String defaultMessage;
/**
* Instantiates a new message
*
* @param defaultMessage <p>The default value of the message</p>
*/
Message(@NotNull String defaultMessage) {
this.defaultMessage = defaultMessage;
}
/**
* Gets the message this enum represents
*
* @return <p>The formatted message</p>
*/
public @NotNull String getMessage() {
return formatMessage(this.defaultMessage);
}
/**
* Gets the message this enum represents
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement to use</p>
* @return <p>The formatted message</p>
*/
public @NotNull String getMessage(@NotNull String placeholder, @NotNull String replacement) {
return formatMessage(this.defaultMessage.replace(placeholder, replacement));
}
/**
* Gets the formatted version of the given message
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private @NotNull String formatMessage(@NotNull String message) {
String prefix = Launchpad.getInstance().getDescription().getPrefix();
return ColorHelper.translateAllColorCodes("#FFE34C[&r&l" + prefix + "#FFE34C]&r " + message);
}
}

View File

@ -1,13 +1,15 @@
package net.knarcraft.launchpad.listener;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.Message;
import net.knarcraft.launchpad.config.LaunchpadMessage;
import net.knarcraft.launchpad.launchpad.LaunchpadBlock;
import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler;
import net.knarcraft.launchpad.launchpad.ModificationRequest;
import net.knarcraft.launchpad.launchpad.ModificationRequestHandler;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -46,13 +48,15 @@ public class LaunchpadModifyListener implements Listener {
}
}
Player player = event.getPlayer();
StringFormatter formatter = Launchpad.getInstance().getStringFormatter();
if (completeSuccess) {
event.setUseItemInHand(Event.Result.DENY);
event.setUseInteractedBlock(Event.Result.DENY);
event.getPlayer().sendMessage(Message.SUCCESS_MODIFIED_LAUNCHPAD.getMessage());
formatter.displaySuccessMessage(player, LaunchpadMessage.SUCCESS_MODIFIED_LAUNCHPAD);
} else {
event.getPlayer().sendMessage(Message.ERROR_NOT_WHITELISTED.getMessage());
formatter.displayErrorMessage(player, LaunchpadMessage.ERROR_NOT_WHITELISTED);
}
}

View File

@ -1,7 +1,7 @@
package net.knarcraft.launchpad.util;
import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.config.Message;
import net.knarcraft.launchpad.config.LaunchpadMessage;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
@ -11,7 +11,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -57,8 +56,8 @@ public final class MaterialHelper {
if (matched != null) {
parsedMaterials.add(matched);
} else {
Launchpad.log(Level.WARNING, Message.ERROR_MATERIAL_NOT_PARSE_ABLE.getMessage("{material}",
materialString));
Launchpad.getInstance().getStringFormatter().replacePlaceholder(
LaunchpadMessage.ERROR_MATERIAL_NOT_PARSE_ABLE, "{material}", materialString);
}
return parsedMaterials;
}
@ -80,8 +79,8 @@ public final class MaterialHelper {
if (tag != null) {
targetSet.addAll(tag.getValues());
} else {
Launchpad.log(Level.WARNING, Message.ERROR_MATERIAL_NOT_PARSE_ABLE.getMessage("{material}",
materialName));
Launchpad.getInstance().getStringFormatter().replacePlaceholder(
LaunchpadMessage.ERROR_MATERIAL_NOT_PARSE_ABLE, "{material}", materialName);
}
return true;
}

View File

@ -0,0 +1,8 @@
en:
ERROR_PLAYER_ONLY: "&cThis command must be used by a player"
ERROR_NOT_WHITELISTED: "&cThe block could not be modified, as it's not whitelisted. If you want to abort changing a launchpad, use \"/launchpad abort\""
ERROR_MATERIAL_NOT_PARSE_ABLE: "&cUnable to parse material: {material}"
SUCCESS_PLUGIN_RELOADED: "&aPlugin reloaded!"
SUCCESS_MODIFICATIONS_CLEARED: "&aCleared your launchpad modification queue"
SUCCESS_CLICK_BLOCK: "&aClick the launchpad you want to create or modify"
SUCCESS_MODIFIED_LAUNCHPAD: "&aThe clicked block's launchpad properties have been modified"