Solved finding what object a hopper pushes items into

Discussion in 'Plugin Development' started by The_Nut, Aug 27, 2013.

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

    The_Nut

    I am directly trying to find all chests that are around a hopper (on the same y level) except if the hopper is pushing its contents into the chest.
    Top View:
    ..C
    C H-C in this view the hopper is pushing to the right or east.
    ..C

    I am unable to figure out how to know what the hopper is pushing into. Any help would be appreciated.
    some code I have to get the chests is attached, I just need to know if the hopper feeds into it.
    Code:java
    1.  
    2. //NOTE: hopperBlock is a Block object that is a hopper located at x, y, z
    3.  
    4. Block target = hopperBlock.getWorld().getBlockAt(x ,y,z-1); //( 0, -1) north
    5. if (target instanceof Chest) {
    6. //need to see if this chest is fed by the hopper block above.
    7. }
    8.  
    9. target = hopperBlock.getWorld().getBlockAt(x+1,y,z ); //( 1, 0) east
    10. if (target instanceof Chest) {
    11. //need to see if this chest is fed by the hopper block above.
    12. }
    13.  
    14. target = hopperBlock.getWorld().getBlockAt(x ,y,z+1); //( 0, +1) south
    15. if (target instanceof Chest) {
    16. //need to see if this chest is fed by the hopper block above.
    17. }
    18.  
    19. target = hopperBlock.getWorld().getBlockAt(x-1,y,z ); //(-1, 0) west
    20. if (target instanceof Chest) {
    21. //need to see if this chest is fed by the hopper block above.
    22. }


    If it is extremely simple, please just slap me, but be nice and let me know the code to get it.

    Nut
     
  2. Offline

    Tarestudio

  3. Offline

    The_Nut

    @Tarestudio
    Thanks for the reply, I am already in the InventoryMoveItemEvent.

    Code:java
    1. public void inventoryMoveHandler(InventoryMoveItemEvent event) {
    2. //Get destination container
    3. InventoryHolder destHolder = event.getDestination().getHolder();
    4.  
    5. //since we only care about hoppers, then ignore everything else
    6. if (destHolder instanceof Hopper) {
    7. //get the block object of the destination hopper.
    8. Block hopperBlock = ((Hopper) destHolder).getBlock();
    9.  
    10. int x = hopperBlock.getX();
    11. int y = hopperBlock.getY();
    12. int z = hopperBlock.getZ();
    13.  
    14. //look for chests around this hopper and make sure they are not the receiver of the hopperBlock
    15.  
    16. Block target = hopperBlock.getWorld().getBlockAt(x ,y,z-1); //( 0, -1) north
    17. if (target instanceof Chest) {
    18. //need to make sure this is not the receiver of the when this hopper pushes out
    19. }
    20.  
    21. target = hopperBlock.getWorld().getBlockAt(x+1,y,z ); //( 1, 0) east
    22. if (target instanceof Chest) {
    23. //need to make sure this is not the receiver of the when this hopper pushes out
    24. }
    25.  
    26. target = hopperBlock.getWorld().getBlockAt(x ,y,z+1); //( 0, +1) south
    27. if (target instanceof Chest) {
    28. //need to make sure this is not the receiver of the when this hopper pushes out
    29. }
    30.  
    31. target = hopperBlock.getWorld().getBlockAt(x-1,y,z ); //(-1, 0) west
    32. if (target instanceof Chest) {
    33. //need to make sure this is not the receiver of the when this hopper pushes out
    34. }
    35. }
    36. }
    37.  


    I am trying to find all chests next to the destination hopper I am looking at that will not have the item pushed into it from the current destination hopper I have in the hopperBlock object.

    I hope that clears up what I am trying to do.

    Nut
     
  4. Offline

    Tarestudio

    The_Nut
    If a 'Source-Hopper' pushes a Item into a 'Destination-Hopper', then you want to find all Chest that are not attached to the 'Destionation-Hopper'? I think you have to get the 'Destination-Hopper' as a Block, and check its Data. The DataID it holds corresponds to the way it is placed, and so to a chest that is attached to it.
     
  5. Offline

    The_Nut

    Tarestudio
    Thank you... that is exactly what I needed. I should have seen it. It is a can't see the forest for the trees kind of thing.
    here is my final code for the function
    Code:java
    1. private HashMap<ItemStack, String> ReturnAttachedAllowedItemsFromChests(Block hopperBlock) {
    2. if (debugLevel > 2) getLogger().info(" Finding chests attached to hopper.");
    3. //declare return container
    4. HashMap<ItemStack, String> itemStackCache = new HashMap<ItemStack, String>();
    5. //save the direction the hopper outputs
    6. byte facing = hopperBlock.getData(); // 0=Facing down, 1= unattached to any container, 2=Facing North, 3=Facing South, 4=Facing West, 5=Facing East
    7.  
    8. Block target = null;
    9. ArrayList<Chest> list = new ArrayList<Chest>();
    10.  
    11. target = hopperBlock.getRelative(BlockFace.NORTH);
    12. if (target.getState() instanceof Chest && facing != 0x2) {
    13. list.add((Chest)target.getState());
    14. }
    15.  
    16. target = hopperBlock.getRelative(BlockFace.EAST);
    17. if (target.getState() instanceof Chest && facing != 0x5) {
    18. list.add((Chest)target.getState());
    19. }
    20.  
    21. target = hopperBlock.getRelative(BlockFace.SOUTH);
    22. if (target.getState() instanceof Chest && facing != 0x3) {
    23. list.add((Chest)target.getState());
    24. }
    25.  
    26. target = hopperBlock.getRelative(BlockFace.WEST);
    27. if (target.getState() instanceof Chest && facing != 0x4) {
    28. list.add((Chest)target.getState());
    29. }
    30.  
    31. for (Chest chest : list) {
    32. //deal with chest
    33.  
    34. //get the stacks out of the chest
    35. ItemStack[] tempStacks = chest.getBlockInventory().getContents();
    36. for (ItemStack itemStack : tempStacks) {
    37. if (itemStack != null) {
    38. //deal with itemStack
    39. itemStack.setAmount(1);
    40. itemStackCache.put(itemStack, GetItemInformationForInventory(itemStack, true));
    41. }
    42. }
    43. }
    44.  
    45. return itemStackCache;
    46. }
    47.  
     
Thread Status:
Not open for further replies.

Share This Page