diff --git a/src/main/java/com/github/cao/awa/conium/entity/event/tick/ConiumEntityTickEvent.kt b/src/main/java/com/github/cao/awa/conium/entity/event/tick/ConiumEntityTickEvent.kt new file mode 100644 index 0000000..951176f --- /dev/null +++ b/src/main/java/com/github/cao/awa/conium/entity/event/tick/ConiumEntityTickEvent.kt @@ -0,0 +1,28 @@ +package com.github.cao.awa.conium.entity.event.tick + +import com.github.cao.awa.conium.event.ConiumEvent +import com.github.cao.awa.conium.event.context.ConiumEventContext +import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires +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.ParameterSelective +import com.github.cao.awa.conium.parameter.ParameterSelective1 +import com.github.cao.awa.conium.parameter.ParameterSelective4 +import net.minecraft.entity.Entity +import net.minecraft.entity.LivingEntity +import net.minecraft.entity.damage.DamageSource +import net.minecraft.world.World + +class ConiumEntityTickEvent : ConiumEvent>() { + override fun requirement(): ConiumEventContext { + return requires( + ConiumEventArgTypes.ENTITY + ).attach( + forever(ConiumEventType.ENTITY_TICK) + ).arise { identity, entity -> + noFailure(identity) { + it.arise(entity) + } + } + } +} diff --git a/src/main/java/com/github/cao/awa/conium/entity/event/tick/ConiumEntityTickedEvent.kt b/src/main/java/com/github/cao/awa/conium/entity/event/tick/ConiumEntityTickedEvent.kt new file mode 100644 index 0000000..3225638 --- /dev/null +++ b/src/main/java/com/github/cao/awa/conium/entity/event/tick/ConiumEntityTickedEvent.kt @@ -0,0 +1,24 @@ +package com.github.cao.awa.conium.entity.event.tick + +import com.github.cao.awa.conium.event.ConiumEvent +import com.github.cao.awa.conium.event.context.ConiumEventContext +import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires +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.ParameterSelective +import com.github.cao.awa.conium.parameter.ParameterSelective1 +import net.minecraft.entity.Entity + +class ConiumEntityTickedEvent : ConiumEvent>() { + override fun requirement(): ConiumEventContext { + return requires( + ConiumEventArgTypes.ENTITY + ).attach( + forever(ConiumEventType.ENTITY_TICKED) + ).arise { identity, entity -> + noFailure(identity) { + it.arise(entity) + } + } + } +} diff --git a/src/main/java/com/github/cao/awa/conium/event/ConiumEvent.kt b/src/main/java/com/github/cao/awa/conium/event/ConiumEvent.kt index 2c616fa..20175be 100644 --- a/src/main/java/com/github/cao/awa/conium/event/ConiumEvent.kt +++ b/src/main/java/com/github/cao/awa/conium/event/ConiumEvent.kt @@ -11,11 +11,15 @@ import com.github.cao.awa.conium.entity.event.damage.ConiumEntityDamageEvent import com.github.cao.awa.conium.entity.event.damage.ConiumEntityDamagedEvent import com.github.cao.awa.conium.entity.event.die.ConiumEntityDeadEvent import com.github.cao.awa.conium.entity.event.die.ConiumEntityDieEvent +import com.github.cao.awa.conium.entity.event.tick.ConiumEntityTickEvent +import com.github.cao.awa.conium.entity.event.tick.ConiumEntityTickedEvent import com.github.cao.awa.conium.event.context.ConiumEventContext import com.github.cao.awa.conium.event.server.tick.ConiumServerTickEvent +import com.github.cao.awa.conium.event.server.tick.ConiumServerTickTailEvent import com.github.cao.awa.conium.event.trigger.ListTriggerable import com.github.cao.awa.conium.event.type.ConiumEventType import com.github.cao.awa.conium.item.event.use.ConiumItemUseOnBlockEvent +import com.github.cao.awa.conium.item.event.use.ConiumItemUsedOnBlockEvent import com.github.cao.awa.conium.parameter.ParameterSelective import com.github.cao.awa.sinuatum.util.collection.CollectionFactor import java.util.* @@ -28,9 +32,15 @@ abstract class ConiumEvent

: ListTriggerable

