The cost to insert
+ * @param messageThe original message to replace the cost placeholder for
+ * @returnThe 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 signSectionThe configuration section containing signs
* @param keyThe sign key which is also the sign's location
- * @throws InvalidConfigurationExceptionIf 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 trackedSignThe tracked sign to refund for
+ * @param refundWhether 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