Skip to content

Commit

Permalink
Use doesInventoryContain for PlayerInvSensor.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Feb 6, 2017
1 parent 720ba29 commit 29f1a3d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sk89q.craftbook.mechanics.ic.gates.world.sensors;

import com.sk89q.craftbook.util.InventoryUtil;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -102,7 +103,7 @@ public boolean isDetected() {
public boolean testPlayer(Player e) {

if(slot == -1 && !inHand)
return e.getInventory().containsAtLeast(item, item.getAmount());
return InventoryUtil.doesInventoryContain(e.getInventory(), false, item);
else if (inHand) { //Eclipse messes with indentation without these {'s
return (e.getInventory().getItemInMainHand() != null && ItemUtil.areItemsIdentical(e.getInventory().getItemInMainHand(), item) && e.getInventory().getItemInMainHand().getAmount() >= item.getAmount())
|| (e.getInventory().getItemInOffHand() != null && ItemUtil.areItemsIdentical(e.getInventory().getItemInOffHand(), item) && e.getInventory().getItemInOffHand().getAmount() >= item.getAmount());
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/sk89q/craftbook/util/InventoryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

/**
* Class for utilities that include adding items to a furnace based on if it is a fuel or not, and adding items to a chest. Also will include methdos for checking contents and removing.
Expand Down Expand Up @@ -126,13 +127,17 @@ public static boolean doesInventoryContain(Inventory inv, boolean exact, ItemSta
if(itemsToFind.isEmpty())
return true;

for (ItemStack item : inv.getContents()) {
List<ItemStack> items = new ArrayList<ItemStack>(Arrays.asList(inv.getContents()));
if (inv instanceof PlayerInventory) {
items.addAll(Arrays.asList(((PlayerInventory) inv).getArmorContents()));
items.add(((PlayerInventory) inv).getItemInOffHand());
}

for (ItemStack item : inv.getContents()) {

This comment has been minimized.

Copy link
@BillyGalbreath

BillyGalbreath Jan 22, 2018

Contributor

Was this on purpose? You populate items, but then use inv.getContents()

I'm about to make a PR that modifies this method quite a bit, so I was wondering so I should include this in my PR or not (to minimize conflicts).

This comment has been minimized.

Copy link
@me4502

me4502 Jan 22, 2018

Author Member

It doesn't appear that was intentional, good catch.

if(!ItemUtil.isStackValid(item))
continue;

for(ItemStack base : stacks) {

if(!itemsToFind.contains(base))
continue;

Expand All @@ -142,7 +147,6 @@ public static boolean doesInventoryContain(Inventory inv, boolean exact, ItemSta
}

if(ItemUtil.areItemsIdentical(base, item)) {

if(exact && base.getAmount() != item.getAmount())
continue;

Expand Down

0 comments on commit 29f1a3d

Please sign in to comment.