Skip to content

Commit

Permalink
Fix script identity bugs in data-driven interaction.
Browse files Browse the repository at this point in the history
Changed item settings to ConiumItemSettings.
  • Loading branch information
cao-awa committed Dec 6, 2024
1 parent 592bddf commit 8ab031d
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 47 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/github/cao/awa/conium/item/ConiumItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ class ConiumItem(private val settings: ConiumItemSettings) : Item(settings.vanil
* @return whether the item's use stat should be incremented
*/
override fun postMine(stack: ItemStack, world: World, state: BlockState, pos: BlockPos, miner: LivingEntity): Boolean {
// Rolling chance using world random.
// If damage chance present, then try to roll a chance, or else direct allow to damage the item.
val canDamage: Boolean = this.settings.durabilityDamageChance?.let {
// Rolling chance using world random.
ConiumRandom.tryChance(it, world.random)
} ?: true
val canDamage: Boolean = ConiumRandom.tryChance(this.settings.durabilityDamageChance, world.random)

// If can damage, then post mine to super.
return canDamage && super.postMine(stack, world, state, pos, miner)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.cao.awa.conium.item.builder

import com.github.cao.awa.conium.item.ConiumItem
import com.github.cao.awa.conium.item.setting.ConiumItemSettings
import com.github.cao.awa.conium.item.template.ConiumItemTemplate
import com.github.cao.awa.conium.template.ConiumBuilderWithTemplates
import net.minecraft.item.Item

abstract class ConiumItemBuilder : ConiumBuilderWithTemplates<
ConiumItemBuilder,
Item.Settings,
ConiumItemSettings,
Item,
ConiumItemTemplate>(
ConiumItem::create
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.github.cao.awa.conium.item.template.bedrock.destory

import com.github.cao.awa.conium.item.ConiumItem
import com.github.cao.awa.conium.item.setting.ConiumItemSettings
import com.github.cao.awa.conium.item.template.ConiumItemTemplate
import com.github.cao.awa.conium.kotlin.extent.json.objectOrBoolean
import com.github.cao.awa.conium.template.ConiumTemplates.BedrockItem.CAN_DESTROY_IN_CREATIVE
import com.google.gson.JsonElement
import net.minecraft.block.BlockState
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.Item
import net.minecraft.registry.RegistryWrapper.WrapperLookup
import net.minecraft.util.math.BlockPos
import net.minecraft.item.Item
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.block.Block
import net.minecraft.block.BlockState
import net.minecraft.world.World

class ConiumBedrockCanDestroyInCreativeTemplate(private val canDestroy: Boolean) : ConiumItemTemplate(name = CAN_DESTROY_IN_CREATIVE) {
Expand All @@ -30,7 +29,7 @@ class ConiumBedrockCanDestroyInCreativeTemplate(private val canDestroy: Boolean)
)!!
}

override fun complete(target: ConiumItem) {
target.canMinePredicate = { _: Item, _: BlockState, _: World, _: BlockPos, player: PlayerEntity -> !player.isCreative || this.canDestroy }
override fun settings(settings: ConiumItemSettings) {
settings.canMinePredicate = { _: Item, _: BlockState, _: World, _: BlockPos, player: PlayerEntity -> !player.isCreative || this.canDestroy }
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.github.cao.awa.conium.item.template.destory

import com.github.cao.awa.conium.item.ConiumItem
import com.github.cao.awa.conium.item.setting.ConiumItemSettings
import com.github.cao.awa.conium.item.template.ConiumItemTemplate
import com.github.cao.awa.conium.template.ConiumTemplates.Item.CAN_DESTROY_IN_CREATIVE
import com.google.gson.JsonElement
import net.minecraft.registry.RegistryWrapper.WrapperLookup
import net.minecraft.item.Item
import net.minecraft.block.BlockState
import net.minecraft.world.World
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.Item
import net.minecraft.registry.RegistryWrapper.WrapperLookup
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World

class ConiumCanDestroyInCreativeTemplate(private val canDestroy: Boolean) : ConiumItemTemplate(name = CAN_DESTROY_IN_CREATIVE) {
companion object {
@JvmStatic
fun create(element: JsonElement, registryLookup: WrapperLookup): ConiumCanDestroyInCreativeTemplate = ConiumCanDestroyInCreativeTemplate(element.asBoolean)
}

override fun complete(target: ConiumItem) {
target.canMinePredicate = { _: Item, _: BlockState, _: World, _: BlockPos, player: PlayerEntity -> !player.isCreative || this.canDestroy }
override fun settings(settings: ConiumItemSettings) {
settings.canMinePredicate = { _: Item, _: BlockState, _: World, _: BlockPos, player: PlayerEntity -> !player.isCreative || this.canDestroy }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.cao.awa.conium.item.template.durability

import com.github.cao.awa.conium.item.ConiumItem
import com.github.cao.awa.conium.item.setting.ConiumItemSettings
import com.github.cao.awa.conium.item.template.ConiumItemTemplate
import com.github.cao.awa.conium.kotlin.extent.component.withComponent
import com.github.cao.awa.conium.kotlin.extent.component.withComputeTool
Expand Down Expand Up @@ -32,12 +32,12 @@ open class ConiumDurabilityTemplate(private val durability: Int, private val dam
}
}

override fun complete(target: ConiumItem) {
override fun settings(settings: ConiumItemSettings) {
// Should increments 'USED' stat when an item has durability.
target.shouldPostHit = true
settings.shouldPostHit = true

// Set durability damage chance.
target.durabilityDamageChance = this.damageChance
settings.durabilityDamageChance = this.damageChance
}

override fun settings(settings: Item.Settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.cao.awa.conium.Conium
import com.github.cao.awa.conium.event.context.ConiumEventContext
import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder
import com.github.cao.awa.conium.event.type.ConiumEventArgTypes
import com.github.cao.awa.conium.item.ConiumItem
import com.github.cao.awa.conium.item.template.ConiumItemTemplate
import com.github.cao.awa.conium.kotlin.extent.item.mergedComponents
import com.github.cao.awa.conium.kotlin.extent.json.ifJsonObject
Expand All @@ -16,24 +17,11 @@ import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.registry.RegistryWrapper.WrapperLookup

class ConiumGlintTemplate(private val glint: Boolean, private val script: (ItemStack) -> Unit = { }) : ConiumItemTemplate(name = GLINT) {
class ConiumGlintTemplate(private val glint: Boolean, private val scriptName: String? = null) : ConiumItemTemplate(name = GLINT) {
companion object {
@JvmStatic
fun create(element: JsonElement, registryLookup: WrapperLookup): ConiumGlintTemplate = element.ifJsonObject({ json: JsonObject ->
val glint: Boolean = json["default"].asBoolean

val changer: (Any) -> Any? = Conium.scriptManager!!.acquireResult(json["script"].asString)

val dynamicGlint: ConiumEventContext<ParameterSelective2<Boolean, Any, ItemStack>> = ConiumEventContextBuilder.unnamed(
ConiumEventArgTypes.ITEM_STACK
) { identity: Any, stack: ItemStack ->
println("Changing item stack: $stack")
stack.mergedComponents[DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE] = changer(identity) as Boolean
}

Conium.scriptManager!!.acquire(json["script"].asString).attach(dynamicGlint)

ConiumGlintTemplate(glint)
ConiumGlintTemplate(json["default"].asBoolean, json["script"].asString)
}) {
ConiumGlintTemplate(it.asBoolean)
}!!
Expand All @@ -43,4 +31,23 @@ class ConiumGlintTemplate(private val glint: Boolean, private val script: (ItemS
// Set glint override.
settings.component(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, this.glint)
}

override fun complete(target: ConiumItem) {
if (this.scriptName != null) {
val changer: (Any) -> Any? = Conium.scriptManager!!.acquireResult(this.scriptName)

val dynamicGlint: ConiumEventContext<ParameterSelective2<Boolean, Any, ItemStack>> = ConiumEventContextBuilder.unnamed(
ConiumEventArgTypes.ITEM_STACK
) { identity: Any, stack: ItemStack ->
if (stack.item != target) {
return@unnamed
}
stack.mergedComponents?.apply {
this[DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE] = changer(identity) as Boolean
}
}

Conium.scriptManager!!.acquire(this.scriptName).attach(dynamicGlint)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.cao.awa.conium.item.template.tool

import com.github.cao.awa.conium.item.ConiumItem
import com.github.cao.awa.conium.item.setting.ConiumItemSettings
import com.github.cao.awa.conium.item.template.ConiumItemTemplate
import com.github.cao.awa.conium.item.template.durability.ConiumDurabilityTemplate
import com.github.cao.awa.conium.kotlin.extent.json.ifJsonObject
Expand Down Expand Up @@ -74,12 +74,12 @@ open class ConiumItemToolTemplate(
fun createEffectiveBlocks(name: String): TagKey<Block> = TagKey.of(RegistryKeys.BLOCK, Identifier.of(name))
}

override fun complete(target: ConiumItem) {
override fun settings(settings: ConiumItemSettings) {
// Item is tool, post hit to increments 'USED' stat data.
target.shouldPostHit = true
settings.shouldPostHit = true

// Set durability damage chance.
target.durabilityDamageChance = this.damageChance
settings.durabilityDamageChance = this.damageChance
}

override fun settings(settings: Item.Settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.github.cao.awa.conium.template.ConiumTemplates.Item
import com.google.gson.JsonElement
import net.minecraft.registry.RegistryWrapper.WrapperLookup

open class ConiumForceMiningSpeedTemplate(private val miningSpeed: Int) : ConiumItemTemplate(name = Item.FORCE_MINING_SPEED) {
open class ConiumForceMiningSpeedTemplate(private val miningSpeed: Float) : ConiumItemTemplate(name = Item.FORCE_MINING_SPEED) {
companion object {
@JvmStatic
fun create(element: JsonElement, registryLookup: WrapperLookup): ConiumForceMiningSpeedTemplate = ConiumForceMiningSpeedTemplate(element.asInt)
fun create(element: JsonElement, registryLookup: WrapperLookup): ConiumForceMiningSpeedTemplate = ConiumForceMiningSpeedTemplate(element.asFloat)
}

override fun settings(settings: ConiumItemSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.cao.awa.conium.block.ConiumBlock
import com.github.cao.awa.conium.block.builder.ConiumBlockBuilder
import com.github.cao.awa.conium.item.builder.bedrock.BedrockSchemaItemBuilder
import com.github.cao.awa.conium.item.builder.conium.ConiumSchemaItemBuilder
import com.github.cao.awa.conium.item.setting.ConiumItemSettings
import com.github.cao.awa.conium.mixin.item.setting.ItemSettingsAccessor
import net.minecraft.component.ComponentMap
import net.minecraft.item.BlockItem
Expand All @@ -15,11 +16,15 @@ import net.minecraft.registry.entry.RegistryEntry
import net.minecraft.util.Identifier

fun ConiumSchemaItemBuilder.register() {
registerItem(this.identifier, ::build)
registerItem(this.identifier) {
build(ConiumItemSettings(it))
}
}

fun BedrockSchemaItemBuilder.register(tagProvider: (RegistryEntry.Reference<Item>) -> Unit = { }) {
tagProvider(registerItem(this.identifier, ::build).registryEntry)
tagProvider(registerItem(this.identifier){
build(ConiumItemSettings(it))
}.registryEntry)
}

fun registerItem(identifier: Identifier, itemProvider: (Item.Settings) -> Item): Item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.github.cao.awa.conium.kotlin.extent.item
import net.minecraft.component.MergedComponentMap
import net.minecraft.item.ItemStack

val ItemStack.mergedComponents: MergedComponentMap get() = this.components as MergedComponentMap
val ItemStack.mergedComponents: MergedComponentMap? get() = this.components as? MergedComponentMap
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.cao.awa.conium.random

import com.github.cao.awa.conium.item.template.durability.ConiumDurabilityTemplate
import net.minecraft.util.math.random.Random

object ConiumRandom {
@JvmStatic
fun tryChance(chance: IntRange, random: Random): Boolean = random.nextBetween(chance.first, chance.last) == 0
fun tryChance(chance: IntRange, random: Random): Boolean = chance === ConiumDurabilityTemplate.defaultChance || random.nextBetween(chance.first, chance.last) == 0
}

0 comments on commit 8ab031d

Please sign in to comment.