diff --git a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt index f33da9b..d20d24b 100644 --- a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt +++ b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt @@ -1,7 +1,6 @@ package com.github.cao.awa.conium.event.context import com.github.cao.awa.conium.event.ConiumEvent -import com.github.cao.awa.conium.event.type.ConiumEventArgTypes import com.github.cao.awa.conium.event.type.ConiumEventType import com.github.cao.awa.conium.parameter.* @@ -17,7 +16,7 @@ object ConiumEventContextBuilder { fun requires( arg1: DynamicArgType ): ConiumEventContext> { - return ConiumEventContext(DynamicArgsBuilder.force(arg1)) + return ConiumEventContext(DynamicArgsBuilder.requires(arg1, true)) } @JvmStatic @@ -26,7 +25,7 @@ object ConiumEventContextBuilder { arg1: DynamicArgType, arg2: DynamicArgType ): ConiumEventContext> { - return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2)) + return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, true)) } @SuppressWarnings("UNCHECKED_CAST") @@ -35,7 +34,7 @@ object ConiumEventContextBuilder { arg2: DynamicArgType, arg3: DynamicArgType ): ConiumEventContext> { - return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3)) + return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, true)) } @JvmStatic @@ -46,7 +45,7 @@ object ConiumEventContextBuilder { arg3: DynamicArgType, arg4: DynamicArgType ): ConiumEventContext> { - return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4)) + return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, true)) } @JvmStatic @@ -58,7 +57,7 @@ object ConiumEventContextBuilder { arg4: DynamicArgType, arg5: DynamicArgType ): ConiumEventContext> { - return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4, arg5)) + return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, true)) } @JvmStatic @@ -71,7 +70,7 @@ object ConiumEventContextBuilder { arg5: DynamicArgType, arg6: DynamicArgType ): ConiumEventContext> { - return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4, arg5, arg6)) + return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, true)) } @JvmStatic @@ -85,7 +84,7 @@ object ConiumEventContextBuilder { arg6: DynamicArgType, arg7: DynamicArgType ): ConiumEventContext> { - return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4, arg5, arg6, arg7)) + return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, arg7, true)) } @JvmStatic diff --git a/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt b/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt index 8c77830..1d8a325 100644 --- a/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt +++ b/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventArgTypes.kt @@ -1,14 +1,18 @@ package com.github.cao.awa.conium.event.type import com.github.cao.awa.conium.parameter.DynamicArgType -import com.github.cao.awa.conium.parameter.DynamicArgsBuilder +import com.github.cao.awa.conium.parameter.DynamicArgsBuilder.Companion.transform import com.github.cao.awa.conium.parameter.type.DynamicArgTypeBuilder.arg import net.minecraft.block.AbstractBlock.AbstractBlockState +import net.minecraft.block.Block import net.minecraft.client.network.ClientPlayerEntity import net.minecraft.client.world.ClientWorld +import net.minecraft.entity.Entity +import net.minecraft.entity.EntityType import net.minecraft.entity.LivingEntity import net.minecraft.entity.damage.DamageSource import net.minecraft.entity.player.PlayerEntity +import net.minecraft.item.Item import net.minecraft.item.ItemPlacementContext import net.minecraft.item.ItemStack import net.minecraft.item.ItemUsageContext @@ -21,6 +25,9 @@ import net.minecraft.util.math.BlockPos import net.minecraft.world.World object ConiumEventArgTypes { + @JvmField + val ITEM: DynamicArgType + @JvmField val ITEM_USAGE_CONTEXT: DynamicArgType @@ -45,6 +52,9 @@ object ConiumEventArgTypes { @JvmField var CLIENT_WORLD: DynamicArgType + @JvmField + val BLOCK: DynamicArgType + @JvmField val BLOCK_POS: DynamicArgType @@ -54,6 +64,12 @@ object ConiumEventArgTypes { @JvmField val BLOCK_HIT_RESULT: DynamicArgType + @JvmField + val ENTITY_TYPE: DynamicArgType> + + @JvmField + val ENTITY: DynamicArgType + @JvmField val LIVING_ENTITY: DynamicArgType @@ -82,89 +98,124 @@ object ConiumEventArgTypes { val DOUBLE: DynamicArgType init { + ITEM = arg( + "item", + transform(::ITEM_STACK, ItemStack::getItem) + ) + ITEM_USAGE_CONTEXT = arg("item_usage_context") ITEM_PLACEMENT_CONTEXT = arg("item_placement_context") ITEM_STACK = arg( "item_stack", - DynamicArgsBuilder.transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.stack } + transform(::ITEM_USAGE_CONTEXT, ItemUsageContext::getStack), + transform(::ITEM_PLACEMENT_CONTEXT, ItemPlacementContext::getStack) ) ACTION_RESULT = arg("action_result") - SERVER = arg("server") + SERVER = arg( + "server", + transform(::SERVER_PLAYER, ServerPlayerEntity::server), + transform(::SERVER_WORLD, ServerWorld::getServer) + ) - WORLD = arg("world") + WORLD = arg( + "world", + transform(::PLAYER, PlayerEntity::getWorld) + ) SERVER_WORLD = arg( "server_world", - DynamicArgsBuilder.transform(::WORLD) { world -> world as? ServerWorld }, - DynamicArgsBuilder.transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.world as? ServerWorld } + transform(::WORLD) { world -> world as? ServerWorld }, + transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.world as? ServerWorld } ) CLIENT_WORLD = arg( "client_world", - DynamicArgsBuilder.transform(::WORLD) { world -> world as? ClientWorld }, - DynamicArgsBuilder.transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.world as? ClientWorld } + transform(::WORLD) { world -> world as? ClientWorld }, + transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.world as? ClientWorld } + ) + + BLOCK = arg( + "block", + transform(::BLOCK_STATE, AbstractBlockState::getBlock) ) BLOCK_POS = arg( "block_pos", - DynamicArgsBuilder.transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.blockPos } + transform(::ITEM_PLACEMENT_CONTEXT, ItemPlacementContext::getBlockPos) ) BLOCK_STATE = arg("block_state") BLOCK_HIT_RESULT = arg("block_hit_result") - LIVING_ENTITY = arg("living_entity") + ENTITY_TYPE = arg( + "entity_type", + transform(::ENTITY, Entity::getType) + ) + + ENTITY = arg( + "entity", + transform(::LIVING_ENTITY) { entity -> entity } + ) + + LIVING_ENTITY = arg( + "living_entity", + transform(::ENTITY) { entity -> entity as? LivingEntity }, + transform(::PLAYER) { player -> player } + ) PLAYER = arg( "player", - DynamicArgsBuilder.transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.player } + transform(::ITEM_PLACEMENT_CONTEXT, ItemPlacementContext::getPlayer), + transform(::LIVING_ENTITY) { entity -> entity as? PlayerEntity }, + transform(::SERVER_PLAYER) { player -> player }, + transform(::CLIENT_PLAYER) { player -> player } ) SERVER_PLAYER = arg( "server_player", - DynamicArgsBuilder.transform(::PLAYER) { player -> player as? ServerPlayerEntity }, - DynamicArgsBuilder.transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.player as? ServerPlayerEntity } + transform(::PLAYER) { player -> player as? ServerPlayerEntity }, + transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.player as? ServerPlayerEntity } ) CLIENT_PLAYER = arg( "client_player", - DynamicArgsBuilder.transform(::PLAYER) { player -> player as? ClientPlayerEntity }, - DynamicArgsBuilder.transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.player as? ClientPlayerEntity } + transform(::PLAYER) { player -> player as? ClientPlayerEntity }, + transform(::ITEM_PLACEMENT_CONTEXT) { placement -> placement.player as? ClientPlayerEntity } ) DAMAGE_SOURCE = arg("damage_source") INT = arg( "int", - DynamicArgsBuilder.transform(::LONG, Long::toInt), - DynamicArgsBuilder.transform(::FLOAT, Float::toInt), - DynamicArgsBuilder.transform(::DOUBLE, Double::toInt), + transform(::LONG, Long::toInt), + transform(::FLOAT, Float::toInt), + transform(::DOUBLE, Double::toInt), ) LONG = arg( "long", - DynamicArgsBuilder.transform(::INT, Int::toLong), - DynamicArgsBuilder.transform(::FLOAT, Float::toLong), - DynamicArgsBuilder.transform(::DOUBLE, Double::toLong), + transform(::INT, Int::toLong), + transform(::FLOAT, Float::toLong), + transform(::DOUBLE, Double::toLong), ) FLOAT = arg( "float", - DynamicArgsBuilder.transform(::INT, Int::toFloat), - DynamicArgsBuilder.transform(::LONG, Long::toFloat), - DynamicArgsBuilder.transform(::DOUBLE, Double::toFloat), + transform(::INT, Int::toFloat), + transform(::LONG, Long::toFloat), + transform(::DOUBLE, Double::toFloat), ) DOUBLE = arg( "double", - DynamicArgsBuilder.transform(::INT, Int::toDouble), - DynamicArgsBuilder.transform(::LONG, Long::toDouble), - DynamicArgsBuilder.transform(::FLOAT, Float::toDouble), + transform(::INT, Int::toDouble), + transform(::LONG, Long::toDouble), + transform(::FLOAT, Float::toDouble), ) } } diff --git a/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java index 9e42dfc..250935d 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/block/BlockItemMixin.java @@ -14,13 +14,16 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(BlockItem.class) -public class BlockItemMixin { +public abstract class BlockItemMixin { + @Shadow public abstract Block getBlock(); + /** * Inject to {@code place} to trigger event {@code PLACE_BLOCK}. * @@ -41,12 +44,14 @@ public void placeBlock(ItemPlacementContext context, CallbackInfoReturnable placeBlockContext = ConiumEvent.request(ConiumEventType.PLACE_BLOCK); + Block block = getBlock(); + // Fill the context args. placeBlockContext.put(ConiumEventArgTypes.ITEM_PLACEMENT_CONTEXT, context); - if (placeBlockContext.presaging(this)) { + if (placeBlockContext.presaging(block)) { // Only presaging state is true can be continues. - placeBlockContext.arising(this); + placeBlockContext.arising(block); } else { // Cancel this event when presaging was rejected the event. cir.setReturnValue(ActionResult.FAIL); @@ -80,6 +85,8 @@ public void placedBlock(Block instance, World world, BlockPos pos, BlockState st // Request the event. ConiumEventContext placedBlockContext = ConiumEvent.request(ConiumEventType.PLACED_BLOCK); + Block block = getBlock(); + // Fill the context args. placedBlockContext.put(ConiumEventArgTypes.WORLD, world); @@ -90,11 +97,11 @@ public void placedBlock(Block instance, World world, BlockPos pos, BlockState st placedBlockContext.put(ConiumEventArgTypes.ITEM_STACK, itemStack); - if (placedBlockContext.presaging(this)) { + if (placedBlockContext.presaging(block)) { // Only presaging state is true can be continues. instance.onPlaced(world, pos, state, placer, itemStack); - placedBlockContext.arising(this); + placedBlockContext.arising(block); } } } diff --git a/src/main/resources/assets/conium/scripts/conium.bedrock.commons.kts b/src/main/resources/assets/conium/scripts/conium.bedrock.commons.kts index 75a8472..71e3ac4 100644 --- a/src/main/resources/assets/conium/scripts/conium.bedrock.commons.kts +++ b/src/main/resources/assets/conium/scripts/conium.bedrock.commons.kts @@ -20,8 +20,8 @@ import com.github.cao.awa.conium.bedrock.system.BedrockSystem ScriptExport( "ConiumBedrockCommons", - { - it("world", AbstractBedrockWorld::class) { instance -> BedrockEventContext.accessWorld(instance) } - it("system", AbstractBedrockSystem::class) { BedrockEventContext.accessSystem() } + { handler -> + handler("world", AbstractBedrockWorld::class) { instance -> BedrockEventContext.accessWorld(instance) } + handler("system", AbstractBedrockSystem::class) { BedrockEventContext.accessSystem() } } ) diff --git a/src/main/resources/assets/conium/scripts/conium.bedrock.script.init.kts b/src/main/resources/assets/conium/scripts/conium.bedrock.script.init.kts index 5518121..7ff3615 100644 --- a/src/main/resources/assets/conium/scripts/conium.bedrock.script.init.kts +++ b/src/main/resources/assets/conium/scripts/conium.bedrock.script.init.kts @@ -1,8 +1,8 @@ BedrockEventContext.newSystem() -ConiumEventContextBuilder.request( - ConiumEventType.SERVER_TICK, - ConiumEventArgTypes.SERVER +request( + SERVER_TICK, + SERVER ).arise { _, server -> BedrockEventContext.system.tick(server) diff --git a/src/main/resources/assets/conium/scripts/conium.commons.kts b/src/main/resources/assets/conium/scripts/conium.commons.kts index 51ee690..9f47b15 100644 --- a/src/main/resources/assets/conium/scripts/conium.commons.kts +++ b/src/main/resources/assets/conium/scripts/conium.commons.kts @@ -20,20 +20,19 @@ import com.github.cao.awa.conium.event.type.ConiumEventArgTypes.ITEM_PLACEMENT_C import com.github.cao.awa.conium.event.type.ConiumEventArgTypes.BLOCK_HIT_RESULT import com.github.cao.awa.conium.event.type.ConiumEventArgTypes.ACTION_RESULT -import com.github.cao.awa.conium.event.type.ConiumEventType -import com.github.cao.awa.conium.event.type.ConiumEventType.SERVER_TICK -import com.github.cao.awa.conium.event.type.ConiumEventType.ITEM_USE_ON_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.BREAKING_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.BREAK_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.BROKEN_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.PLACE_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.PLACED_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.USE_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.USED_BLOCK -import com.github.cao.awa.conium.event.type.ConiumEventType.ENTITY_DAMAGE -import com.github.cao.awa.conium.event.type.ConiumEventType.ENTITY_DAMAGED -import com.github.cao.awa.conium.event.type.ConiumEventType.ENTITY_DIE -import com.github.cao.awa.conium.event.type.ConiumEventType.ENTITY_DEAD +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.SERVER_TICK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.ITEM_USE_ON_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.BREAKING_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.BREAK_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.BROKEN_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.PLACE_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.PLACED_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.USE_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.USED_BLOCK +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.ENTITY_DAMAGE +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.ENTITY_DAMAGED +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.ENTITY_DIE +import com.github.cao.awa.conium.event.type.ConiumEventType.Companion.ENTITY_DEAD import com.github.cao.awa.conium.script.ScriptExport import com.github.cao.awa.conium.script.ScriptExport.Companion.accessExportedField