Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start refactoring to geary setter pattern #82

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mineinabyss.blocky
import com.github.shynixn.mccoroutine.bukkit.launch
import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.components.features.blocks.BlockyDirectional
import com.mineinabyss.blocky.helpers.asRGBColorable
import com.mineinabyss.blocky.helpers.gearyInventory
import com.mineinabyss.blocky.menus.BlockyMainMenu
import com.mineinabyss.blocky.systems.BlockyBlockQuery.prefabKey
Expand All @@ -26,9 +27,6 @@ import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.meta.LeatherArmorMeta
import org.bukkit.inventory.meta.MapMeta
import org.bukkit.inventory.meta.PotionMeta

@OptIn(UnsafeAccessors::class)
class BlockyCommandExecutor : IdofrontCommandExecutor(), TabCompleter {
Expand Down Expand Up @@ -78,9 +76,10 @@ class BlockyCommandExecutor : IdofrontCommandExecutor(), TabCompleter {
}

item.editItemMeta {
(this as? LeatherArmorMeta)?.setColor(color.toColor)
?: (this as? PotionMeta)?.setColor(color.toColor)
?: (this as? MapMeta)?.setColor(color.toColor) ?: return@playerAction
(asRGBColorable() ?: run {
player.error("This item cannot be dyed.")
return@playerAction
0ffz marked this conversation as resolved.
Show resolved Hide resolved
}).color = color.toColor
}
player.success("Dyed item to <$color>$color")
}
Expand Down
25 changes: 11 additions & 14 deletions src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.mineinabyss.blocky

import com.comphenix.protocol.PacketType
import com.comphenix.protocol.ProtocolLibrary
import com.comphenix.protocol.events.ListenerPriority
import com.comphenix.protocol.events.PacketAdapter
import com.comphenix.protocol.events.PacketEvent
import com.mineinabyss.blocky.api.BlockyFurnitures
import com.mineinabyss.blocky.api.events.furniture.BlockyFurnitureInteractEvent
import com.mineinabyss.blocky.assets_generation.MoreCreativeTabsGeneration
import com.mineinabyss.blocky.assets_generation.ResourcepackGeneration
import com.mineinabyss.blocky.compatibility.worldedit.WorldEditListener
import com.mineinabyss.blocky.compatibility.worldedit.WorldEditSupport
import com.mineinabyss.blocky.helpers.FurniturePacketHelpers
import com.mineinabyss.blocky.listeners.*
import com.mineinabyss.blocky.systems.AttemptSpawnFurnitureSystem
import com.mineinabyss.blocky.systems.FurnitureOutlineSystem
import com.mineinabyss.blocky.systems.FurnitureTrackerSystem
import com.mineinabyss.blocky.systems.actions.SetItemOnFurnitureSystem
import com.mineinabyss.blocky.systems.actions.SetMEGModelOnFurnitureSystem
import com.mineinabyss.blocky.systems.actions.SetSeatOnFurnitureSystem
import com.mineinabyss.geary.addons.GearyPhase
import com.mineinabyss.geary.autoscan.autoscan
import com.mineinabyss.geary.modules.geary
Expand All @@ -24,18 +20,13 @@ import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.messaging.logError
import com.mineinabyss.idofront.plugin.Plugins
import com.mineinabyss.idofront.plugin.listeners
import com.mineinabyss.protocolburrito.dsl.protocolManager
import com.mineinabyss.protocolburrito.packets.ServerboundPlayerActionPacketWrap
import com.mineinabyss.protocolburrito.packets.ServerboundUseItemOnPacketWrap
import com.sk89q.worldedit.WorldEdit
import io.papermc.paper.configuration.GlobalConfiguration
import it.unimi.dsi.fastutil.ints.IntArrayList
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket
import net.minecraft.resources.ResourceLocation
import net.minecraft.tags.BlockTags
import org.bukkit.block.data.BlockData
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.plugin.java.JavaPlugin

