Fixed Shops not taking items :D

This commit is contained in:
Luca 2021-11-26 19:09:53 +01:00
parent 5cf80c903f
commit b14b37540b
5 changed files with 91 additions and 76 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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<ItemStack> 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<ItemStack> 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;
}

View File

@ -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();
}
}

View File

@ -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;
}