ChestShop-3/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java

250 lines
9.8 KiB
Java
Raw Normal View History

2013-07-13 23:12:02 +02:00
package com.Acrobot.ChestShop.Listeners.Economy.Plugins;
2013-05-30 16:57:54 +02:00
2016-11-21 17:43:09 +01:00
import java.math.BigDecimal;
import java.util.logging.Level;
2016-11-21 17:43:09 +01:00
import javax.annotation.Nullable;
2013-05-30 16:58:42 +02:00
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
2016-11-21 17:43:09 +01:00
2013-05-30 16:58:42 +02:00
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
2013-05-30 16:58:42 +02:00
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.RegisteredServiceProvider;
2016-11-21 17:43:09 +01:00
import com.Acrobot.ChestShop.ChestShop;
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;
2013-05-30 16:58:42 +02:00
2013-05-30 16:57:54 +02:00
/**
2013-05-30 16:58:42 +02:00
* Represents a Vault connector
*
2013-05-30 16:57:54 +02:00
* @author Acrobot
*/
2013-05-30 16:58:42 +02:00
public class VaultListener implements Listener {
private static Economy provider;
2013-05-30 16:58:42 +02:00
2016-11-21 17:43:09 +01:00
private VaultListener(Economy provider) { VaultListener.provider = provider; }
public static Economy getProvider() { return provider; }
2013-05-30 16:58:42 +02:00
2013-07-21 16:51:36 +02:00
public boolean transactionCanFail() {
if (provider == null) {
return false;
}
return provider.getName().equals("Gringotts")
|| provider.getName().equals("GoldIsMoney")
|| provider.getName().equals("MultiCurrency")
|| provider.getName().equalsIgnoreCase("TheNewEconomy");
2013-07-21 16:51:36 +02:00
}
2013-05-30 16:58:42 +02:00
/**
* Creates a new VaultListener and returns it (if possible)
*
* @return VaultListener
*/
public static @Nullable VaultListener initializeVault() {
2013-07-22 21:37:59 +02:00
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
return null;
}
2013-05-30 16:58:42 +02:00
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
if (rsp == null) {
return null;
}
Economy provider = rsp.getProvider();
if (provider == null) {
return null;
} else {
return new VaultListener(provider);
}
}
@EventHandler
public void onAmountCheck(CurrencyAmountEvent event) {
if (!event.getAmount().equals(BigDecimal.ZERO)) {
return;
}
double balance = 0;
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
2013-08-26 23:36:36 +02:00
if (lastSeen != null) {
try {
balance = provider.getBalance(lastSeen, event.getWorld().getName());
} catch (Exception e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Could not get balance account of " + lastSeen.getUniqueId() + "/" + lastSeen.getName() + "." +
"This is probably due to https://github.com/MilkBowl/Vault/issues/746 and has to be fixed in either Vault directly or your economy plugin." +
"If you are sure it's not this issue then please report the following error.", e);
}
if (balance > Double.MAX_VALUE) {
balance = Double.MAX_VALUE;
}
2013-08-26 23:36:36 +02:00
}
event.setAmount(balance);
}
2013-05-30 16:58:42 +02:00
@EventHandler
public void onCurrencyCheck(CurrencyCheckEvent event) {
2013-07-13 23:12:02 +02:00
if (event.hasEnough()) {
return;
}
2013-05-30 16:58:42 +02:00
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
2013-05-30 16:58:42 +02:00
try {
event.hasEnough(lastSeen != null && provider.has(lastSeen, world.getName(), event.getDoubleAmount()));
} catch (Exception e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Could not check if account of " + lastSeen.getUniqueId() + "/" + lastSeen.getName() + " has " + event.getDoubleAmount() + "." +
"This is probably due to https://github.com/MilkBowl/Vault/issues/746 and has to be fixed in either Vault directly or your economy plugin." +
"If you are sure it's not this issue then please report the following error.", e);
2013-05-30 16:58:42 +02:00
}
}
@EventHandler
public void onAccountCheck(AccountCheckEvent event) {
2013-07-13 23:12:02 +02:00
if (event.hasAccount()) {
return;
}
2013-05-30 16:58:42 +02:00
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
try {
event.hasAccount(lastSeen != null && provider.hasAccount(lastSeen, world.getName()));
} catch (Exception e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Could not check account balance of "+ lastSeen.getUniqueId() + "/" + lastSeen.getName() + "." +
"This is probably due to https://github.com/MilkBowl/Vault/issues/746 and has to be fixed in either Vault directly or your economy plugin." +
"If you are sure it's not this issue then please report the following error.", e);
}
2013-05-30 16:58:42 +02:00
}
@EventHandler
public void onCurrencyFormat(CurrencyFormatEvent event) {
2013-07-13 23:12:02 +02:00
if (!event.getFormattedAmount().isEmpty()) {
return;
}
2013-05-30 16:58:42 +02:00
String formatted = provider.format(event.getDoubleAmount());
event.setFormattedAmount(formatted);
}
@EventHandler
public void onCurrencyAdd(CurrencyAddEvent event) {
2013-07-13 23:12:02 +02:00
if (event.isAdded()) {
return;
}
2013-05-30 16:58:42 +02:00
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getTarget());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
2013-05-30 16:58:42 +02:00
if (lastSeen != null) {
try {
EconomyResponse response = provider.depositPlayer(lastSeen, world.getName(), event.getDoubleAmount());
event.setAdded(response.type == EconomyResponse.ResponseType.SUCCESS);
} catch (Exception e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Could not add money to account of " + lastSeen.getUniqueId() + "/" + lastSeen.getName() + "." +
"This is probably due to https://github.com/MilkBowl/Vault/issues/746 and has to be fixed in either Vault directly or your economy plugin." +
"If you are sure it's not this issue then please report the following error.", e);
}
}
2013-05-30 16:58:42 +02:00
}
@EventHandler
public void onCurrencySubtraction(CurrencySubtractEvent event) {
2013-07-13 23:12:02 +02:00
if (event.isSubtracted()) {
return;
}
2013-05-30 16:58:42 +02:00
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getTarget());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
2013-05-30 16:58:42 +02:00
if (lastSeen != null) {
try {
EconomyResponse response = provider.withdrawPlayer(lastSeen, world.getName(), event.getDoubleAmount());
event.setSubtracted(response.type == EconomyResponse.ResponseType.SUCCESS);
} catch (Exception e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Could not add money to account of " + lastSeen.getUniqueId() + "/" + lastSeen.getName() + "." +
"This is probably due to https://github.com/MilkBowl/Vault/issues/746 and has to be fixed in either Vault directly or your economy plugin." +
"If you are sure it's not this issue then please report the following error.", e);
}
}
2013-05-30 16:58:42 +02:00
}
@EventHandler
public void onCurrencyTransfer(CurrencyTransferEvent event) {
2013-07-13 23:12:02 +02:00
if (event.hasBeenTransferred()) {
return;
}
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(event.getAmount(), event.getSender(), event.getWorld());
onCurrencySubtraction(currencySubtractEvent);
2013-05-30 16:58:42 +02:00
if (!currencySubtractEvent.isSubtracted()) {
return;
}
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(currencySubtractEvent.getAmount(), event.getReceiver(), event.getWorld());
onCurrencyAdd(currencyAddEvent);
event.setTransferred(currencyAddEvent.isAdded());
}
2013-05-30 16:58:42 +02:00
@EventHandler
public void onCurrencyHoldCheck(CurrencyHoldEvent event) {
2014-06-14 20:46:59 +02:00
if (event.getAccount() == null || !transactionCanFail()) {
2013-07-21 16:51:36 +02:00
return;
}
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
String world = event.getWorld().getName();
if (lastSeen == null) {
event.canHold(false);
return;
}
try {
if (!provider.hasAccount(lastSeen, world)) {
event.canHold(false);
return;
}
2013-07-21 16:51:36 +02:00
EconomyResponse response = provider.depositPlayer(lastSeen, world, event.getDoubleAmount());
if (!response.transactionSuccess()) {
event.canHold(false);
return;
}
provider.withdrawPlayer(lastSeen, world, event.getDoubleAmount());
} catch (Exception e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Could not check if account of " + lastSeen.getUniqueId() + "/" + lastSeen.getName() + " can hold " + event.getDoubleAmount() + "." +
"This is probably due to https://github.com/MilkBowl/Vault/issues/746 and has to be fixed in either Vault directly or your economy plugin." +
"If you are sure it's not then please report this error to the devs of ChestShop.", e);
}
2013-05-30 16:58:42 +02:00
}
2013-05-30 16:57:54 +02:00
}