From 35e0666a1dfb32ea3f478c15b5f3952f309799d5 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 13 Jul 2022 15:53:28 +0200 Subject: [PATCH] Performs some minor changes Changes some warning messages to info Displays sign creation cost when unable to create a paid sign Changes spigot dependency to 1.19 Refunds the player during reload/startup when a tracked sign's physical sign has been destroyed --- pom.xml | 2 +- .../paidsigns/formatting/Translator.java | 2 +- .../paidsigns/listener/SignListener.java | 22 +++++-- .../paidsigns/manager/PaidSignManager.java | 2 +- .../paidsigns/manager/TrackedSignManager.java | 66 ++++++++++--------- src/main/resources/strings.yml | 2 +- 6 files changed, 57 insertions(+), 39 deletions(-) diff --git a/pom.xml b/pom.xml index d22cc67..d513c4f 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ org.spigotmc spigot-api - 1.18.1-R0.1-SNAPSHOT + 1.19-R0.1-SNAPSHOT provided diff --git a/src/main/java/net/knarcraft/paidsigns/formatting/Translator.java b/src/main/java/net/knarcraft/paidsigns/formatting/Translator.java index 43acf89..5070282 100644 --- a/src/main/java/net/knarcraft/paidsigns/formatting/Translator.java +++ b/src/main/java/net/knarcraft/paidsigns/formatting/Translator.java @@ -87,7 +87,7 @@ public final class Translator { } try { - PaidSigns.getInstance().getLogger().log(Level.WARNING, "Loading custom strings..."); + PaidSigns.getInstance().getLogger().log(Level.INFO, "Loading custom strings..."); return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(new FileInputStream(strings)))); } catch (FileNotFoundException e) { PaidSigns.getInstance().getLogger().log(Level.WARNING, "Unable to load custom messages"); diff --git a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java index 40ca086..d469966 100644 --- a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java @@ -71,14 +71,13 @@ public class SignListener implements Listener { double cost = paidSign.getCost(); boolean canAfford = EconomyManager.canAfford(player, cost); if (!canAfford) { - player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_CANNOT_AFFORD)); + player.sendMessage(replaceCost(cost, StringFormatter.getTranslatedErrorMessage( + TranslatableMessage.ERROR_CANNOT_AFFORD))); event.setCancelled(true); } else { - String unit = EconomyManager.getCurrency(cost != 1); EconomyManager.withdraw(player, cost); - player.sendMessage(String.format(StringFormatter.replacePlaceholders( - StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_PAID_FOR_SIGN), - new String[]{"{cost}", "{unit}"}, new String[]{"%.2f", "%s"}), cost, unit)); + player.sendMessage(replaceCost(cost, StringFormatter.getTranslatedInfoMessage( + TranslatableMessage.SUCCESS_PAID_FOR_SIGN))); try { TrackedSignManager.addTrackedSign(event.getBlock().getLocation(), player.getUniqueId(), cost); } catch (IOException ignored) { @@ -86,4 +85,17 @@ public class SignListener implements Listener { } } + /** + * Replaces cost and unit placeholders in the given message + * + * @param cost

The cost to insert

+ * @param message

The original message to replace the cost placeholder for

+ * @return

The message with the cost instead of the cost placeholder

