Search in sources :

Example 1 with IMixinBlockSnapshot

use of org.spongepowered.mod.interfaces.IMixinBlockSnapshot 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 2 with IMixinBlockSnapshot

use of org.spongepowered.mod.interfaces.IMixinBlockSnapshot 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)

Aggregations

EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 BlockPos (net.minecraft.util.math.BlockPos)2 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)2 Transaction (org.spongepowered.api.data.Transaction)2 User (org.spongepowered.api.entity.living.player.User)2 World (org.spongepowered.api.world.World)2 PhaseData (org.spongepowered.common.event.tracking.PhaseData)2 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)2 IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)2 IMixinBlockSnapshot (org.spongepowered.mod.interfaces.IMixinBlockSnapshot)2 ImmutableList (com.google.common.collect.ImmutableList)1 IBlockState (net.minecraft.block.state.IBlockState)1 BlockState (org.spongepowered.api.block.BlockState)1