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 signLocationThe location of the permission sign to get
+ * @returnThe permission sign at the given location, or null if no such sign exists
*/ - public static MapThe sign to remove
+ * @param locationThe 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 MapThe clicked sign
+ * @param playerThe player that clicked the sign
+ * @param eventThe 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(); } }