Skip to content

Commit

Permalink
have pistons update nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gutin1 committed Sep 26, 2024
1 parent 56f4501 commit 9a722c8
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.horizonsend.ion.server.features.transport
import net.horizonsend.ion.server.features.world.IonWorld.Companion.ion
import net.horizonsend.ion.server.features.world.chunk.IonChunk
import net.horizonsend.ion.server.listener.SLEventListener
import net.horizonsend.ion.server.miscellaneous.utils.Tasks
import net.horizonsend.ion.server.miscellaneous.utils.coordinates.BlockKey
import net.horizonsend.ion.server.miscellaneous.utils.coordinates.getX
import net.horizonsend.ion.server.miscellaneous.utils.coordinates.getZ
Expand All @@ -13,6 +14,8 @@ import org.bukkit.block.Block
import org.bukkit.block.data.BlockData
import org.bukkit.event.EventHandler
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockPistonExtendEvent
import org.bukkit.event.block.BlockPistonRetractEvent
import org.bukkit.event.block.BlockPlaceEvent

object GlobalNodeManager : SLEventListener() {
Expand All @@ -26,16 +29,32 @@ object GlobalNodeManager : SLEventListener() {

@EventHandler
fun onBlockPlace(event: BlockPlaceEvent) {
val world = event.block.world
handleBlockChange(event.block)
}

handleBlockChange(world, event.block)
@EventHandler
fun onPistonExtend(event: BlockPistonExtendEvent) {
// Delay 1 tick
Tasks.sync { event.blocks.forEach {
handleBlockRemoval(it.world, toBlockKey(it.x, it.y, it.z))
handleBlockChange(it)
}}
}

fun handleBlockChange(world: World, new: Block) {
@EventHandler
fun onPistonRetract(event: BlockPistonRetractEvent) {
// Delay 1 tick
Tasks.sync { event.blocks.forEach {
handleBlockRemoval(it.world, toBlockKey(it.x, it.y, it.z))
handleBlockChange(it)
}}
}

fun handleBlockChange(new: Block) {
val chunkX = new.x.shr(4)
val chunkZ = new.z.shr(4)

val chunk = world.ion.getChunk(chunkX, chunkZ) ?: return
val chunk = new.world.ion.getChunk(chunkX, chunkZ) ?: return
chunk.transportNetwork.processBlockChange(new)
}

Expand Down Expand Up @@ -69,9 +88,9 @@ object GlobalNodeManager : SLEventListener() {
chunk.transportNetwork.refreshBlock(position)
}

fun handleBlockAdditions(world: World, newBlocks: Iterable<Block>) {
fun handleBlockAdditions(newBlocks: Iterable<Block>) {
for (new in newBlocks) {
handleBlockChange(world, new)
handleBlockChange(new)
}
}

Expand Down

0 comments on commit 9a722c8

Please sign in to comment.