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 ;
2019-03-30 16:09:48 +01:00
import java.util.logging.Level ;
2016-11-21 17:43:09 +01:00
import javax.annotation.Nullable ;
2019-04-25 19:44:22 +02:00
import com.Acrobot.ChestShop.Listeners.Economy.EconomyAdapter ;
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 ;
2016-10-13 18:11:23 +02:00
import org.bukkit.OfflinePlayer ;
2013-05-30 16:58:42 +02:00
import org.bukkit.World ;
import org.bukkit.event.EventHandler ;
2019-04-06 17:24:20 +02:00
import org.bukkit.event.server.ServiceRegisterEvent ;
import org.bukkit.event.server.ServiceUnregisterEvent ;
2013-05-30 16:58:42 +02:00
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
* /
2019-04-25 19:44:22 +02:00
public class VaultListener extends EconomyAdapter {
2019-04-06 17:24:20 +02:00
private RegisteredServiceProvider < Economy > rsp ;
2016-10-13 18:11:23 +02:00
private static Economy provider ;
2013-05-30 16:58:42 +02:00
2019-04-06 17:24:20 +02:00
private VaultListener ( ) {
updateEconomyProvider ( ) ;
}
private void updateEconomyProvider ( ) {
rsp = ChestShop . getBukkitServer ( ) . getServicesManager ( ) . getRegistration ( Economy . class ) ;
if ( rsp ! = null ) {
provider = rsp . getProvider ( ) ;
ChestShop . getBukkitLogger ( ) . log ( Level . INFO , " Using " + provider . getName ( ) + " as the Economy provider now. " ) ;
}
}
private boolean checkSetup ( ) {
if ( provider = = null ) {
ChestShop . getBukkitLogger ( ) . log ( Level . SEVERE , " No Vault compatible Economy plugin found! " ) ;
ChestShop . getBukkitServer ( ) . getPluginManager ( ) . disablePlugin ( ChestShop . getPlugin ( ) ) ;
return false ;
}
return true ;
}
2016-10-13 18:11:23 +02:00
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 ( ) {
2018-07-11 19:09:07 +02:00
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 ;
}
2019-04-06 17:24:20 +02:00
return new VaultListener ( ) ;
}
2013-07-22 21:37:59 +02:00
2019-04-06 17:24:20 +02:00
@EventHandler
public void onServiceRegister ( ServiceRegisterEvent event ) {
if ( event . getProvider ( ) . getProvider ( ) instanceof Economy ) {
updateEconomyProvider ( ) ;
2013-05-30 16:58:42 +02:00
}
2019-04-06 17:24:20 +02:00
}
2013-05-30 16:58:42 +02:00
2019-04-06 17:24:20 +02:00
@EventHandler
public void onServiceUnregister ( ServiceUnregisterEvent event ) {
if ( event . getProvider ( ) . getProvider ( ) instanceof Economy ) {
updateEconomyProvider ( ) ;
2013-05-30 16:58:42 +02:00
}
}
2013-07-13 23:14:10 +02:00
@EventHandler
public void onAmountCheck ( CurrencyAmountEvent event ) {
2019-04-06 17:24:20 +02:00
if ( ! checkSetup ( ) | | ! event . getAmount ( ) . equals ( BigDecimal . ZERO ) ) {
2013-07-13 23:14:10 +02:00
return ;
}
2016-07-07 18:15:43 +02:00
double balance = 0 ;
2018-10-23 20:04:18 +02:00
OfflinePlayer lastSeen = Bukkit . getOfflinePlayer ( event . getAccount ( ) ) ;
2013-08-26 23:36:36 +02:00
2016-07-07 18:15:43 +02:00
if ( lastSeen ! = null ) {
2019-03-30 16:09:48 +01:00
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 ) ;
}
2016-07-07 18:15:43 +02:00
if ( balance > Double . MAX_VALUE ) {
balance = Double . MAX_VALUE ;
}
2019-05-01 18:13:47 +02:00
} else {
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " The server could not get the OfflinePlayer with the UUID " + event . getAccount ( ) + " to check balance? " ) ;
2013-08-26 23:36:36 +02:00
}
2019-09-06 14:31:19 +02:00
event . setAmount ( BigDecimal . valueOf ( balance ) ) ;
2013-07-13 23:14:10 +02:00
}
2013-05-30 16:58:42 +02:00
@EventHandler
public void onCurrencyCheck ( CurrencyCheckEvent event ) {
2019-04-06 17:24:20 +02:00
if ( ! checkSetup ( ) | | event . hasEnough ( ) ) {
2013-07-13 23:12:02 +02:00
return ;
}
2013-05-30 16:58:42 +02:00
World world = event . getWorld ( ) ;
2018-10-23 20:04:18 +02:00
OfflinePlayer lastSeen = Bukkit . getOfflinePlayer ( event . getAccount ( ) ) ;
2013-05-30 16:58:42 +02:00
2019-03-30 16:09:48 +01:00
try {
2019-05-01 18:13:47 +02:00
event . hasEnough ( lastSeen ! = null & & provider . has ( lastSeen , world . getName ( ) , event . getAmount ( ) . doubleValue ( ) ) ) ;
2019-03-30 16:09:48 +01:00
} catch ( Exception e ) {
2020-05-03 03:37:06 +02:00
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " Could not check if account of " + lastSeen . getUniqueId ( ) + " / " + lastSeen . getName ( ) + " has " + event . getAmount ( ) + " . " +
2019-03-30 16:09:48 +01:00
" 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 ) {
2019-04-06 17:24:20 +02:00
if ( ! checkSetup ( ) | | event . hasAccount ( ) ) {
2013-07-13 23:12:02 +02:00
return ;
}
2013-05-30 16:58:42 +02:00
World world = event . getWorld ( ) ;
2016-10-13 18:11:23 +02:00
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
2018-10-23 20:04:18 +02:00
OfflinePlayer lastSeen = Bukkit . getOfflinePlayer ( event . getAccount ( ) ) ;
2016-10-13 18:11:23 +02:00
2019-03-30 16:09:48 +01:00
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 ) {
2019-04-06 17:24:20 +02:00
if ( ! checkSetup ( ) | | ! event . getFormattedAmount ( ) . isEmpty ( ) ) {
2013-07-13 23:12:02 +02:00
return ;
}
2020-05-03 03:37:06 +02:00
String formatted = provider . format ( event . getAmount ( ) . doubleValue ( ) ) ;
2013-05-30 16:58:42 +02:00
event . setFormattedAmount ( formatted ) ;
}
@EventHandler
public void onCurrencyAdd ( CurrencyAddEvent event ) {
2019-04-06 17:24:20 +02:00
if ( ! checkSetup ( ) | | event . isAdded ( ) ) {
2013-07-13 23:12:02 +02:00
return ;
}
2013-05-30 16:58:42 +02:00
World world = event . getWorld ( ) ;
2016-10-13 18:11:23 +02:00
//String lastSeen = NameManager.getLastSeenName(event.getTarget());
2018-10-23 20:04:18 +02:00
OfflinePlayer lastSeen = Bukkit . getOfflinePlayer ( event . getTarget ( ) ) ;
2013-05-30 16:58:42 +02:00
2016-07-07 18:15:43 +02:00
if ( lastSeen ! = null ) {
2019-03-30 16:09:48 +01:00
try {
2019-05-01 18:13:47 +02:00
EconomyResponse response = provider . depositPlayer ( lastSeen , world . getName ( ) , event . getAmount ( ) . doubleValue ( ) ) ;
2019-03-30 16:09:48 +01:00
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 ) ;
}
2019-05-01 18:13:47 +02:00
} else {
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " The server could not get the OfflinePlayer with the UUID " + event . getTarget ( ) + " to add " + event . getAmount ( ) + " ? " ) ;
2016-07-07 18:15:43 +02:00
}
2013-05-30 16:58:42 +02:00
}
@EventHandler
public void onCurrencySubtraction ( CurrencySubtractEvent event ) {
2019-04-06 17:24:20 +02:00
if ( ! checkSetup ( ) | | event . isSubtracted ( ) ) {
2013-07-13 23:12:02 +02:00
return ;
}
2013-05-30 16:58:42 +02:00
World world = event . getWorld ( ) ;
2016-10-13 18:11:23 +02:00
//String lastSeen = NameManager.getLastSeenName(event.getTarget());
2018-10-23 20:04:18 +02:00
OfflinePlayer lastSeen = Bukkit . getOfflinePlayer ( event . getTarget ( ) ) ;
2013-05-30 16:58:42 +02:00
2016-07-07 18:15:43 +02:00
if ( lastSeen ! = null ) {
2019-03-30 16:09:48 +01:00
try {
2019-05-01 18:13:47 +02:00
EconomyResponse response = provider . withdrawPlayer ( lastSeen , world . getName ( ) , event . getAmount ( ) . doubleValue ( ) ) ;
2019-03-30 16:09:48 +01:00
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 ) ;
}
2019-05-01 18:13:47 +02:00
} else {
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " The server could not get the OfflinePlayer with the UUID " + event . getTarget ( ) + " to subtract " + event . getAmount ( ) + " ? " ) ;
2016-07-07 18:15:43 +02:00
}
2013-05-30 16:58:42 +02:00
}
@EventHandler
2018-07-11 19:09:07 +02:00
public void onCurrencyTransfer ( CurrencyTransferEvent event ) {
2019-04-25 19:44:22 +02:00
if ( ! checkSetup ( ) ) {
2013-07-13 23:12:02 +02:00
return ;
}
2019-04-25 19:44:22 +02:00
processTransfer ( event ) ;
2013-07-13 23:14:10 +02:00
}
2013-05-30 16:58:42 +02:00
2013-07-13 23:14:10 +02:00
@EventHandler
public void onCurrencyHoldCheck ( CurrencyHoldEvent event ) {
2020-04-24 15:54:48 +02:00
if ( ! checkSetup ( ) | | event . getAccount ( ) = = null | | ! transactionCanFail ( ) | | event . canHold ( ) ) {
2013-07-21 16:51:36 +02:00
return ;
}
2016-10-13 18:11:23 +02:00
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
2018-10-23 20:04:18 +02:00
OfflinePlayer lastSeen = Bukkit . getOfflinePlayer ( event . getAccount ( ) ) ;
2016-07-07 18:15:43 +02:00
String world = event . getWorld ( ) . getName ( ) ;
if ( lastSeen = = null ) {
event . canHold ( false ) ;
2019-05-01 18:13:47 +02:00
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " The server could not get the OfflinePlayer with the UUID " + event . getAccount ( ) + " to check if it can hold " + event . getAmount ( ) + " ? " ) ;
2016-07-07 18:15:43 +02:00
return ;
}
2019-03-30 16:09:48 +01:00
try {
if ( ! provider . hasAccount ( lastSeen , world ) ) {
event . canHold ( false ) ;
return ;
}
2013-07-21 16:51:36 +02:00
2019-05-01 18:13:47 +02:00
EconomyResponse response = provider . depositPlayer ( lastSeen , world , event . getAmount ( ) . doubleValue ( ) ) ;
2013-07-13 23:14:10 +02:00
2019-03-30 16:09:48 +01:00
if ( ! response . transactionSuccess ( ) ) {
event . canHold ( false ) ;
return ;
}
2013-07-13 23:14:10 +02:00
2019-05-01 18:13:47 +02:00
provider . withdrawPlayer ( lastSeen , world , event . getAmount ( ) . doubleValue ( ) ) ;
2019-03-30 16:09:48 +01:00
} catch ( Exception e ) {
2019-05-01 18:13:47 +02:00
ChestShop . getBukkitLogger ( ) . log ( Level . WARNING , " Could not check if account of " + lastSeen . getUniqueId ( ) + " / " + lastSeen . getName ( ) + " can hold " + event . getAmount ( ) + " . " +
2019-03-30 16:09:48 +01:00
" 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
}