Created
December 16, 2012 22:28
-
-
Save anonymous/4313728 to your computer and use it in GitHub Desktop.
A ready-to-go Satoshis/Vault interaction system. Favors Satoshis over Vault, but will fall back if need-be.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import me.meta1203.plugins.satoshis.Satoshis; | |
import me.meta1203.plugins.satoshis.SatoshisEconAPI; | |
import net.milkbowl.vault.economy.Economy; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.entity.Player; | |
import org.bukkit.plugin.Plugin; | |
import org.bukkit.plugin.RegisteredServiceProvider; | |
public class ShopEconAPI { | |
private boolean useVault = false; | |
private Economy vault = null; | |
private SatoshisEconAPI satoshis = null; | |
public SatoshisEconAPI getSatoshisEconomy() { | |
Plugin p = Bukkit.getServer().getPluginManager().getPlugin("Satoshis"); | |
if (p == null || !(p instanceof Satoshis)) { | |
return null; | |
} | |
return Satoshis.econ; | |
} | |
public Econ() { | |
useVault = getSatoshisEconomy() == null; | |
if (useVault) { | |
RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); | |
if (economyProvider != null) { | |
vault = economyProvider.getProvider(); | |
} else { | |
Satellitetrade.log.severe("No economy plugin found! Disabling!"); | |
Bukkit.getPluginManager().disablePlugin(Bukkit.getPluginManager().getPlugin("SatelliteTrade")); | |
} | |
} else { | |
satoshis = getSatoshisEconomy(); | |
} | |
} | |
public String format(double value) { | |
if (useVault) { | |
return vault.format(value); | |
} else { | |
return satoshis.formatValue(value, false); | |
} | |
} | |
public boolean transact(Player from, Player to, double amount) { | |
if (useVault) { | |
if (!vault.has(from.getName(), amount)) { | |
from.sendMessage(ChatColor.RED + "You do not have that much money!"); | |
return false; | |
} | |
vault.withdrawPlayer(from.getName(), amount); | |
vault.depositPlayer(to.getName(), amount); | |
from.sendMessage(ChatColor.GREEN + "Sent " + format(amount) + " from " + from.getName() + " to " + to.getName()); | |
to.sendMessage(ChatColor.GREEN + "Sent " + format(amount) + " from " + from.getName() + " to " + to.getName()); | |
return true; | |
} else { | |
if (!satoshis.hasMoney(from.getName(), amount + (satoshis.buyerorseller ? satoshis.priceOfTax(amount) : 0.0))) { | |
from.sendMessage(ChatColor.RED + "You do not have that much money!" + (satoshis.buyerorseller ? " [Tax is: " + format(satoshis.priceOfTax(amount)) + "]" : "")); | |
return false; | |
} | |
satoshis.transact(from.getDisplayName(), to.getName(), amount); | |
from.sendMessage(ChatColor.GREEN + "Sent " + format(amount) + " from " + from.getName() + " to " + to.getName()); | |
to.sendMessage(ChatColor.GREEN + "Sent " + format(amount) + " from " + from.getName() + " to " + to.getName()); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment