From 8ae72e580341217e85af533f1fafeb758c3db07b Mon Sep 17 00:00:00 2001 From: Daniel V Date: Wed, 11 Jul 2018 13:09:07 -0400 Subject: [PATCH] Feature: Reserve Support (#143) This introduces a new AccountCheckEvent to check if a user actually has an account with the used economy plugin. Also fix CurrencyTransferEvent logic (even though it's not used anywhere currently) --- pom.xml | 11 ++ .../java/com/Acrobot/ChestShop/ChestShop.java | 4 +- .../com/Acrobot/ChestShop/Dependencies.java | 27 ++- .../Economy/Plugins/ReserveListener.java | 179 ++++++++++++++++++ .../Economy/Plugins/VaultListener.java | 15 +- .../Listeners/Player/PlayerInteract.java | 12 +- src/main/resources/plugin.yml | 3 +- 7 files changed, 234 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/ReserveListener.java diff --git a/pom.xml b/pom.xml index 5ba32c2..8512fc3 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,10 @@ local_repo file://${project.basedir}/repo/ + + reserve-repo + https://dl.bintray.com/theneweconomy/java/ + @@ -274,6 +278,13 @@ 1.2.24 provided + + + net.tnemc + Reserve + 0.1.0.10 + provided + diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index 3adcbc0..04911ab 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -99,7 +99,9 @@ public class ChestShop extends JavaPlugin { NameManager.load(); - Dependencies.loadPlugins(); + if (!Dependencies.loadPlugins()) { + return; + } registerEvents(); diff --git a/src/main/java/com/Acrobot/ChestShop/Dependencies.java b/src/main/java/com/Acrobot/ChestShop/Dependencies.java index 169c5d0..6f2dd9c 100644 --- a/src/main/java/com/Acrobot/ChestShop/Dependencies.java +++ b/src/main/java/com/Acrobot/ChestShop/Dependencies.java @@ -2,9 +2,11 @@ package com.Acrobot.ChestShop; import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.ChestShop.Configuration.Properties; +import com.Acrobot.ChestShop.Listeners.Economy.Plugins.ReserveListener; import com.Acrobot.ChestShop.Listeners.Economy.Plugins.VaultListener; import com.Acrobot.ChestShop.Plugins.*; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import net.tnemc.core.Reserve; import org.bukkit.Bukkit; import org.bukkit.event.Listener; import org.bukkit.plugin.Plugin; @@ -15,7 +17,7 @@ import org.bukkit.plugin.PluginManager; * @author Acrobot */ public class Dependencies { - public static void loadPlugins() { + public static boolean loadPlugins() { PluginManager pluginManager = Bukkit.getPluginManager(); for (String dependency : ChestShop.getDependencies()) { @@ -26,19 +28,32 @@ public class Dependencies { } } - loadEconomy(); + return loadEconomy(); } - private static void loadEconomy() { - String plugin = "Vault"; - Listener economy = VaultListener.initializeVault(); + private static boolean loadEconomy() { + String plugin = "none"; + + Listener economy = null; + + if(Bukkit.getPluginManager().getPlugin("Reserve") != null) { + plugin = "Reserve"; + economy = ReserveListener.prepareListener(); + } + + if(Bukkit.getPluginManager().getPlugin("Vault") != null) { + plugin = "Vault"; + economy = VaultListener.initializeVault(); + } if (economy == null) { - return; + ChestShop.getBukkitLogger().severe("No Economy plugin found! You need to install either Vault or Reserve and a compatible economy!"); + return false; } ChestShop.registerListener(economy); ChestShop.getBukkitLogger().info(plugin + " loaded! Found an economy plugin!"); + return true; } private static void initializePlugin(String name, Plugin plugin) { //Really messy, right? But it's short and fast :) diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/ReserveListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/ReserveListener.java new file mode 100644 index 0000000..e2bba2e --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/ReserveListener.java @@ -0,0 +1,179 @@ +package com.Acrobot.ChestShop.Listeners.Economy.Plugins; + +import com.Acrobot.ChestShop.Events.Economy.AccountCheckEvent; +import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent; +import com.Acrobot.ChestShop.Events.Economy.CurrencyAmountEvent; +import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent; +import com.Acrobot.ChestShop.Events.Economy.CurrencyFormatEvent; +import com.Acrobot.ChestShop.Events.Economy.CurrencyHoldEvent; +import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent; +import com.Acrobot.ChestShop.Events.Economy.CurrencyTransferEvent; +import net.tnemc.core.Reserve; +import net.tnemc.core.economy.EconomyAPI; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import javax.annotation.Nullable; +import java.math.BigDecimal; + +/** + * Represents a Reserve connector + * + * @author creatorfromhell + */ +public class ReserveListener implements Listener { + + private static @Nullable EconomyAPI economyAPI; + + public ReserveListener(EconomyAPI api) { + ReserveListener.economyAPI = api; + } + + public static EconomyAPI getProvider() { + return economyAPI; + } + + public boolean provided() { + return economyAPI != null; + } + + public boolean transactionCanFail() { + if (economyAPI == null) { + return false; + } + + return economyAPI.name().equals("Gringotts") + || economyAPI.name().equals("GoldIsMoney") + || economyAPI.name().equals("MultiCurrency") + || economyAPI.name().equalsIgnoreCase("TheNewEconomy"); + } + + public static @Nullable ReserveListener prepareListener() { + if (Bukkit.getPluginManager().getPlugin("Reserve") == null || Reserve.instance().economyProvided()) { + return null; + } + + EconomyAPI api = Reserve.instance().economy(); + + if (api == null) { + return null; + } else { + return new ReserveListener(api); + } + } + + @EventHandler + public void onAmountCheck(CurrencyAmountEvent event) { + if (!event.getAmount().equals(BigDecimal.ZERO)) { + return; + } + final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount()); + + if (lastSeen != null && provided()) { + event.setAmount(economyAPI.getHoldings(event.getAccount(), event.getWorld().getName())); + } + } + + @EventHandler + public void onCurrencyCheck(CurrencyCheckEvent event) { + if (event.hasEnough()) { + return; + } + final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount()); + + if (lastSeen != null && provided()) { + event.hasEnough(economyAPI.hasHoldings(event.getAccount(), + event.getAmount(), + event.getWorld().getName())); + } + } + + @EventHandler + public void onAccountCheck(AccountCheckEvent event) { + if (event.hasAccount()) { + return; + } + final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount()); + event.hasAccount(lastSeen != null && provided() && economyAPI.hasAccount(event.getAccount())); + } + + @EventHandler + public void onCurrencyFormat(CurrencyFormatEvent event) { + if (!event.getFormattedAmount().isEmpty()) { + return; + } + + if (provided()) { + event.setFormattedAmount(economyAPI.format(event.getAmount())); + } + } + + @EventHandler + public void onCurrencyAdd(CurrencyAddEvent event) { + if (event.isAdded()) { + return; + } + final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget()); + + if (lastSeen != null && provided()) { + economyAPI.addHoldings(event.getTarget(), event.getAmount(), event.getWorld().getName()); + } + } + + @EventHandler + public void onCurrencySubtraction(CurrencySubtractEvent event) { + if (event.isSubtracted()) { + return; + } + final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget()); + + if (lastSeen != null && provided()) { + economyAPI.removeHoldings(event.getTarget(), event.getAmount(), event.getWorld().getName()); + } + } + + @EventHandler + public void onCurrencyTransfer(CurrencyTransferEvent event) { + if (event.hasBeenTransferred()) { + return; + } + + CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(event.getAmount(), event.getSender(), event.getWorld()); + onCurrencySubtraction(currencySubtractEvent); + + if (!currencySubtractEvent.isSubtracted()) { + return; + } + + CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(currencySubtractEvent.getAmount(), event.getReceiver(), event.getWorld()); + onCurrencyAdd(currencyAddEvent); + } + + @EventHandler + public void onCurrencyHoldCheck(CurrencyHoldEvent event) { + if (event.getAccount() == null || !transactionCanFail()) { + return; + } + + final OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount()); + + if (lastSeen == null || !provided()) { + event.canHold(false); + return; + } + + final String world = event.getWorld().getName(); + if (!economyAPI.hasAccount(event.getAccount())) { + event.canHold(false); + return; + } + + if (!economyAPI.addHoldings(event.getAccount(), event.getAmount(), world)) { + event.canHold(false); + return; + } + economyAPI.removeHoldings(event.getAccount(), event.getAmount(), world); + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java index 196ebad..97e8313 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java @@ -37,7 +37,14 @@ public class VaultListener implements Listener { public static Economy getProvider() { return provider; } public boolean transactionCanFail() { - return provider.getName().equals("Gringotts") || provider.getName().equals("GoldIsMoney") || provider.getName().equals("MultiCurrency"); + if (provider == null) { + return false; + } + + return provider.getName().equals("Gringotts") + || provider.getName().equals("GoldIsMoney") + || provider.getName().equals("MultiCurrency") + || provider.getName().equalsIgnoreCase("TheNewEconomy"); } /** @@ -159,20 +166,20 @@ public class VaultListener implements Listener { } @EventHandler - public static void onCurrencyTransfer(CurrencyTransferEvent event) { + public void onCurrencyTransfer(CurrencyTransferEvent event) { if (event.hasBeenTransferred()) { return; } CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(event.getAmount(), event.getSender(), event.getWorld()); - ChestShop.callEvent(currencySubtractEvent); + onCurrencySubtraction(currencySubtractEvent); if (!currencySubtractEvent.isSubtracted()) { return; } CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(currencySubtractEvent.getAmount(), event.getReceiver(), event.getWorld()); - ChestShop.callEvent(currencyAddEvent); + onCurrencyAdd(currencyAddEvent); } @EventHandler diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java index 64e2195..ac6efa8 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -5,9 +5,9 @@ import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Containers.AdminInventory; import com.Acrobot.ChestShop.Database.Account; +import com.Acrobot.ChestShop.Events.Economy.AccountCheckEvent; import com.Acrobot.ChestShop.Events.PreTransactionEvent; import com.Acrobot.ChestShop.Events.TransactionEvent; -import com.Acrobot.ChestShop.Listeners.Economy.Plugins.VaultListener; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Plugins.ChestShop; import com.Acrobot.ChestShop.Security; @@ -147,9 +147,13 @@ public class PlayerInteract implements Listener { boolean adminShop = ChestShopSign.isAdminShop(sign); // check if player exists in economy - if (!adminShop && !VaultListener.getProvider().hasAccount(account.getName())) { - player.sendMessage(Messages.prefix(Messages.NO_ECONOMY_ACCOUNT)); - return null; + if (!adminShop) { + AccountCheckEvent event = new AccountCheckEvent(account.getUuid(), player.getWorld()); + Bukkit.getPluginManager().callEvent(event); + if(!event.hasAccount()) { + player.sendMessage(Messages.prefix(Messages.NO_ECONOMY_ACCOUNT)); + return null; + } } Action buy = Properties.REVERSE_BUTTONS ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 609bfdc..8dbeecb 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,8 +4,7 @@ version: '${bukkit.plugin.version}' author: Acrobot authors: ['https://github.com/ChestShop-authors/ChestShop-3/contributors'] description: A chest shop for economy plugins. -depend: [Vault] -softdepend: [LWC, Lockette, Deadbolt, OddItem, WorldGuard, Heroes, SimpleChestLock, Residence, ShowItem] +softdepend: [Vault, Reserve, LWC, Lockette, Deadbolt, OddItem, WorldGuard, Heroes, SimpleChestLock, Residence, ShowItem] commands: iteminfo: