From 71e932bf40a1ec17e3006bddf16b625d2171c683 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 13 Jan 2022 17:14:29 +0100 Subject: [PATCH] Adds some more code for getting closer to a working plugin Loads signs from disk when executing the reload command Adds code for sign registration Saves signs when adding/removing a sign Makes sure to initialize the translator Adds a check for whether a sign is registered --- .../permissionsigns/PermissionSigns.java | 5 ++- .../permissionsigns/SignManager.java | 29 +++++++++++++---- .../command/CreateCommand.java | 2 ++ .../command/ReloadCommand.java | 5 +++ .../formatting/Translator.java | 6 ++-- .../listener/SignListener.java | 32 ++++++++++++++++--- 6 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java index 4dbdff0..c76e2fc 100644 --- a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java +++ b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java @@ -4,6 +4,7 @@ import net.knarcraft.permissionsigns.command.PermissionSignsCommand; import net.knarcraft.permissionsigns.command.PermissionSignsTabCompleter; import net.knarcraft.permissionsigns.container.PermissionSign; import net.knarcraft.permissionsigns.container.SignCreationRequest; +import net.knarcraft.permissionsigns.formatting.Translator; import net.knarcraft.permissionsigns.thread.SignCreationRequestTimeoutThread; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; @@ -27,6 +28,7 @@ public final class PermissionSigns extends JavaPlugin { /** * Instantiates the permission signs class */ + @SuppressWarnings("unused") public PermissionSigns() { super(); instance = this; @@ -128,8 +130,9 @@ public final class PermissionSigns extends JavaPlugin { //TODO: Start sign creation when the create command is used and save the data until an empty sign is right-clicked //TODO: Check for existence of old permission signs when clicked and register them as new permission signs. If - // it has the permissionSigns header and a name matching contents in signdata.yml, add it. + // it has the permissionSigns header and a name matching contents in signs.yml, add it. + Translator.loadLanguages("en"); registerCommands(); BukkitScheduler scheduler = Bukkit.getScheduler(); scheduler.runTaskTimer(this, new SignCreationRequestTimeoutThread(signCreationRequests), 0L, 100L); diff --git a/src/main/java/net/knarcraft/permissionsigns/SignManager.java b/src/main/java/net/knarcraft/permissionsigns/SignManager.java index edc9a7e..8875b1b 100644 --- a/src/main/java/net/knarcraft/permissionsigns/SignManager.java +++ b/src/main/java/net/knarcraft/permissionsigns/SignManager.java @@ -25,12 +25,13 @@ public class SignManager { private static final File signsFile = new File(PermissionSigns.getInstance().getDataFolder(), "signs.yml"); /** - * Gets a copy of all managed signs + * Gets the permission sign at the given location * - * @return

All managed signs

+ * @param signLocation

The location of the permission sign to get

+ * @return

The permission sign at the given location, or null if no such sign exists

*/ - public static Map getSigns() { - return new HashMap<>(managedSigns); + public static PermissionSign getSign(Location signLocation) { + return managedSigns.get(signLocation); } /** @@ -40,15 +41,17 @@ public class SignManager { */ public static void addSign(PermissionSign sign) { managedSigns.put(sign.getSignLocation(), sign); + save(); } /** * Removes a sign * - * @param sign

The sign to remove

+ * @param location

The location of the sign to remove

*/ - public static void removeSign(PermissionSign sign) { - managedSigns.remove(sign.getSignLocation()); + public static void removeSign(Location location) { + managedSigns.remove(location); + save(); } /** @@ -91,6 +94,18 @@ public class SignManager { e.getMessage()); } } + //TODO: Might want to re-draw signs here in case any signs have changed + } + + /** + * Tries to save signs and handles any errors + */ + private static void save() { + try { + saveSigns(); + } catch (IOException e) { + PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to save signs: " + e.getMessage()); + } } /** diff --git a/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java index c0b374b..eb93802 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java @@ -40,6 +40,8 @@ public class CreateCommand implements CommandExecutor { return false; } PermissionSigns.addSignCreationRequest((Player) sender, newSign); + //TODO: Tell the user that the creation request has been created and that they need to right-click an empty + // sign within 60 seconds to register the permissions sign return true; } diff --git a/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java index 0724ed0..f1428b0 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java @@ -1,5 +1,6 @@ package net.knarcraft.permissionsigns.command; +import net.knarcraft.permissionsigns.SignManager; import net.knarcraft.permissionsigns.formatting.StringFormatter; import net.knarcraft.permissionsigns.formatting.TranslatableMessage; import net.knarcraft.permissionsigns.formatting.Translator; @@ -8,12 +9,16 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +/** + * The command used to reload this plugin + */ public class ReloadCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { if (sender.hasPermission("permissionsigns.admin.reload")) { //TODO: Perform a reload from disk + SignManager.loadSigns(); } else { sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED))); } diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java b/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java index 4724b4c..6da4a9d 100644 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java +++ b/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java @@ -13,7 +13,7 @@ import java.util.logging.Level; /** * A tool to get strings translated to the correct language */ -public class Translator { +public final class Translator { private static Map translatedMessages; private static Map backupTranslatedMessages; @@ -21,9 +21,9 @@ public class Translator { /** * Loads the languages used by this translator */ - public Translator() { + public static void loadLanguages(String selectedLanguage) { backupTranslatedMessages = loadTranslatedMessages("en"); - translatedMessages = loadTranslatedMessages("en"); + translatedMessages = loadTranslatedMessages(selectedLanguage); } /** diff --git a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java index 8da85c4..dea9a8c 100644 --- a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java @@ -1,6 +1,8 @@ package net.knarcraft.permissionsigns.listener; import net.knarcraft.permissionsigns.PermissionSigns; +import net.knarcraft.permissionsigns.SignManager; +import net.knarcraft.permissionsigns.container.PermissionSign; import net.knarcraft.permissionsigns.container.SignCreationRequest; import net.knarcraft.permissionsigns.formatting.TranslatableMessage; import net.knarcraft.permissionsigns.formatting.Translator; @@ -48,9 +50,15 @@ public class SignListener implements Listener { } } + /** + * Handles the left click of a sign that might be a permission sign + * + * @param sign

The clicked sign

+ * @param player

The player that clicked the sign

+ * @param event

The triggered interaction event

+ */ private void handleSignLeftClick(Sign sign, Player player, PlayerInteractEvent event) { - //TODO: Check if the sign is a registered permissions sign - boolean registered = true; + boolean registered = SignManager.getSign(sign.getLocation()) != null; if (!registered) { return; } @@ -58,7 +66,7 @@ public class SignListener implements Listener { event.setCancelled(true); player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY)); } else { - //TODO: Un-register the permissions sign + SignManager.removeSign(sign.getLocation()); player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED)); } } @@ -72,7 +80,10 @@ public class SignListener implements Listener { private void handleSignRightClick(Sign sign, Player player) { String[] lines = sign.getLines(); - //TODO: Check if the sign is a registered permissions sign + if (SignManager.getSign(sign.getLocation()) != null) { + //TODO: Perform the permission payment/granting + return; + } //Don't allow non-empty signs to be overwritten if (!Arrays.stream(lines).allMatch(String::isEmpty)) { @@ -84,7 +95,18 @@ public class SignListener implements Listener { return; } - //TODO: Register the sign and remove the request + //Register the sign and remove the request + PermissionSign permissionSign = request.getPermissionSign(); + permissionSign.setSignLocation(sign.getLocation()); + SignManager.addSign(permissionSign); + PermissionSigns.cancelSignCreationRequest(player.getUniqueId()); + + //Display the permission sign information on the sign + String[] signLines = permissionSign.getSignLines(); + for (int i = 0; i < signLines.length; i++) { + sign.setLine(i, signLines[i]); + } + sign.update(); } }