From def2f4265fed05f3c64f3a56cd205f5c3170deaa Mon Sep 17 00:00:00 2001 From: Kams Date: Sun, 28 Aug 2022 14:35:28 +0200 Subject: [PATCH 1/2] Add PlaceholderAPI support TODO: find a way to make it faster --- pom.xml | 10 +++ .../org/gestern/gringotts/AccountChest.java | 2 +- .../java/org/gestern/gringotts/Gringotts.java | 22 +++++ .../gestern/gringotts/GringottsAccount.java | 67 +++++++++++++++ .../org/gestern/gringotts/api/Account.java | 23 +++++ .../gringotts/api/impl/GringottsEco.java | 61 +++++++++++++ .../PlaceholderAPIDependency.java | 64 ++++++++++++++ .../placeholders/PlaceholdersRegister.java | 86 +++++++++++++++++++ src/main/resources/plugin.yml | 2 +- 9 files changed, 335 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/gestern/gringotts/dependency/placeholdersapi/PlaceholderAPIDependency.java create mode 100644 src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java diff --git a/pom.xml b/pom.xml index f533b897..78ed374d 100755 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,12 @@ provided true + + me.clip + placeholderapi + 2.11.2 + provided + net.tnemc Reserve @@ -165,6 +171,10 @@ glaremasters repo https://repo.glaremasters.me/repository/towny/ + + placeholderapi + https://repo.extendedclip.com/content/repositories/placeholderapi/ + diff --git a/src/main/java/org/gestern/gringotts/AccountChest.java b/src/main/java/org/gestern/gringotts/AccountChest.java index 3444dabf..cd6a4244 100755 --- a/src/main/java/org/gestern/gringotts/AccountChest.java +++ b/src/main/java/org/gestern/gringotts/AccountChest.java @@ -73,7 +73,7 @@ public InventoryHolder chest() { * * @return Location of the storage block of this account chest. */ - private Location chestLocation() { + public Location chestLocation() { Block block = Util.chestBlock(sign); return block != null ? block.getLocation() : null; diff --git a/src/main/java/org/gestern/gringotts/Gringotts.java b/src/main/java/org/gestern/gringotts/Gringotts.java index 51f87557..c793e1bc 100644 --- a/src/main/java/org/gestern/gringotts/Gringotts.java +++ b/src/main/java/org/gestern/gringotts/Gringotts.java @@ -40,6 +40,7 @@ import org.gestern.gringotts.data.Migration; import org.gestern.gringotts.dependency.DependencyProviderImpl; import org.gestern.gringotts.dependency.GenericDependency; +import org.gestern.gringotts.dependency.placeholdersapi.PlaceholderAPIDependency; import org.gestern.gringotts.dependency.towny.TownyDependency; import org.gestern.gringotts.event.AccountListener; import org.gestern.gringotts.event.PlayerVaultListener; @@ -201,6 +202,27 @@ public void onLoad() { ); } + try { + Plugin plugin = this.dependencies.hookPlugin( + "PlaceholderAPI", + "me.clip.placeholderapi.PlaceholderAPIPlugin", + "2.11.0" + ); + + if (plugin != null) { + if (!this.dependencies.registerDependency(new PlaceholderAPIDependency( + this, + plugin + ))) { + getLogger().warning("PlaceholderAPI plugin is already assigned into the dependencies."); + } + } + } catch (IllegalArgumentException e) { + getLogger().warning( + "Looks like PlaceholderAPI plugin is not compatible with Gringotts's code." + ); + } + this.registerGenericDependency( "vault", "Vault", diff --git a/src/main/java/org/gestern/gringotts/GringottsAccount.java b/src/main/java/org/gestern/gringotts/GringottsAccount.java index 711095be..92999e73 100644 --- a/src/main/java/org/gestern/gringotts/GringottsAccount.java +++ b/src/main/java/org/gestern/gringotts/GringottsAccount.java @@ -1,6 +1,7 @@ package org.gestern.gringotts; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.block.ShulkerBox; @@ -93,6 +94,33 @@ public long getVaultBalance() { return getTimeout(countChestInventories()); } + /** + * Current balance this account has in chest(s) in cents + * + * @return current balance this account has in chest(s) in cents + */ + public long getVaultBalance(int index) { + return getTimeout(countChestInventory(index)); + } + + /** + * Current balance this account has in chest(s) in cents + * + * @return current balance this account has in chest(s) in cents + */ + public Location getVaultLocation(int index) { + return getTimeout(countChestLocation(index)); + } + + + public List getVaultChests() { + return getTimeout(getChests()); + } + + public int getVaultCount() { + return getVaultChests().size(); + } + /** * Current balance this account has in inventory in cents * @@ -363,6 +391,45 @@ private CompletableFuture countChestInventories() { return callSync(callMe); } + private CompletableFuture countChestInventory(int index) { + Callable callMe = () -> { + List chests = dao.retrieveChests(this); + + if (CONF.usevaultContainer && index < chests.size() && index >= 0) { + return chests.get(index).balance(); + } + + Optional playerOpt = playerOwner(); + if (playerOpt.isPresent()) { + Player player = playerOpt.get(); + + if (CONF.usevaultEnderchest && USE_VAULT_ENDERCHEST.isAllowed(player) && index == -1) { + return new AccountInventory(player.getEnderChest()).balance(); + } + } + return -1L; + }; + + return callSync(callMe); + } + + private CompletableFuture countChestLocation(int index) { + Callable callMe = () -> { + List chests = dao.retrieveChests(this); + + if (CONF.usevaultContainer && index < chests.size() && index >= 0) { + return chests.get(index).chestLocation(); + } + return null; + }; + + return callSync(callMe); + } + + private CompletableFuture> getChests() { + return callSync(() -> dao.retrieveChests(this)); + } + private CompletableFuture countPlayerInventory() { Callable callMe = () -> { long balance = 0; diff --git a/src/main/java/org/gestern/gringotts/api/Account.java b/src/main/java/org/gestern/gringotts/api/Account.java index 5f6ab1d0..0bbde5bd 100644 --- a/src/main/java/org/gestern/gringotts/api/Account.java +++ b/src/main/java/org/gestern/gringotts/api/Account.java @@ -1,5 +1,7 @@ package org.gestern.gringotts.api; +import org.bukkit.Location; + /** * Defines actions possible on an account in an economy. */ @@ -43,6 +45,27 @@ public interface Account { */ double vaultBalance(); + /** + * Return the vault at index balance of this account. + * + * @return the vault at index balance of this account. + */ + double vaultBalance(int index); + + /** + * Return the vault at index location of this account. + * + * @return the vault at index location of this account. + */ + Location vaultLocation(int index); + + /** + * Return the number of vault of this account. + * + * @return the number of vault of this account. + */ + int vaultCount(); + /** * Return the inventory balance of this account. * diff --git a/src/main/java/org/gestern/gringotts/api/impl/GringottsEco.java b/src/main/java/org/gestern/gringotts/api/impl/GringottsEco.java index 0aec2b37..98d357f9 100644 --- a/src/main/java/org/gestern/gringotts/api/impl/GringottsEco.java +++ b/src/main/java/org/gestern/gringotts/api/impl/GringottsEco.java @@ -1,6 +1,7 @@ package org.gestern.gringotts.api.impl; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.gestern.gringotts.AccountInventory; @@ -258,6 +259,36 @@ public double vaultBalance() { return 0; } + /** + * Vault balance double. + * + * @return the double + */ + @Override + public double vaultBalance(int index) { + return 0; + } + + /** + * Vault location. + * + * @return the location + */ + @Override + public Location vaultLocation(int index) { + return null; + } + + /** + * Vault count. + * + * @return the int + */ + @Override + public int vaultCount() { + return 0; + } + /** * Inv balance double. * @@ -562,6 +593,36 @@ public double vaultBalance() { return CONF.getCurrency().getDisplayValue(acc.getVaultBalance()); } + /** + * Vault balance double. + * + * @return the double + */ + @Override + public double vaultBalance(int index) { + return CONF.getCurrency().getDisplayValue(acc.getVaultBalance(index)); + } + + /** + * Vault location. + * + * @return the location + */ + @Override + public Location vaultLocation(int index) { + return acc.getVaultLocation(index); + } + + /** + * Vault count. + * + * @return the int + */ + @Override + public int vaultCount() { + return acc.getVaultCount(); + } + /** * Inv balance double. * diff --git a/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/PlaceholderAPIDependency.java b/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/PlaceholderAPIDependency.java new file mode 100644 index 00000000..1d5e2abb --- /dev/null +++ b/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/PlaceholderAPIDependency.java @@ -0,0 +1,64 @@ +package org.gestern.gringotts.dependency.placeholdersapi; + +import me.clip.placeholderapi.PlaceholderAPIPlugin; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; +import org.gestern.gringotts.Gringotts; +import org.gestern.gringotts.api.dependency.Dependency; +import org.gestern.gringotts.dependency.placeholdersapi.placeholders.PlaceholdersRegister; + +public class PlaceholderAPIDependency implements Dependency, Listener { + + private final Gringotts gringotts; + private final PlaceholderAPIPlugin plugin; + private final String id; + + /** + * Instantiates a new PlaceholderAPI dependency. + * + * @param gringotts the gringotts + * @param plugin the plugin + */ + public PlaceholderAPIDependency(Gringotts gringotts, + Plugin plugin) { + if (plugin == null) { + throw new NullPointerException("'plugin' is null"); + } + + if (!(plugin instanceof PlaceholderAPIPlugin)) { + throw new IllegalArgumentException( + "The 'plugin' needs to be an instance of me.clip.placeholderapi.PlaceholderAPIPlugin" + ); + } + + this.gringotts = gringotts; + this.plugin = (PlaceholderAPIPlugin) plugin; + this.id = "placeholderapi"; + } + + + /** + * Gets id. + * + * @return the id + */ + @Override + public String getId() { + return id; + } + + /** + * Gets plugin. + * + * @return the plugin + */ + @Override + public Plugin getPlugin() { + return this.plugin; + } + + @Override + public void onEnable() { + new PlaceholdersRegister(gringotts).register(); + } +} diff --git a/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java b/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java new file mode 100644 index 00000000..9d0440c7 --- /dev/null +++ b/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java @@ -0,0 +1,86 @@ +package org.gestern.gringotts.dependency.placeholdersapi.placeholders; + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.gestern.gringotts.Gringotts; +import org.gestern.gringotts.api.Account; +import org.gestern.gringotts.api.impl.GringottsEco; +import org.jetbrains.annotations.NotNull; + +public class PlaceholdersRegister extends PlaceholderExpansion { + + private final GringottsEco eco; + + public PlaceholdersRegister(Gringotts plugin) { + eco = (GringottsEco) plugin.getEco(); + } + + @Override + public @NotNull String getIdentifier() { + return "gringotts"; + } + + @Override + public @NotNull String getAuthor() { + return "github.com/CamillePele"; + } + + @Override + public @NotNull String getVersion() { + return "1.0.0"; + } + + @Override + public String onRequest(OfflinePlayer player, String paramString) { + String[] params = paramString.split("_"); + Account account = eco.player(player.getUniqueId()); + + if(params[0].equalsIgnoreCase("balance") || params[0].equalsIgnoreCase("money")) { + + if (params.length == 2) { + if (params[1].equalsIgnoreCase("vault")) { + return String.valueOf(account.vaultBalance()); + } + if (params[1].equalsIgnoreCase("inventory")) { + return String.valueOf(account.invBalance()); + } + } + else if (params.length == 1) { + return String.valueOf(account.balance()); + } + } + else if (params[0].equalsIgnoreCase("vault") && params.length >= 2) { + + if (params[1].equalsIgnoreCase("count")) { + return String.valueOf(account.vaultCount()); + } + else if (params.length == 3) { + try { + int index = Integer.parseInt(params[1]); + if (index >= account.vaultCount()) { + return "out of bounds"; + } + + + if (params[2].equalsIgnoreCase("location") || params[2].equalsIgnoreCase("position")) { + Location loc = account.vaultLocation(index); + if (loc.getWorld() == null) { + return (int)loc.getX() + ", " + (int)loc.getY() + ", " + (int)loc.getZ(); + } + return loc.getWorld().getName() + ", " + (int)loc.getX() + ", " + (int)loc.getY() + ", " + (int)loc.getZ(); + } + + if (params[2].equalsIgnoreCase("balance")) { + return String.valueOf(account.vaultBalance(index)); + } + + } catch (NumberFormatException e) { + return "invalid index"; + } + } + } + + return null; // Placeholder is unknown by the Expansion + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ba98b6b7..840d1c81 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -12,7 +12,7 @@ api-version: "1.15" awareness: - !@UTF-8 -softdepend: [Towny] +softdepend: [Towny, PlaceholderAPI] loadbefore: [Reserve, Vault] commands: From 1f9f14dcebf0b2707c5cf65ead264e8b35738c02 Mon Sep 17 00:00:00 2001 From: Kams Date: Sun, 28 Aug 2022 16:41:26 +0200 Subject: [PATCH 2/2] Fix error when no vault --- .../placeholdersapi/placeholders/PlaceholdersRegister.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java b/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java index 9d0440c7..0eb252d2 100644 --- a/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java +++ b/src/main/java/org/gestern/gringotts/dependency/placeholdersapi/placeholders/PlaceholdersRegister.java @@ -65,6 +65,9 @@ else if (params.length == 3) { if (params[2].equalsIgnoreCase("location") || params[2].equalsIgnoreCase("position")) { Location loc = account.vaultLocation(index); + if (loc == null) { + return null; + } if (loc.getWorld() == null) { return (int)loc.getX() + ", " + (int)loc.getY() + ", " + (int)loc.getZ(); }