+ */ + private String replaceCost(double cost, String message) { + String unit = EconomyManager.getCurrency(cost != 1); + return String.format(StringFormatter.replacePlaceholders(message, new String[]{"{cost}", "{unit}"}, + new String[]{"%.2f", "%s"}), cost, unit); + } + } diff --git a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java index 359f3d8..0fe4086 100644 --- a/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java +++ b/src/main/java/net/knarcraft/paidsigns/manager/PaidSignManager.java @@ -88,7 +88,7 @@ public final class PaidSignManager { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(signsFile); ConfigurationSection signSection = configuration.getConfigurationSection("paidSigns"); if (signSection == null) { - PaidSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in data.yml"); + PaidSigns.getInstance().getLogger().log(Level.INFO, "Signs section not found in data.yml"); return new HashMap<>(); } diff --git a/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java b/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java index 05d738c..bc78b0f 100644 --- a/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java +++ b/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java @@ -9,7 +9,6 @@ import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.block.Sign; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -59,18 +58,7 @@ public final class TrackedSignManager { TrackedSign trackedSign = trackedSigns.get(signLocation); trackedSigns.remove(signLocation); saveTrackedSigns(); - if (!PaidSigns.getInstance().areRefundsEnabled() || !refund) { - return; - } - OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(trackedSign.getPlayerId()); - double refundSum = trackedSign.getCost() / 100 * PaidSigns.getInstance().getRefundPercentage(); - EconomyManager.deposit(offlinePlayer, refundSum); - if (offlinePlayer instanceof Player player) { - player.sendMessage(String.format(StringFormatter.replacePlaceholders( - StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_REFUNDED), - new String[]{"{cost}", "{unit}"}, new String[]{"%.2f", "%s"}), refundSum, - EconomyManager.getCurrency(refundSum != 1))); - } + refund(trackedSign, refund); } /** @@ -82,17 +70,12 @@ public final class TrackedSignManager { trackedSigns = new HashMap<>(); if (signSection == null) { - PaidSigns.getInstance().getLogger().log(Level.WARNING, "Tracked signs section not found in data.yml"); + PaidSigns.getInstance().getLogger().log(Level.INFO, "Tracked signs section not found in data.yml"); return; } for (String key : signSection.getKeys(false)) { - try { - loadSign(signSection, key); - } catch (InvalidConfigurationException e) { - PaidSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load sign " + key + ": " + - e.getMessage()); - } + loadSign(signSection, key); } } @@ -101,9 +84,8 @@ public final class TrackedSignManager { * * @param signSection

The configuration section containing signs

* @param key

The sign key which is also the sign's location

- * @throws InvalidConfigurationException

If unable to load the sign

*/ - private static void loadSign(ConfigurationSection signSection, String key) throws InvalidConfigurationException { + private static void loadSign(ConfigurationSection signSection, String key) { String[] locationParts = key.split(","); Location signLocation; try { @@ -111,20 +93,23 @@ public final class TrackedSignManager { Double.parseDouble(locationParts[1]), Double.parseDouble(locationParts[2]), Double.parseDouble(locationParts[3])); } catch (NumberFormatException exception) { - throw new InvalidConfigurationException("Invalid sign coordinates"); - } - - //Prevent destroyed signs from being tracked indefinitely - if (!(signLocation.getBlock().getState() instanceof Sign)) { - PaidSigns.getInstance().getLogger().log(Level.WARNING, "The sign at " + signLocation + " no longer " + - "exists. Removing from sign tracker."); + PaidSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load tracked sign " + key + ": " + + exception.getMessage()); return; } double cost = signSection.getDouble(key + ".cost"); UUID playerId = UUID.fromString(Objects.requireNonNull(signSection.getString(key + ".playerId"))); - TrackedSign trackedSign = new TrackedSign(playerId, cost); + + //Prevent destroyed signs from being tracked indefinitely + if (!(signLocation.getBlock().getState() instanceof Sign)) { + PaidSigns.getInstance().getLogger().log(Level.WARNING, "The sign at " + signLocation + " no longer " + + "exists. Removing from sign tracker. Refunding the player."); + refund(trackedSign, true); + return; + } + trackedSigns.put(signLocation, trackedSign); } @@ -147,4 +132,25 @@ public final class TrackedSignManager { configuration.save(signsFile); } + /** + * Refunds the player for the sign if refunds are enabled and refund is set to true + * + * @param trackedSign

The tracked sign to refund for

+ * @param refund

Whether to actually refund

+ */ + private static void refund(TrackedSign trackedSign, boolean refund) { + if (!PaidSigns.getInstance().areRefundsEnabled() || !refund) { + return; + } + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(trackedSign.getPlayerId()); + double refundSum = trackedSign.getCost() / 100 * PaidSigns.getInstance().getRefundPercentage(); + EconomyManager.deposit(offlinePlayer, refundSum); + if (offlinePlayer instanceof Player player) { + player.sendMessage(String.format(StringFormatter.replacePlaceholders( + StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_REFUNDED), + new String[]{"{cost}", "{unit}"}, new String[]{"%.2f", "%s"}), refundSum, + EconomyManager.getCurrency(refundSum != 1))); + } + } + } diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml index 03d8867..9bad783 100644 --- a/src/main/resources/strings.yml +++ b/src/main/resources/strings.yml @@ -22,6 +22,6 @@ en: ERROR_INVALID_INPUT: "&bInvalid input: {input}" ERROR_PAID_SIGN_NOT_FOUND: "&bNo such paid sign exists" ERROR_NO_SUCH_CONDITION: "&bThe paid sign you specified has no condition for line {line}" - ERROR_CANNOT_AFFORD: "&bYou cannot afford to create this sign" + ERROR_CANNOT_AFFORD: "&bYou cannot afford to create this sign. The cost is {cost} {unit}" ERROR_INVALID_REGULAR_EXPRESSION: "&bThe provided regular expression is invalid" ERROR_PROPERTY_NOT_RECOGNIZED: "&bThe property you tried to change was not recognized" \ No newline at end of file