Search in sources :

Example 11 with PhaseData

use of org.spongepowered.common.event.tracking.PhaseData in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method createChangeBlockEventPlace.

public static ChangeBlockEvent.Place createChangeBlockEventPlace(BlockEvent.PlaceEvent forgeEvent) {
    final BlockPos pos = forgeEvent.getPos();
    final net.minecraft.world.World world = forgeEvent.getWorld();
    if (world.isRemote) {
        return null;
    }
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData data = phaseTracker.getCurrentPhaseData();
    BlockSnapshot originalSnapshot = ((IMixinBlockSnapshot) forgeEvent.getBlockSnapshot()).createSpongeBlockSnapshot();
    BlockSnapshot finalSnapshot = ((BlockState) forgeEvent.getPlacedBlock()).snapshotFor(new Location<>((World) world, VecHelper.toVector3d(pos)));
    ImmutableList<Transaction<BlockSnapshot>> blockSnapshots = new ImmutableList.Builder<Transaction<BlockSnapshot>>().add(new Transaction<>(originalSnapshot, finalSnapshot)).build();
    User owner = data.context.getOwner().orElse(null);
    User notifier = data.context.getNotifier().orElse(null);
    EntityPlayer player = forgeEvent.getPlayer();
    if (SpongeImplHooks.isFakePlayer(player)) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.FAKE_PLAYER, EntityUtil.toPlayer(player));
    } else if (Sponge.getCauseStackManager().getCurrentCause() == null) {
        Sponge.getCauseStackManager().pushCause(player);
    }
    if (owner != null) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
        if (Sponge.getCauseStackManager().getCurrentCause() == null) {
            Sponge.getCauseStackManager().pushCause(owner);
        }
    } else {
        Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, (User) player);
        if (Sponge.getCauseStackManager().getCurrentCause() == null) {
            Sponge.getCauseStackManager().pushCause(player);
        }
    }
    if (notifier != null) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
    }
    Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_PLACE, (World) world);
    return SpongeEventFactory.createChangeBlockEventPlace(Sponge.getCauseStackManager().getCurrentCause(), blockSnapshots);
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) User(org.spongepowered.api.entity.living.player.User) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) Transaction(org.spongepowered.api.data.Transaction) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot)

Example 12 with PhaseData

use of org.spongepowered.common.event.tracking.PhaseData in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method createChangeBlockEventPlace.

public static ChangeBlockEvent.Place createChangeBlockEventPlace(BlockEvent.MultiPlaceEvent forgeEvent) {
    final net.minecraft.world.World world = forgeEvent.getWorld();
    if (world.isRemote) {
        return null;
    }
    ImmutableList.Builder<Transaction<BlockSnapshot>> builder = new ImmutableList.Builder<Transaction<BlockSnapshot>>();
    for (net.minecraftforge.common.util.BlockSnapshot blockSnapshot : forgeEvent.getReplacedBlockSnapshots()) {
        final BlockPos snapshotPos = blockSnapshot.getPos();
        BlockSnapshot originalSnapshot = ((IMixinBlockSnapshot) blockSnapshot).createSpongeBlockSnapshot();
        BlockSnapshot finalSnapshot = ((World) world).createSnapshot(snapshotPos.getX(), snapshotPos.getY(), snapshotPos.getZ());
        builder.add(new Transaction<>(originalSnapshot, finalSnapshot));
    }
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData data = phaseTracker.getCurrentPhaseData();
    User owner = data.context.getOwner().orElse(null);
    User notifier = data.context.getNotifier().orElse(null);
    EntityPlayer player = forgeEvent.getPlayer();
    if (SpongeImplHooks.isFakePlayer(player)) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.FAKE_PLAYER, EntityUtil.toPlayer(player));
    } else if (Sponge.getCauseStackManager().getCurrentCause() == null) {
        Sponge.getCauseStackManager().pushCause(player);
    }
    if (owner != null) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
        if (Sponge.getCauseStackManager().getCurrentCause() == null) {
            Sponge.getCauseStackManager().pushCause(owner);
        }
    } else {
        Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, (User) player);
        if (Sponge.getCauseStackManager().getCurrentCause() == null) {
            Sponge.getCauseStackManager().pushCause(player);
        }
    }
    if (notifier != null) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
    }
    return SpongeEventFactory.createChangeBlockEventPlace(Sponge.getCauseStackManager().getCurrentCause(), builder.build());
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) User(org.spongepowered.api.entity.living.player.User) ImmutableList(com.google.common.collect.ImmutableList) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Transaction(org.spongepowered.api.data.Transaction) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot)

Example 13 with PhaseData

use of org.spongepowered.common.event.tracking.PhaseData in project SpongeCommon by SpongePowered.

the class MixinEntityFallingBlock method onWorldSetBlockToAir.

