Skip to content

Commit

Permalink
feat: allow multi-seats
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed May 13, 2024
1 parent 23e7401 commit 89b68b4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mineinabyss.blocky.components.features.furniture

import com.mineinabyss.geary.serialization.serializers.InnerSerializer
import com.mineinabyss.idofront.serialization.VectorSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.SetSerializer
import org.bukkit.util.Vector

@Serializable(BlockySeats.Serializer::class)
class BlockySeats(val offsets: Set<@Serializable(VectorSerializer::class) Vector> = emptySet()) {
class Serializer : InnerSerializer<Set<Vector>, BlockySeats>(
serialName = "blocky:seats",
inner = SetSerializer(VectorSerializer),
inverseTransform = { it.offsets },
transform = ::BlockySeats
)
}
30 changes: 17 additions & 13 deletions src/main/kotlin/com/mineinabyss/blocky/helpers/FurnitureHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package com.mineinabyss.blocky.helpers
import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.components.features.BlockyDrops
import com.mineinabyss.blocky.components.features.furniture.BlockyAssociatedSeats
import com.mineinabyss.blocky.components.features.furniture.BlockySeat
import com.mineinabyss.blocky.components.features.furniture.BlockySeats
import com.mineinabyss.blocky.helpers.GenericHelpers.toBlockCenterLocation
import com.mineinabyss.geary.papermc.tracking.entities.helpers.spawnFromPrefab
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.serialization.getOrSetPersisting
import com.mineinabyss.geary.serialization.setPersisting
import com.mineinabyss.idofront.items.asColorable
import com.mineinabyss.idofront.spawning.spawn
Expand All @@ -27,7 +26,6 @@ import org.bukkit.entity.ItemDisplay.ItemDisplayTransform.FIXED
import org.bukkit.entity.ItemDisplay.ItemDisplayTransform.NONE
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import kotlin.math.max

object FurnitureHelpers {
fun targetBlock(placedAgainst: Block, blockFace: BlockFace): Block? {
Expand Down Expand Up @@ -98,16 +96,22 @@ object FurnitureHelpers {
}.getOrThrow() as? ItemDisplay
}

fun spawnFurnitureSeat(furniture: ItemDisplay, seat: BlockySeat) {
furniture.location.add(seat.offset).spawn<ArmorStand> {
isPersistent = false
isVisible = false
isMarker = true
isSilent = true
isSmall = true
setGravity(false)
setRotation(furniture.yaw, 0F)
}?.let { furniture.toGeary().getOrSetPersisting { BlockyAssociatedSeats() }._seats.add(it.uniqueId) }
fun spawnFurnitureSeat(furniture: ItemDisplay, seats: BlockySeats) {
furniture.toGeary().setPersisting(
BlockyAssociatedSeats(
seats.offsets.mapNotNull { seatOffset ->
furniture.location.add(seatOffset).spawn<ArmorStand> {
isPersistent = false
isVisible = false
isMarker = true
isSilent = true
isSmall = true
setGravity(false)
setRotation(furniture.yaw, 0F)
}?.uniqueId
}.toMutableList()
)
)
}

fun clearFurnitureSeats(furniture: ItemDisplay) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.mineinabyss.blocky.api.events.furniture.BlockyFurniturePlaceEvent
import com.mineinabyss.blocky.blocky
import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.components.features.BlockyPlacableOn
import com.mineinabyss.blocky.components.features.furniture.BlockySeat
import com.mineinabyss.blocky.components.features.furniture.BlockySeats
import com.mineinabyss.blocky.helpers.*
import com.mineinabyss.geary.papermc.tracking.entities.events.GearyEntityAddToWorldEvent
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
Expand Down Expand Up @@ -191,7 +191,7 @@ class BlockyFurnitureListener : Listener {

@EventHandler(priority = EventPriority.HIGHEST)
fun PlayerQuitEvent.onQuit() {
(player.vehicle as? ArmorStand)?.toGearyOrNull()?.get<BlockySeat>() ?: return
(player.vehicle as? ArmorStand)?.toGearyOrNull()?.get<BlockySeats>() ?: return
player.leaveVehicle()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.mineinabyss.blocky.systems.actions

import com.mineinabyss.blocky.blocky
import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.components.features.furniture.BlockySeat
import com.mineinabyss.blocky.components.features.furniture.BlockySeats
import com.mineinabyss.blocky.helpers.FurnitureHelpers
import com.mineinabyss.geary.modules.GearyModule
import com.mineinabyss.geary.observers.events.OnSet
Expand All @@ -13,13 +13,13 @@ import org.bukkit.Bukkit
import org.bukkit.entity.ItemDisplay

fun GearyModule.createFurnitureSeatSetter() = observe<OnSet>()
.involving(query<BukkitEntity, BlockyFurniture, BlockySeat>())
.exec { (entity, _, seat) ->
.involving(query<BukkitEntity, BlockyFurniture, BlockySeats>())
.exec { (entity, _, seats) ->
val display = entity as? ItemDisplay ?: return@exec
val seat = seat
val seats = seats

FurnitureHelpers.clearFurnitureSeats(display)
Bukkit.getScheduler().scheduleSyncDelayedTask(blocky.plugin, {
FurnitureHelpers.spawnFurnitureSeat(display, seat)
FurnitureHelpers.spawnFurnitureSeat(display, seats)
}, 1L)
}

0 comments on commit 89b68b4

Please sign in to comment.