From 7c01b434e6fe0f6c1f17290dc61cf89c0ab2a3b4 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 29 Nov 2022 00:11:25 +0000 Subject: [PATCH] Fix placeholder on home slots economy warning, close #278 --- .../net/william278/huskhomes/HuskHomes.java | 37 ++++++++++++++----- .../huskhomes/command/SetHomeCommand.java | 9 ++++- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index 84bb746f..f90c0dd6 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -226,17 +226,36 @@ public interface HuskHomes { @NotNull Set getPluginHooks(); + /** + * Finds the {@link PluginHook} of the given class instance from the set of active {@link PluginHook}s + * + * @param hookClass the class of the {@link PluginHook} to get + * @param the type of the {@link PluginHook} + * @return the {@link PluginHook} instance, or an empty {@link Optional} if not found + */ + default Optional getHook(@NotNull Class hookClass) { + return getPluginHooks().stream() + .filter(hook -> hook.getClass().isInstance(hookClass)) + .findFirst() + .map(hookClass::cast); + } + /** * Gets the {@link MapHook} being used to display public homes and warps on a web map, if there is one, and it is enabled * - * @return the {@link MapHook} being used, if there is one, and it is enabled + * @return the {@link MapHook} optionally being used */ default Optional getMapHook() { - return getSettings().doMapHook ? getPluginHooks() - .stream() - .filter(pluginHook -> pluginHook instanceof MapHook) - .findFirst() - .map(hook -> (MapHook) hook) : Optional.empty(); + return getSettings().doMapHook ? getHook(MapHook.class) : Optional.empty(); + } + + /** + * Gets the {@link EconomyHook} being used to charge players for commands, if there is one, and it is enabled + * + * @return the {@link EconomyHook} optionally being used + */ + default Optional getEconomyHook() { + return getSettings().economy ? getHook(EconomyHook.class) : Optional.empty(); } /** @@ -250,8 +269,7 @@ public interface HuskHomes { default boolean validateEconomyCheck(@NotNull OnlineUser player, @NotNull Settings.EconomyAction action) { final Optional cost = getSettings().getEconomyCost(action).map(Math::abs); if (cost.isPresent() && !player.hasPermission(Permission.BYPASS_ECONOMY_CHECKS.node)) { - final Optional hook = getPluginHooks().stream().filter(pluginHook -> - pluginHook instanceof EconomyHook).findFirst().map(pluginHook -> (EconomyHook) pluginHook); + final Optional hook = getEconomyHook(); if (hook.isPresent()) { if (cost.get() > hook.get().getPlayerBalance(player)) { getLocales().getLocale("error_insufficient_funds", hook.get().formatCurrency(cost.get())) @@ -274,8 +292,7 @@ public interface HuskHomes { final Optional cost = getSettings().getEconomyCost(action).map(Math::abs); if (cost.isPresent() && !player.hasPermission(Permission.BYPASS_ECONOMY_CHECKS.node)) { - final Optional hook = getPluginHooks().stream().filter(pluginHook -> - pluginHook instanceof EconomyHook).findFirst().map(pluginHook -> (EconomyHook) pluginHook); + final Optional hook = getEconomyHook(); if (hook.isPresent()) { hook.get().changePlayerBalance(player, -cost.get()); getLocales().getLocale(action.confirmationLocaleId, hook.get().formatCurrency(cost.get())) diff --git a/common/src/main/java/net/william278/huskhomes/command/SetHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/SetHomeCommand.java index 9fd3a55d..fa7d3c47 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SetHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SetHomeCommand.java @@ -71,18 +71,23 @@ public class SetHomeCommand extends CommandBase { final int freeHomes = onlineUser.getFreeHomes(plugin.getSettings().freeHomeSlots, plugin.getSettings().stackPermissionLimits); if (fetchedData.isPresent()) { + final Settings.EconomyAction action = Settings.EconomyAction.ADDITIONAL_HOME_SLOT; newSlotNeeded.set((currentHomes.size() + 1) > (freeHomes + fetchedData.get().homeSlots())); // If a new slot is needed, validate the user has enough funds to purchase one if (newSlotNeeded.get()) { - if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.ADDITIONAL_HOME_SLOT)) { + if (!plugin.validateEconomyCheck(onlineUser, action)) { return; } userDataToUpdate.set(new UserData(onlineUser, (currentHomes.size() + 1) - freeHomes, fetchedData.get().ignoringTeleports(), fetchedData.get().rtpCooldown())); } else { if (currentHomes.size() == freeHomes) { - plugin.getLocales().getLocale("set_home_used_free_slots", Integer.toString(freeHomes)) + plugin.getEconomyHook() + .flatMap(economyHook -> plugin.getSettings().getEconomyCost(action) + .map(economyHook::formatCurrency)) + .flatMap(formatted -> plugin.getLocales().getLocale("set_home_used_free_slots", + Integer.toString(freeHomes), formatted)) .ifPresent(onlineUser::sendMessage); } }