From 167c0f2b5e0c183068c9998e64871b74034bbfb9 Mon Sep 17 00:00:00 2001 From: cnr Date: Mon, 25 Jul 2016 22:10:58 -0500 Subject: [PATCH] Reimplement EnergyShopButton with new machinery --- .../core/shop/item/SalesPackageProcessor.java | 27 ------- .../clans/economy/GoldPurchaseProcessor.java | 74 +++++++++++++++++++ .../clans/shop/energy/EnergyShopButton.java | 37 ++++------ 3 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldPurchaseProcessor.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/shop/item/SalesPackageProcessor.java b/Plugins/Mineplex.Core/src/mineplex/core/shop/item/SalesPackageProcessor.java index d7c75985f..8fd1e6fd2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/shop/item/SalesPackageProcessor.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/shop/item/SalesPackageProcessor.java @@ -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() - { - @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; diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldPurchaseProcessor.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldPurchaseProcessor.java new file mode 100644 index 000000000..43df9ade0 --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/economy/GoldPurchaseProcessor.java @@ -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; + } + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/shop/energy/EnergyShopButton.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/shop/energy/EnergyShopButton.java index cea8d672a..b28c2558f 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/shop/energy/EnergyShopButton.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/shop/energy/EnergyShopButton.java @@ -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( - _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)); } }