() { @JvmField val itemUseOnBlockEvent = ConiumItemUseOnBlockEvent() + @JvmField + val itemUsedOnBlockEvent = ConiumItemUsedOnBlockEvent() + @JvmField val serverTick = ConiumServerTickEvent() + @JvmField + val serverTickTail = ConiumServerTickTailEvent() + @JvmField val breakingBlock = ConiumBreakingBlockEvent() @@ -52,6 +62,12 @@ abstract class ConiumEvent

: ListTriggerable

() { @JvmField val usedBlock = ConiumUsedBlockEvent() + @JvmField + val entityTick = ConiumEntityTickEvent() + + @JvmField + val entityTicked = ConiumEntityTickedEvent() + @JvmField val entityDamage = ConiumEntityDamageEvent() @@ -92,7 +108,9 @@ abstract class ConiumEvent

: ListTriggerable

() { @JvmStatic fun init() { this.events[ConiumEventType.ITEM_USE_ON_BLOCK] = this.itemUseOnBlockEvent + this.events[ConiumEventType.ITEM_USED_ON_BLOCK] = this.itemUsedOnBlockEvent this.events[ConiumEventType.SERVER_TICK] = this.serverTick + this.events[ConiumEventType.SERVER_TICK_TAIL] = this.serverTickTail this.events[ConiumEventType.BREAKING_BLOCK] = this.breakingBlock this.events[ConiumEventType.BREAK_BLOCK] = this.breakBlock @@ -102,6 +120,8 @@ abstract class ConiumEvent

: ListTriggerable

() { this.events[ConiumEventType.USE_BLOCK] = this.useBlock this.events[ConiumEventType.USED_BLOCK] = this.usedBlock + this.events[ConiumEventType.ENTITY_TICK] = this.entityTick + this.events[ConiumEventType.ENTITY_TICKED] = this.entityTicked this.events[ConiumEventType.ENTITY_DAMAGE] = this.entityDamage this.events[ConiumEventType.ENTITY_DAMAGED] = this.entityDamaged this.events[ConiumEventType.ENTITY_DIE] = this.entityDie diff --git a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt index 609b350..03a5184 100644 --- a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt +++ b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt @@ -36,8 +36,9 @@ class ConiumEventContext

( return this } - fun put(arg: DynamicArgType, value: X) { + fun put(arg: DynamicArgType, value: X): ConiumEventContext

{ this.args[arg] = value + return this } fun dynamic(arg: DynamicArgType) { diff --git a/src/main/java/com/github/cao/awa/conium/event/server/tick/ConiumServerTickTailEvent.kt b/src/main/java/com/github/cao/awa/conium/event/server/tick/ConiumServerTickTailEvent.kt new file mode 100644 index 0000000..8d3c203 --- /dev/null +++ b/src/main/java/com/github/cao/awa/conium/event/server/tick/ConiumServerTickTailEvent.kt @@ -0,0 +1,24 @@ +package com.github.cao.awa.conium.event.server.tick + +import com.github.cao.awa.conium.event.ConiumEvent +import com.github.cao.awa.conium.event.context.ConiumEventContext +import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires +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.ParameterSelective +import com.github.cao.awa.conium.parameter.ParameterSelective1 +import net.minecraft.server.MinecraftServer + +class ConiumServerTickTailEvent : ConiumEvent>() { + override fun requirement(): ConiumEventContext { + return requires( + ConiumEventArgTypes.SERVER, + ).attach( + forever(ConiumEventType.SERVER_TICK_TAIL) + ).arise { identity, server -> + noFailure(identity) { + it.arise(server) + } + } + } +} diff --git a/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventType.kt b/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventType.kt index 6cdc37e..2c3266d 100644 --- a/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventType.kt +++ b/src/main/java/com/github/cao/awa/conium/event/type/ConiumEventType.kt @@ -13,9 +13,15 @@ class ConiumEventType { @JvmField val SERVER_TICK: ConiumEventType = ConiumEventType() + @JvmField + val SERVER_TICK_TAIL: ConiumEventType = ConiumEventType() + @JvmField val ITEM_USE_ON_BLOCK: ConiumEventType = ConiumEventType() + @JvmField + val ITEM_USED_ON_BLOCK: ConiumEventType = ConiumEventType() + // Block. /** * The event where that block is be mining. @@ -74,6 +80,12 @@ class ConiumEventType { @JvmField val USED_BLOCK: ConiumEventType = ConiumEventType() + @JvmField + val ENTITY_TICK: ConiumEventType> = ConiumEventType() + + @JvmField + val ENTITY_TICKED: ConiumEventType> = ConiumEventType() + // Entity. @JvmField val ENTITY_DAMAGE: ConiumEventType> = ConiumEventType() diff --git a/src/main/java/com/github/cao/awa/conium/item/event/use/ConiumItemUsedOnBlockEvent.kt b/src/main/java/com/github/cao/awa/conium/item/event/use/ConiumItemUsedOnBlockEvent.kt new file mode 100644 index 0000000..964b94d --- /dev/null +++ b/src/main/java/com/github/cao/awa/conium/item/event/use/ConiumItemUsedOnBlockEvent.kt @@ -0,0 +1,32 @@ +package com.github.cao.awa.conium.item.event.use + +import com.github.cao.awa.conium.event.context.ConiumEventContext +import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.requires +import com.github.cao.awa.conium.event.type.ConiumEventArgTypes +import com.github.cao.awa.conium.event.type.ConiumEventType +import com.github.cao.awa.conium.item.event.ConiumItemEvent +import com.github.cao.awa.conium.parameter.ParameterSelective +import com.github.cao.awa.conium.parameter.ParameterSelective3 +import net.minecraft.item.ItemUsageContext +import net.minecraft.server.world.ServerWorld +import net.minecraft.util.ActionResult + +class ConiumItemUsedOnBlockEvent : ConiumItemEvent>() { + override fun requirement(): ConiumEventContext { + return requires( + ConiumEventArgTypes.SERVER_WORLD, + ConiumEventArgTypes.ITEM_USAGE_CONTEXT, + ConiumEventArgTypes.ACTION_RESULT + ).attach( + forever(ConiumEventType.ITEM_USED_ON_BLOCK) + ).arise { identity, world, context, result -> + noFailure(identity) { + it.arise( + world, + context, + result + ) + } + } + } +} diff --git a/src/main/java/com/github/cao/awa/conium/mixin/block/BlockStateMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/block/BlockStateMixin.java index 637411d..48e7666 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/block/BlockStateMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/block/BlockStateMixin.java @@ -18,6 +18,8 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import com.github.cao.awa.conium.block.event.use.ConiumUseBlockEvent; +import com.github.cao.awa.conium.block.event.use.ConiumUsedBlockEvent; @Mixin(AbstractBlock.AbstractBlockState.class) public abstract class BlockStateMixin { @@ -27,28 +29,53 @@ public abstract class BlockStateMixin { cancellable = true ) public void breakingBlock(World world, BlockPos pos, PlayerEntity player, CallbackInfo ci) { - ConiumEventContext eventContext = ConiumEvent.request(ConiumEventType.BREAKING_BLOCK); + // Request the breaking block context. + ConiumEventContext breakingContext = ConiumEvent.request(ConiumEventType.BREAKING_BLOCK); AbstractBlock.AbstractBlockState self = cast(); Block block = self.getBlock(); // Fill the context args. - eventContext.put(ConiumEventArgTypes.WORLD, world); + breakingContext.put(ConiumEventArgTypes.WORLD, world); - eventContext.put(ConiumEventArgTypes.PLAYER, player); + breakingContext.put(ConiumEventArgTypes.PLAYER, player); - eventContext.put(ConiumEventArgTypes.BLOCK_POS, pos); - eventContext.put(ConiumEventArgTypes.BLOCK_STATE, self); + breakingContext.put(ConiumEventArgTypes.BLOCK_POS, pos); + breakingContext.put(ConiumEventArgTypes.BLOCK_STATE, self); - if (eventContext.presaging(block)) { - eventContext.arising(block); + if (breakingContext.presaging(block)) { + breakingContext.arising(block); } else { // Cancel this event when presaging was rejected the event. ci.cancel(); } } + /** + * Redirect the 'onUse' calling of block using, make event context and trigger the event. + * + * @param instance the block instance + * @param blockState the block state of current context + * @param world the world of the block + * @param blockPos the position of the block + * @param playerEntity the user + * @param blockHitResult the hit result of the user using to block + * + * @see ConiumEventType#USE_BLOCK + * @see ConiumEventType#USED_BLOCK + * @see ConiumUseBlockEvent + * @see ConiumUsedBlockEvent + * @see Block + * @see BlockState + * @see ActionResult + * + * @author cao_awa + * + * @return the action result + * + * @since 1.0.0 + */ @Redirect( method = "onUse", at = @At( @@ -57,27 +84,29 @@ public void breakingBlock(World world, BlockPos pos, PlayerEntity player, Callba ) ) public ActionResult useBlock(Block instance, BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, BlockHitResult blockHitResult) { + // Create using block context. ConiumEventContext usingContext = ConiumEvent.request(ConiumEventType.USE_BLOCK); - ConiumEventContext usedContext = ConiumEvent.request(ConiumEventType.USED_BLOCK); Block block = blockState.getBlock(); // Fill the context args. - usingContext.put(ConiumEventArgTypes.WORLD, world); - - usingContext.put(ConiumEventArgTypes.PLAYER, playerEntity); - - usingContext.put(ConiumEventArgTypes.BLOCK_POS, blockPos); - usingContext.put(ConiumEventArgTypes.BLOCK_STATE, blockState); - usingContext.put(ConiumEventArgTypes.BLOCK_HIT_RESULT, blockHitResult); - - usedContext.inherit(usingContext); + usingContext.put(ConiumEventArgTypes.WORLD, world) + .put(ConiumEventArgTypes.PLAYER, playerEntity) + .put(ConiumEventArgTypes.BLOCK_POS, blockPos) + .put(ConiumEventArgTypes.BLOCK_STATE, blockState) + .put(ConiumEventArgTypes.BLOCK_HIT_RESULT, blockHitResult); if (usingContext.presaging(block)) { usingContext.arising(block); + // Invoke the 'onUse' method. ActionResult result = ((AbstractBlockMixin) instance).invokeOnUse(blockState, world, blockPos, playerEntity, blockHitResult); + // Create used block context. + ConiumEventContext usedContext = ConiumEvent.request(ConiumEventType.USED_BLOCK); + usedContext.inherit(usingContext); + + // Used context has action result to acquire the result, this result not cancel or modifiable. usedContext.put(ConiumEventArgTypes.ACTION_RESULT, result); if (usedContext.presaging(block)) { diff --git a/src/main/java/com/github/cao/awa/conium/mixin/client/interaction/ClientPlayerInteractionManagerMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/client/interaction/ClientPlayerInteractionManagerMixin.java index 2755674..2e05d21 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/client/interaction/ClientPlayerInteractionManagerMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/client/interaction/ClientPlayerInteractionManagerMixin.java @@ -24,19 +24,19 @@ public class ClientPlayerInteractionManagerMixin { ) ) public void breakBlock(Block instance, WorldAccess world, BlockPos pos, BlockState state) { - ConiumEventContext eventContext = ConiumEvent.request(ConiumEventType.BROKEN_BLOCK); + // Request the broken block event. + ConiumEventContext brokenContext = ConiumEvent.request(ConiumEventType.BROKEN_BLOCK); // Fill the context args. - eventContext.put(ConiumEventArgTypes.WORLD, (World) world); + brokenContext.put(ConiumEventArgTypes.WORLD, (World) world) + .put(ConiumEventArgTypes.BLOCK_POS, pos) + .put(ConiumEventArgTypes.BLOCK_STATE, state); - eventContext.put(ConiumEventArgTypes.BLOCK_POS, pos); - eventContext.put(ConiumEventArgTypes.BLOCK_STATE, state); - - if (eventContext.presaging(instance)) { + if (brokenContext.presaging(instance)) { // Only presaging state is true can be continues. - instance.onBroken(world, pos, state); + brokenContext.arising(instance); - eventContext.arising(instance); + instance.onBroken(world, pos, state); } } } diff --git a/src/main/java/com/github/cao/awa/conium/mixin/entity/LivingEntityMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/entity/LivingEntityMixin.java index 6c604cc..923af06 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/entity/LivingEntityMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/entity/LivingEntityMixin.java @@ -28,7 +28,7 @@ private LivingEntity cast() { cancellable = true ) public void damage(ServerWorld world, DamageSource damageSource, float amount, CallbackInfoReturnable cir) { - // Request the entity damage event. + // Request the entity damaging event. ConiumEventContext eventContext = ConiumEvent.request(ConiumEventType.ENTITY_DAMAGE); LivingEntity self = cast(); @@ -36,11 +36,10 @@ public void damage(ServerWorld world, DamageSource damageSource, float amount, C EntityType type = self.getType(); // Fill the context args. - eventContext.put(ConiumEventArgTypes.WORLD, world); - - eventContext.put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource); - eventContext.put(ConiumEventArgTypes.LIVING_ENTITY, self); - eventContext.put(ConiumEventArgTypes.FLOAT, amount); + eventContext.put(ConiumEventArgTypes.WORLD, world) + .put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource) + .put(ConiumEventArgTypes.LIVING_ENTITY, self) + .put(ConiumEventArgTypes.FLOAT, amount); if (eventContext.presaging(type)) { // Only presaging state is true can be continues. @@ -64,11 +63,10 @@ public void damaged(ServerWorld world, DamageSource damageSource, float amount, EntityType type = self.getType(); // Fill the context args. - eventContext.put(ConiumEventArgTypes.WORLD, world); - - eventContext.put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource); - eventContext.put(ConiumEventArgTypes.LIVING_ENTITY, self); - eventContext.put(ConiumEventArgTypes.FLOAT, amount); + eventContext.put(ConiumEventArgTypes.WORLD, world) + .put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource) + .put(ConiumEventArgTypes.LIVING_ENTITY, self) + .put(ConiumEventArgTypes.FLOAT, amount); // The entity damaged event is not cancelable, only arising the context. if (eventContext.presaging(type)) { @@ -90,8 +88,8 @@ public void dying(DamageSource damageSource, CallbackInfo ci) { EntityType type = self.getType(); // Fill the context args. - eventContext.put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource); - eventContext.put(ConiumEventArgTypes.LIVING_ENTITY, self); + eventContext.put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource) + .put(ConiumEventArgTypes.LIVING_ENTITY, self); if (eventContext.presaging(type)) { // Only presaging state is true can be continues. @@ -115,8 +113,8 @@ public void dead(DamageSource damageSource, CallbackInfo ci) { EntityType type = self.getType(); // Fill the context args. - eventContext.put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource); - eventContext.put(ConiumEventArgTypes.LIVING_ENTITY, self); + eventContext.put(ConiumEventArgTypes.DAMAGE_SOURCE, damageSource) + .put(ConiumEventArgTypes.LIVING_ENTITY, self); // The entity dead event is not cancelable, only arising the context. if (eventContext.presaging(type)) { diff --git a/src/main/java/com/github/cao/awa/conium/mixin/item/FuelRegistryMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/item/fuel/FuelRegistryMixin.java similarity index 97% rename from src/main/java/com/github/cao/awa/conium/mixin/item/FuelRegistryMixin.java rename to src/main/java/com/github/cao/awa/conium/mixin/item/fuel/FuelRegistryMixin.java index fc5cd58..ea6b924 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/item/FuelRegistryMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/item/fuel/FuelRegistryMixin.java @@ -1,4 +1,4 @@ -package com.github.cao.awa.conium.mixin.item; +package com.github.cao.awa.conium.mixin.item.fuel; import com.github.cao.awa.conium.Conium; import net.minecraft.item.FuelRegistry; diff --git a/src/main/java/com/github/cao/awa/conium/mixin/item/stack/ItemStackMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/item/stack/ItemStackMixin.java index ec440a4..9e643db 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/item/stack/ItemStackMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/item/stack/ItemStackMixin.java @@ -88,29 +88,36 @@ private void overallComponents(Consumer> action) { at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;") ) public ActionResult handleUseOnBlock(Item instance, ItemUsageContext context) { - ConiumEventContext eventContext = ConiumEvent.request(ConiumEventType.ITEM_USE_ON_BLOCK); + // Request the item using on block context. + ConiumEventContext usingContext = ConiumEvent.request(ConiumEventType.ITEM_USE_ON_BLOCK); // Fill the context args. - eventContext.put(ConiumEventArgTypes.WORLD, context.getWorld()); + usingContext.put(ConiumEventArgTypes.WORLD, context.getWorld()) + .put(ConiumEventArgTypes.ITEM_USAGE_CONTEXT, context) + .put(ConiumEventArgTypes.PLAYER, context.getPlayer()) + .put(ConiumEventArgTypes.BLOCK_POS, context.getBlockPos()); - eventContext.put(ConiumEventArgTypes.ITEM_USAGE_CONTEXT, context); + if (usingContext.presaging(instance)) { + usingContext.arising(instance); - eventContext.put(ConiumEventArgTypes.PLAYER, context.getPlayer()); + // Invoke 'useOnBlock' method. + ActionResult result = instance.useOnBlock(context); - eventContext.put(ConiumEventArgTypes.BLOCK_POS, context.getBlockPos()); + // Request the item used on block context. + ConiumEventContext usedContext = ConiumEvent.request(ConiumEventType.ITEM_USED_ON_BLOCK); + usedContext.inherit(usingContext); - ActionResult result = ActionResult.FAIL; + // Used context has action result to acquire the result, this result not cancel or modifiable. + usedContext.put(ConiumEventArgTypes.ACTION_RESULT, result); - if (eventContext.presaging(instance)) { - result = instance.useOnBlock(context); - - eventContext.put(ConiumEventArgTypes.ACTION_RESULT, result); - - if (!eventContext.arising(instance) && result != ActionResult.FAIL) { - result = ActionResult.PASS; + if (usedContext.presaging(instance)) { + usedContext.arising(instance); } - } - return result; + return result; + } else { + // Cancel this event when presaging was rejected the event. + return ActionResult.FAIL; + } } } diff --git a/src/main/java/com/github/cao/awa/conium/mixin/server/MinecraftServerMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/server/MinecraftServerMixin.java index 45efae6..912917e 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/server/MinecraftServerMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/server/MinecraftServerMixin.java @@ -19,13 +19,29 @@ public class MinecraftServerMixin { method = "tick", at = @At("HEAD") ) - public void listenTick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { - ConiumEventContext eventContext = ConiumEvent.request(ConiumEventType.SERVER_TICK); + public void tickStart(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { + // Request the server ticking context. + ConiumEventContext tickingContext = ConiumEvent.request(ConiumEventType.SERVER_TICK); - eventContext.put(ConiumEventArgTypes.SERVER, cast()); + tickingContext.put(ConiumEventArgTypes.SERVER, cast()); - if (eventContext.presaging(this)) { - eventContext.arising(this); + if (tickingContext.presaging(this)) { + tickingContext.arising(this); + } + } + + @Inject( + method = "tick", + at = @At("TAIL") + ) + public void tickTail(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { + // Request the server ticked context. + ConiumEventContext tickedContext = ConiumEvent.request(ConiumEventType.SERVER_TICK_TAIL); + + tickedContext.put(ConiumEventArgTypes.SERVER, cast()); + + if (tickedContext.presaging(this)) { + tickedContext.arising(this); } } diff --git a/src/main/java/com/github/cao/awa/conium/mixin/server/interaction/ServerPlayerInteractionManagerMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/server/interaction/ServerPlayerInteractionManagerMixin.java index ea28fe9..25e0e88 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/server/interaction/ServerPlayerInteractionManagerMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/server/interaction/ServerPlayerInteractionManagerMixin.java @@ -55,13 +55,10 @@ public void tryBreakBlock(BlockPos pos, CallbackInfoReturnable cir) { ConiumEventContext breakContext = ConiumEvent.request(ConiumEventType.BREAK_BLOCK); // Fill the context args. - - breakContext.put(ConiumEventArgTypes.WORLD, world); - - breakContext.put(ConiumEventArgTypes.PLAYER, player); - - breakContext.put(ConiumEventArgTypes.BLOCK_POS, pos); - breakContext.put(ConiumEventArgTypes.BLOCK_STATE, blockState); + breakContext.put(ConiumEventArgTypes.WORLD, world) + .put(ConiumEventArgTypes.PLAYER, player) + .put(ConiumEventArgTypes.BLOCK_POS, pos) + .put(ConiumEventArgTypes.BLOCK_STATE, blockState); // Arising the block break context. if (breakContext.presaging(block)) { @@ -75,8 +72,6 @@ public void tryBreakBlock(BlockPos pos, CallbackInfoReturnable cir) { BlockState brokenState = block.onBreak(this.world, pos, blockState, this.player); boolean removedBlock = this.world.removeBlock(pos, false); if (removedBlock) { - block.onBroken(this.world, pos, brokenState); - // Request the block broken event. ConiumEventContext brokenContext = ConiumEvent.request(ConiumEventType.BROKEN_BLOCK); @@ -88,6 +83,8 @@ public void tryBreakBlock(BlockPos pos, CallbackInfoReturnable cir) { if (breakContext.presaging(block)) { breakContext.arising(block); } + + block.onBroken(this.world, pos, brokenState); } if (isCreative()) { diff --git a/src/main/java/com/github/cao/awa/conium/mixin/server/world/ServerWorldMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/server/world/ServerWorldMixin.java new file mode 100644 index 0000000..ca6a5e5 --- /dev/null +++ b/src/main/java/com/github/cao/awa/conium/mixin/server/world/ServerWorldMixin.java @@ -0,0 +1,67 @@ +package com.github.cao.awa.conium.mixin.server.world; + +import com.github.cao.awa.conium.event.ConiumEvent; +import com.github.cao.awa.conium.event.context.ConiumEventContext; +import com.github.cao.awa.conium.event.type.ConiumEventArgTypes; +import com.github.cao.awa.conium.event.type.ConiumEventType; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ServerWorld.class) +public class ServerWorldMixin { + @Inject( + method = "tickEntity", + at = @At( + value = "HEAD", + target = "Lnet/minecraft/entity/Entity;tick()V" + ), + cancellable = true + ) + public void tickEntity(Entity entity, CallbackInfo ci) { + // Request the entity ticking context. + ConiumEventContext tickingContext = ConiumEvent.request(ConiumEventType.ENTITY_TICK); + + EntityType type = entity.getType(); + + // Fill the context args. + tickingContext.put(ConiumEventArgTypes.WORLD, (World) (Object) this) + .put(ConiumEventArgTypes.ENTITY, entity); + + if (tickingContext.presaging(type)) { + // Only presaging state is true can be continued. + tickingContext.arising(type); + } else { + // Cancel this event when presaging was rejected the event. + ci.cancel(); + } + } + + @Inject( + method = "tickEntity", + at = @At( + value = "RETURN", + target = "Lnet/minecraft/entity/Entity;tick()V" + ) + ) + public void tickedEntity(Entity entity, CallbackInfo ci) { + // Request the entity ticked context. + ConiumEventContext tickedContext = ConiumEvent.request(ConiumEventType.ENTITY_TICKED); + + EntityType type = entity.getType(); + + // Fill the context args. + tickedContext.put(ConiumEventArgTypes.WORLD, (World) (Object) this) + .put(ConiumEventArgTypes.ENTITY, entity); + + if (tickedContext.presaging(type)) { + // Only presaging state is true can be continued. + tickedContext.arising(type); + } + } +} diff --git a/src/main/resources/conium.mixins.json b/src/main/resources/conium.mixins.json index b38a63a..54669f1 100644 --- a/src/main/resources/conium.mixins.json +++ b/src/main/resources/conium.mixins.json @@ -18,7 +18,7 @@ "entity.EntityAccessor", "entity.LivingEntityMixin", "entity.attribute.DefaultAttributeRegistryMixin", - "item.FuelRegistryMixin", + "item.fuel.FuelRegistryMixin", "item.ItemMixin", "item.setting.ItemSettingsAccessor", "item.stack.ItemStackMixin", @@ -28,7 +28,8 @@ "registry.RegistryEntryReferenceMixin", "registry.SimpleRegistryMixin", "server.MinecraftServerMixin", - "server.interaction.ServerPlayerInteractionManagerMixin" + "server.interaction.ServerPlayerInteractionManagerMixin", + "server.world.ServerWorldMixin" ], "client": [ "client.MinecraftClientAccessor",