Created
November 13, 2019 18:26
-
-
Save vznxyz/24df16df3652e763cb5966931907f74a to your computer and use it in GitHub Desktop.
finds hoppers and chests starting from a hopper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private fun findHoppersAndChests(block: Block, | |
| lambda: (Set<Hopper>, Set<Chest>) -> Unit, | |
| hoppers: HashSet<Hopper> = hashSetOf(block.state.data as Hopper), | |
| chests: HashSet<Chest> = hashSetOf()) { | |
| when (block.type) { | |
| Material.HOPPER -> { | |
| val hopper = block.state.data as org.bukkit.material.Hopper | |
| val relative = block.getRelative(hopper.facing) | |
| if (relative.type == Material.CHEST || relative.type == Material.TRAPPED_CHEST) { | |
| chests.add(relative as Chest) | |
| findHoppersAndChests(relative, lambda, hoppers, chests) | |
| return | |
| } else { | |
| lambda.invoke(hoppers, chests) | |
| return | |
| } | |
| } | |
| Material.CHEST, | |
| Material.TRAPPED_CHEST -> { | |
| val chest = block.state.data as Chest | |
| var hopperBlock: Block? = null | |
| for (face in FACES) { | |
| val relative = block.getRelative(face) | |
| // relative block is a chest | |
| if (relative.type == block.type) { | |
| // if the chest inventory and relative chest inventory match we have both chest's locations | |
| if (chest.inventory == (relative.state.data as Chest).blockInventory) { | |
| chests.add(relative as Chest) | |
| } | |
| } else if (relative.type == Material.HOPPER) { // or relative block is a hopper | |
| val hopper = relative.state.data as org.bukkit.material.Hopper | |
| // hopper is facing towards the chest | |
| if (hopper.facing == face.oppositeFace) { | |
| hopperBlock = relative | |
| hoppers.add(relative.state as Hopper) | |
| } | |
| } | |
| } | |
| if (hopperBlock != null) { | |
| findHoppersAndChests(hopperBlock, lambda, hoppers, chests) | |
| } else { | |
| lambda.invoke(hoppers, chests) | |
| return | |
| } | |
| } | |
| else -> { | |
| lambda.invoke(hoppers, chests) | |
| return | |
| } | |
| } | |
| } | |
| companion object { | |
| private val FACES = arrayListOf( | |
| BlockFace.NORTH, | |
| BlockFace.EAST, | |
| BlockFace.SOUTH, | |
| BlockFace.WEST | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment