Search in sources :

Example 1 with GrowablePhaseContext

use of org.spongepowered.common.event.tracking.phase.block.GrowablePhaseContext in project SpongeCommon by SpongePowered.

the class BoneMealItemMixin_Tracker method tracker$wrapGrowWithPhaseEntry.

/**
 * @author gabizou - March 20th, 2019 - 1.12.2
 * @reason To allow growing to be captured via bonemeal without
 * explicitly attempting to capture the growth, we can enter an
 * alternate phase blockState that will specifically track the block
 * changes as growth, and therefor will be able to throw
 * {@link ChangeBlockEvent.Grow} without specifying the cases
 * where the phase blockState needs to throw the event during another
 * phases's capturing.
 */
@SuppressWarnings({ "Duplicates" })
// @Group(name = "org.spongepowered.tracker:bonemeal", min = 1, max = 1)
@Redirect(method = "growCrop", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/BonemealableBlock;performBonemeal(Lnet/minecraft/server/level/ServerLevel;Ljava/util/Random;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;)V"), // Will be removed once the above github issue is resolved with a proper solution
require = 0, // Even though we're in a group, expecting this to succeed in forge environments will not work since there is a different mixin
expect = 0)
private static void tracker$wrapGrowWithPhaseEntry(final BonemealableBlock iGrowable, final ServerLevel worldIn, final Random rand, final BlockPos pos, final BlockState state, final ItemStack stack) {
    if (((LevelBridge) worldIn).bridge$isFake() || !ShouldFire.CHANGE_BLOCK_EVENT_ALL) {
        iGrowable.performBonemeal(worldIn, rand, pos, state);
        return;
    }
    final PhaseContext<@NonNull ?> current = PhaseTracker.getInstance().getPhaseContext();
    final boolean doesEvent = current.doesBlockEventTracking();
    if (doesEvent) {
        // We can enter the new phase state.
        try (final GrowablePhaseContext context = BlockPhase.State.GROWING.createPhaseContext(PhaseTracker.SERVER).provideItem(stack).world(worldIn).block(state).pos(pos)) {
            context.buildAndSwitch();
            iGrowable.performBonemeal(worldIn, rand, pos, state);
        }
    }
}
Also used : GrowablePhaseContext(org.spongepowered.common.event.tracking.phase.block.GrowablePhaseContext) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Redirect (org.spongepowered.asm.mixin.injection.Redirect)1 GrowablePhaseContext (org.spongepowered.common.event.tracking.phase.block.GrowablePhaseContext)1