Search in sources :

Example 11 with IPhaseState

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

the class GeneralPhase method performPostBlockAdditions.

@SuppressWarnings("unchecked")
private static void performPostBlockAdditions(PhaseContext<?> postContext, List<Transaction<BlockSnapshot>> transactions, IPhaseState<?> unwindingState, PhaseContext<?> unwindingPhaseContext) {
    // We have to use a proxy so that our pending changes are notified such that any accessors from block
    // classes do not fail on getting the incorrect block state from the IBlockAccess
    final SpongeProxyBlockAccess proxyBlockAccess = new SpongeProxyBlockAccess(transactions);
    final CapturedMultiMapSupplier<BlockPos, ItemDropData> capturedBlockDrops = postContext.getBlockDropSupplier();
    final CapturedMultiMapSupplier<BlockPos, EntityItem> capturedBlockItemEntityDrops = postContext.getBlockItemDropSupplier();
    for (Transaction<BlockSnapshot> transaction : transactions) {
        if (!transaction.isValid()) {
            // Don't use invalidated block transactions during notifications, these only need to be restored
            continue;
        }
        // Handle custom replacements
        if (transaction.getCustom().isPresent()) {
            transaction.getFinal().restore(true, BlockChangeFlags.ALL);
        }
        final SpongeBlockSnapshot oldBlockSnapshot = (SpongeBlockSnapshot) transaction.getOriginal();
        final SpongeBlockSnapshot newBlockSnapshot = (SpongeBlockSnapshot) transaction.getFinal();
        // Handle item drops captured
        final Location<World> worldLocation = oldBlockSnapshot.getLocation().get();
        final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) worldLocation.getExtent();
        final BlockPos pos = ((IMixinLocation) (Object) worldLocation).getBlockPos();
        capturedBlockDrops.acceptAndRemoveIfPresent(pos, items -> TrackingUtil.spawnItemDataForBlockDrops(items, oldBlockSnapshot, unwindingPhaseContext, unwindingState));
        capturedBlockItemEntityDrops.acceptAndRemoveIfPresent(pos, items -> TrackingUtil.spawnItemEntitiesForBlockDrops(items, oldBlockSnapshot, unwindingPhaseContext, unwindingState));
        final WorldServer worldServer = mixinWorldServer.asMinecraftWorld();
        SpongeHooks.logBlockAction(worldServer, oldBlockSnapshot.blockChange, transaction);
        final SpongeBlockChangeFlag spongeFlag = oldBlockSnapshot.getChangeFlag();
        final int updateFlag = spongeFlag.getRawFlag();
        final IBlockState originalState = (IBlockState) oldBlockSnapshot.getState();
        final IBlockState newState = (IBlockState) newBlockSnapshot.getState();
        // Containers get placed automatically
        final CapturedSupplier<BlockSnapshot> capturedBlockSupplier = postContext.getCapturedBlockSupplier();
        if (spongeFlag.performBlockPhysics() && originalState.getBlock() != newState.getBlock() && !SpongeImplHooks.hasBlockTileEntity(newState.getBlock(), newState)) {
            newState.getBlock().onBlockAdded(worldServer, pos, newState);
            postContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
                final ArrayList<Entity> capturedEntities = new ArrayList<>(entities);
                ((IPhaseState) unwindingState).postProcessSpawns(unwindingPhaseContext, capturedEntities);
            });
            capturedBlockSupplier.acceptAndClearIfNotEmpty(blocks -> {
                final List<BlockSnapshot> blockSnapshots = new ArrayList<>(blocks);
                processBlockTransactionListsPost(postContext, blockSnapshots, unwindingState, unwindingPhaseContext);
            });
        }
        proxyBlockAccess.proceed();
        ((IPhaseState) unwindingState).handleBlockChangeWithUser(oldBlockSnapshot.blockChange, transaction, unwindingPhaseContext);
        if (spongeFlag.isNotifyClients()) {
            // Since notifyBlockUpdate is basically to tell clients that the block position has changed,
            // we need to respect that flag
            worldServer.notifyBlockUpdate(pos, originalState, newState, updateFlag);
        }
        if (spongeFlag.updateNeighbors()) {
            // Notify neighbors only if the change flag allowed it.
            mixinWorldServer.spongeNotifyNeighborsPostBlockChange(pos, originalState, newState, spongeFlag);
        } else if (spongeFlag.notifyObservers()) {
            worldServer.updateObservingBlocksAt(pos, newState.getBlock());
        }
        capturedBlockSupplier.acceptAndClearIfNotEmpty(blocks -> {
            final List<BlockSnapshot> blockSnapshots = new ArrayList<>(blocks);
            blocks.clear();
            processBlockTransactionListsPost(postContext, blockSnapshots, unwindingState, unwindingPhaseContext);
        });
    }
}
Also used : SpongeBlockChangeFlag(org.spongepowered.common.world.SpongeBlockChangeFlag) Entity(org.spongepowered.api.entity.Entity) IBlockState(net.minecraft.block.state.IBlockState) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) ArrayList(java.util.ArrayList) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) World(org.spongepowered.api.world.World) SpongeProxyBlockAccess(org.spongepowered.common.world.SpongeProxyBlockAccess) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockPos(net.minecraft.util.math.BlockPos) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) EntityItem(net.minecraft.entity.item.EntityItem)