var prefabMap = mapOf<BlockData, PrefabKey>()
Expand All @@ -58,7 +49,13 @@ class BlockyPlugin : JavaPlugin() {

BlockyCommandExecutor()

geary.pipeline.addSystems(FurnitureTrackerSystem(), FurnitureOutlineSystem())
geary.pipeline.addSystems(
AttemptSpawnFurnitureSystem(),
SetItemOnFurnitureSystem(),
SetSeatOnFurnitureSystem(),
SetMEGModelOnFurnitureSystem(),
FurnitureOutlineSystem()
)
FurniturePacketHelpers.registerPacketListeners()

listeners(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mineinabyss.blocky.components.core

import com.mineinabyss.blocky.serializers.BrightnessSerializer
import com.mineinabyss.idofront.serialization.ColorSerializer
import com.mineinabyss.idofront.serialization.SerializableItemStack
import com.mineinabyss.idofront.serialization.Vector3fSerializer
import com.mineinabyss.idofront.serialization.toSerializable
Expand Down Expand Up @@ -85,4 +86,8 @@ data class BlockyFurniture(
data class PreventItemStackUpdate(
val forceWhenDifferentMaterial: Boolean = true,
)

@Serializable
@SerialName("blocky:furniture_color")
data class Color(val color: @Serializable(with = ColorSerializer::class) org.bukkit.Color)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.mineinabyss.blocky.components.features.furniture

import com.mineinabyss.geary.datatypes.ComponentDefinition
import com.mineinabyss.geary.papermc.bridge.events.EventHelpers
import com.mineinabyss.geary.papermc.bridge.events.entities.OnSpawn
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("blocky:modelengine")
data class BlockyModelEngine(val modelId: String)
data class BlockyModelEngine(val modelId: String) {
companion object : ComponentDefinition by EventHelpers.defaultTo<OnSpawn>()
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.mineinabyss.blocky.components.features.furniture

import com.mineinabyss.geary.datatypes.ComponentDefinition
import com.mineinabyss.geary.papermc.bridge.events.EventHelpers
import com.mineinabyss.geary.papermc.bridge.events.entities.OnSpawn
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("blocky:seat")
data class BlockySeat (val heightOffset: Double = 0.0)
data class BlockySeat(val heightOffset: Double = 0.0) {
companion object : ComponentDefinition by EventHelpers.defaultTo<OnSpawn>()
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package com.mineinabyss.blocky.helpers

import com.github.shynixn.mccoroutine.bukkit.launch
import com.mineinabyss.blocky.blocky
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.BlockyModelEngine
import com.mineinabyss.blocky.components.features.furniture.BlockySeat
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.papermc.tracking.items.components.SetItem
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.idofront.items.editItemMeta
import com.mineinabyss.idofront.messaging.broadcastVal
import com.mineinabyss.idofront.plugin.Plugins
import com.mineinabyss.idofront.spawning.spawn
import com.ticxo.modelengine.api.ModelEngineAPI
import io.papermc.paper.math.Position
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import net.kyori.adventure.text.Component
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.Rotation
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
Expand All @@ -33,9 +23,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 org.bukkit.inventory.meta.LeatherArmorMeta
import org.bukkit.inventory.meta.MapMeta
import org.bukkit.inventory.meta.PotionMeta
import kotlin.math.max

object FurnitureHelpers {
Expand All @@ -44,13 +31,25 @@ object FurnitureHelpers {
else placedAgainst.getRelative(blockFace).takeUnless { !it.type.isAir && it.isReplaceable }
}

fun collisionHitboxLocations(rotation: Float, center: Location, hitbox: Set<BlockyFurniture.CollisionHitbox>): List<Location> =
fun collisionHitboxLocations(
rotation: Float,
center: Location,
hitbox: Set<BlockyFurniture.CollisionHitbox>
): List<Location> =
hitbox.map { c -> c.location.groundRotate(rotation).add(center) }

fun collisionHitboxPositions(rotation: Float, center: Location, hitbox: Set<BlockyFurniture.CollisionHitbox>): List<Position> =
fun collisionHitboxPositions(
rotation: Float,
center: Location,
hitbox: Set<BlockyFurniture.CollisionHitbox>
): List<Position> =
collisionHitboxLocations(rotation, center, hitbox).map { Position.block(it) }

fun interactionHitboxLocations(rotation: Float, center: Location, hitbox: Set<BlockyFurniture.InteractionHitbox>): List<Location> =
fun interactionHitboxLocations(
rotation: Float,
center: Location,
hitbox: Set<BlockyFurniture.InteractionHitbox>
): List<Location> =
hitbox.map { c -> c.originOffset.groundRotate(rotation).add(center) }

fun rotation(yaw: Float, nullFurniture: BlockyFurniture?): Rotation {
Expand All @@ -65,9 +64,11 @@ object FurnitureHelpers {
fun yaw(rotation: Rotation) = Rotation.entries.indexOf(rotation) * 360f / 8f

fun hasEnoughSpace(blockyFurniture: BlockyFurniture, loc: Location, yaw: Float): Boolean {
return if (blockyFurniture.collisionHitbox.isEmpty() && blockyFurniture.interactionHitbox.isEmpty()) true
else collisionHitboxLocations(yaw, loc, blockyFurniture.collisionHitbox).all { adjacent -> adjacent.block.isReplaceable } &&
interactionHitboxLocations(yaw, loc, blockyFurniture.interactionHitbox).all { adjacent -> adjacent.block.isReplaceable }
if (blockyFurniture.collisionHitbox.isEmpty() && blockyFurniture.interactionHitbox.isEmpty()) return true

return collisionHitboxLocations(yaw, loc, blockyFurniture.collisionHitbox)
.plus(interactionHitboxLocations(yaw, loc, blockyFurniture.interactionHitbox))
.all { adjacent -> adjacent.block.isReplaceable }
}


Expand All @@ -79,72 +80,43 @@ object FurnitureHelpers {
): ItemDisplay? {
val gearyEntity = prefabKey.toEntityOrNull() ?: return null
val furniture = gearyEntity.get<BlockyFurniture>() ?: return null
val itemStack = furniture.properties.itemStack?.toItemStackOrNull() ?: item ?: gearyEntity.get<SetItem>()?.item?.toItemStack() ?: return null
val furnitureItem = itemStack.clone().editItemMeta {
displayName(Component.empty())
(this as? LeatherArmorMeta)?.setColor((itemStack.itemMeta as? LeatherArmorMeta)?.color)
?: (this as? PotionMeta)?.setColor((itemStack.itemMeta as? PotionMeta)?.color)
?: (this as? MapMeta)?.setColor((itemStack.itemMeta as? MapMeta)?.color) ?: return@editItemMeta
}

val spawnLoc = loc.clone().toBlockCenterLocation().apply {
if (furniture.properties.displayTransform == NONE) this.y += 0.5 * furniture.properties.scale.y
this.yaw = yaw(rotation(yaw, furniture))
this.pitch = if (furniture.properties.displayTransform == FIXED) 90f else 0f
}

val newFurniture = spawnLoc.spawn<ItemDisplay> {
isPersistent = furniture.properties.persistent

furniture.properties.let { properties ->
itemDisplayTransform = properties.displayTransform
displayWidth = properties.displayWidth
displayHeight = properties.displayHeight
brightness = properties.brightness
billboard = properties.trackingRotation
properties.viewRange?.let { viewRange = it }
properties.shadowRadius?.let { shadowRadius = it }
properties.shadowStrength?.let { shadowStrength = it }
transformation = transformation.apply { scale.set(properties.scale) }
}
this.itemStack = furnitureItem
} ?: return null

blocky.plugin.launch { newFurniture.delayUntilTracked().extend(gearyEntity) }
gearyEntity.get<BlockyModelEngine>()?.let { meg ->
if (!Plugins.isEnabled("ModelEngine")) return@let
val activeModel = ModelEngineAPI.createActiveModel(meg.modelId) ?: return@let
ModelEngineAPI.getOrCreateModeledEntity(newFurniture).apply {
addModel(activeModel, false)
isBaseEntityVisible = false
isModelRotationLocked = false
}
}
// Try to get held item's color, used to dye furniture

gearyEntity.get<BlockySeat>()?.let { seat ->
if (furniture.collisionHitbox.isNotEmpty()) {
collisionHitboxLocations(yaw, newFurniture.location, furniture.collisionHitbox)
.forEach { _ -> spawnFurnitureSeat(newFurniture, yaw - 180, seat.heightOffset) }
} else spawnFurnitureSeat(newFurniture, yaw, seat.heightOffset)
}
val color = item?.itemMeta?.asRGBColorable()?.color

return newFurniture
return spawnLoc.spawnFromPrefab(prefabKey, initEvent = {
if (color != null) set(BlockyFurniture.Color(color))
}).getOrThrow() as ItemDisplay
}

//TODO Fix seat breaking below 0.0 offset and remove max() check here
fun spawnFurnitureSeat(furniture: ItemDisplay, yaw: Float, height: Double = 0.0) =
furniture.location.toBlockCenterLocation().apply { y += max(0.0, height) }.spawn<ArmorStand> {
fun spawnFurnitureSeat(furniture: ItemDisplay, yaw: Float, height: Double = 0.0, location: Location) =
location.toBlockCenterLocation().apply { y += max(0.0, height) }.spawn<ArmorStand> {
isVisible = false
isMarker = true
isSilent = true
isSmall = true
setGravity(false)
setRotation(yaw, 0F)
}?.let { seat ->
seat.isPersistent = false
furniture.toGeary().getOrSetPersisting { BlockyAssociatedSeats() }._seats.add(seat.uniqueId)
seat
}

fun clearFurnitureSeats(furniture: ItemDisplay) {
val gearyFurniture = furniture.toGearyOrNull() ?: return
gearyFurniture.get<BlockyAssociatedSeats>()?.seats?.forEach { it.remove() }
gearyFurniture.remove<BlockyAssociatedSeats>()
}

internal fun ItemDisplay.handleFurnitureDrops(player: Player) {
this.toGearyOrNull()?.get<BlockyDrops>()?.let { GenericHelpers.handleBlockDrop(it, player, location) }
}
Expand Down
44 changes: 44 additions & 0 deletions src/main/kotlin/com/mineinabyss/blocky/helpers/ItemMetaHelpers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.mineinabyss.blocky.helpers

import org.bukkit.Color
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.inventory.meta.LeatherArmorMeta
import org.bukkit.inventory.meta.MapMeta
import org.bukkit.inventory.meta.PotionMeta

interface RGBColorable {
var color: Color?
}

/**
* These different ItemMeta classes don't share a common color property so we use this :(
*/
fun ItemMeta.asRGBColorable(): RGBColorable? {
return when (val meta = this) {
is LeatherArmorMeta -> object : RGBColorable {
override var color: Color?
get() = meta.color
set(value) {
meta.setColor(value)
}
}

is PotionMeta -> object : RGBColorable {
override var color: Color?
get() = meta.color
set(value) {
meta.color = value
}
}

is MapMeta -> object : RGBColorable {
override var color: Color?
get() = meta.color
set(value) {
meta.color = value
}
}

else -> null
}
}
Loading
Loading