Correct loading of item requirements, fixes #1207

This commit is contained in:
PikaMug 2020-04-17 02:06:26 -04:00
parent 0559cf4a2d
commit 4f7d6dd07b
2 changed files with 28 additions and 27 deletions

View File

@ -1646,6 +1646,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
throw new QuestFormatException("Requirement items has invalid formatting", questKey);
}
}
reqs.setItems(temp);
if (config.contains("quests." + questKey + ".requirements.remove-items")) {
if (ConfigUtil.checkList(config.getList("quests." + questKey + ".requirements.remove-items"),
Boolean.class)) {

View File

@ -25,45 +25,45 @@ public class InventoryUtil {
/**
* Adds item to player's inventory. If full, item is dropped at player's location.
*
* @throws NullPointerException when ItemStack is null
* @param player to try giving item to
* @param item Item with amount to add
* @throws NullPointerException if item is null
*/
public static void addItem(Player p, ItemStack i) throws Exception {
if (i == null) {
throw new NullPointerException("Null item while trying to add to inventory of " + p.getName());
public static void addItem(Player player, ItemStack item) throws NullPointerException {
if (item == null) {
throw new NullPointerException("Null item while trying to add to inventory of " + player.getName());
}
PlayerInventory inv = p.getInventory();
if (i != null) {
HashMap<Integer, ItemStack> leftover = inv.addItem(i);
if (leftover != null) {
if (leftover.isEmpty() == false) {
for (ItemStack i2 : leftover.values()) {
p.getWorld().dropItem(p.getLocation(), i2);
}
PlayerInventory inv = player.getInventory();
HashMap<Integer, ItemStack> leftovers = inv.addItem(item);
if (leftovers != null) {
if (leftovers.isEmpty() == false) {
for (ItemStack leftover : leftovers.values()) {
player.getWorld().dropItem(player.getLocation(), leftover);
}
}
}
}
/**
* Removes item from player's inventory
* Removes item from player's inventory.
*
* @param inventory Inventory to remove from
* @param is Item with amount to remove
* @param item Item with amount to remove
* @return true if successful
*/
public static boolean removeItem(Inventory inventory, ItemStack is) {
int amount = is.getAmount();
HashMap<Integer, ? extends ItemStack> allItems = inventory.all(is.getType());
public static boolean removeItem(Inventory inventory, ItemStack item) {
int amount = item.getAmount();
HashMap<Integer, ? extends ItemStack> allItems = inventory.all(item.getType());
HashMap<Integer, Integer> removeFrom = new HashMap<Integer, Integer>();
int foundAmount = 0;
for (Map.Entry<Integer, ? extends ItemStack> item : allItems.entrySet()) {
if (ItemUtil.compareItems(is, item.getValue(), true) == 0) {
if (item.getValue().getAmount() >= amount - foundAmount) {
removeFrom.put(item.getKey(), amount - foundAmount);
for (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 += item.getValue().getAmount();
removeFrom.put(item.getKey(), item.getValue().getAmount());
foundAmount += items.getValue().getAmount();
removeFrom.put(items.getKey(), items.getValue().getAmount());
}
if (foundAmount >= amount) {
break;
@ -72,12 +72,12 @@ public class InventoryUtil {
}
if (foundAmount == amount) {
for (Map.Entry<Integer, Integer> toRemove : removeFrom.entrySet()) {
ItemStack item = inventory.getItem(toRemove.getKey());
if (item.getAmount() - toRemove.getValue() <= 0) {
ItemStack i = inventory.getItem(toRemove.getKey());
if (i.getAmount() - toRemove.getValue() <= 0) {
inventory.clear(toRemove.getKey());
} else {
item.setAmount(item.getAmount() - toRemove.getValue());
inventory.setItem(toRemove.getKey(), item);
i.setAmount(i.getAmount() - toRemove.getValue());
inventory.setItem(toRemove.getKey(), i);
}
}
return true;