Example 12 with IPhaseState

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

the class PostState method appendContextPreExplosion.

@SuppressWarnings("unchecked")
@Override
public void appendContextPreExplosion(ExplosionContext explosionContext, UnwindingPhaseContext context) {
    final IPhaseState phaseState = context.getUnwindingState();
    final PhaseContext<?> unwinding = context.getUnwindingContext();
    phaseState.appendContextPreExplosion(explosionContext, unwinding);
}
Also used : IPhaseState(org.spongepowered.common.event.tracking.IPhaseState)

Example 13 with IPhaseState

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

the class PostState method unwind.

@SuppressWarnings("unchecked")
@Override
public void unwind(UnwindingPhaseContext context) {
    final IPhaseState unwindingState = context.getUnwindingState();
    final PhaseContext<?> unwindingContext = context.getUnwindingContext();
    this.postDispatch(unwindingState, unwindingContext, context);
}
Also used : IPhaseState(org.spongepowered.common.event.tracking.IPhaseState)

Example 14 with IPhaseState

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

the class MixinDedicatedServer method isBlockProtected.

/**
 * @author zml - March 9th, 2016
 * @author blood - July 7th, 2016 - Add cause tracker handling for throwing pre change block checks
 * @author gabizou - July 7th, 2016 - Update for 1.10's cause tracking changes
 *
 * @reason Change spawn protection to take advantage of Sponge permissions. Rather than affecting only the default world like vanilla, this
 * will apply to any world. Additionally, fire a spawn protection event
 */
@Overwrite
@Override
public boolean isBlockProtected(net.minecraft.world.World worldIn, BlockPos pos, EntityPlayer playerIn) {
    // Mods such as ComputerCraft and Thaumcraft check this method before attempting to set a blockstate.
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData peek = phaseTracker.getCurrentPhaseData();
    final IPhaseState phaseState = peek.state;
    if (phaseState == null || !phaseState.isInteraction()) {
        // TODO BLOCK_PROTECTED flag
        if (SpongeCommonEventFactory.callChangeBlockEventPre((IMixinWorldServer) worldIn, pos, playerIn).isCancelled()) {
            return true;
        }
    }
    BlockPos spawnPoint = worldIn.getSpawnPoint();
    int protectionRadius = getSpawnProtectionSize();
    return protectionRadius > 0 && Math.max(Math.abs(pos.getX() - spawnPoint.getX()), Math.abs(pos.getZ() - spawnPoint.getZ())) <= protectionRadius && !((Player) playerIn).hasPermission("minecraft.spawn-protection.override");
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) PhaseData(org.spongepowered.common.event.tracking.PhaseData) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) BlockPos(net.minecraft.util.math.BlockPos) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 15 with IPhaseState

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

the class ContainerUtil method performBlockInventoryDrops.

/**
 * Replacement helper method for {@link MixinInventoryHelper#spongeDropInventoryItems(World, double, double, double, IInventory)}
 * to perform cause tracking related drops. This is specific for blocks, not for any other cases.
 *
 * @param worldServer The world server
 * @param x the x position
 * @param y the y position
 * @param z the z position
 * @param inventory The inventory to drop items from
 */
