diff --git a/build.gradle.kts b/build.gradle.kts index 31334933..f790ef66 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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" diff --git a/gradle.properties b/gradle.properties index 2baffaa2..4b1e18d5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ group=com.mineinabyss version=0.10 -idofrontVersion=0.25.7 +idofrontVersion=0.25.17-dev.0 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e77b30d7..a8cc5438 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -gearyPaper = "0.30.20" +gearyPaper = "0.31.0-dev.2" guiy="0.12.4" [libraries] diff --git a/src/main/kotlin/com/mineinabyss/blocky/BlockyAddon.kt b/src/main/kotlin/com/mineinabyss/blocky/BlockyAddon.kt new file mode 100644 index 00000000..956e504a --- /dev/null +++ b/src/main/kotlin/com/mineinabyss/blocky/BlockyAddon.kt @@ -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() + } +} diff --git a/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt b/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt index 74fac135..a2a96919 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/BlockyBrigadierCommands.kt @@ -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 @@ -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() @@ -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!") } } @@ -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) @@ -62,4 +68,4 @@ object BlockyBrigadierCommands { } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt b/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt index 8a97417c..6abee1a5 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/BlockyPlugin.kt @@ -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 @@ -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) } } @@ -33,16 +25,6 @@ class BlockyPlugin : JavaPlugin() { BlockyBrigadierCommands.registerCommands() - geary.run { - createFurnitureOutlineSystem() - createFurnitureSpawner() - createFurnitureItemSetter() - createFurnitureSeatSetter() - createFurnitureMEGModelSetter() - - furnitureHitboxSetter() - } - listeners( BlockyGenericListener(), BlockyFurnitureListener(), @@ -72,19 +54,21 @@ class BlockyPlugin : JavaPlugin() { if (caveVineBlocks.isEnabled) listeners(BlockyCaveVineListener()) } - ResourcepackGeneration().generateDefaultAssets() + ResourcepackGeneration(gearyPaper.worldManager.global).generateDefaultAssets() } fun createBlockyContext() { DI.remove() + // 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) } diff --git a/src/main/kotlin/com/mineinabyss/blocky/api/BlockyBlocks.kt b/src/main/kotlin/com/mineinabyss/blocky/api/BlockyBlocks.kt index 529e5a39..000cc035 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/api/BlockyBlocks.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/api/BlockyBlocks.kt @@ -1,9 +1,12 @@ 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 @@ -11,38 +14,49 @@ 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() != null + context(Geary) val String.isBlockyBlock get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.has() == true + context(Geary) val PrefabKey.isBlockyBlock get() = this.toEntityOrNull()?.has() == true + context(Geary) val BlockData.isBlockyBlock get() = this.toGearyOrNull()?.has() == true - val ItemStack.isBlockyBlock get() = this.decode() != null - val String.isBlockyBlock get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.has() == true - val PrefabKey.isBlockyBlock get() = this.toEntityOrNull()?.has() == true val Location.isBlockyBlock get() = block.toGearyOrNull()?.has() == true val Block.isBlockyBlock get() = this.toGearyOrNull()?.has() == true - val BlockData.isBlockyBlock get() = this.toGearyOrNull()?.has() == true - val String.isBlockyNoteBlock get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.NOTEBLOCK - val PrefabKey.isBlockyNoteBlock get() = this.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.NOTEBLOCK - val ItemStack.isBlockyNoteBlock get() = this.decode()?.blockType == SetBlock.BlockType.NOTEBLOCK - val Location.isBlockyNoteBlock get() = this.block.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.NOTEBLOCK + context(Geary) val String.isBlockyNoteBlock + get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.NOTEBLOCK + context(Geary) val PrefabKey.isBlockyNoteBlock + get() = this.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.NOTEBLOCK + context(Geary) val ItemStack.isBlockyNoteBlock get() = this.decode()?.blockType == SetBlock.BlockType.NOTEBLOCK + + val Location.isBlockyNoteBlock + get() = this.block.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.NOTEBLOCK val Block.isBlockyNoteBlock get() = this.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.NOTEBLOCK - val String.isBlockyWire get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.WIRE - val PrefabKey.isBlockyWire get() = this.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.WIRE - val ItemStack.isBlockyWire get() = this.decode()?.blockType == SetBlock.BlockType.WIRE + context(Geary) val String.isBlockyWire + get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.WIRE + context(Geary) val PrefabKey.isBlockyWire + get() = this.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.WIRE + context(Geary) val ItemStack.isBlockyWire get() = this.decode()?.blockType == SetBlock.BlockType.WIRE val Location.isBlockyWire get() = this.block.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.WIRE val Block.isBlockyWire get() = this.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.WIRE - val String.isBlockyCaveVine get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.CAVEVINE - val PrefabKey.isBlockyCaveVine get() = this.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.CAVEVINE - val ItemStack.isBlockyCaveVine get() = this.decode()?.blockType == SetBlock.BlockType.CAVEVINE - val Location.isBlockyCaveVine get() = this.block.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.CAVEVINE + context(Geary) val String.isBlockyCaveVine + get() = PrefabKey.ofOrNull(this)?.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.CAVEVINE + context(Geary) val PrefabKey.isBlockyCaveVine + get() = this.toEntityOrNull()?.get()?.blockType == SetBlock.BlockType.CAVEVINE + context(Geary) val ItemStack.isBlockyCaveVine get() = this.decode()?.blockType == SetBlock.BlockType.CAVEVINE + val Location.isBlockyCaveVine + get() = this.block.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.CAVEVINE val Block.isBlockyCaveVine get() = this.toGearyOrNull()?.get()?.blockType == SetBlock.BlockType.CAVEVINE - val PrefabKey.blockyBlock get() = this.toEntityOrNull()?.get() - val ItemStack.blockyBlock get() = this.decode() + context(Geary) val PrefabKey.blockyBlock get() = this.toEntityOrNull()?.get() + context(Geary) val ItemStack.blockyBlock get() = this.decode() val Location.blockyBlock get() = this.block.toGearyOrNull()?.get() val Block.blockyBlock get() = this.toGearyOrNull()?.get() - 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() ?: return false val blockData = gearyBlocks.createBlockData(prefabKey) ?: return false @@ -50,7 +64,7 @@ object BlockyBlocks { 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() != true) return false location.block.blockData = data diff --git a/src/main/kotlin/com/mineinabyss/blocky/api/BlockyFurnitures.kt b/src/main/kotlin/com/mineinabyss/blocky/api/BlockyFurnitures.kt index fb03514e..0d32c076 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/api/BlockyFurnitures.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/api/BlockyFurnitures.kt @@ -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 @@ -34,21 +37,21 @@ object BlockyFurnitures { val GearyEntity.isModelEngineFurniture: Boolean get() = this.has() //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() || 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() - val PrefabKey.blockyFurniture get() = this.toEntityOrNull()?.get() + context(Geary) val ItemStack.blockyFurniture get() = this.decode() + context(Geary) val PrefabKey.blockyFurniture get() = this.toEntityOrNull()?.get() //TODO toGearyOrNull wouldnt work here as furniture isnt in geary - val Block.blockyFurniture get() = this.toGearyOrNull()?.get() + context(Geary) val Block.blockyFurniture get() = this.toGearyOrNull()?.get() val Block.baseFurniture: ItemDisplay? get() = FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(this.toBlockPos()) @@ -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) diff --git a/src/main/kotlin/com/mineinabyss/blocky/api/events/block/BlockyBlockEvent.kt b/src/main/kotlin/com/mineinabyss/blocky/api/events/block/BlockyBlockEvent.kt index 7a48e59d..6b007ff8 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/api/events/block/BlockyBlockEvent.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/api/events/block/BlockyBlockEvent.kt @@ -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() override fun getHandlers() = handlerList diff --git a/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt b/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt index 967ab157..9df4d2c2 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/assets_generation/ResourcepackGeneration.kt @@ -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 @@ -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("]") diff --git a/src/main/kotlin/com/mineinabyss/blocky/components/core/VanillaNoteBlock.kt b/src/main/kotlin/com/mineinabyss/blocky/components/core/VanillaNoteBlock.kt index 4a9a762e..41b9619a 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/components/core/VanillaNoteBlock.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/components/core/VanillaNoteBlock.kt @@ -4,6 +4,7 @@ import com.mineinabyss.blocky.components.features.blocks.BlockyInstrument import com.mineinabyss.blocky.helpers.GenericHelpers.toBlockCenterLocation import com.mineinabyss.blocky.helpers.isVanillaNoteBlock import com.mineinabyss.blocky.helpers.persistentDataContainer +import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.papermc.datastore.encode import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull import com.mineinabyss.idofront.location.up @@ -23,7 +24,7 @@ import kotlin.math.pow @Serializable @SerialName("blocky:vanilla_note_block") data class VanillaNoteBlock(private var note: Int = 0, private var powered: Boolean = false) { - + context(Geary) fun interact(block: Block, source: Player? = null, action: Action) { playSoundNaturally(block, source) if (action == Action.RIGHT_CLICK_BLOCK) note(block, (note + 1) % 25) @@ -31,6 +32,9 @@ data class VanillaNoteBlock(private var note: Int = 0, private var powered: Bool // Use method and private var to avoid issues with class changing but not pdc entry fun powered(): Boolean = powered + + + context(Geary) fun powered(block: Block, state: Boolean) { if (powered == state || !block.isVanillaNoteBlock) return powered = state @@ -38,12 +42,15 @@ data class VanillaNoteBlock(private var note: Int = 0, private var powered: Bool } fun note(): Int = note + + context(Geary) fun note(block: Block, note: Int) { if (this.note == note || !block.isVanillaNoteBlock) return this.note = note block.persistentDataContainer.encode(this) } + context(Geary) private fun playSoundNaturally(block: Block, source: Player? = null) { val particleColor = note.toDouble() / 24.0 val sound = block.instrumentSound() @@ -64,6 +71,7 @@ data class VanillaNoteBlock(private var note: Int = 0, private var powered: Bool private fun isSkullAbove(block: Block) = (block.getRelative(BlockFace.UP).state as CraftBlockState).handle.instrument().worksAboveNoteBlock() + context(Geary) private fun Block.instrumentSound(): String { val (stateAbove, stateBelow) = (getRelative(BlockFace.UP).state as CraftBlockState) to (getRelative(BlockFace.DOWN).state as CraftBlockState) val instrumentAbove = stateAbove.handle.instrument().takeIf { it.worksAboveNoteBlock() } diff --git a/src/main/kotlin/com/mineinabyss/blocky/helpers/BlockStateCorrection.kt b/src/main/kotlin/com/mineinabyss/blocky/helpers/BlockStateCorrection.kt index 0ec5de66..f9b7f840 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/helpers/BlockStateCorrection.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/helpers/BlockStateCorrection.kt @@ -1,6 +1,8 @@ package com.mineinabyss.blocky.helpers import com.destroystokyo.paper.MaterialSetTag +import com.mineinabyss.geary.papermc.withGeary +import com.mineinabyss.idofront.nms.aliases.NMSPlayer import net.minecraft.util.Mth import net.minecraft.world.InteractionHand import net.minecraft.world.InteractionResult @@ -19,8 +21,12 @@ import org.bukkit.inventory.EquipmentSlot import org.bukkit.inventory.ItemStack object BlockStateCorrection { - fun placeItemAsBlock(player: Player, slot: EquipmentSlot, itemStack: ItemStack) { - val placedItem = itemStack.takeIf(CopperHelpers::isBlockyCopper)?.let(CopperHelpers::convertToBlockyType) ?: CopperHelpers.convertToFakeType(itemStack) + fun placeItemAsBlock(player: Player, slot: EquipmentSlot, itemStack: ItemStack) = player.withGeary { + val placedItem = itemStack + .takeIf { CopperHelpers.isBlockyCopper(it) } + ?.let { CopperHelpers.convertToBlockyType(it) } + ?: CopperHelpers.convertToFakeType(itemStack) + val nmsStack = CraftItemStack.asNMSCopy(placedItem) val blockItem = nmsStack.item as? BlockItem val serverPlayer = (player as CraftPlayer).handle @@ -28,13 +34,15 @@ object BlockStateCorrection { val hand = if (slot == EquipmentSlot.HAND) InteractionHand.MAIN_HAND else InteractionHand.OFF_HAND val placeContext = when {// Shulker-Boxes are DirectionalPlace based unlike other directional-blocks - MaterialSetTag.SHULKER_BOXES.isTagged(placedItem.type) -> - DirectionalPlaceContext(serverPlayer.level(), hitResult.blockPos, hitResult.direction, nmsStack, hitResult.direction.opposite) + MaterialSetTag.SHULKER_BOXES.isTagged(placedItem.type) -> DirectionalPlaceContext( + serverPlayer.level(), hitResult.blockPos, hitResult.direction, nmsStack, hitResult.direction.opposite + ) + else -> BlockPlaceContext(serverPlayer.level(), serverPlayer, hand, nmsStack, hitResult) } blockItem?.let { - if (blockItem.place(placeContext) == InteractionResult.FAIL) return + if (blockItem.place(placeContext) == InteractionResult.FAIL) return@withGeary // Seems shulkers for some reason do not adhere to the place-item subtraction by default if (placeContext is DirectionalPlaceContext && player.getGameMode() != GameMode.CREATIVE) placedItem.subtract(1) @@ -44,7 +52,10 @@ object BlockStateCorrection { } ?: serverPlayer.gameMode.useItem(serverPlayer, serverPlayer.level(), nmsStack, hand) } - private fun playerPOVHitResult(player: net.minecraft.world.entity.player.Player, fluidHandling: ClipContext.Fluid = ClipContext.Fluid.ANY): BlockHitResult { + private fun playerPOVHitResult( + player: NMSPlayer, + fluidHandling: ClipContext.Fluid = ClipContext.Fluid.ANY, + ): BlockHitResult { val f = player.xRot val g = player.yRot val vec3 = player.eyePosition diff --git a/src/main/kotlin/com/mineinabyss/blocky/helpers/CaveVineHelpers.kt b/src/main/kotlin/com/mineinabyss/blocky/helpers/CaveVineHelpers.kt index daa7929a..cdc9ba53 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/helpers/CaveVineHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/helpers/CaveVineHelpers.kt @@ -1,8 +1,10 @@ package com.mineinabyss.blocky.helpers import com.mineinabyss.blocky.api.events.block.BlockyBlockBreakEvent +import com.mineinabyss.geary.modules.Geary +import com.mineinabyss.geary.papermc.toGeary +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 io.th0rgal.protectionlib.ProtectionLib import org.bukkit.Material @@ -15,14 +17,18 @@ import org.bukkit.inventory.ItemStack object CaveVineHelpers { val defaultBlockData = Material.CAVE_VINES.createBlockData() - fun blockyCaveVine(setBlock: SetBlock) : BlockData { - return gearyBlocks.block2Prefab.blockMap[setBlock.blockType]!![setBlock.blockId] + context(Geary) + fun blockyCaveVine(setBlock: SetBlock): BlockData { + return getAddon(BlockTracking).block2Prefab.blockMap[setBlock.blockType]!![setBlock.blockId] } - fun isBlockyCaveVine(block: Block) = block.type == Material.CAVE_VINES && block.blockData in gearyBlocks.block2Prefab + fun isBlockyCaveVine(block: Block) = block.type == Material.CAVE_VINES && block.blockData in block.world.toGeary() + .getAddon(BlockTracking).block2Prefab + + context(Geary) fun isBlockyCaveVine(itemStack: ItemStack) = itemStack.decode()?.blockType == SetBlock.BlockType.CAVEVINE - fun breakCaveVineBlock(block: Block, player: Player?): Boolean { + fun breakCaveVineBlock(block: Block, player: Player?): Boolean = with(block.world.toGeary()) { val gearyBlock = block.toGearyOrNull() ?: return false if (!gearyBlock.has()) return false diff --git a/src/main/kotlin/com/mineinabyss/blocky/helpers/CopperHelpers.kt b/src/main/kotlin/com/mineinabyss/blocky/helpers/CopperHelpers.kt index b32bd045..bd8f260c 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/helpers/CopperHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/helpers/CopperHelpers.kt @@ -1,12 +1,14 @@ package com.mineinabyss.blocky.helpers +import com.mineinabyss.blocky.api.BlockyBlocks.gearyBlocks import com.mineinabyss.blocky.blocky import com.mineinabyss.blocky.components.core.WaxedCopperBlock +import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.papermc.datastore.encode import com.mineinabyss.geary.papermc.datastore.has import com.mineinabyss.geary.papermc.datastore.remove import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock -import com.mineinabyss.geary.papermc.tracking.blocks.gearyBlocks +import com.mineinabyss.geary.papermc.withGeary import com.mineinabyss.geary.prefabs.PrefabKey import org.bukkit.Material import org.bukkit.block.Block @@ -18,53 +20,98 @@ import org.bukkit.block.data.type.TrapDoor import org.bukkit.inventory.ItemStack object CopperHelpers { - fun isFeatureEnabled(blockData: BlockData): Boolean { return when (blockData) { is Stairs -> blocky.config.stairBlocks.isEnabled is Slab -> blocky.config.slabBlocks.isEnabled is Door -> blocky.config.doorBlocks.isEnabled is TrapDoor -> blocky.config.trapdoorBlocks.isEnabled - else -> blocky.config.grateBlocks.isEnabled.takeIf { blockData.material in BLOCKY_GRATE.plus(COPPER_GRATE) } ?: false + else -> blocky.config.grateBlocks.isEnabled + .takeIf { blockData.material in BLOCKY_GRATE.plus(COPPER_GRATE) } + ?: false } } fun convertToFakeType(itemStack: ItemStack): ItemStack = itemStack.takeIf { itemStack.type in BLOCKY_COPPER } ?.takeIf { isFeatureEnabled(it.type.createBlockData()) } ?.withType(VANILLA_COPPER.elementAt(BLOCKY_COPPER.indexOf(itemStack.type))) ?: itemStack - fun isFakeWaxedCopper(block: Block) = (block.type in VANILLA_COPPER) && block.persistentDataContainer.has() - fun isFakeWaxedCopper(itemStack: ItemStack) = itemStack.type.name.contains("WAXED") && itemStack.type.isBlock && !isBlockyCopper(itemStack) + + fun isFakeWaxedCopper(block: Block) = (block.type in VANILLA_COPPER) && block.container { has() } + context(Geary) fun isFakeWaxedCopper(itemStack: ItemStack) = + itemStack.type.name.contains("WAXED") && itemStack.type.isBlock && !isBlockyCopper(itemStack) + fun setFakeWaxedCopper(block: Block, value: Boolean) = when { - !value -> block.persistentDataContainer.remove() - block.type in VANILLA_COPPER -> block.persistentDataContainer.encode(WaxedCopperBlock()) + !value -> block.container { remove() } + block.type in VANILLA_COPPER -> block.container { encode(WaxedCopperBlock()) } else -> {} } - fun convertToBlockyType(itemStack: ItemStack): ItemStack = itemStack.toGearyOrNull()?.prefabs?.first()?.get() - ?.let(gearyBlocks::createBlockData)?.takeIf { isFeatureEnabled(it) }?.let { itemStack.withType(it.material) } ?: itemStack + context(Geary) + fun convertToBlockyType(itemStack: ItemStack): ItemStack = + itemStack.toGearyOrNull()?.prefabs?.first()?.get() + ?.let(gearyBlocks::createBlockData) + ?.takeIf { isFeatureEnabled(it) } + ?.let { itemStack.withType(it.material) } + ?: itemStack + fun isBlockyCopper(block: Block) = block.type in BLOCKY_COPPER fun isBlockyCopper(blockData: BlockData) = blockData.material in BLOCKY_COPPER - fun isBlockyCopper(itemStack: ItemStack) = isBlockyStair(itemStack) || isBlockySlab(itemStack) || isBlockyDoor(itemStack) || isBlockyTrapDoor(itemStack) || isBlockyGrate(itemStack) - fun isBlockyStair(block: Block) = block.blockData is Stairs && block.blockData in gearyBlocks.block2Prefab - fun isBlockyStair(itemStack: ItemStack) = itemStack.decode()?.blockType == SetBlock.BlockType.STAIR - fun isBlockySlab(block: Block) = block.blockData is Slab && block.blockData in gearyBlocks.block2Prefab - fun isBlockySlab(itemStack: ItemStack) = itemStack.decode()?.blockType == SetBlock.BlockType.SLAB - fun isBlockyDoor(block: Block) = block.blockData is Door && block.blockData in gearyBlocks.block2Prefab - fun isBlockyDoor(itemStack: ItemStack) = itemStack.decode()?.blockType == SetBlock.BlockType.DOOR - fun isBlockyTrapDoor(block: Block) = block.blockData is TrapDoor && block.blockData in gearyBlocks.block2Prefab - fun isBlockyTrapDoor(itemStack: ItemStack) = itemStack.decode()?.blockType == SetBlock.BlockType.TRAPDOOR - fun isBlockyGrate(block: Block) = block.type in BLOCKY_GRATE && block.blockData in gearyBlocks.block2Prefab - fun isBlockyGrate(itemStack: ItemStack) = itemStack.decode()?.blockType == SetBlock.BlockType.GRATE + fun isBlockyStair(block: Block) = + block.withGeary { block.blockData is Stairs && block.blockData in gearyBlocks.block2Prefab } + + fun isBlockySlab(block: Block) = + block.withGeary { block.blockData is Slab && block.blockData in gearyBlocks.block2Prefab } + + fun isBlockyDoor(block: Block) = + block.withGeary { block.blockData is Door && block.blockData in gearyBlocks.block2Prefab } + + fun isBlockyTrapDoor(block: Block) = + block.withGeary { block.blockData is TrapDoor && block.blockData in gearyBlocks.block2Prefab } + + fun isBlockyGrate(block: Block) = + block.withGeary { block.type in BLOCKY_GRATE && block.blockData in gearyBlocks.block2Prefab } + + context(Geary) fun isBlockyCopper(itemStack: ItemStack) = + isBlockyStair(itemStack) || isBlockySlab(itemStack) || isBlockyDoor(itemStack) || isBlockyTrapDoor(itemStack) || isBlockyGrate( + itemStack + ) + + context(Geary) fun isBlockyStair(itemStack: ItemStack) = + itemStack.decode()?.blockType == SetBlock.BlockType.STAIR + + context(Geary) fun isBlockySlab(itemStack: ItemStack) = + itemStack.decode()?.blockType == SetBlock.BlockType.SLAB + + context(Geary) fun isBlockyDoor(itemStack: ItemStack) = + itemStack.decode()?.blockType == SetBlock.BlockType.DOOR + + context(Geary) fun isBlockyTrapDoor(itemStack: ItemStack) = + itemStack.decode()?.blockType == SetBlock.BlockType.TRAPDOOR + + context(Geary) fun isBlockyGrate(itemStack: ItemStack) = + itemStack.decode()?.blockType == SetBlock.BlockType.GRATE val BLOCKY_COPPER = setOf( - Material.WAXED_CUT_COPPER_SLAB, Material.WAXED_CUT_COPPER_STAIRS, - Material.WAXED_EXPOSED_CUT_COPPER_SLAB, Material.WAXED_EXPOSED_CUT_COPPER_STAIRS, - Material.WAXED_OXIDIZED_CUT_COPPER_SLAB, Material.WAXED_OXIDIZED_CUT_COPPER_STAIRS, - Material.WAXED_WEATHERED_CUT_COPPER_SLAB, Material.WAXED_WEATHERED_CUT_COPPER_STAIRS, - Material.WAXED_COPPER_DOOR, Material.WAXED_COPPER_TRAPDOOR, Material.WAXED_COPPER_GRATE, - Material.WAXED_EXPOSED_COPPER_DOOR, Material.WAXED_EXPOSED_COPPER_TRAPDOOR, Material.WAXED_EXPOSED_COPPER_GRATE, - Material.WAXED_OXIDIZED_COPPER_DOOR, Material.WAXED_OXIDIZED_COPPER_TRAPDOOR, Material.WAXED_OXIDIZED_COPPER_GRATE, - Material.WAXED_WEATHERED_COPPER_DOOR, Material.WAXED_WEATHERED_COPPER_TRAPDOOR, Material.WAXED_WEATHERED_COPPER_GRATE + Material.WAXED_CUT_COPPER_SLAB, + Material.WAXED_CUT_COPPER_STAIRS, + Material.WAXED_EXPOSED_CUT_COPPER_SLAB, + Material.WAXED_EXPOSED_CUT_COPPER_STAIRS, + Material.WAXED_OXIDIZED_CUT_COPPER_SLAB, + Material.WAXED_OXIDIZED_CUT_COPPER_STAIRS, + Material.WAXED_WEATHERED_CUT_COPPER_SLAB, + Material.WAXED_WEATHERED_CUT_COPPER_STAIRS, + Material.WAXED_COPPER_DOOR, + Material.WAXED_COPPER_TRAPDOOR, + Material.WAXED_COPPER_GRATE, + Material.WAXED_EXPOSED_COPPER_DOOR, + Material.WAXED_EXPOSED_COPPER_TRAPDOOR, + Material.WAXED_EXPOSED_COPPER_GRATE, + Material.WAXED_OXIDIZED_COPPER_DOOR, + Material.WAXED_OXIDIZED_COPPER_TRAPDOOR, + Material.WAXED_OXIDIZED_COPPER_GRATE, + Material.WAXED_WEATHERED_COPPER_DOOR, + Material.WAXED_WEATHERED_COPPER_TRAPDOOR, + Material.WAXED_WEATHERED_COPPER_GRATE ) val VANILLA_COPPER = setOf( diff --git a/src/main/kotlin/com/mineinabyss/blocky/helpers/FurnitureHelpers.kt b/src/main/kotlin/com/mineinabyss/blocky/helpers/FurnitureHelpers.kt index 0b043b84..102fef1d 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/helpers/FurnitureHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/helpers/FurnitureHelpers.kt @@ -5,7 +5,9 @@ import com.mineinabyss.blocky.components.features.BlockyDrops import com.mineinabyss.blocky.components.features.furniture.BlockyAssociatedSeats import com.mineinabyss.blocky.components.features.furniture.BlockySeats import com.mineinabyss.blocky.helpers.GenericHelpers.toBlockCenterLocation +import com.mineinabyss.geary.papermc.toEntityOrNull import com.mineinabyss.geary.papermc.tracking.entities.helpers.spawnFromPrefab +import com.mineinabyss.geary.papermc.tracking.entities.helpers.withGeary import com.mineinabyss.geary.papermc.tracking.entities.toGeary import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull import com.mineinabyss.geary.prefabs.PrefabKey @@ -37,21 +39,21 @@ object FurnitureHelpers { fun collisionHitboxLocations( rotation: Float, center: Location, - hitbox: Set + hitbox: Set, ): List = hitbox.map { c -> c.location.groundRotate(rotation).add(center) } fun collisionHitboxPositions( rotation: Float, center: Location, - hitbox: Set + hitbox: Set, ): List = collisionHitboxLocations(rotation, center, hitbox).map { Position.block(it) } fun interactionHitboxLocations( rotation: Float, center: Location, - hitbox: Set + hitbox: Set, ): List = hitbox.map { i -> center.clone().plus(i.offset(rotation)) } fun rotation(yaw: Float, nullFurniture: BlockyFurniture?): Rotation { @@ -78,8 +80,8 @@ object FurnitureHelpers { prefabKey: PrefabKey, loc: Location, yaw: Float = loc.yaw, - item: ItemStack? - ): ItemDisplay? { + item: ItemStack?, + ): ItemDisplay? = loc.withGeary { val gearyEntity = prefabKey.toEntityOrNull() ?: return null val furniture = gearyEntity.get() ?: return null diff --git a/src/main/kotlin/com/mineinabyss/blocky/helpers/GenericHelpers.kt b/src/main/kotlin/com/mineinabyss/blocky/helpers/GenericHelpers.kt index 1cad5fc9..f892ded8 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/helpers/GenericHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/helpers/GenericHelpers.kt @@ -16,11 +16,15 @@ import com.mineinabyss.blocky.components.features.mining.BlockyMining import com.mineinabyss.blocky.components.features.mining.ToolType import com.mineinabyss.geary.datatypes.Component import com.mineinabyss.geary.datatypes.GearyEntity +import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.papermc.datastore.encode +import com.mineinabyss.geary.papermc.toEntityOrNull +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 com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.geary.papermc.tracking.items.ItemTracking import com.mineinabyss.geary.papermc.tracking.items.inventory.toGeary +import com.mineinabyss.geary.papermc.withGeary import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.idofront.nms.nbt.fastPDC import com.mineinabyss.idofront.spawning.spawn @@ -61,8 +65,18 @@ val Block.persistentDataContainer get() = customBlockData as PersistentDataConta val Block.customBlockData get() = CustomBlockData(this, blocky.plugin) fun Block.toBlockPos() = BlockPos(this.x, this.y, this.z) -internal fun ItemStack.toGearyOrNull(): GearyEntity? = gearyItems.itemProvider.deserializeItemStackToEntity(this.fastPDC) -internal inline fun ItemStack.decode(): T? = gearyItems.itemProvider.deserializeItemStackToEntity(this.fastPDC)?.get() +inline fun Block.container(run: context(Geary, PersistentDataContainer) () -> T) = withGeary { + run(this@withGeary, persistentDataContainer) +} + +context(Geary) +internal fun ItemStack.toGearyOrNull(): GearyEntity? = + getAddon(ItemTracking).itemProvider.deserializeItemStackToEntity(this.fastPDC) + +context(Geary) +internal inline fun ItemStack.decode(): T? = + getAddon(ItemTracking).itemProvider.deserializeItemStackToEntity(this.fastPDC)?.get() + internal val Player.gearyInventory get() = inventory.toGeary() fun placeBlockyBlock( @@ -72,7 +86,7 @@ fun placeBlockyBlock( against: Block, face: BlockFace, newData: BlockData -) { +) = with(player.world.toGeary()) { val targetBlock = if (against.isReplaceable) against else against.getRelative(face) if (!targetBlock.type.isAir && !targetBlock.isLiquid && targetBlock.type != Material.LIGHT) return if (!against.isBlockyBlock && !newData.isBlockyBlock && !CopperHelpers.isBlockyCopper(against) && !CopperHelpers.isBlockyCopper(newData)) return @@ -105,7 +119,7 @@ fun placeBlockyBlock( targetBlock.world.sendGameEvent(null, GameEvent.BLOCK_PLACE, targetBlock.location.toVector()) } -fun handleBlockyDrops(block: Block, player: Player) { +fun handleBlockyDrops(block: Block, player: Player) = with(block.world.toGeary()) { val gearyBlock = block.toGearyOrNull() ?: return val drop = gearyBlock.get() ?: return if (!gearyBlock.has()) return @@ -142,20 +156,22 @@ object GenericHelpers { block.world.getNearbyEntities(BoundingBox.of(block.location.toCenterLocation(), 0.5, 0.5, 0.5)) .any { it is LivingEntity && (it !is Player || it.gameMode != GameMode.SPECTATOR) } - fun Block.isInteractable() = when { - isBlockyBlock || isBlockyFurniture || CaveVineHelpers.isBlockyCaveVine(this) -> false - blockData is Stairs || blockData is Fence -> false - !type.isInteractable || type in setOf( - Material.PUMPKIN, - Material.MOVING_PISTON, - Material.REDSTONE_ORE, - Material.REDSTONE_WIRE - ) -> false - - else -> true + fun Block.isInteractable() = with(world.toGeary()) { + when { + isBlockyBlock || isBlockyFurniture || CaveVineHelpers.isBlockyCaveVine(this@isInteractable) -> false + blockData is Stairs || blockData is Fence -> false + !type.isInteractable || type in setOf( + Material.PUMPKIN, + Material.MOVING_PISTON, + Material.REDSTONE_ORE, + Material.REDSTONE_WIRE + ) -> false + + else -> true + } } - fun directionalId(gearyEntity: GearyEntity, face: BlockFace, player: Player?): Int { + fun directionalId(gearyEntity: GearyEntity, face: BlockFace, player: Player?): Int = with(gearyEntity.world) { return gearyEntity.get()?.let { directional -> if (directional.isLogType) { return when (face) { diff --git a/src/main/kotlin/com/mineinabyss/blocky/helpers/NoteBlockHelpers.kt b/src/main/kotlin/com/mineinabyss/blocky/helpers/NoteBlockHelpers.kt index b932b77a..9a0b735b 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/helpers/NoteBlockHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/helpers/NoteBlockHelpers.kt @@ -1,17 +1,19 @@ package com.mineinabyss.blocky.helpers import com.mineinabyss.blocky.api.BlockyBlocks.blockyBlock +import com.mineinabyss.blocky.api.BlockyBlocks.gearyBlocks import com.mineinabyss.blocky.components.core.VanillaNoteBlock import com.mineinabyss.blocky.components.features.BlockyBreaking import com.mineinabyss.blocky.components.features.blocks.BlockyDirectional import com.mineinabyss.blocky.components.features.mining.ToolType import com.mineinabyss.geary.datatypes.GearyEntity +import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.papermc.datastore.decode import com.mineinabyss.geary.papermc.datastore.encode +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.papermc.withGeary import com.mineinabyss.geary.prefabs.PrefabKey -import com.mineinabyss.idofront.serialization.SerializableItemStack import com.mineinabyss.idofront.serialization.toSerializable import org.bukkit.Material import org.bukkit.block.Block @@ -26,26 +28,29 @@ import org.bukkit.inventory.ItemStack * Note: For directional Blocky-blocks, use [getBlockyNoteBlock(org.bukkit.block.BlockFace, org.bukkit.entity.Player)] instead * This will just use the parent-block of the BlockyDirectional component, which is not what you want */ +context(Geary) fun PrefabKey.blockyNoteBlock(): BlockData { val blockID = (this.toEntityOrNull()?.get()?.parentBlock?.blockyBlock ?: blockyBlock)?.blockId ?: 0 return gearyBlocks.block2Prefab.blockMap[SetBlock.BlockType.NOTEBLOCK]!![blockID] } -fun GearyEntity.blockyNoteBlock(face: BlockFace = BlockFace.NORTH, player: Player? = null): BlockData { - val directional = GenericHelpers.directionalId(this, face, player) +fun GearyEntity.blockyNoteBlock(face: BlockFace = BlockFace.NORTH, player: Player? = null): BlockData = with(world) { + val directional = GenericHelpers.directionalId(this@blockyNoteBlock, face, player) return gearyBlocks.block2Prefab.blockMap[SetBlock.BlockType.NOTEBLOCK]!![directional] } // If the blockmap doesn't contain data, it means it's a vanilla note block -val Block.isVanillaNoteBlock get() = blockData is NoteBlock && blockData !in gearyBlocks.block2Prefab -val BlockData.isVanillaNoteBlock get() = this is NoteBlock && this !in gearyBlocks.block2Prefab +val Block.isVanillaNoteBlock get() = withGeary { blockData.isVanillaNoteBlock } +context(Geary) val BlockData.isVanillaNoteBlock get() = this is NoteBlock && this !in gearyBlocks.block2Prefab -val Block.vanillaNoteBlock get() = persistentDataContainer.decode() - ?: VanillaNoteBlock().takeIf { isVanillaNoteBlock }?.apply { persistentDataContainer.encode(this) } +val Block.vanillaNoteBlock + get() = container { + decode() ?: VanillaNoteBlock().takeIf { isVanillaNoteBlock }?.apply { encode(this) } + } -val Block.isBlockyNoteBlock get() = blockData is NoteBlock && blockData in gearyBlocks.block2Prefab -val BlockData.isBlockyNoteBlock get() = this is NoteBlock && this in gearyBlocks.block2Prefab +val Block.isBlockyNoteBlock get() = withGeary { blockData.isBlockyNoteBlock } +context(Geary) val BlockData.isBlockyNoteBlock get() = this is NoteBlock && this in gearyBlocks.block2Prefab object NoteBlockHelpers { diff --git a/src/main/kotlin/com/mineinabyss/blocky/helpers/TripWireHelpers.kt b/src/main/kotlin/com/mineinabyss/blocky/helpers/TripWireHelpers.kt index 714eaa1a..43091306 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/helpers/TripWireHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/helpers/TripWireHelpers.kt @@ -1,10 +1,11 @@ package com.mineinabyss.blocky.helpers +import com.mineinabyss.blocky.api.BlockyBlocks.gearyBlocks import com.mineinabyss.blocky.api.events.block.BlockyBlockBreakEvent import com.mineinabyss.blocky.components.features.wire.BlockyTallWire +import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.papermc.datastore.decode 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 io.th0rgal.protectionlib.ProtectionLib import org.bukkit.Material @@ -12,6 +13,7 @@ import org.bukkit.block.Block import org.bukkit.block.BlockFace import org.bukkit.entity.Player +context(Geary) fun SetBlock.blockyTripWire() = gearyBlocks.block2Prefab.blockMap[blockType]!![blockId] fun breakWireBlock(block: Block, player: Player?): Boolean { @@ -37,7 +39,8 @@ fun breakWireBlock(block: Block, player: Player?): Boolean { } fun handleTallWire(block: Block) { - val tallWire = block.persistentDataContainer.decode() ?: BlockyTallWire(block.getRelative(BlockFace.UP).location) + val tallWire = block.container { decode() } + ?: BlockyTallWire(block.getRelative(BlockFace.UP).location) tallWire.baseWire?.let { it.customBlockData.clear() it.type = Material.AIR diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCaveVineListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCaveVineListener.kt index a2862b8a..d2fc3608 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCaveVineListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCaveVineListener.kt @@ -2,23 +2,20 @@ package com.mineinabyss.blocky.listeners import com.mineinabyss.blocky.helpers.CaveVineHelpers import com.mineinabyss.blocky.helpers.GenericHelpers.isInteractable -import com.mineinabyss.blocky.helpers.decode import com.mineinabyss.blocky.helpers.gearyInventory import com.mineinabyss.blocky.helpers.placeBlockyBlock import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock +import com.mineinabyss.geary.papermc.withGeary import com.mineinabyss.idofront.util.to import io.papermc.paper.event.block.BlockBreakBlockEvent import org.bukkit.Material import org.bukkit.block.BlockFace -import org.bukkit.block.data.type.CaveVines import org.bukkit.block.data.type.CaveVinesPlant 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.BlockDropItemEvent -import org.bukkit.event.block.BlockFromToEvent import org.bukkit.event.block.BlockGrowEvent import org.bukkit.event.block.BlockPlaceEvent import org.bukkit.event.player.PlayerInteractEvent @@ -32,7 +29,7 @@ class BlockyCaveVineListener : Listener { } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - fun BlockPlaceEvent.onGlowBerryPlace() { + fun BlockPlaceEvent.onGlowBerryPlace() = block.withGeary { if (itemInHand.type != Material.GLOW_BERRIES || CaveVineHelpers.isBlockyCaveVine(itemInHand)) return if (blockPlaced.type != Material.CAVE_VINES || CaveVineHelpers.isBlockyCaveVine(blockAgainst)) return @@ -70,8 +67,9 @@ class BlockyCaveVineListener : Listener { } @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - fun PlayerInteractEvent.prePlaceBlockyCaveVine() { - val (block, item, hand) = (clickedBlock ?: return) to (item?.takeIf { it.type != Material.GLOW_BERRIES } ?: return) to (hand ?: return) + fun PlayerInteractEvent.prePlaceBlockyCaveVine() = player.withGeary { + val (block, item, hand) = (clickedBlock ?: return) to (item?.takeIf { it.type != Material.GLOW_BERRIES } + ?: return) to (hand ?: return) if (action != Action.RIGHT_CLICK_BLOCK || hand != EquipmentSlot.HAND) return if (!player.isSneaking && block.isInteractable()) return diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCopperListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCopperListener.kt index ad276540..e0df4008 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCopperListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyCopperListener.kt @@ -1,23 +1,18 @@ package com.mineinabyss.blocky.listeners import com.destroystokyo.paper.MaterialTags -import com.mineinabyss.blocky.blocky +import com.mineinabyss.blocky.api.BlockyBlocks.gearyBlocks import com.mineinabyss.blocky.helpers.CopperHelpers import com.mineinabyss.blocky.helpers.GenericHelpers.isInteractable import com.mineinabyss.blocky.helpers.customBlockData import com.mineinabyss.blocky.helpers.gearyInventory import com.mineinabyss.blocky.helpers.placeBlockyBlock import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock -import com.mineinabyss.geary.papermc.tracking.blocks.gearyBlocks +import com.mineinabyss.geary.papermc.withGeary import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.idofront.util.to import org.bukkit.GameMode import org.bukkit.Material -import org.bukkit.block.data.BlockData -import org.bukkit.block.data.type.Door -import org.bukkit.block.data.type.Slab -import org.bukkit.block.data.type.Stairs -import org.bukkit.block.data.type.TrapDoor import org.bukkit.event.Event import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority @@ -32,8 +27,9 @@ import org.bukkit.inventory.ItemStack class BlockyCopperListener : Listener { @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - fun PlayerInteractEvent.onPlacingBlockyCopper() { - val (block, item, hand) = (clickedBlock ?: return) to (item ?: return) to (hand?.takeIf { it == EquipmentSlot.HAND } ?: return) + fun PlayerInteractEvent.onPlacingBlockyCopper() = player.withGeary { + val (block, item, hand) = (clickedBlock ?: return) to (item + ?: return) to (hand?.takeIf { it == EquipmentSlot.HAND } ?: return) val prefabKey = player.gearyInventory?.get(hand)?.prefabs?.firstOrNull()?.get() ?: return val blockData = gearyBlocks.createBlockData(prefabKey) ?: return player.gearyInventory?.get(hand)?.get()?.takeIf { it.blockType.isCopper } ?: return @@ -94,5 +90,4 @@ class BlockyCopperListener : Listener { block.customBlockData.clear() if (player.gameMode != GameMode.CREATIVE) block.world.dropItemNaturally(block.location, ItemStack(waxedType)) } - } diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyGenericListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyGenericListener.kt index 2951d625..1d2e6480 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyGenericListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyGenericListener.kt @@ -16,7 +16,6 @@ import com.mineinabyss.blocky.helpers.isVanillaNoteBlock import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull -import com.mineinabyss.idofront.messaging.broadcastVal import com.mineinabyss.idofront.util.to import io.th0rgal.protectionlib.ProtectionLib import org.bukkit.GameMode diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyMiddleClickListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyMiddleClickListener.kt index 895c7e71..3e3e164e 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyMiddleClickListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyMiddleClickListener.kt @@ -6,8 +6,10 @@ import com.mineinabyss.blocky.helpers.CopperHelpers import com.mineinabyss.blocky.helpers.FurniturePacketHelpers import com.mineinabyss.blocky.helpers.gearyInventory import com.mineinabyss.blocky.helpers.toBlockPos +import com.mineinabyss.geary.papermc.toEntityOrNull import com.mineinabyss.geary.papermc.tracking.blocks.helpers.prefabKey -import com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.geary.papermc.tracking.items.ItemTracking +import com.mineinabyss.geary.papermc.withGeary import com.mineinabyss.geary.prefabs.PrefabKey import org.bukkit.FluidCollisionMode import org.bukkit.Material @@ -23,24 +25,34 @@ class BlockyMiddleClickListener : Listener { @EventHandler(priority = EventPriority.LOWEST) fun InventoryCreativeEvent.middleClickBlockyItem() { val player = clickedInventory?.holder as? Player ?: return - when { - click != ClickType.CREATIVE || slotType != InventoryType.SlotType.QUICKBAR -> return - (cursor.type in mutableSetOf(Material.NOTE_BLOCK, Material.STRING, Material.CAVE_VINES, Material.BARRIER, Material.PETRIFIED_OAK_SLAB) - .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.baseFurnitureFromCollisionHitbox(it) }?.prefabKey ?: return - val prefabKey = lookingAtPrefab.toEntityOrNull()?.get()?.parentBlock ?: lookingAtPrefab + player.withGeary { + when { + click != ClickType.CREATIVE || slotType != InventoryType.SlotType.QUICKBAR -> return + (cursor.type in buildSet { + add(Material.NOTE_BLOCK) + add(Material.STRING) + add(Material.CAVE_VINES) + add(Material.BARRIER) + add(Material.PETRIFIED_OAK_SLAB) + addAll(CopperHelpers.BLOCKY_SLABS) + addAll(CopperHelpers.BLOCKY_STAIRS) + }) -> { + val lookingAtPrefab = player.getTargetBlockExact(5, FluidCollisionMode.NEVER)?.prefabKey + ?: player.getTargetEntity(5)?.prefabKey ?: player.getTargetBlockExact(5)?.toBlockPos() + ?.let { FurniturePacketHelpers.baseFurnitureFromCollisionHitbox(it) }?.prefabKey ?: return + val prefabKey = + lookingAtPrefab.toEntityOrNull()?.get()?.parentBlock ?: lookingAtPrefab - val existingSlot = (0..8).firstOrNull { - player.gearyInventory?.get(it)?.prefabs?.firstOrNull()?.get() == prefabKey + val existingSlot = (0..8).firstOrNull { + player.gearyInventory?.get(it)?.prefabs?.firstOrNull()?.get() == prefabKey + } + if (existingSlot != null) { + player.inventory.heldItemSlot = existingSlot + isCancelled = true + return + } + cursor = getAddon(ItemTracking).createItem(prefabKey) ?: return } - if (existingSlot != null) { - player.inventory.heldItemSlot = existingSlot - isCancelled = true - return - } - cursor = gearyItems.createItem(prefabKey) ?: return } } } diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyNoteBlockListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyNoteBlockListener.kt index 3d6bb5d1..5cb362ff 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyNoteBlockListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyNoteBlockListener.kt @@ -2,7 +2,6 @@ package com.mineinabyss.blocky.listeners import com.jeff_media.customblockdata.CustomBlockData import com.mineinabyss.blocky.api.BlockyBlocks.isBlockyBlock -import com.mineinabyss.idofront.util.to import com.mineinabyss.blocky.api.events.block.BlockyBlockInteractEvent import com.mineinabyss.blocky.blocky import com.mineinabyss.blocky.components.core.VanillaNoteBlock @@ -17,6 +16,7 @@ import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull import com.mineinabyss.idofront.entities.rightClicked +import com.mineinabyss.idofront.util.to import org.bukkit.GameMode import org.bukkit.Material import org.bukkit.block.data.type.NoteBlock @@ -39,7 +39,8 @@ class BlockyNoteBlockListener : Listener { if (player.gameMode == GameMode.CREATIVE || !block.isVanillaNoteBlock) return - val mining = PlayerMiningAttribute(NoteBlockHelpers.vanillaBreakingComponent.createBreakingModifier(player, block)) + val mining = + PlayerMiningAttribute(NoteBlockHelpers.vanillaBreakingComponent.createBreakingModifier(player, block)) player.toGearyOrNull()?.set(mining) mining.addTransientModifier(player) } @@ -65,7 +66,8 @@ class BlockyNoteBlockListener : Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) fun PlayerInteractEvent.onPlaceAgainstBlockyBlock() { - val (placedAgainst, item, hand) = (clickedBlock?.takeIf { it.isBlockyBlock } ?: return) to (item?.takeIf { it.type.isBlock } ?: return) to (hand ?: return) + val (placedAgainst, item, hand) = (clickedBlock?.takeIf { it.isBlockyBlock } + ?: return) to (item?.takeIf { it.type.isBlock } ?: return) to (hand ?: return) if (action != Action.RIGHT_CLICK_BLOCK) return if (!BlockyBlockInteractEvent(placedAgainst, player, hand, item, blockFace).callEvent()) isCancelled = true @@ -76,8 +78,11 @@ class BlockyNoteBlockListener : Listener { @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) fun PlayerInteractEvent.onPrePlacingBlockyNoteBlock() { - val (block, item, hand) = (clickedBlock ?: return) to (item ?: return) to (hand?.takeIf { it == EquipmentSlot.HAND } ?: return) - val gearyItem = player.gearyInventory?.get(hand)?.takeIf { it.get()?.blockType == SetBlock.BlockType.NOTEBLOCK } ?: return + val (block, item, hand) = (clickedBlock ?: return) to (item + ?: return) to (hand?.takeIf { it == EquipmentSlot.HAND } ?: return) + val gearyItem = + player.gearyInventory?.get(hand)?.takeIf { it.get()?.blockType == SetBlock.BlockType.NOTEBLOCK } + ?: return if (action != Action.RIGHT_CLICK_BLOCK || (!player.isSneaking && block.isInteractable())) return setUseInteractedBlock(Event.Result.DENY) @@ -93,21 +98,22 @@ class BlockyNoteBlockListener : Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) fun BlockPlaceEvent.onPlaceNoteBlock() { - if (blockPlaced.isVanillaNoteBlock) blockPlaced.persistentDataContainer.encode(VanillaNoteBlock()) + if (blockPlaced.isVanillaNoteBlock) blockPlaced.container { encode(VanillaNoteBlock()) } } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) fun ChunkLoadEvent.migrateOnChunkLoad() { CustomBlockData.getBlocksWithCustomData(blocky.plugin, chunk) - .filter { it.blockData is NoteBlock && it.persistentDataContainer.has() }.forEach { block -> + .filter { it.blockData is NoteBlock && it.container { has() } } + .forEach { block -> // If block doesn't have VANILLA_NOTEBLOCK_KEY or NOTE_KEY, // assume it to be a vanilla and convert it to custom if (block.customBlockData.isEmpty) { - block.persistentDataContainer.encode(VanillaNoteBlock(0)) + block.container { encode(VanillaNoteBlock(0)) } block.blockData = Material.NOTE_BLOCK.createBlockData() } else { // If block has NOTE_KEY, aka it was a custom vanilla block, convert to full vanilla - block.persistentDataContainer.encode(VanillaNoteBlock((block.blockData as NoteBlock).note.id.toInt())) + block.container { encode(VanillaNoteBlock((block.blockData as NoteBlock).note.id.toInt())) } block.blockData = Material.NOTE_BLOCK.createBlockData() } } diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyWireListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyWireListener.kt index de385241..4253d710 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyWireListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/BlockyWireListener.kt @@ -4,14 +4,15 @@ import com.mineinabyss.blocky.api.BlockyBlocks.isBlockyBlock import com.mineinabyss.blocky.api.events.block.BlockyBlockPlaceEvent import com.mineinabyss.blocky.components.features.wire.BlockyTallWire import com.mineinabyss.blocky.helpers.* -import com.mineinabyss.idofront.util.to import com.mineinabyss.blocky.helpers.GenericHelpers.isInteractable import com.mineinabyss.geary.papermc.datastore.decode import com.mineinabyss.geary.papermc.datastore.encode import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock import com.mineinabyss.geary.papermc.tracking.blocks.helpers.prefabKey import com.mineinabyss.geary.papermc.tracking.blocks.helpers.toGearyOrNull -import com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.geary.papermc.tracking.items.ItemTracking +import com.mineinabyss.geary.papermc.withGeary +import com.mineinabyss.idofront.util.to import io.papermc.paper.event.block.BlockBreakBlockEvent import org.bukkit.Material import org.bukkit.block.BlockFace @@ -19,23 +20,25 @@ import org.bukkit.block.data.type.Tripwire import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.Listener -import org.bukkit.event.block.* +import org.bukkit.event.block.Action +import org.bukkit.event.block.BlockBreakEvent +import org.bukkit.event.block.BlockPistonExtendEvent import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.inventory.EquipmentSlot class BlockyWireListener : Listener { @EventHandler - fun BlockPistonExtendEvent.cancelBlockyPiston() { + fun BlockPistonExtendEvent.cancelBlockyPiston() = block.withGeary { blocks.filter { it.type == Material.TRIPWIRE }.forEach { wire -> val gearyEntity = wire.prefabKey ?: return@forEach - gearyItems.createItem(gearyEntity)?.let { wire.world.dropItemNaturally(wire.location, it) } + getAddon(ItemTracking).createItem(gearyEntity)?.let { wire.world.dropItemNaturally(wire.location, it) } wire.type = Material.AIR } } @EventHandler(priority = EventPriority.NORMAL) - fun PlayerInteractEvent.onInteract() { + fun PlayerInteractEvent.onInteract() = player.withGeary { if (action != Action.RIGHT_CLICK_BLOCK || clickedBlock?.type != Material.TRIPWIRE) return if (hand != EquipmentSlot.HAND) return @@ -65,7 +68,7 @@ class BlockyWireListener : Listener { } @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - fun PlayerInteractEvent.prePlaceBlockyWire() { + fun PlayerInteractEvent.prePlaceBlockyWire() = player.withGeary { val (block, item, hand) = (clickedBlock ?: return) to (item ?: return) to (hand ?: return) if (action != Action.RIGHT_CLICK_BLOCK || hand != EquipmentSlot.HAND) return @@ -90,8 +93,8 @@ class BlockyWireListener : Listener { if (block.toGearyOrNull()?.has() != true) return blockAbove.type = Material.TRIPWIRE - blockAbove.persistentDataContainer.encode(BlockyTallWire(block.location)) - block.persistentDataContainer.encode(BlockyTallWire(blockAbove.location)) + blockAbove.container { encode(BlockyTallWire(block.location)) } + block.container { encode(BlockyTallWire(blockAbove.location)) } } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @@ -99,7 +102,7 @@ class BlockyWireListener : Listener { if (block.type != Material.TRIPWIRE) return if (block.toGearyOrNull()?.has() == true) return - val mainWire = block.persistentDataContainer.decode()?.baseWire ?: return + val mainWire = block.container { decode() }?.baseWire ?: return if (mainWire.type != Material.TRIPWIRE) return if (mainWire.toGearyOrNull()?.has() != true) return breakWireBlock(mainWire, player) diff --git a/src/main/kotlin/com/mineinabyss/blocky/listeners/VanillaNoteBlockListener.kt b/src/main/kotlin/com/mineinabyss/blocky/listeners/VanillaNoteBlockListener.kt index 08640084..9641dbe2 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/listeners/VanillaNoteBlockListener.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/listeners/VanillaNoteBlockListener.kt @@ -1,6 +1,7 @@ package com.mineinabyss.blocky.listeners import com.mineinabyss.blocky.helpers.vanillaNoteBlock +import com.mineinabyss.geary.papermc.toGeary import com.mineinabyss.idofront.entities.rightClicked import org.bukkit.Material import org.bukkit.block.BlockFace @@ -15,15 +16,14 @@ import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.inventory.meta.SkullMeta class VanillaNoteBlockListener: Listener { - @EventHandler - fun NotePlayEvent.onNotePlay() { + fun NotePlayEvent.onNotePlay() = with(block.world.toGeary()) { isCancelled = true block.vanillaNoteBlock?.interact(block, null, Action.LEFT_CLICK_BLOCK) } @EventHandler - fun BlockPhysicsEvent.onRedstone() { + fun BlockPhysicsEvent.onRedstone() = with(block.world.toGeary()) { val vanillaNoteBlock = block.takeIf { it.type == Material.NOTE_BLOCK }?.vanillaNoteBlock ?: return if (!block.isBlockIndirectlyPowered) return vanillaNoteBlock.powered(block, false) @@ -32,7 +32,7 @@ class VanillaNoteBlockListener: Listener { } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - fun PlayerInteractEvent.onChangingNote() { + fun PlayerInteractEvent.onChangingNote() = with(player.world.toGeary()) { val (block, vanillaNoteBlock) = (clickedBlock ?: return) to (clickedBlock?.vanillaNoteBlock ?: return) val (mainHand, offHand) = player.inventory.let { it.itemInMainHand to it.itemInOffHand } @@ -43,4 +43,4 @@ class VanillaNoteBlockListener: Listener { vanillaNoteBlock.interact(block, player, Action.RIGHT_CLICK_BLOCK) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/mineinabyss/blocky/menus/BlockyNavigation.kt b/src/main/kotlin/com/mineinabyss/blocky/menus/BlockyNavigation.kt index a343d192..5801df25 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/menus/BlockyNavigation.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/menus/BlockyNavigation.kt @@ -6,7 +6,8 @@ import com.mineinabyss.blocky.helpers.composables.Button import com.mineinabyss.blocky.systems.blockPrefabs import com.mineinabyss.blocky.systems.furniturePrefabs import com.mineinabyss.blocky.systems.plantPrefabs -import com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.geary.papermc.tracking.items.ItemTracking +import com.mineinabyss.geary.papermc.withGeary import com.mineinabyss.guiy.components.CreativeItem import com.mineinabyss.guiy.components.Item import com.mineinabyss.guiy.components.VerticalGrid @@ -48,12 +49,14 @@ fun BlockyMainMenu(player: Player) { BlockyUIScope(player).apply { nav.withScreen(setOf(player), onEmpty = owner::exit) { screen -> val items = remember(screen) { - when (screen) { - is BlockyScreen.Block -> blockPrefabs - is BlockyScreen.Wire -> plantPrefabs - is BlockyScreen.Furniture -> furniturePrefabs - else -> return@remember emptyList() - }.sortedBy { it.prefabKey.full }.map { gearyItems.createItem(it.prefabKey) } + player.withGeary { + when (screen) { + is BlockyScreen.Block -> blockPrefabs + is BlockyScreen.Wire -> plantPrefabs + is BlockyScreen.Furniture -> furniturePrefabs + else -> return@remember emptyList() + }.sortedBy { it.prefabKey.full }.map { getAddon(ItemTracking).createItem(it.prefabKey) } + } } val hasMultiplePages by remember(screen) { mutableStateOf(items.size.toDouble().div(9 * 5) > 1) } var title by remember(screen) { mutableStateOf(handleTitle(screen, 0, hasMultiplePages)) } @@ -65,8 +68,18 @@ fun BlockyMainMenu(player: Player) { else -> { Scrollable( items, line, ScrollDirection.VERTICAL, - nextButton = { ScrollDownButton(Modifier.at(5, 0).clickable { line++; title = handleTitle(screen, line, hasMultiplePages) }) }, - previousButton = { ScrollUpButton(Modifier.at(2, 0).clickable { line--; title = handleTitle(screen, line, hasMultiplePages) }) }, + nextButton = { + ScrollDownButton(Modifier + .at(5, 0) + .clickable { line++; title = handleTitle(screen, line, hasMultiplePages) } + ) + }, + previousButton = { + ScrollUpButton(Modifier + .at(2, 0) + .clickable { line--; title = handleTitle(screen, line, hasMultiplePages) } + ) + }, NavbarPosition.BOTTOM, null ) { pageItems -> VerticalGrid(Modifier.size(9, 5)) { diff --git a/src/main/kotlin/com/mineinabyss/blocky/systems/AttemptSpawnFurnitureSystem.kt b/src/main/kotlin/com/mineinabyss/blocky/systems/AttemptSpawnFurnitureSystem.kt index 99e7dca6..cbfe0db2 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/systems/AttemptSpawnFurnitureSystem.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/systems/AttemptSpawnFurnitureSystem.kt @@ -1,22 +1,19 @@ package com.mineinabyss.blocky.systems import com.mineinabyss.blocky.components.core.BlockyFurniture -import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.Geary +import com.mineinabyss.geary.modules.observeWithData import com.mineinabyss.geary.papermc.tracking.entities.components.AttemptSpawn import com.mineinabyss.geary.serialization.setPersisting -import com.mineinabyss.geary.systems.builders.observeWithData -import com.mineinabyss.geary.systems.query.Query +import com.mineinabyss.geary.systems.query.query import com.mineinabyss.idofront.spawning.spawn import com.mineinabyss.idofront.typealiases.BukkitEntity import org.bukkit.entity.ItemDisplay -fun GearyModule.createFurnitureSpawner() = observeWithData() - .exec(object : Query() { - val furniture by get() - val color by get().orNull() - }) { +fun Geary.createFurnitureSpawner() = observeWithData() + .exec(query()) { (furniture, color) -> event.location.spawn { - val properties = it.furniture.properties + val properties = furniture.properties isPersistent = properties.persistent itemDisplayTransform = properties.displayTransform @@ -32,7 +29,7 @@ fun GearyModule.createFurnitureSpawner() = observeWithData() translation.set(properties.translation) } - it.color?.let { entity.setPersisting(it) } + color?.let { entity.setPersisting(it) } entity.set(this) } } diff --git a/src/main/kotlin/com/mineinabyss/blocky/systems/BlockyQueries.kt b/src/main/kotlin/com/mineinabyss/blocky/systems/BlockyQueries.kt index 7f5e43dc..dade44c6 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/systems/BlockyQueries.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/systems/BlockyQueries.kt @@ -4,13 +4,13 @@ import com.mineinabyss.blocky.blocky import com.mineinabyss.blocky.components.core.BlockyFurniture import com.mineinabyss.blocky.components.features.blocks.BlockyDirectional import com.mineinabyss.blocky.components.features.furniture.BlockyModelEngine +import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.papermc.tracking.blocks.components.SetBlock -import com.mineinabyss.geary.papermc.tracking.blocks.gearyBlocks import com.mineinabyss.geary.prefabs.PrefabKey import com.mineinabyss.geary.prefabs.configuration.components.Prefab import com.mineinabyss.geary.systems.query.GearyQuery -class BlockyQuery : GearyQuery() { +class BlockyQuery(world: Geary) : GearyQuery(world) { val prefabKey by get() val block by get().orNull() val directional by get().orNull() @@ -25,7 +25,7 @@ class BlockyQuery : GearyQuery() { } } -class BlockyBlockQuery : GearyQuery() { +class BlockyBlockQuery(world: Geary) : GearyQuery(world) { val prefabKey by get() val block by get() val directional by get().orNull() @@ -40,7 +40,7 @@ class BlockyBlockQuery : GearyQuery() { } } -class BlockyFurnitureQuery : GearyQuery() { +class BlockyFurnitureQuery(world: Geary) : GearyQuery(world) { val key by get() val modelEngine by get().orNull() diff --git a/src/main/kotlin/com/mineinabyss/blocky/systems/FurnitureOutlineSystem.kt b/src/main/kotlin/com/mineinabyss/blocky/systems/FurnitureOutlineSystem.kt index 0570b2b6..a60f1029 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/systems/FurnitureOutlineSystem.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/systems/FurnitureOutlineSystem.kt @@ -3,9 +3,8 @@ package com.mineinabyss.blocky.systems import com.mineinabyss.blocky.blocky import com.mineinabyss.blocky.components.core.BlockyFurniture import com.mineinabyss.blocky.helpers.FurniturePacketHelpers -import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull -import com.mineinabyss.geary.systems.builders.system import com.mineinabyss.geary.systems.query.query import com.mineinabyss.idofront.time.ticks import net.minecraft.world.entity.EntityType @@ -18,7 +17,7 @@ import org.bukkit.entity.Player import kotlin.jvm.optionals.getOrNull -fun GearyModule.createFurnitureOutlineSystem() = +fun Geary.createFurnitureOutlineSystem() = system(query()).every(4.ticks).exec { (player) -> if (blocky.config.furniture.showOutlines && player.isConnected) findTargetFurnitureHitbox(player)?.let { FurniturePacketHelpers.sendHitboxOutlinePacket(it, player) diff --git a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetFurnitureHitboxSystem.kt b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetFurnitureHitboxSystem.kt index 8571d15e..b808334e 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetFurnitureHitboxSystem.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetFurnitureHitboxSystem.kt @@ -5,21 +5,20 @@ import com.github.shynixn.mccoroutine.bukkit.minecraftDispatcher import com.mineinabyss.blocky.blocky import com.mineinabyss.blocky.components.core.BlockyFurniture import com.mineinabyss.blocky.helpers.FurniturePacketHelpers -import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.Geary +import com.mineinabyss.geary.modules.observe import com.mineinabyss.geary.observers.events.OnSet -import com.mineinabyss.geary.systems.builders.observe import com.mineinabyss.geary.systems.query.query import kotlinx.coroutines.delay import org.bukkit.entity.ItemDisplay -fun GearyModule.furnitureHitboxSetter() = observe() +fun Geary.furnitureHitboxSetter() = observe() .involving(query()) .exec { (itemDisplay, _) -> - blocky.plugin.launch(blocky.plugin.minecraftDispatcher) { delay(1) FurniturePacketHelpers.sendInteractionHitboxPackets(itemDisplay) FurniturePacketHelpers.sendCollisionHitboxPacket(itemDisplay) FurniturePacketHelpers.sendLightPacket(itemDisplay) } - } \ No newline at end of file + } diff --git a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetItemOnFurnitureSystem.kt b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetItemOnFurnitureSystem.kt index 25a1294f..094f8645 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetItemOnFurnitureSystem.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetItemOnFurnitureSystem.kt @@ -1,29 +1,24 @@ package com.mineinabyss.blocky.systems.actions import com.mineinabyss.blocky.components.core.BlockyFurniture -import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.Geary +import com.mineinabyss.geary.modules.observe import com.mineinabyss.geary.observers.events.OnSet import com.mineinabyss.geary.papermc.tracking.items.components.SetItem -import com.mineinabyss.geary.systems.builders.observe -import com.mineinabyss.geary.systems.query.Query +import com.mineinabyss.geary.systems.query.query import com.mineinabyss.idofront.items.asColorable import com.mineinabyss.idofront.items.editItemMeta import net.kyori.adventure.text.Component import org.bukkit.entity.ItemDisplay -fun GearyModule.createFurnitureItemSetter() = observe() +fun Geary.createFurnitureItemSetter() = observe() .involving() - .exec(object : Query() { - val itemDisplay by get() - val furniture by get() - val color by get().orNull() - val setItem by get() - }) { - val itemStack = it.furniture.properties.itemStack?.toItemStackOrNull() ?: it.setItem.item.toItemStack() + .exec(query()) { (itemDisplay, furniture, color, setItem) -> + val itemStack = furniture.properties.itemStack?.toItemStackOrNull() ?: setItem.item.toItemStack() val furnitureItem = itemStack.clone().editItemMeta { itemName(Component.empty()) displayName(Component.empty()) - it.color?.color?.let { c -> asColorable()?.color = c } + color?.color?.let { c -> asColorable()?.color = c } } - it.itemDisplay.setItemStack(furnitureItem) + itemDisplay.setItemStack(furnitureItem) } diff --git a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetMEGModelOnFurnitureSystem.kt b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetMEGModelOnFurnitureSystem.kt index 2d06d73a..b7508abb 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetMEGModelOnFurnitureSystem.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetMEGModelOnFurnitureSystem.kt @@ -3,16 +3,16 @@ 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.BlockyModelEngine -import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.Geary +import com.mineinabyss.geary.modules.observe import com.mineinabyss.geary.observers.events.OnSet -import com.mineinabyss.geary.systems.builders.observe import com.mineinabyss.geary.systems.query.query import com.mineinabyss.idofront.plugin.Plugins import com.ticxo.modelengine.api.ModelEngineAPI import org.bukkit.Bukkit import org.bukkit.entity.ItemDisplay -fun GearyModule.createFurnitureMEGModelSetter() = observe() +fun Geary.createFurnitureMEGModelSetter() = observe() .involving(query()) .exec { (itemDisplay, _, modelengine) -> // Save for scheduled task diff --git a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetSeatOnFurnitureSystem.kt b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetSeatOnFurnitureSystem.kt index 81919bb2..5a42d73c 100644 --- a/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetSeatOnFurnitureSystem.kt +++ b/src/main/kotlin/com/mineinabyss/blocky/systems/actions/SetSeatOnFurnitureSystem.kt @@ -4,14 +4,14 @@ import com.mineinabyss.blocky.blocky import com.mineinabyss.blocky.components.core.BlockyFurniture import com.mineinabyss.blocky.components.features.furniture.BlockySeats import com.mineinabyss.blocky.helpers.FurnitureHelpers -import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.modules.Geary +import com.mineinabyss.geary.modules.observe import com.mineinabyss.geary.observers.events.OnSet -import com.mineinabyss.geary.systems.builders.observe import com.mineinabyss.geary.systems.query.query import org.bukkit.Bukkit import org.bukkit.entity.ItemDisplay -fun GearyModule.createFurnitureSeatSetter() = observe() +fun Geary.createFurnitureSeatSetter() = observe() .involving(query()) .exec { (itemDisplay, _, seats) -> FurnitureHelpers.clearFurnitureSeats(itemDisplay)