Skip to content

Commit

Permalink
refactor: use Events for handling furniture collisionHitbox interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Mar 28, 2024
1 parent 72ed3ef commit f377c81
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 53 deletions.
1 change: 0 additions & 1 deletion src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class BlockyPlugin : JavaPlugin() {

createFurnitureOutlineSystem()
}
FurniturePacketHelpers.registerPacketListeners()

listeners(
BlockyGenericListener(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ object BlockyFurnitures {
val Block.blockyFurniture get() = this.toGearyOrNull()?.get<BlockyFurniture>()

val Block.baseFurniture: ItemDisplay?
get() = FurniturePacketHelpers.getBaseFurnitureFromCollisionHitbox(this.toBlockPos())
get() = FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(this.toBlockPos())
val Interaction.baseFurniture: ItemDisplay?
get() = FurniturePacketHelpers.getBaseFurnitureFromInteractionEntity(this.entityId)
get() = FurniturePacketHelpers.baseFurnitureFromInteractionHitbox(this.entityId)

val ItemDisplay.seats: List<Entity>
get() = this.toGearyOrNull()?.get<BlockyAssociatedSeats>()?.seats ?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.blocky.api.events.furniture

import org.bukkit.Location
import org.bukkit.entity.Entity
import org.bukkit.entity.Player
import org.bukkit.event.Cancellable
Expand All @@ -13,7 +14,7 @@ class BlockyFurnitureInteractEvent(
val player: Player,
val hand: EquipmentSlot,
val itemInHand: ItemStack,
val clickedRelativePosition: Vector?,
val interactionPoint: Location = entity.location,
) : BlockyFurnitureEvent(entity), Cancellable {

private var cancelled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,31 @@ package com.mineinabyss.blocky.helpers

import com.comphenix.protocol.events.PacketContainer
import com.comphenix.protocol.wrappers.BlockPosition
import com.mineinabyss.blocky.api.BlockyFurnitures
import com.mineinabyss.blocky.api.BlockyFurnitures.isBlockyFurniture
import com.mineinabyss.blocky.api.BlockyFurnitures.isModelEngineFurniture
import com.mineinabyss.blocky.api.events.furniture.BlockyFurnitureInteractEvent
import com.mineinabyss.blocky.blocky
import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.components.features.BlockyLight
import com.mineinabyss.blocky.components.features.furniture.BlockyModelEngine
import com.mineinabyss.blocky.helpers.FurnitureHelpers.collisionHitboxPositions
import com.mineinabyss.blocky.helpers.GenericHelpers.toBlockCenterLocation
import com.mineinabyss.blocky.helpers.GenericHelpers.toEntity
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.protocolburrito.dsl.protocolManager
import com.mineinabyss.protocolburrito.dsl.sendTo
import com.mineinabyss.protocolburrito.packets.ServerboundPlayerActionPacketWrap
import com.mineinabyss.protocolburrito.packets.ServerboundUseItemOnPacketWrap
import com.ticxo.modelengine.api.ModelEngineAPI
import it.unimi.dsi.fastutil.ints.IntList
import net.minecraft.core.BlockPos
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket
import net.minecraft.network.syncher.EntityDataSerializers
import net.minecraft.network.syncher.SynchedEntityData
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.EntityType
import net.minecraft.world.phys.Vec3
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.block.data.type.Light
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack
import org.bukkit.entity.ItemDisplay
import org.bukkit.entity.Player
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.util.Vector
import org.joml.Vector3f
import java.util.*

Expand All @@ -63,40 +52,12 @@ object FurniturePacketHelpers {
private val hitboxOutlineIdMap = mutableMapOf<FurnitureUUID, IntList>()
private val outlinePlayerMap = mutableMapOf<UUID, UUID>()

fun getBaseFurnitureFromInteractionEntity(id: Int) =
fun baseFurnitureFromInteractionHitbox(id: Int) =
interactionHitboxIdMap.firstOrNull { id in it.entityIds }?.furniture

fun getBaseFurnitureFromCollisionHitbox(pos: BlockPosition) =
fun baseFurnitureFromCollisionHitbox(pos: BlockPosition) =
collisionHitboxPosMap.entries.firstOrNull { pos in it.value }?.key?.toEntity() as? ItemDisplay

internal fun registerPacketListeners() {

protocolManager(blocky.plugin) {
// Handles left-clicks with Collision Barrier hitbox
onReceive<ServerboundPlayerActionPacketWrap> { wrap ->
Bukkit.getScheduler().callSyncMethod(blocky.plugin) {
getBaseFurnitureFromCollisionHitbox(wrap.pos)?.let { baseFurniture ->
isCancelled = true
if ((wrap.handle as ServerboundPlayerActionPacket).action == ServerboundPlayerActionPacket.Action.START_DESTROY_BLOCK)
BlockyFurnitures.removeFurniture(baseFurniture, player)
}

}
}
// Handles right-clicks with Collision Barrier hitbox
// Cancelled so client doesn't remove the "Ghost Block"
onReceive<ServerboundUseItemOnPacketWrap> { wrap ->
Bukkit.getScheduler().callSyncMethod(blocky.plugin) {
getBaseFurnitureFromCollisionHitbox(BlockPosition.getConverter().getSpecific(wrap.blockHit.blockPos))?.let { baseFurniture ->
val clickedRelativePosition = Vector(wrap.blockHit.location.x, wrap.blockHit.location.y, wrap.blockHit.location.z)
isCancelled = true

BlockyFurnitureInteractEvent(baseFurniture, player, EquipmentSlot.HAND, player.inventory.itemInMainHand, clickedRelativePosition).callEvent()}
}
}
}
}

/**
* Sends a packet to show the interaction hitbox of the given furniture to the given player.
* @param furniture The furniture to show the interaction hitbox of.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import com.mineinabyss.blocky.helpers.*
import com.mineinabyss.geary.papermc.tracking.entities.events.GearyEntityAddToWorldEvent
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.idofront.messaging.broadcastVal
import com.mineinabyss.idofront.messaging.logSuccess
import com.mineinabyss.idofront.plugin.Plugins
import com.ticxo.modelengine.api.events.BaseEntityInteractEvent
import io.papermc.paper.event.packet.PlayerChunkLoadEvent
Expand All @@ -34,6 +32,8 @@ import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockDamageEvent
import org.bukkit.event.player.PlayerChangedWorldEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerQuitEvent
Expand Down Expand Up @@ -132,27 +132,50 @@ class BlockyFurnitureListener : Listener {

@EventHandler
fun PlayerUseUnknownEntityEvent.onInteract() {
val baseFurniture = FurniturePacketHelpers.getBaseFurnitureFromInteractionEntity(entityId) ?: return
val baseFurniture = FurniturePacketHelpers.baseFurnitureFromInteractionHitbox(entityId) ?: return
blocky.plugin.launch(blocky.plugin.minecraftDispatcher) {
when {
isAttack -> BlockyFurnitures.removeFurniture(baseFurniture, player)
else -> BlockyFurnitureInteractEvent(
baseFurniture, player, hand, player.inventory.itemInMainHand, clickedRelativePosition
baseFurniture, player, hand, player.inventory.itemInMainHand, baseFurniture.location.add(clickedRelativePosition ?: Vector())
).callEvent()
}
}
}

@EventHandler
fun PlayerInteractEvent.onInteract() {
val baseFurniture = FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(interactionPoint?.block?.toBlockPos() ?: return) ?: return
BlockyFurnitureInteractEvent(baseFurniture, player, hand!!, player.inventory.itemInMainHand, interactionPoint ?: baseFurniture.location).callEvent()
}

@EventHandler(priority = EventPriority.LOWEST)
fun BlockDamageEvent.onCollisionHitboxDamage() {
//TODO Implement custom break speed logic for this
// Should work no issues
FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(block.toBlockPos())?.let {
BlockyFurnitures.removeFurniture(it, player)
}
}

@EventHandler(priority = EventPriority.LOWEST)
fun BlockBreakEvent.onCollisionHitboxBreak() {
// Mainly for players in creative-mode
FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(block.toBlockPos())?.let {
BlockyFurnitures.removeFurniture(it, player)
}
}

init {
if (Plugins.isEnabled("ModelEngine")) {
blocky.logger.iSuccess("ModelEngine detected, enabling ModelEngine-Furniture-Interaction Listener!")
blocky.logger.s("ModelEngine detected, enabling ModelEngine-Furniture-Interaction Listener!")
Bukkit.getPluginManager().registerEvents(object : Listener {
@EventHandler
fun BaseEntityInteractEvent.onModelEngineInteract() {
val baseEntity = (baseEntity.original as? ItemDisplay)?.takeIf { it.isBlockyFurniture } ?: return
when {
action == BaseEntityInteractEvent.Action.ATTACK -> BlockyFurnitures.removeFurniture(baseEntity, player)
else -> BlockyFurnitureInteractEvent(baseEntity, player, slot, player.inventory.itemInMainHand, clickedPosition).callEvent()
else -> BlockyFurnitureInteractEvent(baseEntity, player, slot, player.inventory.itemInMainHand, baseEntity.location.add(clickedPosition ?: Vector())).callEvent()
}
}
}, blocky.plugin)
Expand All @@ -163,7 +186,7 @@ class BlockyFurnitureListener : Listener {
fun BlockyFurnitureInteractEvent.onSitting() {
if (!ProtectionLib.canInteract(player, entity.location) || player.isSneaking) return

player.sitOnBlockySeat(baseEntity, baseEntity.location.add(clickedRelativePosition ?: Vector()))
player.sitOnBlockySeat(baseEntity, interactionPoint)
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BlockyMiddleClickListener : Listener {
.apply { addAll(CopperHelpers.BLOCKY_SLABS).apply { addAll(CopperHelpers.BLOCKY_STAIRS) } }) -> {
val lookingAtPrefab = player.getTargetBlockExact(5, FluidCollisionMode.NEVER)?.prefabKey ?:
player.getTargetEntity(5)?.prefabKey ?: player.getTargetBlockExact(5)?.toBlockPos()
?.let { FurniturePacketHelpers.getBaseFurnitureFromCollisionHitbox(it) }?.prefabKey ?: return
?.let { FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(it) }?.prefabKey ?: return
val prefabKey = lookingAtPrefab.toEntityOrNull()?.get<BlockyDirectional>()?.parentBlock ?: lookingAtPrefab

val existingSlot = (0..8).firstOrNull {
Expand Down

0 comments on commit f377c81

Please sign in to comment.