I have a game with an inventory, you can pick up items by walking on them. When you walk on an item and if it isn't already in your inventory it is added to the next free slot. If it is already in your inventory it should stack the items to a maximum of 99 similar to minecraft. The problem is (for example) the first three slots are empty and the fourth has wood in it. when you pick up another piece of wood it should stack with the wood in slot four. It doesn't, instead it starts a new stack in the first empty slot. In order for the wood to stack it has to be in the first empty slot. That's bad.
My Array looks like this:
public static ArrayList<cell> c = new ArrayList<cell>();
My code:
for (int i = 0; i < c.toArray().length; i++) {
for (int i1 = 0; i1 < play.items.p.toArray().length; i1++) {
if(c.get(i).holdingid == play.items.p.get(i1).holdingid){
if(play.items.p.get(i1).r.intersects(play.p.r) && play.items.p.get(i1).remove == false && c.get(i).stack < 99){
c.get(i).stack+=1;
play.items.p.get(i1).remove = true;
//break;
}
}else
if (c.get(i).holdingid == 0) {
if(play.items.p.get(i1).r.intersects(play.p.r) && play.items.p.get(i1).remove == false){
play.items.p.get(i1).remove = true;
c.get(i).holdingid = play.items.p.get(i1).holdingid;
c.get(i).stack+=1;
break;
}
}
}
The first "if" condition checks to see if there are any of the same item you just picked up are in your inventory. The second checks if the item isn't in your inventory and then if it adds it to the next empty slot. I have 9 slots. The problem is that if the first if checks the first slot and then if there isn't a stack of that item in your inventory already, it moves on to the next condition and checks if slot 1 is empty and then creates a new stack of that item. similar to minecraft. I need the first if loop to check all slots and if none of them have the same item that you just picked up it moves on to the second if loop. What I currently have check the 1st slot with the first if and then the second and then moves on to the next slot. How can I fix this to check all the slots with the first if and then if none of them have got the item you just picked up, it starts checking all the slots again with the second.
My slot class:
public class cell {
public int x, y;
public int id;
public int holdingid;
public boolean sel = false;
public boolean stackable = false;
public int stack = 0;
private Image img;
public Rectangle r = new Rectangle();
public cell(int x,int y,int id,int holdingid){
this.x = x;
this.y = y;
this.id = id;
r = new Rectangle(x+3,y+3,10,10);
this.holdingid =holdingid;
}
public void tick(){
r = new Rectangle(x+3,y+3,10,10);
}
public void render(Graphics g){
if (sel) {
g.setColor(new Color(0, 168, 92, 174));
g.fillRect(x, y, 60, 60);
g.setColor(Color.blue);
g.drawRect(x, y, 60, 60);
} else {
g.setColor(new Color(0, 168, 92, 174));
g.fillRect(x, y, 60, 60);
g.setColor(Color.yellow);
g.drawRect(x, y, 60, 60);
}
if(holdingid != 0){
ImageIcon i2 = new ImageIcon("res/x.png");
img = i2.getImage();
g.drawImage(img, x+3, y+3, 10, 10, null);
if(r.intersects(Comp.mx,Comp.my,1,1) && Comp.ml){
holdingid = 0;
stack = 0;
}
}
if(holdingid == 0){
//empty
}else if(holdingid == 1){
ImageIcon i2 = new ImageIcon("res/shovel.png");
img = i2.getImage();
g.drawImage(img, x+8, y+8, 40, 40, null);
}else if(holdingid == 2){
ImageIcon i112 = new ImageIcon("res/axe.png");
img = i112.getImage();
g.drawImage(img, x+10, y+10, 40, 40, null);
}else if(holdingid == 3){
ImageIcon i12 = new ImageIcon("res/clippers.png");
img = i12.getImage();
g.drawImage(img, x+12, y+10, 40, 40, null);
}else if(holdingid == 4){
ImageIcon i12 = new ImageIcon("res/poo.png");
img = i12.getImage();
g.drawImage(img, x+12, y+10, 40, 40, null);
}else if(holdingid == 5){
ImageIcon i12 = new ImageIcon("res/items/acorn.png");
img = i12.getImage();
g.drawImage(img, x+22, y+20, 20, 20, null);
}else if(holdingid == 6){
ImageIcon i12 = new ImageIcon("res/items/wood.png");
img = i12.getImage();
g.drawImage(img, x+15, y+17, 30, 30, null);
}else if(holdingid == 7){
ImageIcon i12 = new ImageIcon("res/items/lax.png");
img = i12.getImage();
g.drawImage(img, x+12, y+10, 40, 40, null);
}
if(holdingid != 0 && holdingid != 1&& holdingid != 2&& holdingid != 3){
g.setColor(Color.black);
g.setFont(new Font("italic", Font.BOLD, 15));
g.drawString(""+stack, x+6,y+57);
g.setColor(Color.white);
g.setFont(new Font("italic", Font.BOLD, 15));
g.drawString(""+stack, x+4,y+55);
}
}