Search in sources :

Example 1 with BasicPluginContext

use of org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext in project SpongeCommon by SpongePowered.

the class SpongeBlockVolumeWorker method iterate.

@Override
public void iterate(BlockVolumeVisitor<V> visitor) {
    final int xMin = this.volume.getBlockMin().getX();
    final int yMin = this.volume.getBlockMin().getY();
    final int zMin = this.volume.getBlockMin().getZ();
    final int xMax = this.volume.getBlockMax().getX();
    final int yMax = this.volume.getBlockMax().getY();
    final int zMax = this.volume.getBlockMax().getZ();
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
        BasicPluginContext context = PluginPhase.State.BLOCK_WORKER.createPhaseContext().source(this).buildAndSwitch()) {
        for (int z = zMin; z <= zMax; z++) {
            for (int y = yMin; y <= yMax; y++) {
                for (int x = xMin; x <= xMax; x++) {
                    visitor.visit(this.volume, x, y, z);
                }
            }
        }
    }
}
Also used : BasicPluginContext(org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext) CauseStackManager(org.spongepowered.api.event.CauseStackManager)

Example 2 with BasicPluginContext

use of org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext in project SpongeCommon by SpongePowered.

the class MixinEntity method setLocation.

@Override
public boolean setLocation(Location<World> location) {
    checkNotNull(location, "The location was null!");
    if (isRemoved()) {
        return false;
    }
    try (final BasicPluginContext context = PluginPhase.State.TELEPORT.createPhaseContext().buildAndSwitch()) {
        // TODO Add a 'Move' plugin phase or just keep it under Teleport?
        try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            if (!Sponge.getCauseStackManager().getCurrentContext().containsKey(EventContextKeys.TELEPORT_TYPE)) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.TELEPORT_TYPE, TeleportTypes.PLUGIN);
            }
            // TODO These methods need a Cause (maybe wait till Cause PR)
            // TODO Need to not fire Teleport in all cases (especially when movement is local)
            MoveEntityEvent.Teleport event = EntityUtil.handleDisplaceEntityTeleportEvent((net.minecraft.entity.Entity) (Object) this, location);
            if (event.isCancelled()) {
                return false;
            }
            location = event.getToTransform().getLocation();
            this.rotationPitch = (float) event.getToTransform().getPitch();
            this.rotationYaw = (float) event.getToTransform().getYaw();
        }
        final IMixinChunkProviderServer chunkProviderServer = (IMixinChunkProviderServer) ((WorldServer) this.world).getChunkProvider();
        chunkProviderServer.setForceChunkRequests(true);
        final net.minecraft.entity.Entity thisEntity = (net.minecraft.entity.Entity) (Object) this;
        final List<net.minecraft.entity.Entity> passengers = thisEntity.getPassengers();
        boolean isTeleporting = true;
        if (location.getExtent().getUniqueId() != ((World) this.world).getUniqueId()) {
            final net.minecraft.world.World nmsWorld = (net.minecraft.world.World) location.getExtent();
            if ((net.minecraft.entity.Entity) (Object) this instanceof EntityPlayerMP) {
                // Close open containers
                final EntityPlayerMP entityPlayerMP = (EntityPlayerMP) (Object) this;
                if (entityPlayerMP.openContainer != entityPlayerMP.inventoryContainer) {
                    // Call API method to make sure we capture it
                    ((Player) entityPlayerMP).closeInventory();
                }
            }
            EntityUtil.changeWorld((net.minecraft.entity.Entity) (Object) this, location, ((IMixinWorldServer) this.world).getDimensionId(), ((IMixinWorldServer) nmsWorld).getDimensionId());
        } else {
            double distance = location.getPosition().distance(this.getPosition());
            if (distance <= 4) {
                isTeleporting = false;
            }
            if (thisEntity instanceof EntityPlayerMP && ((EntityPlayerMP) thisEntity).connection != null) {
                final EntityPlayerMP entityPlayerMP = (EntityPlayerMP) thisEntity;
                // No reason to attempt to load chunks unless we're teleporting
                if (isTeleporting) {
                    // Close open containers
                    if (entityPlayerMP.openContainer != entityPlayerMP.inventoryContainer) {
                        // Call API method to make sure we capture it
                        ((Player) entityPlayerMP).closeInventory();
                    }
                    ((WorldServer) location.getExtent()).getChunkProvider().loadChunk(location.getChunkPosition().getX(), location.getChunkPosition().getZ());
                }
                entityPlayerMP.connection.setPlayerLocation(location.getX(), location.getY(), location.getZ(), thisEntity.rotationYaw, thisEntity.rotationPitch);
                // Set last move to teleport target
                ((IMixinNetHandlerPlayServer) entityPlayerMP.connection).setLastMoveLocation(null);
            } else {
                setPosition(location.getPosition().getX(), location.getPosition().getY(), location.getPosition().getZ());
            }
        }
        if (isTeleporting) {
            // Re-attach passengers
            for (net.minecraft.entity.Entity passenger : passengers) {
                passenger.startRiding(thisEntity, true);
            }
        }
        chunkProviderServer.setForceChunkRequests(false);
        return true;
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Player(org.spongepowered.api.entity.living.player.Player) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) World(org.spongepowered.api.world.World) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) BasicPluginContext(org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext) CauseStackManager(org.spongepowered.api.event.CauseStackManager) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinNetHandlerPlayServer(org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer)

Example 3 with BasicPluginContext

use of org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext in project SpongeCommon by SpongePowered.

the class SpongeBlockVolumeWorker method map.