public static void performBlockInventoryDrops(WorldServer worldServer, double x, double y, double z, IInventory inventory) {
    final PhaseData currentPhase = PhaseTracker.getInstance().getCurrentPhaseData();
    final IPhaseState currentState = currentPhase.state;
    if (currentState.tracksBlockSpecificDrops()) {
        final PhaseContext<?> context = currentPhase.context;
        if (!currentState.ignoresItemPreMerging() && SpongeImpl.getGlobalConfig().getConfig().getOptimizations().doDropsPreMergeItemDrops()) {
            // Add itemstack to pre merge list
            final Multimap<BlockPos, ItemDropData> multimap = context.getBlockDropSupplier().get();
            final BlockPos pos = new BlockPos(x, y, z);
            final Collection<ItemDropData> itemStacks = multimap.get(pos);
            for (int i = 0; i < inventory.getSizeInventory(); i++) {
                final net.minecraft.item.ItemStack itemStack = inventory.getStackInSlot(i);
                if (!itemStack.isEmpty()) {
                    SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(itemStack).position(VecHelper.toVector3d(pos)).build());
                }
            }
        } else {
            // Don't do pre-merging - directly spawn in item
            final Multimap<BlockPos, EntityItem> multimap = context.getBlockItemDropSupplier().get();
            final BlockPos pos = new BlockPos(x, y, z);
            final Collection<EntityItem> itemStacks = multimap.get(pos);
            for (int j = 0; j < inventory.getSizeInventory(); j++) {
                final net.minecraft.item.ItemStack itemStack = inventory.getStackInSlot(j);
                if (!itemStack.isEmpty()) {
                    float f = RANDOM.nextFloat() * 0.8F + 0.1F;
                    float f1 = RANDOM.nextFloat() * 0.8F + 0.1F;
                    float f2 = RANDOM.nextFloat() * 0.8F + 0.1F;
                    while (!itemStack.isEmpty()) {
                        int i = RANDOM.nextInt(21) + 10;
                        EntityItem entityitem = new EntityItem(worldServer, x + f, y + f1, z + f2, itemStack.splitStack(i));
                        entityitem.motionX = RANDOM.nextGaussian() * 0.05;
                        entityitem.motionY = RANDOM.nextGaussian() * 0.05 + 0.2;
                        entityitem.motionZ = RANDOM.nextGaussian() * 0.05;
                        itemStacks.add(entityitem);
                    }
                }
            }
        }
        return;
    }
    // Finally, just default to spawning the entities normally, regardless of the case.
    for (int i = 0; i < inventory.getSizeInventory(); i++) {
        final net.minecraft.item.ItemStack itemStack = inventory.getStackInSlot(i);
        if (!itemStack.isEmpty()) {
            InventoryHelper.spawnItemStack(worldServer, x, y, z, itemStack);
        }
    }
}
Also used : PhaseData(org.spongepowered.common.event.tracking.PhaseData) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) ItemStack(net.minecraft.item.ItemStack) BlockPos(net.minecraft.util.math.BlockPos) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

IPhaseState (org.spongepowered.common.event.tracking.IPhaseState)26 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)20 PhaseData (org.spongepowered.common.event.tracking.PhaseData)10 Redirect (org.spongepowered.asm.mixin.injection.Redirect)7 IBlockState (net.minecraft.block.state.IBlockState)5 Overwrite (org.spongepowered.asm.mixin.Overwrite)5 BlockPos (net.minecraft.util.math.BlockPos)4 LocatableBlock (org.spongepowered.api.world.LocatableBlock)4 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)4 EntityItem (net.minecraft.entity.item.EntityItem)3 CauseStackManager (org.spongepowered.api.event.CauseStackManager)3 World (org.spongepowered.api.world.World)3 ArrayList (java.util.ArrayList)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Chunk (net.minecraft.world.chunk.Chunk)2 BlockState (org.spongepowered.api.block.BlockState)2 Entity (org.spongepowered.api.entity.Entity)2 Player (org.spongepowered.api.entity.living.player.Player)2 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)2