Add economical listeners

This commit is contained in:
Acrobot 2013-05-30 16:58:42 +02:00
parent 2620b56555
commit 2c0207adb1
2 changed files with 152 additions and 2 deletions

View File

@ -1,7 +1,68 @@
package com.Acrobot.ChestShop.Listeners.Economy;
import com.Acrobot.ChestShop.Events.Economy.*;
import com.nijikokun.register.payment.forChestShop.Method;
import com.nijikokun.register.payment.forChestShop.Methods;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import javax.annotation.Nullable;
/**
* Represents a Register connector
*
* @author Acrobot
*/
public class RegisterListener {
public class RegisterListener implements Listener {
private Method paymentMethod;
private RegisterListener(Method paymentMethod) {
this.paymentMethod = paymentMethod;
}
public static @Nullable RegisterListener initializeRegister() {
Method method = Methods.load();
if (method == null) {
return null;
}
return new RegisterListener(method);
}
@EventHandler
public void onCurrencyCheck(CurrencyCheckEvent event) {
paymentMethod.getAccount(event.getAccount()).hasEnough(event.getDoubleAmount());
}
@EventHandler
public void onAccountCheck(AccountCheckEvent event) {
paymentMethod.hasAccount(event.getAccount());
}
@EventHandler
public void onCurrencyFormat(CurrencyFormatEvent event) {
String formatted = paymentMethod.format(event.getDoubleAmount());
event.setFormattedAmount(formatted);
}
@EventHandler
public void onCurrencyAdd(CurrencyAddEvent event) {
paymentMethod.getAccount(event.getTarget()).add(event.getDoubleAmount());
}
@EventHandler
public void onCurrencySubtract(CurrencySubtractEvent event) {
paymentMethod.getAccount(event.getTarget()).subtract(event.getDoubleAmount());
}
@EventHandler
public void onCurrencyTransfer(CurrencyTransferEvent event) {
boolean subtracted = paymentMethod.getAccount(event.getSender()).subtract(event.getDoubleAmount());
if (subtracted) {
paymentMethod.getAccount(event.getReceiver()).add(event.getDoubleAmount());
}
}
}

View File

@ -1,7 +1,96 @@
package com.Acrobot.ChestShop.Listeners.Economy;
import com.Acrobot.ChestShop.Events.Economy.*;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.RegisteredServiceProvider;
import javax.annotation.Nullable;
/**
* Represents a Vault connector
*
* @author Acrobot
*/
public class VaultListener {
public class VaultListener implements Listener {
private final Economy provider;
private VaultListener(Economy provider) {
this.provider = provider;
}
/**
* Creates a new VaultListener and returns it (if possible)
*
* @return VaultListener
*/
public static @Nullable VaultListener initializeVault() {
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 onCurrencyCheck(CurrencyCheckEvent event) {
World world = event.getWorld();
if (!provider.has(event.getAccount(), world.getName(), event.getDoubleAmount())) {
event.hasEnough(false);
}
}
@EventHandler
public void onAccountCheck(AccountCheckEvent event) {
World world = event.getWorld();
if (!provider.hasAccount(event.getAccount(), world.getName())) {
event.setOutcome(false);
}
}
@EventHandler
public void onCurrencyFormat(CurrencyFormatEvent event) {
String formatted = provider.format(event.getDoubleAmount());
event.setFormattedAmount(formatted);
}
@EventHandler
public void onCurrencyAdd(CurrencyAddEvent event) {
World world = event.getWorld();
provider.depositPlayer(event.getTarget(), world.getName(), event.getDoubleAmount());
}
@EventHandler
public void onCurrencySubtraction(CurrencySubtractEvent event) {
World world = event.getWorld();
provider.withdrawPlayer(event.getTarget(), world.getName(), event.getDoubleAmount());
}
@EventHandler
public void onCurrencyTransfer(CurrencyTransferEvent event) {
World world = event.getWorld();
EconomyResponse response = provider.withdrawPlayer(event.getSender(), world.getName(), event.getDoubleAmount());
if (response.transactionSuccess()) {
provider.depositPlayer(event.getReceiver(), world.getName(), event.getDoubleAmount());
}
}
}