Update and improve economy handling (#1042)
This commit is contained in:
parent
287bf2202f
commit
2df648f50f
|
|
@ -0,0 +1,139 @@
|
||||||
|
package me.ryanhamshire.GriefPrevention;
|
||||||
|
|
||||||
|
import net.milkbowl.vault.economy.Economy;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.server.PluginDisableEvent;
|
||||||
|
import org.bukkit.event.server.PluginEnableEvent;
|
||||||
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listener for events which may result in a change in the active Economy.
|
||||||
|
*/
|
||||||
|
public class EconomyHandler implements Listener
|
||||||
|
{
|
||||||
|
|
||||||
|
private final GriefPrevention instance;
|
||||||
|
private boolean setupDone = false;
|
||||||
|
private EconomyWrapper economy = null;
|
||||||
|
|
||||||
|
public EconomyHandler(GriefPrevention instance)
|
||||||
|
{
|
||||||
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the current Economy inside of a wrapper class.
|
||||||
|
*
|
||||||
|
* @return the current wrapped Economy or null if no Economy is active
|
||||||
|
*/
|
||||||
|
EconomyWrapper getWrapper()
|
||||||
|
{
|
||||||
|
// Attempt to load the Economy if it is not already set up.
|
||||||
|
loadEconomy(false);
|
||||||
|
|
||||||
|
return economy;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EventHandler for PluginEnableEvents in case of an Economy being enabled.
|
||||||
|
*
|
||||||
|
* @param event the PluginEnableEvent
|
||||||
|
*/
|
||||||
|
@EventHandler
|
||||||
|
private void onPluginEnable(PluginEnableEvent event)
|
||||||
|
{
|
||||||
|
loadEconomy(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EventHandler for PluginDisableEvents in case of an Economy being disabled.
|
||||||
|
*
|
||||||
|
* @param event the PluginDisableEvent
|
||||||
|
*/
|
||||||
|
@EventHandler
|
||||||
|
private void onPluginDisable(PluginDisableEvent event)
|
||||||
|
{
|
||||||
|
loadEconomy(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to change economy. If the setup state does not match the
|
||||||
|
* provided value this does nothing to prevent unnecessary loads.
|
||||||
|
*
|
||||||
|
* @param setupState the expected setup state
|
||||||
|
*/
|
||||||
|
private void loadEconomy(boolean setupState)
|
||||||
|
{
|
||||||
|
// If no change is likely, have we already obtained the Economy?
|
||||||
|
if (setupState != setupDone) return;
|
||||||
|
|
||||||
|
// Are we configured to allow transactions?
|
||||||
|
if (!(instance.config_economy_claimBlocksPurchaseCost > 0 || instance.config_economy_claimBlocksSellValue > 0))
|
||||||
|
{
|
||||||
|
finishSetup(false, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure Vault present.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Class.forName("net.milkbowl.vault.economy.Economy");
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException e)
|
||||||
|
{
|
||||||
|
finishSetup(false, "ERROR: GriefPrevention requires Vault for economy integration.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RegisteredServiceProvider<Economy> registration = instance.getServer().getServicesManager().getRegistration(Economy.class);
|
||||||
|
|
||||||
|
// Ensure an Economy is available.
|
||||||
|
if (registration == null)
|
||||||
|
{
|
||||||
|
finishSetup(false, "ERROR: Vault was unable to find a supported economy plugin. Either install a Vault-compatible economy plugin, or set both of the economy config variables to zero.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Economy newEconomy = registration.getProvider();
|
||||||
|
|
||||||
|
// If Economy hasn't changed, do nothing.
|
||||||
|
if (economy != null && economy.getEconomy().equals(newEconomy)) return;
|
||||||
|
|
||||||
|
// Set setupDone false to force log line for changing Economy.
|
||||||
|
setupDone = false;
|
||||||
|
economy = new EconomyWrapper(newEconomy);
|
||||||
|
|
||||||
|
finishSetup(true, "Hooked into economy: " + economy.economy.getName() + ". Ready to buy/sell claim blocks!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finishSetup(boolean ready, String log) {
|
||||||
|
if (!ready) this.economy = null;
|
||||||
|
|
||||||
|
if (log != null && !setupDone) GriefPrevention.AddLogEntry(log);
|
||||||
|
|
||||||
|
this.setupDone = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper class used to prevent Bukkit from logging an error and
|
||||||
|
* preventing registering events for the listener when Vault is not loaded.
|
||||||
|
*/
|
||||||
|
static class EconomyWrapper
|
||||||
|
{
|
||||||
|
|
||||||
|
private final Economy economy;
|
||||||
|
|
||||||
|
private EconomyWrapper(Economy economy)
|
||||||
|
{
|
||||||
|
this.economy = economy;
|
||||||
|
}
|
||||||
|
|
||||||
|
Economy getEconomy()
|
||||||
|
{
|
||||||
|
return this.economy;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -49,7 +49,6 @@ import org.bukkit.inventory.EquipmentSlot;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.bukkit.scheduler.BukkitTask;
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
|
|
@ -176,6 +175,7 @@ public class GriefPrevention extends JavaPlugin
|
||||||
public boolean config_lockDeathDropsInPvpWorlds; //whether players' dropped on death items are protected in pvp worlds
|
public boolean config_lockDeathDropsInPvpWorlds; //whether players' dropped on death items are protected in pvp worlds
|
||||||
public boolean config_lockDeathDropsInNonPvpWorlds; //whether players' dropped on death items are protected in non-pvp worlds
|
public boolean config_lockDeathDropsInNonPvpWorlds; //whether players' dropped on death items are protected in non-pvp worlds
|
||||||
|
|
||||||
|
private EconomyHandler economyHandler;
|
||||||
public int config_economy_claimBlocksMaxBonus; //max "bonus" blocks a player can buy. set to zero for no limit.
|
public int config_economy_claimBlocksMaxBonus; //max "bonus" blocks a player can buy. set to zero for no limit.
|
||||||
public double config_economy_claimBlocksPurchaseCost; //cost to purchase a claim block. set to zero to disable purchase.
|
public double config_economy_claimBlocksPurchaseCost; //cost to purchase a claim block. set to zero to disable purchase.
|
||||||
public double config_economy_claimBlocksSellValue; //return on a sold claim block. set to zero to disable sale.
|
public double config_economy_claimBlocksSellValue; //return on a sold claim block. set to zero to disable sale.
|
||||||
|
|
@ -231,9 +231,6 @@ public class GriefPrevention extends JavaPlugin
|
||||||
private String databasePassword;
|
private String databasePassword;
|
||||||
|
|
||||||
|
|
||||||
//reference to the economy plugin, if economy integration is enabled
|
|
||||||
public static Economy economy = null;
|
|
||||||
|
|
||||||
//how far away to search from a tree trunk for its branch blocks
|
//how far away to search from a tree trunk for its branch blocks
|
||||||
public static final int TREE_RADIUS = 5;
|
public static final int TREE_RADIUS = 5;
|
||||||
|
|
||||||
|
|
@ -361,41 +358,9 @@ public class GriefPrevention extends JavaPlugin
|
||||||
EntityEventHandler entityEventHandler = new EntityEventHandler(this.dataStore, this);
|
EntityEventHandler entityEventHandler = new EntityEventHandler(this.dataStore, this);
|
||||||
pluginManager.registerEvents(entityEventHandler, this);
|
pluginManager.registerEvents(entityEventHandler, this);
|
||||||
|
|
||||||
//if economy is enabled
|
//vault-based economy integration
|
||||||
if (this.config_economy_claimBlocksPurchaseCost > 0 || this.config_economy_claimBlocksSellValue > 0)
|
economyHandler = new EconomyHandler(this);
|
||||||
{
|
pluginManager.registerEvents(economyHandler, this);
|
||||||
//try to load Vault
|
|
||||||
GriefPrevention.AddLogEntry("GriefPrevention requires Vault for economy integration.");
|
|
||||||
GriefPrevention.AddLogEntry("Attempting to load Vault...");
|
|
||||||
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
|
|
||||||
GriefPrevention.AddLogEntry("Vault loaded successfully!");
|
|
||||||
|
|
||||||
//ask Vault to hook into an economy plugin
|
|
||||||
GriefPrevention.AddLogEntry("Looking for a Vault-compatible economy plugin...");
|
|
||||||
if (economyProvider != null)
|
|
||||||
{
|
|
||||||
GriefPrevention.economy = economyProvider.getProvider();
|
|
||||||
|
|
||||||
//on success, display success message
|
|
||||||
if (GriefPrevention.economy != null)
|
|
||||||
{
|
|
||||||
GriefPrevention.AddLogEntry("Hooked into economy: " + GriefPrevention.economy.getName() + ".");
|
|
||||||
GriefPrevention.AddLogEntry("Ready to buy/sell claim blocks!");
|
|
||||||
}
|
|
||||||
|
|
||||||
//otherwise error message
|
|
||||||
else
|
|
||||||
{
|
|
||||||
GriefPrevention.AddLogEntry("ERROR: Vault was unable to find a supported economy plugin. Either install a Vault-compatible economy plugin, or set both of the economy config variables to zero.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//another error case
|
|
||||||
else
|
|
||||||
{
|
|
||||||
GriefPrevention.AddLogEntry("ERROR: Vault was unable to find a supported economy plugin. Either install a Vault-compatible economy plugin, or set both of the economy config variables to zero.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//cache offline players
|
//cache offline players
|
||||||
OfflinePlayer[] offlinePlayers = this.getServer().getOfflinePlayers();
|
OfflinePlayer[] offlinePlayers = this.getServer().getOfflinePlayers();
|
||||||
|
|
@ -1794,7 +1759,8 @@ public class GriefPrevention extends JavaPlugin
|
||||||
else if (cmd.getName().equalsIgnoreCase("buyclaimblocks") && player != null)
|
else if (cmd.getName().equalsIgnoreCase("buyclaimblocks") && player != null)
|
||||||
{
|
{
|
||||||
//if economy is disabled, don't do anything
|
//if economy is disabled, don't do anything
|
||||||
if (GriefPrevention.economy == null)
|
EconomyHandler.EconomyWrapper economyWrapper = economyHandler.getWrapper();
|
||||||
|
if (economyWrapper == null)
|
||||||
{
|
{
|
||||||
GriefPrevention.sendMessage(player, TextMode.Err, Messages.BuySellNotConfigured);
|
GriefPrevention.sendMessage(player, TextMode.Err, Messages.BuySellNotConfigured);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -1813,10 +1779,12 @@ public class GriefPrevention extends JavaPlugin
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Economy economy = economyWrapper.getEconomy();
|
||||||
|
|
||||||
//if no parameter, just tell player cost per block and balance
|
//if no parameter, just tell player cost per block and balance
|
||||||
if (args.length != 1)
|
if (args.length != 1)
|
||||||
{
|
{
|
||||||
GriefPrevention.sendMessage(player, TextMode.Info, Messages.BlockPurchaseCost, String.valueOf(GriefPrevention.instance.config_economy_claimBlocksPurchaseCost), String.valueOf(GriefPrevention.economy.getBalance(player.getName())));
|
GriefPrevention.sendMessage(player, TextMode.Info, Messages.BlockPurchaseCost, String.valueOf(GriefPrevention.instance.config_economy_claimBlocksPurchaseCost), String.valueOf(economy.getBalance(player)));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -1840,7 +1808,7 @@ public class GriefPrevention extends JavaPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
//if the player can't afford his purchase, send error message
|
//if the player can't afford his purchase, send error message
|
||||||
double balance = economy.getBalance(player.getName());
|
double balance = economy.getBalance(player);
|
||||||
double totalCost = blockCount * GriefPrevention.instance.config_economy_claimBlocksPurchaseCost;
|
double totalCost = blockCount * GriefPrevention.instance.config_economy_claimBlocksPurchaseCost;
|
||||||
if (totalCost > balance)
|
if (totalCost > balance)
|
||||||
{
|
{
|
||||||
|
|
@ -1861,7 +1829,7 @@ public class GriefPrevention extends JavaPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
//withdraw cost
|
//withdraw cost
|
||||||
economy.withdrawPlayer(player.getName(), totalCost);
|
economy.withdrawPlayer(player, totalCost);
|
||||||
|
|
||||||
//add blocks
|
//add blocks
|
||||||
playerData.setBonusClaimBlocks(playerData.getBonusClaimBlocks() + blockCount);
|
playerData.setBonusClaimBlocks(playerData.getBonusClaimBlocks() + blockCount);
|
||||||
|
|
@ -1879,7 +1847,8 @@ public class GriefPrevention extends JavaPlugin
|
||||||
else if (cmd.getName().equalsIgnoreCase("sellclaimblocks") && player != null)
|
else if (cmd.getName().equalsIgnoreCase("sellclaimblocks") && player != null)
|
||||||
{
|
{
|
||||||
//if economy is disabled, don't do anything
|
//if economy is disabled, don't do anything
|
||||||
if (GriefPrevention.economy == null)
|
EconomyHandler.EconomyWrapper economyWrapper = economyHandler.getWrapper();
|
||||||
|
if (economyWrapper == null)
|
||||||
{
|
{
|
||||||
GriefPrevention.sendMessage(player, TextMode.Err, Messages.BuySellNotConfigured);
|
GriefPrevention.sendMessage(player, TextMode.Err, Messages.BuySellNotConfigured);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -1936,7 +1905,7 @@ public class GriefPrevention extends JavaPlugin
|
||||||
{
|
{
|
||||||
//compute value and deposit it
|
//compute value and deposit it
|
||||||
double totalValue = blockCount * GriefPrevention.instance.config_economy_claimBlocksSellValue;
|
double totalValue = blockCount * GriefPrevention.instance.config_economy_claimBlocksSellValue;
|
||||||
economy.depositPlayer(player.getName(), totalValue);
|
economyWrapper.getEconomy().depositPlayer(player, totalValue);
|
||||||
|
|
||||||
//subtract blocks
|
//subtract blocks
|
||||||
playerData.setBonusClaimBlocks(playerData.getBonusClaimBlocks() - blockCount);
|
playerData.setBonusClaimBlocks(playerData.getBonusClaimBlocks() - blockCount);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
name: GriefPrevention
|
name: GriefPrevention
|
||||||
main: me.ryanhamshire.GriefPrevention.GriefPrevention
|
main: me.ryanhamshire.GriefPrevention.GriefPrevention
|
||||||
softdepend: [Vault, Multiverse-Core, My_Worlds, MystCraft, Transporter, TheUnderground, WorldGuard, WorldEdit, RoyalCommands, MultiWorld, Denizen, Hyperconomy]
|
softdepend: [Vault, Multiverse-Core, My_Worlds, MystCraft, Transporter, TheUnderground, WorldGuard, WorldEdit, RoyalCommands, MultiWorld, Denizen]
|
||||||
dev-url: https://dev.bukkit.org/projects/grief-prevention
|
dev-url: https://dev.bukkit.org/projects/grief-prevention
|
||||||
loadbefore: [TheUnderground]
|
loadbefore: [TheUnderground]
|
||||||
version: '${project.version}-${git.commit.id.abbrev}'
|
version: '${project.version}-${git.commit.id.abbrev}'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user