Permit multiple stacks of same item Requirement, fixes #1457

This commit is contained in:
PikaMug 2020-12-01 02:21:53 -05:00
parent f435d871dd
commit e267d6bd1b
3 changed files with 47 additions and 20 deletions

View File

@ -27,9 +27,10 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import com.codisimus.plugins.phatloots.PhatLootsAPI;
import com.codisimus.plugins.phatloots.loot.CommandLoot;
@ -448,20 +449,14 @@ public class Quest {
}
if (player.isOnline()) {
final Player p = (Player)player;
final PlayerInventory inventory = p.getInventory();
int num = 0;
final Inventory fakeInv = Bukkit.createInventory(null, InventoryType.PLAYER);
fakeInv.setContents(p.getInventory().getContents().clone());
for (final ItemStack is : reqs.getItems()) {
for (final ItemStack stack : inventory.getContents()) {
if (stack != null) {
if (ItemUtil.compareItems(is, stack, true) == 0) {
num += stack.getAmount();
}
}
}
if (num < is.getAmount()) {
if (InventoryUtil.canRemoveItem(fakeInv, is)) {
InventoryUtil.removeItem(fakeInv, is);
} else {
return false;
}
num = 0;
}
for (final String s : reqs.getPermissions()) {
if (p.hasPermission(s) == false) {

View File

@ -39,10 +39,10 @@ import org.bukkit.conversations.Conversable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.Crops;
@ -781,15 +781,14 @@ public class Quester implements Comparable<Quester> {
}
}
if (player.isOnline()) {
final PlayerInventory inventory = getPlayer().getInventory();
final Inventory fakeInv = Bukkit.createInventory(null, InventoryType.PLAYER);
fakeInv.setContents(getPlayer().getInventory().getContents().clone());
int num = 0;
for (final ItemStack is : reqs.getItems()) {
for (final ItemStack stack : inventory.getContents()) {
if (stack != null) {
if (ItemUtil.compareItems(is, stack, true) == 0) {
num += stack.getAmount();
}
}
if (InventoryUtil.canRemoveItem(fakeInv, is)) {
InventoryUtil.removeItem(fakeInv, is);
num += is.getAmount();
}
if (num >= is.getAmount()) {
finishedRequirements.add(ChatColor.GREEN + "" + is.getAmount() + " " + ItemUtil.getName(is));
@ -798,6 +797,7 @@ public class Quester implements Comparable<Quester> {
}
num = 0;
}
for (final String perm :reqs.getPermissions()) {
if (getPlayer().hasPermission(perm)) {
finishedRequirements.add(ChatColor.GREEN + Lang.get("permissionDisplay") + " " + perm);

View File

@ -45,6 +45,38 @@ public class InventoryUtil {
}
}
/**
* Whether an item can be removed from player's inventory.
*
* @param inventory Inventory to check
* @param item Item with amount for removal
* @return true if possible
*/
public static boolean canRemoveItem(final Inventory inventory, final ItemStack item) {
final int amount = item.getAmount();
final HashMap<Integer, ? extends ItemStack> allItems = inventory.all(item.getType());
final HashMap<Integer, Integer> removeFrom = new HashMap<Integer, Integer>();
int foundAmount = 0;
for (final Map.Entry<Integer, ? extends ItemStack> items : allItems.entrySet()) {
if (ItemUtil.compareItems(item, items.getValue(), true) == 0) {
if (items.getValue().getAmount() >= amount - foundAmount) {
removeFrom.put(items.getKey(), amount - foundAmount);
foundAmount = amount;
} else {
foundAmount += items.getValue().getAmount();
removeFrom.put(items.getKey(), items.getValue().getAmount());
}
if (foundAmount >= amount) {
break;
}
}
}
if (foundAmount == amount) {
return true;
}
return false;
}
/**
* Removes item from player's inventory.
*