From 255b66c4bde23337c10055ed86c9b9dc45f8b3aa Mon Sep 17 00:00:00 2001 From: William Date: Tue, 23 Nov 2021 00:52:04 +0000 Subject: [PATCH] Added ability to define maximum public homes through permission nodes, Close #91 --- .../huskhomes2/commands/EditHomeCommand.java | 51 +++++++++++-------- src/main/resources/languages/en-gb.yml | 3 +- src/main/resources/languages/it-it.yml | 3 +- src/main/resources/languages/pl.yml | 1 + src/main/resources/languages/zh-cn.yml | 3 +- src/main/resources/languages/zh-tw.yml | 3 +- 6 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java b/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java index 33ee4678..d9e12e7f 100644 --- a/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/EditHomeCommand.java @@ -4,11 +4,7 @@ import de.themoep.minedown.MineDown; import me.william278.huskhomes2.EditingHandler; import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.MessageManager; -import me.william278.huskhomes2.api.events.PlayerChangeHomeDescriptionEvent; -import me.william278.huskhomes2.api.events.PlayerMakeHomePrivateEvent; -import me.william278.huskhomes2.api.events.PlayerMakeHomePublicEvent; -import me.william278.huskhomes2.api.events.PlayerRelocateHomeEvent; -import me.william278.huskhomes2.api.events.PlayerRenameHomeEvent; +import me.william278.huskhomes2.api.events.*; import me.william278.huskhomes2.data.DataManager; import me.william278.huskhomes2.integrations.VaultIntegration; import me.william278.huskhomes2.teleport.points.Home; @@ -30,6 +26,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.logging.Level; +import java.util.stream.Stream; public class EditHomeCommand extends CommandBase implements TabCompleter { @@ -237,21 +234,6 @@ public class EditHomeCommand extends CommandBase implements TabCompleter { if (makingHomePublic) { // Making a private home if (!privacyUpdatingHome.isPublic()) { - if (HuskHomes.getSettings().doEconomy()) { - double publicHomeCost = HuskHomes.getSettings().getPublicHomeCost(); - if (publicHomeCost > 0) { - if (!VaultIntegration.takeMoney(p, publicHomeCost)) { - MessageManager.sendMessage(p, "error_insufficient_funds", VaultIntegration.format(publicHomeCost)); - return; - } else { - MessageManager.sendMessage(p, "edit_home_privacy_public_success_economy", privacyUpdatingHome.getName(), VaultIntegration.format(publicHomeCost)); - } - } else { - MessageManager.sendMessage(p, "edit_home_privacy_public_success", privacyUpdatingHome.getName()); - } - } else { - MessageManager.sendMessage(p, "edit_home_privacy_public_success", privacyUpdatingHome.getName()); - } PlayerMakeHomePublicEvent makeHomePublicEvent = new PlayerMakeHomePublicEvent(p, privacyUpdatingHome); Bukkit.getScheduler().runTask(plugin, () -> { Bukkit.getPluginManager().callEvent(makeHomePublicEvent); @@ -259,6 +241,35 @@ public class EditHomeCommand extends CommandBase implements TabCompleter { return; } Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + // Check the player is allowed to set an additional public home + try (Connection connection = HuskHomes.getConnection()) { + final Stream playerHomes = DataManager.getPlayerHomes(p.getName(), connection).stream().filter(Home::isPublic); + final int publicHomeLimit = Home.getPublicHomeLimit(p); + if ((playerHomes.count() + 1) > publicHomeLimit) { + MessageManager.sendMessage(p, "error_edit_home_maximum_public_homes", Integer.toString(publicHomeLimit)); + return; + } + } catch (SQLException e) { + plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred editing a home's privacy."); + return; + } + + // Check the player has enough money to make the home public + if (HuskHomes.getSettings().doEconomy()) { + double publicHomeCost = HuskHomes.getSettings().getPublicHomeCost(); + if (publicHomeCost > 0) { + if (!VaultIntegration.takeMoney(p, publicHomeCost)) { + MessageManager.sendMessage(p, "error_insufficient_funds", VaultIntegration.format(publicHomeCost)); + return; + } else { + MessageManager.sendMessage(p, "edit_home_privacy_public_success_economy", privacyUpdatingHome.getName(), VaultIntegration.format(publicHomeCost)); + } + } else { + MessageManager.sendMessage(p, "edit_home_privacy_public_success", privacyUpdatingHome.getName()); + } + } else { + MessageManager.sendMessage(p, "edit_home_privacy_public_success", privacyUpdatingHome.getName()); + } try (Connection connection = HuskHomes.getConnection()) { DataManager.updateHomePrivacy(privacyUpdatingHome.getOwnerUsername(), privacyUpdatingHome.getName(), true, connection); PublicHomeCommand.updatePublicHomeTabCache(); diff --git a/src/main/resources/languages/en-gb.yml b/src/main/resources/languages/en-gb.yml index 41dd21e3..66b420c2 100644 --- a/src/main/resources/languages/en-gb.yml +++ b/src/main/resources/languages/en-gb.yml @@ -118,4 +118,5 @@ error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring error_no_offline_position: '[Error:](#ff3300) [Could not find where %1% last logged out.](#ff7e5e)' error_invalid_player: '[Error:](#ff3300) [Please specify the name of a valid player!](#ff7e5e)' error_no_homes_set_other: '[Error:](#ff3300) [%1% hasn''t set any homes!](#ff7e5e)' -error_home_invalid_other: '[Error:](#ff3300) [%1% doesn''t have a home set named "%2%"](#ff7e5e)' \ No newline at end of file +error_home_invalid_other: '[Error:](#ff3300) [%1% doesn''t have a home set named "%2%"](#ff7e5e)' +error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)' \ No newline at end of file diff --git a/src/main/resources/languages/it-it.yml b/src/main/resources/languages/it-it.yml index 150ef1ce..3e774d54 100644 --- a/src/main/resources/languages/it-it.yml +++ b/src/main/resources/languages/it-it.yml @@ -118,4 +118,5 @@ error_invalid_player: '[Errore:](#ff3300) [Specifica un nome valido!](#ff7e5e)' error_no_homes_set_other: '[Errore:](#ff3300) [%1% non ha nessuna home!](#ff7e5e)' error_home_invalid_other: '[Errore:](#ff3300) [%1% non ha una home chiamata "%2%"](#ff7e5e)' home_deleted_other: '[Hai cancellato la home](#00fb9a)[%2%](#00fb9a bold)[di %1%](#00fb9a) ' -edit_home_privacy_private_other: '• Accesso: &#ff7e5e&Privato &7(Solo %1% può usarla)' \ No newline at end of file +edit_home_privacy_private_other: '• Accesso: &#ff7e5e&Privato &7(Solo %1% può usarla)' +error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)' \ No newline at end of file diff --git a/src/main/resources/languages/pl.yml b/src/main/resources/languages/pl.yml index e8fe0358..d680292b 100644 --- a/src/main/resources/languages/pl.yml +++ b/src/main/resources/languages/pl.yml @@ -119,3 +119,4 @@ error_no_homes_set_other: '[Error:](#ff3300) [%1% nie ma żadnych domów!](#ff7e error_home_invalid_other: '[Error:](#ff3300) [%1% nie ma domu nazwanego "%2%"](#ff7e5e)' home_deleted_other: '[Pomyślnie usunięto dom %1%,](#00fb9a) [%2%](#00fb9a bold)' edit_home_privacy_private_other: '• Dostęp: &#ff7e5e&Prywatny &7(Tylko %1% może to użyć)' +error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)' \ No newline at end of file diff --git a/src/main/resources/languages/zh-cn.yml b/src/main/resources/languages/zh-cn.yml index 632e818b..fd2e767f 100644 --- a/src/main/resources/languages/zh-cn.yml +++ b/src/main/resources/languages/zh-cn.yml @@ -118,4 +118,5 @@ error_invalid_player: '[错误:](#ff3300) [请指定一个有效的玩家名称! error_no_homes_set_other: '[错误:](#ff3300) [%1% 没有设置任何一个家!](#ff7e5e)' error_home_invalid_other: '[错误:](#ff3300) [%1% 没有叫做 "%2%" 的家](#ff7e5e)' home_deleted_other: '[成功删除了 %1% 的名字叫](#00fb9a) [%2%](#00fb9a bold) [的家](#00fb9a)' -edit_home_privacy_private_other: '• 隐私性: &#ff7e5e&私有的 &7(只有 %1% 可以使用该家)' \ No newline at end of file +edit_home_privacy_private_other: '• 隐私性: &#ff7e5e&私有的 &7(只有 %1% 可以使用该家)' +error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)' \ No newline at end of file diff --git a/src/main/resources/languages/zh-tw.yml b/src/main/resources/languages/zh-tw.yml index 88cab5c8..1e242f86 100644 --- a/src/main/resources/languages/zh-tw.yml +++ b/src/main/resources/languages/zh-tw.yml @@ -118,4 +118,5 @@ error_home_invalid_other: '[Error:](#ff3300) [%1% doesn''t have a home set named home_deleted_other: '[Successfully deleted %1%''s home,](#00fb9a) [%2%](#00fb9a bold)' edit_home_privacy_private_other: '• Access: &#ff7e5e&Private &7(Only %1% can use it)' edit_home_title_other: '\n[Edit home details for %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[:](#00fb9a)' -command_list_item: '[/%1%](#00fb9a show_text=�fb9a&%2% suggest_command=/%1%) [•](white) [%3%](gray)' \ No newline at end of file +command_list_item: '[/%1%](#00fb9a show_text=�fb9a&%2% suggest_command=/%1%) [•](white) [%3%](gray)' +error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a maximum of %1% homes public.](#ff7e5e)' \ No newline at end of file