@Override
public void map(BlockVolumeMapper mapper, MutableBlockVolume destination) {
    final Vector3i offset = align(destination);
    final int xOffset = offset.getX();
    final int yOffset = offset.getY();
    final int zOffset = offset.getZ();
    final UnmodifiableBlockVolume unmodifiableVolume = this.volume.getUnmodifiableBlockView();
    final int xMin = unmodifiableVolume.getBlockMin().getX();
    final int yMin = unmodifiableVolume.getBlockMin().getY();
    final int zMin = unmodifiableVolume.getBlockMin().getZ();
    final int xMax = unmodifiableVolume.getBlockMax().getX();
    final int yMax = unmodifiableVolume.getBlockMax().getY();
    final int zMax = unmodifiableVolume.getBlockMax().getZ();
    // a single go, requiring only one event
    try (BasicPluginContext phaseState = PluginPhase.State.BLOCK_WORKER.createPhaseContext().source(this).buildAndSwitch()) {
        for (int z = zMin; z <= zMax; z++) {
            for (int y = yMin; y <= yMax; y++) {
                for (int x = xMin; x <= xMax; x++) {
                    final BlockState block = mapper.map(unmodifiableVolume, x, y, z);
                    destination.setBlock(x + xOffset, y + yOffset, z + zOffset, block);
                }
            }
        }
    }
}
Also used : BasicPluginContext(org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext) BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) UnmodifiableBlockVolume(org.spongepowered.api.world.extent.UnmodifiableBlockVolume)

Example 4 with BasicPluginContext

use of org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext in project SpongeCommon by SpongePowered.

the class SpongeBlockVolumeWorker method merge.

@Override
public void merge(BlockVolume second, BlockVolumeMerger merger, MutableBlockVolume destination) {
    final Vector3i offsetSecond = align(second);
    final int xOffsetSecond = offsetSecond.getX();
    final int yOffsetSecond = offsetSecond.getY();
    final int zOffsetSecond = offsetSecond.getZ();
    final Vector3i offsetDestination = align(destination);
    final int xOffsetDestination = offsetDestination.getX();
    final int yOffsetDestination = offsetDestination.getY();
    final int zOffsetDestination = offsetDestination.getZ();
    final UnmodifiableBlockVolume firstUnmodifiableVolume = this.volume.getUnmodifiableBlockView();
    final int xMin = firstUnmodifiableVolume.getBlockMin().getX();
    final int yMin = firstUnmodifiableVolume.getBlockMin().getY();
    final int zMin = firstUnmodifiableVolume.getBlockMin().getZ();
    final int xMax = firstUnmodifiableVolume.getBlockMax().getX();
    final int yMax = firstUnmodifiableVolume.getBlockMax().getY();
    final int zMax = firstUnmodifiableVolume.getBlockMax().getZ();
    final UnmodifiableBlockVolume secondUnmodifiableVolume = second.getUnmodifiableBlockView();
    try (BasicPluginContext context = PluginPhase.State.BLOCK_WORKER.createPhaseContext().source(this).buildAndSwitch()) {
        for (int z = zMin; z <= zMax; z++) {
            for (int y = yMin; y <= yMax; y++) {
                for (int x = xMin; x <= xMax; x++) {
                    final BlockState block = merger.merge(firstUnmodifiableVolume, x, y, z, secondUnmodifiableVolume, x + xOffsetSecond, y + yOffsetSecond, z + zOffsetSecond);
                    destination.setBlock(x + xOffsetDestination, y + yOffsetDestination, z + zOffsetDestination, block);
                }
            }
        }
    }
}
Also used : BasicPluginContext(org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext) BlockState(org.spongepowered.api.block.BlockState) Vector3i(com.flowpowered.math.vector.Vector3i) UnmodifiableBlockVolume(org.spongepowered.api.world.extent.UnmodifiableBlockVolume)

Example 5 with BasicPluginContext

use of org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext in project SpongeCommon by SpongePowered.

the class MixinWorldServer method spawnEntity.

@Override
public boolean spawnEntity(Entity entity) {
    checkNotNull(entity, "The entity cannot be null!");
    if (!PhaseTracker.validateEntitySpawn(this, entity)) {
        return true;
    }
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final IPhaseState state = phaseTracker.getCurrentState();
    if (!state.alreadyCapturingEntitySpawns()) {
        try (final BasicPluginContext context = PluginPhase.State.CUSTOM_SPAWN.createPhaseContext().addCaptures().buildAndSwitch()) {
            phaseTracker.spawnEntityWithCause(this, entity);
            return true;
        }
    }
    return phaseTracker.spawnEntityWithCause(this, entity);
}
Also used : BasicPluginContext(org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState)

Aggregations

BasicPluginContext (org.spongepowered.common.event.tracking.phase.plugin.BasicPluginContext)5 Vector3i (com.flowpowered.math.vector.Vector3i)2 BlockState (org.spongepowered.api.block.BlockState)2 CauseStackManager (org.spongepowered.api.event.CauseStackManager)2 UnmodifiableBlockVolume (org.spongepowered.api.world.extent.UnmodifiableBlockVolume)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 Entity (org.spongepowered.api.entity.Entity)1 Player (org.spongepowered.api.entity.living.player.Player)1 MoveEntityEvent (org.spongepowered.api.event.entity.MoveEntityEvent)1 World (org.spongepowered.api.world.World)1 IPhaseState (org.spongepowered.common.event.tracking.IPhaseState)1 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)1 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)1 IMixinNetHandlerPlayServer (org.spongepowered.common.interfaces.network.IMixinNetHandlerPlayServer)1 IMixinChunkProviderServer (org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)1