Mineplex2018-withcommit/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldManager.java

170 lines
4.9 KiB
Java

package mineplex.game.clans.economy;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.game.clans.items.economy.GoldToken;
import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.CurrencyType;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.creature.Creature;
import mineplex.core.donation.DonationManager;
import mineplex.core.donation.Donor;
import mineplex.core.energy.Energy;
import mineplex.game.clans.Clans;
import mineplex.game.clans.clans.ClansManager;
import mineplex.game.clans.fields.repository.FieldRepository;
import mineplex.game.clans.items.generation.WeightSet;
import mineplex.game.clans.shop.bank.BankShop;
import mineplex.minecraft.game.core.condition.ConditionFactory;
import mineplex.minecraft.game.core.condition.ConditionManager;
public class GoldManager extends MiniPlugin
{
public static final int GEM_CONVERSION_RATE = 50; // The number of gold coins when converted from a single gem
public static final double DEATH_TAX = 0.04d; // Percentage of gold lost on death
private static GoldManager _instance;
public static GoldManager getInstance() { return _instance; }
private DonationManager _donationManager;
private TransferTracker _transferTracker;
private BankShop _bankShop;
public GoldManager(ClansManager plugin, CoreClientManager clientManager, DonationManager donationManager)
{
super("Clans Gold", plugin.getPlugin());
_instance = this;
_donationManager = donationManager;
_transferTracker = new TransferTracker();
_bankShop = new BankShop(plugin, clientManager, donationManager);
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event)
{
Player player = event.getEntity();
Player killer = player.getKiller();
int gold = getGold(player);
int droppedGold = (int) (gold * DEATH_TAX);
if (droppedGold > 0)
{
deductGold(player, droppedGold);
notify(player, String.format("You dropped %d gold on your death!", droppedGold));
if (killer != null)
{
addGold(killer, droppedGold);
notify(killer, String.format("You looted %d gold off of %s's corpse!", droppedGold, player.getName()));
}
}
}
@EventHandler
public void playerCmd(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().startsWith("/gold"))
{
notify(event.getPlayer(), "Your Balance is " + C.cYellow + getGold(event.getPlayer()) + "g");
addGold(event.getPlayer(), 100000);
event.setCancelled(true);
}
}
public int getGold(Player player)
{
return getDonor(player).getGold();
}
public int getGems(Player player)
{
return getDonor(player).GetGems();
}
public void transferGemsToCoins(Player player, int gemAmount)
{
int gemCount = getGems(player);
int goldCount = gemAmount * GEM_CONVERSION_RATE;
if (gemCount >= gemAmount)
{
deductGems(player, gemAmount);
addGold(player, goldCount);
notify(player, String.format("You have transferred %d gems into %d gold coins!", gemCount, goldCount));
}
}
/**
* @param player - the player to be checked for whether they can transfer gems into coins
* @return true, if the player has not converted gems into coins within the
* last day, false otherwise.
*/
public boolean canTransferGems(Player player)
{
return !_transferTracker.hasTransferredToday(player);
}
public void addGold(Player player, int amount)
{
_donationManager.RewardGoldLater("GoldManager", player, amount);
}
public void deductGold(Player player, int amount)
{
addGold(player, -amount);
}
public void cashIn(Player player, GoldToken token)
{
int value = token.getGoldValue();
addGold(player, value);
notify(player, String.format("You have cashed in a gold token worth %dg!", value));
}
public void purchaseToken(Player player, int tokenValue)
{
GoldToken token = new GoldToken(tokenValue);
deductGold(player, tokenValue);
player.getInventory().addItem(token.toItemStack());
notify(player, String.format("You have purchased a gold token worth %dg!", tokenValue));
}
public void addGems(Player player, int amount)
{
_donationManager.RewardGemsLater("GoldManager", player, amount);
}
public void deductGems(Player player, int amount)
{
addGems(player, -amount);
}
private Donor getDonor(Player player)
{
return _donationManager.Get(player.getName());
}
private void notify(Player player, String message)
{
UtilPlayer.message(player, F.main("Gold", message));
}
}