Here is the code for my Inventory
public static class Inventory {
public int width, height;
private Item[] items;
public Inventory(int width, int height) {
this.width = width;
this.height = height;
items = new Item[width * height];
}
public Item getItem(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height) return null;
return items[x + y * width];
}
public void addItem(int x, int y, Item item) {
if (x < 0 || x >= width || y < 0 || y >= height) return;
items[x + y * width] = item;
}
public void addItem(Item item) {
for (int y = 0; y < height; y++) {
boolean br = false;
for (int x = 0; x < width; x++) {
Item itemInSlot = getItem(x, y);
if (itemInSlot == null) {
addItem(x, y, item);
br = true;
break;
}
}
if (br) break;
}
}
public boolean contains(Class<Item> cls) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Item item = items[x+y*width];
// if item instanceof cls.getType() return true;
}
}
return false;
if(inventory.contains(Axe.class)) {
chopTree();
}
}
}
I want to be able to check if my items array contains an item that is instanceof specified class.
So for example:
if(inventory.contains(Axe.class)) {
chopTree();
}
Ummm is it possible to pass in a ReferenceType? I could then just do item instanceof ReferenceType if I could have referenceType as an object.