Skip to content

Commit

Permalink
refactor: move hitbox-spawn logic to observer
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed May 14, 2024
1 parent 1e43081 commit 9b79a97
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class BlockyPlugin : JavaPlugin() {
createFurnitureSeatSetter()
createFurnitureMEGModelSetter()

furnitureHitboxSetter()

createFurnitureOutlineSystem()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataContainer
import org.bukkit.util.BoundingBox
import java.util.*
import kotlin.math.pow
import kotlin.random.Random

const val VANILLA_STONE_PLACE = "blocky.stone.place"
Expand Down Expand Up @@ -145,6 +146,8 @@ fun handleBlockyDrops(block: Block, player: Player) {

object GenericHelpers {

val simulationDistance = (Bukkit.getServer().simulationDistance * 16.0).pow(2)

fun blockStandingOn(entity: LivingEntity): Block {
val block = entity.location.block
val blockBelow = block.getRelative(BlockFace.DOWN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@ class BlockyFurnitureListener : Listener {
}
}

@EventHandler(priority = EventPriority.HIGH)
fun GearyEntityAddToWorldEvent.onAddFurniture() {
val furniture = entity as? ItemDisplay ?: return
val simulationDistance = (Bukkit.getServer().simulationDistance * 16.0).pow(2)
blocky.plugin.server.onlinePlayers.filterNotNull().filter {
it.world == entity.world && it.location.distanceSquared(entity.location) < simulationDistance
}.forEach { player ->
blocky.plugin.launch(blocky.plugin.minecraftDispatcher) {
delay(1)
FurniturePacketHelpers.sendInteractionEntityPacket(furniture, player)
FurniturePacketHelpers.sendCollisionHitboxPacket(furniture, player)
FurniturePacketHelpers.sendLightPacket(furniture, player)
}
}
}

@EventHandler
fun PlayerChunkUnloadEvent.onUnloadChunk() {
chunk.entities.filterIsInstance<ItemDisplay>().forEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mineinabyss.blocky.systems.actions

import com.github.shynixn.mccoroutine.bukkit.launch
import com.github.shynixn.mccoroutine.bukkit.minecraftDispatcher
import com.mineinabyss.blocky.blocky
import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.helpers.FurniturePacketHelpers
import com.mineinabyss.blocky.helpers.GenericHelpers
import com.mineinabyss.geary.modules.GearyModule
import com.mineinabyss.geary.observers.events.OnSet
import com.mineinabyss.geary.systems.builders.observe
import com.mineinabyss.geary.systems.query.query
import kotlinx.coroutines.delay
import org.bukkit.entity.ItemDisplay

fun GearyModule.furnitureHitboxSetter() = observe<OnSet>()
.involving(query<ItemDisplay, BlockyFurniture>())
.exec { (itemDisplay, _) ->
val itemDisplay = itemDisplay

blocky.plugin.launch(blocky.plugin.minecraftDispatcher) {
delay(1)
blocky.plugin.server.onlinePlayers.filterNotNull().filter {
it.world == itemDisplay.world && it.location.distanceSquared(itemDisplay.location) < GenericHelpers.simulationDistance
}.forEach { player ->
FurniturePacketHelpers.sendInteractionEntityPacket(itemDisplay, player)
FurniturePacketHelpers.sendCollisionHitboxPacket(itemDisplay, player)
FurniturePacketHelpers.sendLightPacket(itemDisplay, player)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.components.features.furniture.BlockyModelEngine
import com.mineinabyss.geary.modules.GearyModule
import com.mineinabyss.geary.observers.events.OnSet
import com.mineinabyss.geary.systems.builders.observe
import com.mineinabyss.geary.systems.builders.observeWithData
import com.mineinabyss.geary.systems.query.query
import com.mineinabyss.idofront.plugin.Plugins
Expand All @@ -13,7 +14,7 @@ import com.ticxo.modelengine.api.ModelEngineAPI
import org.bukkit.Bukkit
import org.bukkit.entity.ItemDisplay

fun GearyModule.createFurnitureMEGModelSetter() = observeWithData<OnSet>()
fun GearyModule.createFurnitureMEGModelSetter() = observe<OnSet>()
.involving(query<ItemDisplay, BlockyFurniture, BlockyModelEngine>())
.exec { (itemDisplay, _, modelengine) ->
// Save for scheduled task
Expand Down

0 comments on commit 9b79a97

Please sign in to comment.