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
This commit is contained in:
Kristian Knarvik 2022-01-13 17:14:29 +01:00
parent a77c297e58
commit 71e932bf40
6 changed files with 63 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import net.knarcraft.permissionsigns.command.PermissionSignsCommand;
import net.knarcraft.permissionsigns.command.PermissionSignsTabCompleter; import net.knarcraft.permissionsigns.command.PermissionSignsTabCompleter;
import net.knarcraft.permissionsigns.container.PermissionSign; import net.knarcraft.permissionsigns.container.PermissionSign;
import net.knarcraft.permissionsigns.container.SignCreationRequest; import net.knarcraft.permissionsigns.container.SignCreationRequest;
import net.knarcraft.permissionsigns.formatting.Translator;
import net.knarcraft.permissionsigns.thread.SignCreationRequestTimeoutThread; import net.knarcraft.permissionsigns.thread.SignCreationRequestTimeoutThread;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
@ -27,6 +28,7 @@ public final class PermissionSigns extends JavaPlugin {
/** /**
* Instantiates the permission signs class * Instantiates the permission signs class
*/ */
@SuppressWarnings("unused")
public PermissionSigns() { public PermissionSigns() {
super(); super();
instance = this; 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: 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 //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(); registerCommands();
BukkitScheduler scheduler = Bukkit.getScheduler(); BukkitScheduler scheduler = Bukkit.getScheduler();
scheduler.runTaskTimer(this, new SignCreationRequestTimeoutThread(signCreationRequests), 0L, 100L); scheduler.runTaskTimer(this, new SignCreationRequestTimeoutThread(signCreationRequests), 0L, 100L);

View File

@ -25,12 +25,13 @@ public class SignManager {
private static final File signsFile = new File(PermissionSigns.getInstance().getDataFolder(), "signs.yml"); 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 <p>All managed signs</p> * @param signLocation <p>The location of the permission sign to get</p>
* @return <p>The permission sign at the given location, or null if no such sign exists</p>
*/ */
public static Map<Location, PermissionSign> getSigns() { public static PermissionSign getSign(Location signLocation) {
return new HashMap<>(managedSigns); return managedSigns.get(signLocation);
} }
/** /**
@ -40,15 +41,17 @@ public class SignManager {
*/ */
public static void addSign(PermissionSign sign) { public static void addSign(PermissionSign sign) {
managedSigns.put(sign.getSignLocation(), sign); managedSigns.put(sign.getSignLocation(), sign);
save();
} }
/** /**
* Removes a sign * Removes a sign
* *
* @param sign <p>The sign to remove</p> * @param location <p>The location of the sign to remove</p>
*/ */
public static void removeSign(PermissionSign sign) { public static void removeSign(Location location) {
managedSigns.remove(sign.getSignLocation()); managedSigns.remove(location);
save();
} }
/** /**
@ -91,6 +94,18 @@ public class SignManager {
e.getMessage()); 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());
}
} }
/** /**

View File

@ -40,6 +40,8 @@ public class CreateCommand implements CommandExecutor {
return false; return false;
} }
PermissionSigns.addSignCreationRequest((Player) sender, newSign); 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; return true;
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.permissionsigns.command; package net.knarcraft.permissionsigns.command;
import net.knarcraft.permissionsigns.SignManager;
import net.knarcraft.permissionsigns.formatting.StringFormatter; import net.knarcraft.permissionsigns.formatting.StringFormatter;
import net.knarcraft.permissionsigns.formatting.TranslatableMessage; import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
import net.knarcraft.permissionsigns.formatting.Translator; import net.knarcraft.permissionsigns.formatting.Translator;
@ -8,12 +9,16 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/**
* The command used to reload this plugin
*/
public class ReloadCommand implements CommandExecutor { public class ReloadCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender.hasPermission("permissionsigns.admin.reload")) { if (sender.hasPermission("permissionsigns.admin.reload")) {
//TODO: Perform a reload from disk //TODO: Perform a reload from disk
SignManager.loadSigns();
} else { } else {
sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED))); sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)));
} }

View File

@ -13,7 +13,7 @@ import java.util.logging.Level;
/** /**
* A tool to get strings translated to the correct language * A tool to get strings translated to the correct language
*/ */
public class Translator { public final class Translator {
private static Map<TranslatableMessage, String> translatedMessages; private static Map<TranslatableMessage, String> translatedMessages;
private static Map<TranslatableMessage, String> backupTranslatedMessages; private static Map<TranslatableMessage, String> backupTranslatedMessages;
@ -21,9 +21,9 @@ public class Translator {
/** /**
* Loads the languages used by this translator * Loads the languages used by this translator
*/ */
public Translator() { public static void loadLanguages(String selectedLanguage) {
backupTranslatedMessages = loadTranslatedMessages("en"); backupTranslatedMessages = loadTranslatedMessages("en");
translatedMessages = loadTranslatedMessages("en"); translatedMessages = loadTranslatedMessages(selectedLanguage);
} }
/** /**

View File

@ -1,6 +1,8 @@
package net.knarcraft.permissionsigns.listener; package net.knarcraft.permissionsigns.listener;
import net.knarcraft.permissionsigns.PermissionSigns; 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.container.SignCreationRequest;
import net.knarcraft.permissionsigns.formatting.TranslatableMessage; import net.knarcraft.permissionsigns.formatting.TranslatableMessage;
import net.knarcraft.permissionsigns.formatting.Translator; 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 <p>The clicked sign</p>
* @param player <p>The player that clicked the sign</p>
* @param event <p>The triggered interaction event</p>
*/
private void handleSignLeftClick(Sign sign, Player player, PlayerInteractEvent event) { private void handleSignLeftClick(Sign sign, Player player, PlayerInteractEvent event) {
//TODO: Check if the sign is a registered permissions sign boolean registered = SignManager.getSign(sign.getLocation()) != null;
boolean registered = true;
if (!registered) { if (!registered) {
return; return;
} }
@ -58,7 +66,7 @@ public class SignListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY)); player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY));
} else { } else {
//TODO: Un-register the permissions sign SignManager.removeSign(sign.getLocation());
player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED)); player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED));
} }
} }
@ -72,7 +80,10 @@ public class SignListener implements Listener {
private void handleSignRightClick(Sign sign, Player player) { private void handleSignRightClick(Sign sign, Player player) {
String[] lines = sign.getLines(); 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 //Don't allow non-empty signs to be overwritten
if (!Arrays.stream(lines).allMatch(String::isEmpty)) { if (!Arrays.stream(lines).allMatch(String::isEmpty)) {
@ -84,7 +95,18 @@ public class SignListener implements Listener {
return; 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();
} }
} }