Reimplement EnergyShopButton with new machinery

This commit is contained in:
cnr 2016-07-25 22:10:58 -05:00
parent 3cddf35d12
commit 167c0f2b5e
3 changed files with 90 additions and 48 deletions

View File

@ -11,33 +11,6 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
public class SalesPackageProcessor implements ConfirmationProcessor {
/*
int goldCount = _donationManager.Get(_player).getGold();
int cost = _salesItem.getCost(_currencyType);
if (cost > goldCount)
showResultsPage(TransactionResponse.InsufficientFunds);
else
{
_donationManager.rewardGold(new Callback<Boolean>()
{
@Override
public void run(Boolean data)
{
if (data)
{
showResultsPage(TransactionResponse.Success);
_player.playSound(_player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f);
}
else
{
showResultsPage(TransactionResponse.Failed);
}
}
}, "Energy Purchase", _player.getName(), getClientManager().Get(_player).getAccountId(), -cost);
}
*/
private final GlobalCurrency _currencyType;
private final SalesPackageBase _salesItem;
private final DonationManager _donationManager;

View File

@ -0,0 +1,74 @@
package mineplex.game.clans.economy;
import mineplex.core.common.util.C;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.server.util.TransactionResponse;
import mineplex.core.shop.confirmation.ConfirmationCallback;
import mineplex.core.shop.confirmation.ConfirmationProcessor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
public class GoldPurchaseProcessor implements ConfirmationProcessor {
private final Player _player;
private final int _price;
private final GoldManager _goldManager;
private final Runnable _runAfterPurchase;
public GoldPurchaseProcessor(Player player, int price, GoldManager goldManager, Runnable runAfterPurchase)
{
_player = player;
_price = price;
_goldManager = goldManager;
_runAfterPurchase = runAfterPurchase;
}
@Override
public void init(Inventory inventory) {
inventory.setItem(4, new ItemBuilder(ClansCurrency.GOLD.getDisplayMaterial()).setTitle(ClansCurrency.GOLD.getPrefix()).addLore(C.cGray + _price + " " + ClansCurrency.GOLD.getPrefix() + " will be", C.cGray + "deducted from your account balance").build());
}
@Override
public void process(ConfirmationCallback callback) {
int goldCount = _goldManager.Get(_player).getBalance();
if (_price > goldCount)
showResults(callback, TransactionResponse.InsufficientFunds);
else
{
_goldManager.rewardGold(data ->
{
if (data)
{
showResults(callback, TransactionResponse.Success);
_player.playSound(_player.getLocation(), Sound.LEVEL_UP, 2f, 1.5f);
}
else
{
showResults(callback, TransactionResponse.Failed);
}
}, _player, -_price, true);
}
}
private void showResults(ConfirmationCallback callback, TransactionResponse response)
{
switch (response)
{
case Failed:
callback.reject("There was an error processing your request.");
break;
case AlreadyOwns:
callback.reject("You already own this package.");
break;
case InsufficientFunds:
callback.reject("Your account has insufficient funds.");
break;
case Success:
callback.resolve("Your purchase was successful.");
_runAfterPurchase.run();
break;
}
}
}

View File

@ -1,15 +1,24 @@
package mineplex.game.clans.shop.energy;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilServer;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.shop.confirmation.ConfirmationPage;
import mineplex.core.shop.item.IButton;
import mineplex.game.clans.clans.ClanEnergyManager;
import mineplex.game.clans.clans.ClanInfo;
import mineplex.game.clans.clans.event.PreEnergyShopBuyEvent;
import mineplex.game.clans.economy.GoldPurchaseProcessor;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
public class EnergyShopButton implements IButton
{
private static final ItemStack ICON = new ItemBuilder(Material.REDSTONE).setTitle(ChatColor.RESET + "Clan Energy").build();
private ClanEnergyManager _energyManager;
private EnergyPage _page;
private int _energyToPurchase;
@ -33,30 +42,16 @@ public class EnergyShopButton implements IButton
return;
}
/*_page.getShop().openPageForPlayer(player, new ConfirmationPage<ClanEnergyManager, EnergyShop>(
_page.getPlugin(), _page.getShop(), _page.getClientManager(), _page.getDonationManager(), new Runnable()
_page.getShop().openPageForPlayer(player, new ConfirmationPage<>(player, _page, new GoldPurchaseProcessor(player, _cost, _energyManager.getClansManager().getGoldManager(), () ->
{
public void run()
{
_clanInfo.adjustEnergy(_energyToPurchase);
_page.refresh();
_energyManager.runAsync(new Runnable()
{
@Override
public void run()
{
_energyManager.getClansManager().getClanDataAccess().updateEnergy(_clanInfo);
}
});
// Notify
_energyManager.getClansManager().messageClan(_clanInfo, F.main("Energy", F.name(player.getName()) + " purchased " + F.elem(_energyToPurchase + " Energy") + " for the clan"));
}
}, _page, new EnergyPackage(_energyToPurchase + " Energy", _cost), ClansCurrency.GOLD, player));*/
// TODO
_clanInfo.adjustEnergy(_energyToPurchase);
_page.refresh();
_energyManager.runAsync(() -> _energyManager.getClansManager().getClanDataAccess().updateEnergy(_clanInfo));
// Notify
_energyManager.getClansManager().messageClan(_clanInfo, F.main("Energy", F.name(player.getName()) + " purchased " + F.elem(_energyToPurchase + " Energy") + " for the clan"));
}), ICON));
}
}