@Inject(method = "onUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;setBlockToAir(Lnet/minecraft/util/math/BlockPos;)Z", shift = At.Shift.AFTER), cancellable = true)
private void onWorldSetBlockToAir(CallbackInfo ci) {
    final BlockPos pos = new BlockPos((EntityFallingBlock) (Object) this);
    if (((IMixinWorld) this.world).isFake()) {
        this.world.setBlockToAir(pos);
        return;
    }
    // Ideally, at this point we should still be in the EntityTickState and only this block should
    // be changing. What we need to do here is throw the block event specifically for setting air
    // and THEN if this one cancels, we should kill this entity off, unless we want some duplication
    // of falling blocks
    final PhaseData currentPhaseData = PhaseTracker.getInstance().getCurrentPhaseData();
    this.world.setBlockToAir(pos);
    // By this point, we should have one captured block at least.
    if (!TrackingUtil.processBlockCaptures(currentPhaseData.context.getCapturedBlocks(), currentPhaseData.state, currentPhaseData.context)) {
        // So, it's been cancelled, we want to absolutely remove this entity.
        // And we want to stop the entity update at this point.
        this.setDead();
        ci.cancel();
    }
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) BlockPos(net.minecraft.util.math.BlockPos) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 14 with PhaseData

use of org.spongepowered.common.event.tracking.PhaseData in project SpongeCommon by SpongePowered.

the class ExplosionContext method populateFromCurrentState.

@SuppressWarnings("unchecked")
public ExplosionContext populateFromCurrentState() {
    final PhaseData currentPhaseData = PhaseTracker.getInstance().getCurrentPhaseData();
    ((IPhaseState) currentPhaseData.state).appendContextPreExplosion(this, currentPhaseData.context);
    return this;
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState)

Example 15 with PhaseData

use of org.spongepowered.common.event.tracking.PhaseData in project SpongeCommon by SpongePowered.

the class MixinNetHandlerPlayServer method onProcessRightClickBlock.

@Redirect(method = "processTryUseItemOnBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/management/PlayerInteractionManager;processRightClickBlock(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;Lnet/minecraft/util/EnumHand;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;FFF)Lnet/minecraft/util/EnumActionResult;"))
public EnumActionResult onProcessRightClickBlock(PlayerInteractionManager interactionManager, EntityPlayer player, net.minecraft.world.World worldIn, @Nullable ItemStack stack, EnumHand hand, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ) {
    EnumActionResult actionResult = interactionManager.processRightClickBlock(this.player, worldIn, stack, hand, pos, facing, hitX, hitY, hitZ);
    // since the client will send the server a CPacketTryUseItem right after this packet is done processing.
    if (actionResult != EnumActionResult.SUCCESS) {
        // If a plugin or mod has changed the item, avoid restoring
        if (!SpongeCommonEventFactory.playerInteractItemChanged) {
            final PhaseTracker phaseTracker = PhaseTracker.getInstance();
            final PhaseData peek = phaseTracker.getCurrentPhaseData();
            final ItemStack itemStack = ItemStackUtil.toNative(((PacketContext<?>) peek.context).getItemUsed());
            // cancelling item usage (e.g. eating food) that occurs while targeting a block
            if (!ItemStack.areItemStacksEqual(itemStack, player.getHeldItem(hand)) || SpongeCommonEventFactory.interactBlockEventCancelled) {
                PacketPhaseUtil.handlePlayerSlotRestore((EntityPlayerMP) player, itemStack, hand);
            }
        }
    }
    SpongeCommonEventFactory.playerInteractItemChanged = false;
    SpongeCommonEventFactory.interactBlockEventCancelled = false;
    return actionResult;
}
Also used : EnumActionResult(net.minecraft.util.EnumActionResult) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) PhaseData(org.spongepowered.common.event.tracking.PhaseData) ItemStack(net.minecraft.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

PhaseData (org.spongepowered.common.event.tracking.PhaseData)30 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)22 BlockPos (net.minecraft.util.math.BlockPos)13 IPhaseState (org.spongepowered.common.event.tracking.IPhaseState)10 User (org.spongepowered.api.entity.living.player.User)9 IBlockState (net.minecraft.block.state.IBlockState)8 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 World (org.spongepowered.api.world.World)8 Overwrite (org.spongepowered.asm.mixin.Overwrite)8 CauseStackManager (org.spongepowered.api.event.CauseStackManager)6 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)6 ItemStack (net.minecraft.item.ItemStack)5 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)5 Transaction (org.spongepowered.api.data.Transaction)5 BlockState (org.spongepowered.api.block.BlockState)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 Redirect (org.spongepowered.asm.mixin.injection.Redirect)4 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)4 IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)4 Block (net.minecraft.block.Block)3