From b14b37540bc7fd74c83fe62cdf356641d5c6bf1d Mon Sep 17 00:00:00 2001 From: Luca Date: Fri, 26 Nov 2021 19:09:53 +0100 Subject: [PATCH] Fixed Shops not taking items :D --- .../java/rip/tilly/bedwars/game/Game.java | 6 -- .../tilly/bedwars/menus/shop/ShopButton.java | 67 ++++++++++-------- .../tilly/bedwars/upgrades/UpgradeItem.java | 70 ++++++++++--------- .../rip/tilly/bedwars/utils/PlayerUtil.java | 14 ++++ .../utils/menusystem/menu/UpgradesMenu.java | 10 +-- 5 files changed, 91 insertions(+), 76 deletions(-) diff --git a/src/main/java/rip/tilly/bedwars/game/Game.java b/src/main/java/rip/tilly/bedwars/game/Game.java index ae441e8..b21ffdb 100644 --- a/src/main/java/rip/tilly/bedwars/game/Game.java +++ b/src/main/java/rip/tilly/bedwars/game/Game.java @@ -18,10 +18,8 @@ import rip.tilly.bedwars.game.arena.CopiedArena; import rip.tilly.bedwars.generators.Generator; import rip.tilly.bedwars.generators.GeneratorTier; import rip.tilly.bedwars.generators.GeneratorType; -import rip.tilly.bedwars.playerdata.PlayerData; import rip.tilly.bedwars.utils.CC; import rip.tilly.bedwars.utils.CustomLocation; -import rip.tilly.bedwars.utils.LocationUtils; import rip.tilly.bedwars.utils.TimeUtils; import java.util.*; @@ -121,10 +119,6 @@ public class Game { ++this.durationTimer; } - public boolean isPartyMatch() { - return (this.teams.get(0).getAllPlayers().size() >= 2 || this.teams.get(1).getAllPlayers().size() >= 2); - } - public void addSpectator(UUID uuid) { this.spectators.add(uuid); } diff --git a/src/main/java/rip/tilly/bedwars/menus/shop/ShopButton.java b/src/main/java/rip/tilly/bedwars/menus/shop/ShopButton.java index 961e1b1..70c7545 100644 --- a/src/main/java/rip/tilly/bedwars/menus/shop/ShopButton.java +++ b/src/main/java/rip/tilly/bedwars/menus/shop/ShopButton.java @@ -10,6 +10,7 @@ import rip.tilly.bedwars.BedWars; import rip.tilly.bedwars.playerdata.PlayerData; import rip.tilly.bedwars.utils.CC; import rip.tilly.bedwars.utils.ItemBuilder; +import rip.tilly.bedwars.utils.PlayerUtil; import rip.tilly.bedwars.utils.menu.Button; import java.util.ArrayList; @@ -83,36 +84,46 @@ public class ShopButton extends Button { } } - PlayerData playerData = BedWars.getInstance().getPlayerDataManager().getPlayerData(player.getUniqueId()); - if (costItems >= cost) { - for (ItemStack contents : player.getInventory().getContents()) { - if (contents != null) { - if (contents.getType() == costType) { - if (contents.getAmount() == cost) { - player.getInventory().removeItem(contents); - } else { - contents.setAmount(contents.getAmount() - cost); - } - } - } - } - if (color) { - player.getInventory().addItem(new ItemBuilder(material) - .amount(amount) - .durability(playerData.getPlayerTeam().getColorData()) - .hideFlags() - .build()); - } else { - player.getInventory().addItem(new ItemBuilder(material) - .amount(amount) - .durability(data) - .hideFlags() - .build()); - } - playNeutral(player); - } else { + if (costItems < cost) { player.sendMessage(CC.translate("&cYou don't have enough " + costTypeName + "!")); playFail(player); + return; } + + PlayerData playerData = BedWars.getInstance().getPlayerDataManager().getPlayerData(player.getUniqueId()); + + int finalCost = cost; + for (ItemStack i : player.getInventory().getContents()) { + if (i == null) { + continue; + } + if (i.getType() == costType) { + if (i.getAmount() < finalCost) { + finalCost -= i.getAmount(); + PlayerUtil.minusAmount(player, i, i.getAmount()); + player.updateInventory(); + } else { + PlayerUtil.minusAmount(player, i, finalCost); + player.updateInventory(); + break; + } + } + } + + if (color) { + player.getInventory().addItem(new ItemBuilder(material) + .amount(amount) + .durability(playerData.getPlayerTeam().getColorData()) + .hideFlags() + .build()); + } else { + player.getInventory().addItem(new ItemBuilder(material) + .amount(amount) + .durability(data) + .hideFlags() + .build()); + } + + playNeutral(player); } } diff --git a/src/main/java/rip/tilly/bedwars/upgrades/UpgradeItem.java b/src/main/java/rip/tilly/bedwars/upgrades/UpgradeItem.java index 601c2e5..05172cc 100644 --- a/src/main/java/rip/tilly/bedwars/upgrades/UpgradeItem.java +++ b/src/main/java/rip/tilly/bedwars/upgrades/UpgradeItem.java @@ -8,10 +8,10 @@ import rip.tilly.bedwars.game.GameTeam; import rip.tilly.bedwars.playerdata.currentgame.TeamUpgrades; import rip.tilly.bedwars.utils.CC; import rip.tilly.bedwars.utils.ItemBuilder; +import rip.tilly.bedwars.utils.PlayerUtil; import java.util.Arrays; import java.util.List; -import java.util.ListIterator; import java.util.stream.Collectors; public class UpgradeItem { @@ -54,8 +54,18 @@ public class UpgradeItem { lore.add(CC.translate("")); + int costItems = 0; + for (ItemStack contents : player.getInventory().getContents()) { + if (contents != null) { + if (contents.getType() == Material.DIAMOND) { + costItems += contents.getAmount(); + } + } + } + int cost = this.upgrade.getCostForLevel(level); + if (teamUpgrades.getCostToUpgrade(this.upgrade) != -1) { - if (this.canBuy(player, level + 1)) { + if (costItems >= cost) { lore.add(CC.translate("&aClick to purchase")); } else { lore.add(CC.translate("&cNot enough diamonds")); @@ -70,28 +80,34 @@ public class UpgradeItem { } public void buy(Player player, int level, Game game, GameTeam gameTeam, TeamUpgrades teamUpgrades) { - if (!this.canBuy(player, level)) { + int costItems = 0; + for (ItemStack contents : player.getInventory().getContents()) { + if (contents != null) { + if (contents.getType() == Material.DIAMOND) { + costItems += contents.getAmount(); + } + } + } + + int cost = this.upgrade.getCostForLevel(level); + if (costItems < cost) { + player.sendMessage(CC.translate("&cYou don't have enough Diamonds!")); return; } - int price = this.upgrade.getCostForLevel(level); - int removed = 0; - - for (ListIterator listIterator = player.getInventory().iterator(); listIterator.hasNext();) { - ItemStack itemStack = listIterator.next(); - - if (itemStack != null && itemStack.getType() == Material.DIAMOND) { - if (itemStack.getAmount() >= price) { - removed += itemStack.getAmount() - price; - - itemStack.setAmount(itemStack.getAmount() - price); + int finalCost = cost; + for (ItemStack i : player.getInventory().getContents()) { + if (i == null) { + continue; + } + if (i.getType() == Material.DIAMOND) { + if (i.getAmount() < finalCost) { + finalCost -= i.getAmount(); + PlayerUtil.minusAmount(player, i, i.getAmount()); + player.updateInventory(); } else { - removed += itemStack.getAmount(); - - itemStack.setAmount(0); - } - - if (price <= removed) { + PlayerUtil.minusAmount(player, i, finalCost); + player.updateInventory(); break; } } @@ -100,20 +116,6 @@ public class UpgradeItem { teamUpgrades.upgrade(player, game, gameTeam, this.upgrade); } - public boolean canBuy(Player player, int level) { - int amount = 0; - - for (ListIterator listIterator = player.getInventory().iterator(); listIterator.hasNext();) { - ItemStack itemStack = listIterator.next(); - - if (itemStack != null && itemStack.getType() == Material.DIAMOND) { - amount += itemStack.getAmount(); - } - } - - return amount >= this.upgrade.getCostForLevel(level); - } - public Upgrade getUpgrade() { return this.upgrade; } diff --git a/src/main/java/rip/tilly/bedwars/utils/PlayerUtil.java b/src/main/java/rip/tilly/bedwars/utils/PlayerUtil.java index 8f5a75c..aed7f6e 100644 --- a/src/main/java/rip/tilly/bedwars/utils/PlayerUtil.java +++ b/src/main/java/rip/tilly/bedwars/utils/PlayerUtil.java @@ -3,6 +3,7 @@ package rip.tilly.bedwars.utils; import org.bukkit.GameMode; import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; public class PlayerUtil { @@ -28,4 +29,17 @@ public class PlayerUtil { ((CraftPlayer) player).getHandle().getDataWatcher().watch(9, (byte) 0); player.updateInventory(); } + + public static void minusAmount(Player p, ItemStack i, int amount) { + if (i.getAmount() - amount <= 0) { + if (p.getInventory().getItemInHand().equals(i)) { + p.getInventory().setItemInHand(null); + } else { + p.getInventory().removeItem(i); + } + return; + } + i.setAmount(i.getAmount() - amount); + p.updateInventory(); + } } diff --git a/src/main/java/rip/tilly/bedwars/utils/menusystem/menu/UpgradesMenu.java b/src/main/java/rip/tilly/bedwars/utils/menusystem/menu/UpgradesMenu.java index 77833a9..066babc 100644 --- a/src/main/java/rip/tilly/bedwars/utils/menusystem/menu/UpgradesMenu.java +++ b/src/main/java/rip/tilly/bedwars/utils/menusystem/menu/UpgradesMenu.java @@ -71,15 +71,9 @@ public class UpgradesMenu extends Menu { return; } - if (upgradeItem.canBuy(player, teamUpgrades.getLevelForUpgrade(upgradeItem.getUpgrade()) + 1)) { - upgradeItem.buy(player, teamUpgrades.getLevelForUpgrade(upgradeItem.getUpgrade()) + 1, game, gameTeam, teamUpgrades); + upgradeItem.buy(player, teamUpgrades.getLevelForUpgrade(upgradeItem.getUpgrade()) + 1, game, gameTeam, teamUpgrades); - new UpgradesMenu(this.main.getPlayerMenuUtil(player)).open(player); - - break; - } - - player.sendMessage(CC.translate("&cYou do not have enough diamonds for &c&l" + upgradeItem.getUpgrade().getFormattedName() + " " + upgradeItem.getUpgrade().getNumberToRomanNumeral(teamUpgrades.getLevelForUpgrade(upgradeItem.getUpgrade()) + 1))); + new UpgradesMenu(this.main.getPlayerMenuUtil(player)).open(player); break; }