From 50da7139476a989b3a6555ad7733917749bb8405 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 8 May 2017 22:01:27 +0100 Subject: [PATCH] Remove the previous upgrade when switching upgrades --- .../game/games/moba/gold/GoldManager.java | 12 ++++++ .../arcade/game/games/moba/kit/HeroKit.java | 38 +---------------- .../game/games/moba/kit/common/SkillBow.java | 23 ++++++++++ .../games/moba/kit/common/SkillSword.java | 24 +++++++++++ .../arcade/game/games/moba/shop/MobaShop.java | 42 +++++++++++++++---- .../games/moba/shop/MobaShopCategoryMenu.java | 8 ++-- .../game/games/moba/shop/MobaShopMenu.java | 5 +++ 7 files changed, 103 insertions(+), 49 deletions(-) diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/gold/GoldManager.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/gold/GoldManager.java index cfd386e62..79edc6baa 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/gold/GoldManager.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/gold/GoldManager.java @@ -13,6 +13,7 @@ import nautilus.game.arcade.game.games.moba.structure.point.CapturePoint; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; import java.util.Collections; import java.util.HashMap; @@ -74,6 +75,12 @@ public class GoldManager implements Listener } } + @EventHandler + public void playerQuit(PlayerQuitEvent event) + { + _playerGold.remove(event.getPlayer()); + } + @EventHandler public void passiveGain(UpdateEvent event) { @@ -105,6 +112,11 @@ public class GoldManager implements Listener } } + public int getGold(Player player) + { + return _playerGold.get(player); + } + public void addGold(Player player, int amount) { _playerGold.put(player, _playerGold.get(player) + amount); diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/HeroKit.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/HeroKit.java index cf31fe78e..ce57121ca 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/HeroKit.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/HeroKit.java @@ -2,30 +2,22 @@ package nautilus.game.arcade.game.games.moba.kit; import mineplex.core.common.util.C; import mineplex.core.common.util.F; -import mineplex.core.common.util.UtilItem; import mineplex.core.common.util.UtilPlayer; import mineplex.core.itemstack.ItemBuilder; import mineplex.core.recharge.Recharge; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import nautilus.game.arcade.ArcadeManager; -import nautilus.game.arcade.game.games.moba.Moba; import nautilus.game.arcade.game.games.moba.MobaRole; -import nautilus.game.arcade.game.games.moba.kit.common.SkillBow; -import nautilus.game.arcade.game.games.moba.kit.common.SkillSword; -import nautilus.game.arcade.game.games.moba.shop.MobaItem; import nautilus.game.arcade.kit.Kit; import nautilus.game.arcade.kit.KitAvailability; import nautilus.game.arcade.kit.Perk; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; -import java.util.List; - public class HeroKit extends Kit { @@ -147,10 +139,7 @@ public class HeroKit extends Kit inventory.setItem(AMMO_SLOT, _ammo); inventory.setItem(RECALL_SLOT, RECALL_ITEM); - Moba host = (Moba) Manager.GetGame(); - List ownedItems = host.getShop().getOwnedItems(player); - - perkLoop: for (Perk perk : GetPerks()) + for (Perk perk : GetPerks()) { if (!(perk instanceof HeroSkill)) { @@ -159,31 +148,6 @@ public class HeroKit extends Kit HeroSkill skill = (HeroSkill) perk; - // Sword upgrades - if (skill instanceof SkillSword) - { - for (MobaItem item : ownedItems) - { - if (UtilItem.isSword(item.getItem())) - { - player.getInventory().setItem(skill.getSlot(), item.getItem()); - continue perkLoop; - } - } - } - // Bow upgrades - else if (skill instanceof SkillBow) - { - for (MobaItem item : ownedItems) - { - if (item.getItem().getType() == Material.BOW) - { - player.getInventory().setItem(skill.getSlot(), item.getItem()); - continue perkLoop; - } - } - } - if (skill.isOnCooldown(player)) { player.getInventory().setItem(skill.getSlot(), skill.getCooldownItem()); diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillBow.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillBow.java index 3ce67db5e..3edfe97a5 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillBow.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillBow.java @@ -1,9 +1,14 @@ package nautilus.game.arcade.game.games.moba.kit.common; +import nautilus.game.arcade.game.games.moba.Moba; import nautilus.game.arcade.game.games.moba.kit.HeroSkill; +import nautilus.game.arcade.game.games.moba.shop.MobaItem; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.List; + public class SkillBow extends HeroSkill { @@ -17,4 +22,22 @@ public class SkillBow extends HeroSkill { super("Bow", DESCRIPTION, SKILL_ITEM, slot, null); } + + @Override + public void giveItem(Player player) + { + Moba host = (Moba) Manager.GetGame(); + List ownedItems = host.getShop().getOwnedItems(player); + + for (MobaItem item : ownedItems) + { + if (item.getItem().getType() == Material.BOW) + { + player.getInventory().setItem(getSlot(), item.getItem()); + return; + } + } + + player.getInventory().setItem(getSlot(), SKILL_ITEM); + } } diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillSword.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillSword.java index 864c35188..8b892c4eb 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillSword.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/kit/common/SkillSword.java @@ -1,12 +1,18 @@ package nautilus.game.arcade.game.games.moba.kit.common; import mineplex.core.common.util.C; +import mineplex.core.common.util.UtilItem; import mineplex.core.itemstack.ItemBuilder; +import nautilus.game.arcade.game.games.moba.Moba; import nautilus.game.arcade.game.games.moba.kit.HeroSkill; +import nautilus.game.arcade.game.games.moba.shop.MobaItem; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.List; + public class SkillSword extends HeroSkill { @@ -23,4 +29,22 @@ public class SkillSword extends HeroSkill { super("Sword", DESCRIPTION, SKILL_ITEM, slot, null); } + + @Override + public void giveItem(Player player) + { + Moba host = (Moba) Manager.GetGame(); + List ownedItems = host.getShop().getOwnedItems(player); + + for (MobaItem item : ownedItems) + { + if (UtilItem.isSword(item.getItem())) + { + player.getInventory().setItem(getSlot(), item.getItem()); + return; + } + } + + player.getInventory().setItem(getSlot(), SKILL_ITEM); + } } diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShop.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShop.java index 0518b8c21..fd0933d45 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShop.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShop.java @@ -1,18 +1,18 @@ package nautilus.game.arcade.game.games.moba.shop; -import mineplex.core.common.Rank; import mineplex.core.common.util.*; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.minecraft.game.core.combat.CombatComponent; import mineplex.minecraft.game.core.combat.event.CombatDeathEvent; +import mineplex.minecraft.game.core.damage.CustomDamageEvent; import nautilus.game.arcade.events.GameStateChangeEvent; -import nautilus.game.arcade.game.DebugCommand; import nautilus.game.arcade.game.Game.GameState; import nautilus.game.arcade.game.games.moba.Moba; import nautilus.game.arcade.game.games.moba.MobaPlayer; import nautilus.game.arcade.game.games.moba.MobaRole; import nautilus.game.arcade.game.games.moba.kit.CooldownCalculateEvent; +import nautilus.game.arcade.game.games.moba.shop.MobaShopMenu.MobaCategoryButton; import nautilus.game.arcade.game.games.moba.shop.assassin.MobaAssassinShop; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity; @@ -22,6 +22,7 @@ import org.bukkit.entity.Player; import org.bukkit.entity.Villager; import org.bukkit.entity.Villager.Profession; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; @@ -109,16 +110,14 @@ public class MobaShop implements Listener } @EventHandler - public void npcDamage(EntityDamageEvent event) + public void npcDamage(CustomDamageEvent event) { - for (Entry entry : _entities.entrySet()) + for (LivingEntity entity : _entities.keySet()) { - LivingEntity entity = entry.getKey(); - - if (entity.equals(event.getEntity())) + if (entity.equals(event.GetDamageeEntity())) { entity.setFireTicks(0); - event.setCancelled(true); + event.SetCancelled("Shop NPC"); return; } } @@ -165,9 +164,18 @@ public class MobaShop implements Listener public void purchaseItem(Player player, MobaItem item) { + List owned = _upgrades.get(player); + MobaShopCategory category = getCategory(item); + + if (!category.isAllowingMultiple()) + { + owned.removeIf(previousItem -> getCategory(previousItem) == category); + } + player.sendMessage(F.main("Game", "Purchased " + F.greenElem(item.getItem().getItemMeta().getDisplayName()) + ".")); _host.getGoldManager().removeGold(player, item.getCost()); - _upgrades.get(player).add(item); + owned.add(item); + _host.GetKit(player).ApplyKit(player); } public boolean ownsItem(Player player, MobaItem item) @@ -180,6 +188,22 @@ public class MobaShop implements Listener return _upgrades.get(player); } + private MobaShopCategory getCategory(MobaItem item) + { + for (MobaShopMenu menu : _roleMenus.values()) + { + for (MobaShopCategory category : menu.getCategories()) + { + if (category.getItems().contains(item)) + { + return category; + } + } + } + + return null; + } + /* Handle MobaItem events */ diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopCategoryMenu.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopCategoryMenu.java index 3dd51cc3a..69477ad5b 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopCategoryMenu.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopCategoryMenu.java @@ -41,20 +41,22 @@ public class MobaShopCategoryMenu extends Menu ItemBuilder builder = new ItemBuilder(item.getItem()); boolean owns = _shop.ownsItem(player, item); boolean canPurchase = _host.getGoldManager().hasGold(player, item.getCost()); + int gold = _host.getGoldManager().getGold(player); builder.setTitle((canPurchase ? C.cGreen : C.cRed) + item.getItem().getItemMeta().getDisplayName()); + builder.addLore(""); if (owns) { - builder.addLore(C.cRed + "You already have purchased this upgrade"); + builder.addLore(C.cRed + "You already have purchased this upgrade."); } else { - builder.addLore("Cost: " + C.cGold + item.getCost()); + builder.addLore(C.cWhite + "Cost: " + C.cGold + item.getCost(), C.cWhite + "Your Gold: " + C.cGold + gold, ""); if (canPurchase) { - builder.addLore(C.cGreen + "Click to purchase"); + builder.addLore(C.cGreen + "Click to purchase."); } else { diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopMenu.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopMenu.java index 5c3d4582e..515445f82 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopMenu.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/moba/shop/MobaShopMenu.java @@ -121,4 +121,9 @@ public class MobaShopMenu extends Menu new MobaShopCategoryMenu(_host, _shop, _category, getPlugin()).open(player); } } + + public List getCategories() + { + return _categories; + } }