Skip to content

Commit

Permalink
fix: BlockySeat functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jan 22, 2024
1 parent f17b133 commit 853d285
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object BlockyFurnitures {
val ItemDisplay.seats: List<Entity>
get() = this.toGearyOrNull()?.get<BlockyAssociatedSeats>()?.seats ?: emptyList()

fun getBlockySeat(itemDisplay: ItemDisplay, location: Location = itemDisplay.location): Entity? =
fun blockySeat(itemDisplay: ItemDisplay, location: Location = itemDisplay.location): Entity? =
itemDisplay.seats.minByOrNull { it.location.distanceSquared(location) }

val ItemDisplay.blockySeat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.bukkit.craftbukkit.v1_20_R2.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 Down Expand Up @@ -91,13 +92,10 @@ object FurniturePacketHelpers {
onReceive<ServerboundUseItemOnPacketWrap> { wrap ->
Bukkit.getScheduler().callSyncMethod(blocky.plugin) {
getBaseFurnitureFromCollisionHitbox(wrap.blockHit.blockPos)?.let { baseFurniture ->
isCancelled = true
BlockyFurnitureInteractEvent(
baseFurniture, player,
EquipmentSlot.HAND, player.inventory.itemInMainHand,
null, null
).callEvent()
}
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()}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent
import com.destroystokyo.paper.event.player.PlayerUseUnknownEntityEvent
import com.github.shynixn.mccoroutine.bukkit.launch
import com.github.shynixn.mccoroutine.bukkit.minecraftDispatcher
import com.mineinabyss.blocky.api.BlockyFurnitures.getBlockySeat
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.BlockyFurnitures.removeFurniture
import com.mineinabyss.blocky.api.events.furniture.BlockyFurnitureInteractEvent
import com.mineinabyss.blocky.api.events.furniture.BlockyFurniturePlaceEvent
import com.mineinabyss.blocky.blocky
Expand Down Expand Up @@ -107,7 +105,7 @@ class BlockyFurnitureListener : Listener {
val newFurniture = FurnitureHelpers.placeBlockyFurniture(prefabKey, targetBlock.location, yaw, item) ?: return

if (!BlockyFurniturePlaceEvent(newFurniture, player, hand, item).callEvent()) {
removeFurniture(newFurniture)
BlockyFurnitures.removeFurniture(newFurniture)
return
}

Expand All @@ -121,7 +119,7 @@ class BlockyFurnitureListener : Listener {
val baseFurniture = FurniturePacketHelpers.getBaseFurnitureFromInteractionEntity(entityId) ?: return
blocky.plugin.launch(blocky.plugin.minecraftDispatcher) {
when {
isAttack -> removeFurniture(baseFurniture, player)
isAttack -> BlockyFurnitures.removeFurniture(baseFurniture, player)
else -> BlockyFurnitureInteractEvent(
baseFurniture, player, hand, player.inventory.itemInMainHand, clickedRelativePosition
).callEvent()
Expand All @@ -135,13 +133,10 @@ class BlockyFurnitureListener : Listener {
Bukkit.getPluginManager().registerEvents(object : Listener {
@EventHandler
fun BaseEntityInteractEvent.onModelEngineInteract() {
val baseEntity = baseEntity.original as? ItemDisplay ?: return
if (!baseEntity.isBlockyFurniture || !baseEntity.isModelEngineFurniture) return
val baseEntity = (baseEntity.original as? ItemDisplay)?.takeIf { it.isBlockyFurniture } ?: return
when {
action == BaseEntityInteractEvent.Action.ATTACK -> removeFurniture(baseEntity, player)
else -> BlockyFurnitureInteractEvent(
baseEntity, player, slot, player.inventory.itemInMainHand, clickedPosition
).callEvent()
action == BaseEntityInteractEvent.Action.ATTACK -> BlockyFurnitures.removeFurniture(baseEntity, player)
else -> BlockyFurnitureInteractEvent(baseEntity, player, slot, player.inventory.itemInMainHand, clickedPosition).callEvent()
}
}
}, blocky.plugin)
Expand All @@ -162,6 +157,6 @@ class BlockyFurnitureListener : Listener {
}

private fun Player.sitOnBlockySeat(entity: ItemDisplay, location: Location = entity.location) {
if (this.passengers.isEmpty()) getBlockySeat(entity, location)?.addPassenger(this)
BlockyFurnitures.blockySeat(entity, location)?.addPassenger(this)
}
}

0 comments on commit 853d285

Please sign in to comment.