Function only returns adjacent chest

Discussion in 'Plugin Development' started by Darkman2412, Jul 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Darkman2412

    Hi,

    I just found a bug in my AutoFurnace plugin... But I can't fix it :/
    This is what getInputChest() needs to do:
    If there are two blocks at the left side of the furnace AND the left chest is not empty, the function will return the left chest...
    If there is only one chest at the left side of the furnace, it will return the adjacent chest.
    Else it just returns null.

    But now it only returns the adjacent chest. :/
    My function:
    Code:
    private Block getInputChest(Block furnace) {
    	Furnace state = (Furnace) furnace.getState();
    	BlockFace facing = ((FurnaceAndDispenser) state.getData()).getFacing();
    	BlockFace dir = directions.get((directions.indexOf(facing) + 1)%directions.size());
            Block adjacentchest = furnace.getRelative(dir);
            Block doublechest = adjacentchest.getRelative(dir);
    
            plugin.getServer().getPlayer("darkman2412").sendMessage(adjacentchest.getType().name()); //debug
            plugin.getServer().getPlayer("darkman2412").sendMessage(doublechest.getType().name()); //debug
            // Both messages send me CHEST.
    
    
            if(doublechest.getType()!=Material.CHEST&&adjacentchest.getType()!=Material.CHEST)
            	return null;
    
            Chest state1 = (Chest) doublechest.getState();
        	ItemStack[] items = state1.getInventory().getContents();
        	for (ItemStack item : items) {
        	    if (item != null) {
        	        return doublechest;
        	    }
        	}
    
            if(adjacentchest.getType() == Material.CHEST&&doublechest.getType()!=Material.CHEST)
                return adjacentchest;
    
            return null;
        }
    Does anyone see what's wrong?

    Thanks in advance,

    Darkman2412
     
  2. Offline

    feildmaster

    Fill up your double chest with items, then post the results.

    also, test if this would work:
    Code:
    if (items.length >0) return doublechest;
     
  3. Offline

    Darkman2412

    ok, I think i found the bug:
    Code:
    if(adjacentchest.getType() == Material.CHEST&&doublechest.getType()!=Material.CHEST)
                return adjacentchest;
    It needs to be
    Code:
    if(adjacentchest.getType() == Material.CHEST)
                return adjacentchest;
    Weird that i didn't saw it XD
     
  4. Offline

    feildmaster

    ahh... yes that would be a conflicting statement indeed. I completely ignored that myself.

    I had originally thought it just wasn't returning doublechest.

    Also, if the code I posted above works, you should use that.

    Actually. I just thought about the logic behind this one @Darkman2412

    It should be the following:
    Code:
        private Block getInputChest(Block furnace) {
    	Furnace state = (Furnace) furnace.getState();
    	BlockFace facing = ((FurnaceAndDispenser) state.getData()).getFacing();
    	BlockFace dir = directions.get((directions.indexOf(facing) + 1)%directions.size());
            Block adjacentchest = furnace.getRelative(dir);
            Block doublechest = adjacentchest.getRelative(dir);
    
            if(doublechest.getType()==Material.CHEST&&adjacentchest.getType()==Material.CHEST) {
                Chest state1 = (Chest) doublechest.getState();
                ItemStack[] items = state1.getInventory().getContents();
                for (ItemStack item : items)
        	        if (isSmeltable(item.getType()))
        	            return doublechest;
            } else if (adjacentchest.getType() == Material.CHEST) {
                Chest state1 = (Chest) doublechest.getState();
                ItemStack[] items = state1.getInventory().getContents();
                for (ItemStack item : items)
        	        if (isSmeltable(item.getType()))
        	            return adjacentchest;
            }
            return null;
        }
    
        private boolean isSmeltable(Material material) {
            // Check for == "ores", return true;
            return false;
        }
    
    That is, if you don't mind my helping you. :)

    I wonder if you even NEED this code... I bet you probably already check what chest has ores don't you. I'm just being a busybody.

    In which case you'd need simply this:

    Code:
        private Block getInputChest(Block furnace) {
    	Furnace state = (Furnace) furnace.getState();
    	BlockFace facing = ((FurnaceAndDispenser) state.getData()).getFacing();
    	BlockFace dir = directions.get((directions.indexOf(facing) + 1)%directions.size());
            Block adjacentchest = furnace.getRelative(dir);
            Block doublechest = adjacentchest.getRelative(dir);
    
            if(doublechest.getType()==Material.CHEST&&adjacentchest.getType()==Material.CHEST) {
                Chest state1 = (Chest) doublechest.getState();
                ItemStack[] items = state1.getInventory().getContents();
        	        if (items.length > 0)
        	            return doublechest;
            } else if (adjacentchest.getType() == Material.CHEST) {
                Chest state1 = (Chest) doublechest.getState();
                ItemStack[] items = state1.getInventory().getContents();
        	        if (items.length > 0)
        	            return adjacentchest;
            }
            return null;
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  5. Offline

    Darkman2412

    I already add the smeltable thing in v1.4, wich I'm going to release in a couple hours. :p

    Hmm
    v1.4 needs to wait... I'm getting this bug again D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
Thread Status:
Not open for further replies.

Share This Page