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)
This commit is contained in:
Daniel V 2018-07-11 13:09:07 -04:00 committed by Max Lee
parent 7311907559
commit 8ae72e5803
7 changed files with 234 additions and 17 deletions

11
pom.xml
View File

@ -49,6 +49,10 @@
<id>local_repo</id>
<url>file://${project.basedir}/repo/</url>
</repository>
<repository>
<id>reserve-repo</id>
<url>https://dl.bintray.com/theneweconomy/java/</url>
</repository>
</repositories>
<dependencies>
@ -274,6 +278,13 @@
<version>1.2.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.tnemc</groupId>
<artifactId>Reserve</artifactId>
<version>0.1.0.10</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -99,7 +99,9 @@ public class ChestShop extends JavaPlugin {
NameManager.load();
Dependencies.loadPlugins();
if (!Dependencies.loadPlugins()) {
return;
}
registerEvents();

View File

@ -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 :)

View File

@ -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);
}
}

View File

@ -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

View File

@ -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;

View File

@ -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: