Skip to content

Commit

Permalink
Add more arg type and transformers.
Browse files Browse the repository at this point in the history
Fix bug of bedrock and common scripts unable to run in last commit.
Fix bug of game break when placing block.
  • Loading branch information
cao-awa committed Nov 18, 2024
1 parent 11c543e commit 7a6fb8f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -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.*

Expand All @@ -17,7 +16,7 @@ object ConiumEventContextBuilder {
fun <P1> requires(
arg1: DynamicArgType<P1>
): ConiumEventContext<ParameterSelective2<Boolean, Any, P1>> {
return ConiumEventContext(DynamicArgsBuilder.force(arg1))
return ConiumEventContext(DynamicArgsBuilder.requires(arg1, true))
}

@JvmStatic
Expand All @@ -26,7 +25,7 @@ object ConiumEventContextBuilder {
arg1: DynamicArgType<P1>,
arg2: DynamicArgType<P2>
): ConiumEventContext<ParameterSelective3<Boolean, Any, P1, P2>> {
return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2))
return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, true))
}

@SuppressWarnings("UNCHECKED_CAST")
Expand All @@ -35,7 +34,7 @@ object ConiumEventContextBuilder {
arg2: DynamicArgType<P2>,
arg3: DynamicArgType<P3>
): ConiumEventContext<ParameterSelective4<Boolean, Any, P1, P2, P3>> {
return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3))
return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, true))
}

@JvmStatic
Expand All @@ -46,7 +45,7 @@ object ConiumEventContextBuilder {
arg3: DynamicArgType<P3>,
arg4: DynamicArgType<P4>
): ConiumEventContext<ParameterSelective5<Boolean, Any, P1, P2, P3, P4>> {
return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4))
return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, true))
}

@JvmStatic
Expand All @@ -58,7 +57,7 @@ object ConiumEventContextBuilder {
arg4: DynamicArgType<P4>,
arg5: DynamicArgType<P5>
): ConiumEventContext<ParameterSelective6<Boolean, Any, P1, P2, P3, P4, P5>> {
return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4, arg5))
return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, true))
}

@JvmStatic
Expand All @@ -71,7 +70,7 @@ object ConiumEventContextBuilder {
arg5: DynamicArgType<P5>,
arg6: DynamicArgType<P6>
): ConiumEventContext<ParameterSelective7<Boolean, Any, P1, P2, P3, P4, P5, P6>> {
return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4, arg5, arg6))
return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, true))
}

@JvmStatic
Expand All @@ -85,7 +84,7 @@ object ConiumEventContextBuilder {
arg6: DynamicArgType<P6>,
arg7: DynamicArgType<P7>
): ConiumEventContext<ParameterSelective8<Boolean, Any, P1, P2, P3, P4, P5, P6, P7>> {
return ConiumEventContext(DynamicArgsBuilder.force(arg1, arg2, arg3, arg4, arg5, arg6, arg7))
return ConiumEventContext(DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, arg7, true))
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,6 +25,9 @@ import net.minecraft.util.math.BlockPos
import net.minecraft.world.World

object ConiumEventArgTypes {
@JvmField
val ITEM: DynamicArgType<Item>

@JvmField
val ITEM_USAGE_CONTEXT: DynamicArgType<ItemUsageContext>

Expand All @@ -45,6 +52,9 @@ object ConiumEventArgTypes {
@JvmField
var CLIENT_WORLD: DynamicArgType<ClientWorld>

@JvmField
val BLOCK: DynamicArgType<Block>

@JvmField
val BLOCK_POS: DynamicArgType<BlockPos>

Expand All @@ -54,6 +64,12 @@ object ConiumEventArgTypes {
@JvmField
val BLOCK_HIT_RESULT: DynamicArgType<BlockHitResult>

@JvmField
val ENTITY_TYPE: DynamicArgType<EntityType<*>>

@JvmField
val ENTITY: DynamicArgType<Entity>

@JvmField
val LIVING_ENTITY: DynamicArgType<LivingEntity>

Expand Down Expand Up @@ -82,89 +98,124 @@ object ConiumEventArgTypes {
val DOUBLE: DynamicArgType<Double>

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),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
Expand All @@ -41,12 +44,14 @@ public void placeBlock(ItemPlacementContext context, CallbackInfoReturnable<Acti
// Request the event.
ConiumEventContext<?> 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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
BedrockEventContext.newSystem()

ConiumEventContextBuilder.request(
ConiumEventType.SERVER_TICK,
ConiumEventArgTypes.SERVER
request(
SERVER_TICK,
SERVER
).arise { _, server ->
BedrockEventContext.system.tick(server)

Expand Down
27 changes: 13 additions & 14 deletions src/main/resources/assets/conium/scripts/conium.commons.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7a6fb8f

Please sign in to comment.