Skip to content

Commit

Permalink
refactor: Update for geary DI rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Oct 26, 2024
1 parent fcf34fa commit 1986efe
Show file tree
Hide file tree
Showing 33 changed files with 414 additions and 250 deletions.
10 changes: 10 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ tasks {
}
}

kotlin {
compilerOptions {
freeCompilerArgs.addAll(
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
"-opt-in=kotlin.ExperimentalUnsignedTypes",
"-Xcontext-receivers"
)
}
}

paper {
main = "com.mineinabyss.blocky.BlockyPlugin"
bootstrapper = "com.mineinabyss.blocky.BlockyBootstrap"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group=com.mineinabyss
version=0.10
idofrontVersion=0.25.7
idofrontVersion=0.25.17-dev.0

2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
gearyPaper = "0.30.20"
gearyPaper = "0.31.0-dev.2"
guiy="0.12.4"

[libraries]
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/com/mineinabyss/blocky/BlockyAddon.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mineinabyss.blocky

import com.mineinabyss.blocky.systems.actions.createFurnitureItemSetter
import com.mineinabyss.blocky.systems.actions.createFurnitureMEGModelSetter
import com.mineinabyss.blocky.systems.actions.createFurnitureSeatSetter
import com.mineinabyss.blocky.systems.actions.furnitureHitboxSetter
import com.mineinabyss.blocky.systems.createFurnitureOutlineSystem
import com.mineinabyss.blocky.systems.createFurnitureSpawner
import com.mineinabyss.geary.addons.dsl.createAddon
import com.mineinabyss.geary.autoscan.autoscan

val BlockyAddon = createAddon("Blocky", configuration = {
autoscan(BlockyPlugin::class.java.classLoader, "com.mineinabyss.blocky") {
components()
}
}) {
systems {
createFurnitureOutlineSystem()
createFurnitureSpawner()
createFurnitureItemSetter()
createFurnitureSeatSetter()
createFurnitureMEGModelSetter()
furnitureHitboxSetter()
}
}
16 changes: 11 additions & 5 deletions src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import com.github.shynixn.mccoroutine.bukkit.launch
import com.mineinabyss.blocky.assets_generation.ResourcepackGeneration
import com.mineinabyss.blocky.menus.BlockyMainMenu
import com.mineinabyss.blocky.systems.allBlockyPrefabs
import com.mineinabyss.geary.papermc.tracking.items.gearyItems
import com.mineinabyss.geary.papermc.gearyPaper
import com.mineinabyss.geary.papermc.toGeary
import com.mineinabyss.geary.papermc.tracking.items.ItemTracking
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.prefabs.prefabs
import com.mineinabyss.geary.prefabs.Prefabs
import com.mineinabyss.guiy.inventory.guiy
import com.mineinabyss.idofront.commands.brigadier.commands
import com.mineinabyss.idofront.messaging.error
Expand All @@ -23,6 +25,8 @@ object BlockyBrigadierCommands {
"blocky" {
"reload" {
executes {
val geary = gearyPaper.worldManager.global
val prefabs = geary.getAddon(Prefabs)
blocky.plugin.createBlockyContext()
blocky.plugin.launch {
val blockyPrefabs = blocky.prefabQuery.entities()
Expand All @@ -36,7 +40,7 @@ object BlockyBrigadierCommands {
blockyPrefabs.filter { it !in inheritedPrefabs }.sortedBy { it.prefabs.size }
.forEach { prefabs.loader.reload(it) }
}
ResourcepackGeneration().generateDefaultAssets()
ResourcepackGeneration(geary).generateDefaultAssets()
sender.success("Blocky has been reloaded!")
}
}
Expand All @@ -46,7 +50,9 @@ object BlockyBrigadierCommands {
}
val amount by IntegerArgumentType.integer(1)
playerExecutes {
val (player, item, amount) = (executor as? Player ?: return@playerExecutes) to item()!! to amount()!!
val gearyItems = location.world.toGeary().getAddon(ItemTracking)
val (player, item, amount) = (executor as? Player
?: return@playerExecutes) to item()!! to amount()!!
if (player.inventory.firstEmpty() == -1) return@playerExecutes player.error("No empty slots in inventory")

val itemstack = gearyItems.createItem(PrefabKey.of(item.asString()))?.asQuantity(amount)
Expand All @@ -62,4 +68,4 @@ object BlockyBrigadierCommands {
}
}
}
}
}
36 changes: 10 additions & 26 deletions src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import com.jeff_media.customblockdata.CustomBlockData
import com.mineinabyss.blocky.assets_generation.ResourcepackGeneration
import com.mineinabyss.blocky.listeners.*
import com.mineinabyss.blocky.systems.*
import com.mineinabyss.blocky.systems.actions.createFurnitureItemSetter
import com.mineinabyss.blocky.systems.actions.createFurnitureMEGModelSetter
import com.mineinabyss.blocky.systems.actions.createFurnitureSeatSetter
import com.mineinabyss.blocky.systems.actions.furnitureHitboxSetter
import com.mineinabyss.geary.autoscan.autoscan
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.systems.builders.cache
import com.mineinabyss.geary.papermc.configure
import com.mineinabyss.geary.papermc.gearyPaper
import com.mineinabyss.idofront.config.config
import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.messaging.observeLogger
Expand All @@ -19,12 +14,9 @@ import io.papermc.paper.configuration.GlobalConfiguration
import org.bukkit.plugin.java.JavaPlugin

class BlockyPlugin : JavaPlugin() {

override fun onLoad() {
geary {
autoscan(classLoader, "com.mineinabyss.blocky") {
all()
}
gearyPaper.configure {
install(BlockyAddon)
}
}

Expand All @@ -33,16 +25,6 @@ class BlockyPlugin : JavaPlugin() {

BlockyBrigadierCommands.registerCommands()

geary.run {
createFurnitureOutlineSystem()
createFurnitureSpawner()
createFurnitureItemSetter()
createFurnitureSeatSetter()
createFurnitureMEGModelSetter()

furnitureHitboxSetter()
}

listeners(
BlockyGenericListener(),
BlockyFurnitureListener(),
Expand Down Expand Up @@ -72,19 +54,21 @@ class BlockyPlugin : JavaPlugin() {
if (caveVineBlocks.isEnabled) listeners(BlockyCaveVineListener())
}

ResourcepackGeneration().generateDefaultAssets()
ResourcepackGeneration(gearyPaper.worldManager.global).generateDefaultAssets()
}


fun createBlockyContext() {
DI.remove<BlockyContext>()
// TODO update to use per world syntax when geary adds it
val geary = gearyPaper.worldManager.global
val blockyContext = object : BlockyContext {
override val plugin = this@BlockyPlugin
override val logger by plugin.observeLogger()
override val config: BlockyConfig by config("config", dataFolder.toPath(), BlockyConfig())
override val prefabQuery = geary.cache(BlockyQuery())
override val blockQuery = geary.cache(BlockyBlockQuery())
override val furnitureQuery = geary.cache(BlockyFurnitureQuery())
override val prefabQuery = geary.cache(::BlockyQuery)
override val blockQuery = geary.cache(::BlockyBlockQuery)
override val furnitureQuery = geary.cache(::BlockyFurnitureQuery)
}
DI.add<BlockyContext>(blockyContext)
}
Expand Down
56 changes: 35 additions & 21 deletions src/main/kotlin/com/mineinabyss/blocky/api/BlockyBlocks.kt
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
package com.mineinabyss.blocky.api

import com.mineinabyss.blocky.helpers.*
import com.mineinabyss.blocky.helpers.decode
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.toEntityOrNull
import com.mineinabyss.geary.papermc.tracking.blocks.BlockTracking
import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock
import com.mineinabyss.geary.papermc.tracking.blocks.gearyBlocks
import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull
import com.mineinabyss.geary.papermc.tracking.entities.helpers.withGeary
import com.mineinabyss.geary.prefabs.PrefabKey
import org.bukkit.Location
import org.bukkit.block.Block
import org.bukkit.block.data.BlockData
import org.bukkit.inventory.ItemStack

object BlockyBlocks {
val Geary.gearyBlocks get() = getAddon(BlockTracking)

context(Geary) val ItemStack.isBlockyBlock get() = this.decode<SetBlock>() != null
context(Geary) val String.isBlockyBlock get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.has<SetBlock>() == true
context(Geary) val PrefabKey.isBlockyBlock get() = this.toEntityOrNull()?.has<SetBlock>() == true
context(Geary) val BlockData.isBlockyBlock get() = this.toGearyOrNull()?.has<SetBlock>() == true

val ItemStack.isBlockyBlock get() = this.decode<SetBlock>() != null
val String.isBlockyBlock get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.has<SetBlock>() == true
val PrefabKey.isBlockyBlock get() = this.toEntityOrNull()?.has<SetBlock>() == true
val Location.isBlockyBlock get() = block.toGearyOrNull()?.has<SetBlock>() == true
val Block.isBlockyBlock get() = this.toGearyOrNull()?.has<SetBlock>() == true
val BlockData.isBlockyBlock get() = this.toGearyOrNull()?.has<SetBlock>() == true

val String.isBlockyNoteBlock get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK
val PrefabKey.isBlockyNoteBlock get() = this.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK
val ItemStack.isBlockyNoteBlock get() = this.decode<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK
val Location.isBlockyNoteBlock get() = this.block.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK
context(Geary) val String.isBlockyNoteBlock
get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK
context(Geary) val PrefabKey.isBlockyNoteBlock
get() = this.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK
context(Geary) val ItemStack.isBlockyNoteBlock get() = this.decode<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK

val Location.isBlockyNoteBlock
get() = this.block.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK
val Block.isBlockyNoteBlock get() = this.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.NOTEBLOCK

val String.isBlockyWire get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.WIRE
val PrefabKey.isBlockyWire get() = this.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.WIRE
val ItemStack.isBlockyWire get() = this.decode<SetBlock>()?.blockType == SetBlock.BlockType.WIRE
context(Geary) val String.isBlockyWire
get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.WIRE
context(Geary) val PrefabKey.isBlockyWire
get() = this.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.WIRE
context(Geary) val ItemStack.isBlockyWire get() = this.decode<SetBlock>()?.blockType == SetBlock.BlockType.WIRE
val Location.isBlockyWire get() = this.block.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.WIRE
val Block.isBlockyWire get() = this.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.WIRE

val String.isBlockyCaveVine get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
val PrefabKey.isBlockyCaveVine get() = this.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
val ItemStack.isBlockyCaveVine get() = this.decode<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
val Location.isBlockyCaveVine get() = this.block.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
context(Geary) val String.isBlockyCaveVine
get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
context(Geary) val PrefabKey.isBlockyCaveVine
get() = this.toEntityOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
context(Geary) val ItemStack.isBlockyCaveVine get() = this.decode<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
val Location.isBlockyCaveVine
get() = this.block.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE
val Block.isBlockyCaveVine get() = this.toGearyOrNull()?.get<SetBlock>()?.blockType == SetBlock.BlockType.CAVEVINE

val PrefabKey.blockyBlock get() = this.toEntityOrNull()?.get<SetBlock>()
val ItemStack.blockyBlock get() = this.decode<SetBlock>()
context(Geary) val PrefabKey.blockyBlock get() = this.toEntityOrNull()?.get<SetBlock>()
context(Geary) val ItemStack.blockyBlock get() = this.decode<SetBlock>()
val Location.blockyBlock get() = this.block.toGearyOrNull()?.get<SetBlock>()
val Block.blockyBlock get() = this.toGearyOrNull()?.get<SetBlock>()

fun placeBlockyBlock(location: Location, prefabKey: PrefabKey): Boolean {
fun placeBlockyBlock(location: Location, prefabKey: PrefabKey): Boolean = location.withGeary {
val gearyEntity = prefabKey.toEntityOrNull() ?: return false
val blockyBlock = gearyEntity.get<SetBlock>() ?: return false
val blockData = gearyBlocks.createBlockData(prefabKey) ?: return false

return placeBlockyBlock(location, blockData)
}

internal fun placeBlockyBlock(location: Location, data: BlockData): Boolean {
internal fun placeBlockyBlock(location: Location, data: BlockData): Boolean = location.withGeary {
if (gearyBlocks.block2Prefab[data]?.toEntityOrNull()?.has<SetBlock>() != true) return false
location.block.blockData = data

Expand Down
21 changes: 13 additions & 8 deletions src/main/kotlin/com/mineinabyss/blocky/api/BlockyFurnitures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import com.mineinabyss.blocky.helpers.FurniturePacketHelpers
import com.mineinabyss.blocky.helpers.decode
import com.mineinabyss.blocky.helpers.toBlockPos
import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.toEntityOrNull
import com.mineinabyss.geary.papermc.toGeary
import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.papermc.tracking.items.gearyItems
import com.mineinabyss.geary.papermc.tracking.items.ItemTracking
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.idofront.events.call
import io.th0rgal.protectionlib.ProtectionLib
Expand All @@ -34,21 +37,21 @@ object BlockyFurnitures {
val GearyEntity.isModelEngineFurniture: Boolean get() = this.has<BlockyModelEngine>()

//TODO toGearyOrNull wouldnt work here as furniture isnt in geary
val Block.isBlockyFurniture get() = this.toGearyOrNull()?.isBlockyFurniture ?: false
val Block.isBlockyFurniture get() = with(world.toGeary()) { toGearyOrNull()?.isBlockyFurniture ?: false }
val GearyEntity.isBlockyFurniture get() = has<BlockyFurniture>() || this.isModelEngineFurniture
val Entity.isBlockyFurniture: Boolean
get() = when (this) {
is ItemDisplay -> this.toGearyOrNull()?.isBlockyFurniture == true
else -> false
} || isModelEngineFurniture
val String.isBlockyFurniture get() = PrefabKey.of(this).toEntityOrNull()?.isBlockyFurniture ?: false
val PrefabKey.isBlockyFurniture get() = this.toEntityOrNull()?.isBlockyFurniture ?: false
context(Geary) val String.isBlockyFurniture get() = PrefabKey.of(this).toEntityOrNull()?.isBlockyFurniture ?: false
context(Geary) val PrefabKey.isBlockyFurniture get() = this.toEntityOrNull()?.isBlockyFurniture ?: false

val ItemStack.blockyFurniture get() = this.decode<BlockyFurniture>()
val PrefabKey.blockyFurniture get() = this.toEntityOrNull()?.get<BlockyFurniture>()
context(Geary) val ItemStack.blockyFurniture get() = this.decode<BlockyFurniture>()
context(Geary) val PrefabKey.blockyFurniture get() = this.toEntityOrNull()?.get<BlockyFurniture>()

//TODO toGearyOrNull wouldnt work here as furniture isnt in geary
val Block.blockyFurniture get() = this.toGearyOrNull()?.get<BlockyFurniture>()
context(Geary) val Block.blockyFurniture get() = this.toGearyOrNull()?.get<BlockyFurniture>()

val Block.baseFurniture: ItemDisplay?
get() = FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(this.toBlockPos())
Expand All @@ -64,11 +67,13 @@ object BlockyFurnitures {
val ItemDisplay.blockySeat
get() = this.seats.minByOrNull { it.location.distanceSquared(this.location) }

context(Geary)
fun placeFurniture(prefabKey: PrefabKey, location: Location) =
placeFurniture(prefabKey, location, 0f)

context(Geary)
fun placeFurniture(prefabKey: PrefabKey, location: Location, yaw: Float) =
gearyItems.createItem(prefabKey)?.let { FurnitureHelpers.placeBlockyFurniture(prefabKey, location, yaw, it) }
getAddon(ItemTracking).createItem(prefabKey)?.let { FurnitureHelpers.placeBlockyFurniture(prefabKey, location, yaw, it) }

fun placeFurniture(prefabKey: PrefabKey, location: Location, yaw: Float, itemStack: ItemStack) =
FurnitureHelpers.placeBlockyFurniture(prefabKey, location, yaw, itemStack)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.mineinabyss.blocky.api.events.block

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.toGeary
import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock
import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull
import org.bukkit.block.Block
import org.bukkit.event.HandlerList
import org.bukkit.event.block.BlockEvent

open class BlockyBlockEvent(block: Block) : BlockEvent(block) {
open class BlockyBlockEvent(
block: Block
) : BlockEvent(block) {
val blockyBlock get() = block.toGearyOrNull()?.get<SetBlock>()

override fun getHandlers() = handlerList
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.mineinabyss.blocky.assets_generation

import com.mineinabyss.blocky.api.BlockyBlocks.gearyBlocks
import com.mineinabyss.blocky.blocky
import com.mineinabyss.blocky.components.core.BlockyInfo
import com.mineinabyss.blocky.components.core.BlockyPack
import com.mineinabyss.blocky.components.features.blocks.BlockyDirectional
import com.mineinabyss.blocky.helpers.CopperHelpers
import com.mineinabyss.blocky.systems.blockPrefabs
import com.mineinabyss.blocky.systems.plantPrefabs
import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.toEntityOrNull
import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock
import com.mineinabyss.geary.papermc.tracking.blocks.gearyBlocks
import com.mineinabyss.geary.prefabs.PrefabKey
import net.kyori.adventure.key.Key
import org.bukkit.Material
Expand All @@ -21,8 +22,9 @@ import team.unnamed.creative.blockstate.Variant
import team.unnamed.creative.model.Model
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter

class ResourcepackGeneration {

class ResourcepackGeneration(
geary: Geary
): Geary by geary {
private val resourcePack = ResourcePack.resourcePack()
private val BlockData.propertiesAsString get() = this.asString.substringAfter("[").substringBeforeLast("]")

Expand Down
Loading

0 comments on commit 1986efe

Please sign in to comment.