Search in sources :

Example 6 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class ChunkMapMixin method impl$onSave.

@Inject(method = "save", at = @At(value = "HEAD"), cancellable = true)
private void impl$onSave(final ChunkAccess var1, final CallbackInfoReturnable<Boolean> cir) {
    if (var1 instanceof WorldChunk) {
        if (ShouldFire.CHUNK_EVENT_SAVE_PRE) {
            final Vector3i chunkPos = new Vector3i(var1.getPos().x, 0, var1.getPos().z);
            final ChunkEvent.Save.Pre postSave = SpongeEventFactory.createChunkEventSavePre(PhaseTracker.getInstance().currentCause(), ((WorldChunk) var1), chunkPos, (ResourceKey) (Object) this.level.dimension().location());
            SpongeCommon.post(postSave);
            if (postSave.isCancelled()) {
                cir.setReturnValue(false);
            }
        }
    }
}
Also used : WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 7 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class EntityVolumeTest method onRegisterCommands.

@Listener
public void onRegisterCommands(final RegisterCommandEvent<Command.Parameterized> event) {
    Command.Parameterized command = Command.builder().executor((ctx) -> {
        final Object root = ctx.cause().root();
        if (!(root instanceof Locatable)) {
            throw new CommandException(Component.text("You must be locatable to use this command!"));
        }
        final Audience audience = ctx.cause().audience();
        final ServerLocation serverLocation = ((Locatable) root).serverLocation();
        final WorldChunk chunk = serverLocation.world().chunk(serverLocation.chunkPosition());
        final Collection<? extends Entity> chunkEntities = chunk.entities();
        final Collection<? extends Entity> worldEntities = serverLocation.world().entities();
        final boolean worldContainsChunkEntities = serverLocation.world().entities().containsAll(chunkEntities);
        audience.sendMessage(testResult("World contains chunk entities test", worldContainsChunkEntities));
        final boolean worldContainsMoreEntitiesThanChunk = worldEntities.size() > chunkEntities.size();
        audience.sendMessage(testResult("World contains more entities than chunk test", worldContainsMoreEntitiesThanChunk).append(Component.text(" (World " + worldEntities.size() + " vs Chunk " + chunkEntities.size() + ")")));
        final boolean chunkEntitiesIsSameAsAABB = chunk.entities(AABB.of(chunk.min(), chunk.max())).equals(chunkEntities);
        audience.sendMessage(testResult(".entities is the same as AABB of chunk", chunkEntitiesIsSameAsAABB));
        audience.sendMessage(Component.text("See console for a list of all entities."));
        this.logger.info(chunkEntities.size() + " entities in chunk " + chunk.chunkPosition() + ":\n" + chunkEntities);
        this.logger.info("---------");
        this.logger.info(worldEntities.size() + " entities in world " + serverLocation.world().properties().key() + ":\n" + worldEntities);
        return CommandResult.success();
    }).build();
    event.register(this.plugin, command, "checkentitymethods");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) Command(org.spongepowered.api.command.Command) Inject(com.google.inject.Inject) Collection(java.util.Collection) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) AABB(org.spongepowered.api.util.AABB) Entity(org.spongepowered.api.entity.Entity) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) Logger(org.apache.logging.log4j.Logger) Locatable(org.spongepowered.api.world.Locatable) PluginContainer(org.spongepowered.plugin.PluginContainer) Audience(net.kyori.adventure.audience.Audience) Component(net.kyori.adventure.text.Component) Listener(org.spongepowered.api.event.Listener) CommandException(org.spongepowered.api.command.exception.CommandException) RegisterCommandEvent(org.spongepowered.api.event.lifecycle.RegisterCommandEvent) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Entity(org.spongepowered.api.entity.Entity) Command(org.spongepowered.api.command.Command) Audience(net.kyori.adventure.audience.Audience) ServerLocation(org.spongepowered.api.world.server.ServerLocation) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Collection(java.util.Collection) CommandException(org.spongepowered.api.command.exception.CommandException) Locatable(org.spongepowered.api.world.Locatable) Listener(org.spongepowered.api.event.Listener)

Example 8 with WorldChunk

use of org.spongepowered.api.world.chunk.WorldChunk in project SpongeCommon by SpongePowered.

the class LevelChunkMixin_API method blockEntityStream.

@Override
public VolumeStream<WorldChunk, BlockEntity> blockEntityStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"), Objects.requireNonNull(options, "options"));
    final boolean shouldCarbonCopy = options.carbonCopy();
    final Vector3i size = max.sub(min).add(1, 1, 1);
    @MonotonicNonNull final ObjectArrayMutableBlockEntityBuffer backingVolume;
    if (shouldCarbonCopy) {
        backingVolume = new ObjectArrayMutableBlockEntityBuffer(min, size);
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<WorldChunk, BlockEntity, net.minecraft.world.level.block.entity.BlockEntity, ChunkAccess, BlockPos>generateStream(options, // Ref
    (WorldChunk) this, (LevelChunk) (Object) this, // Entity Accessor
    this::impl$getBlockEntitiesStream, // IdentityFunction
    VolumeStreamUtils.getBlockEntityOrCloneToBackingVolume(shouldCarbonCopy, backingVolume, this.level), // Biome by block position
    (key, biome) -> key, // Filtered Position Entity Accessor
    (blockPos, world) -> {
        final net.minecraft.world.level.block.entity.@Nullable BlockEntity tileEntity = shouldCarbonCopy ? (net.minecraft.world.level.block.entity.BlockEntity) backingVolume.blockEntity(blockPos.getX(), blockPos.getY(), blockPos.getZ()).orElse(null) : ((LevelReader) world).getBlockEntity(blockPos);
        return new Tuple<>(blockPos, tileEntity);
    });
}
Also used : ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) BlockPos(net.minecraft.core.BlockPos) ObjectArrayMutableBlockEntityBuffer(org.spongepowered.common.world.volume.buffer.blockentity.ObjectArrayMutableBlockEntityBuffer) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Tuple(net.minecraft.util.Tuple) BlockEntity(org.spongepowered.api.block.entity.BlockEntity)

Aggregations

WorldChunk (org.spongepowered.api.world.chunk.WorldChunk)8 Vector3i (org.spongepowered.math.vector.Vector3i)6 Tuple (net.minecraft.util.Tuple)4 ChunkAccess (net.minecraft.world.level.chunk.ChunkAccess)4 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)4 BlockPos (net.minecraft.core.BlockPos)3 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)3 Entity (org.spongepowered.api.entity.Entity)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 UUID (java.util.UUID)2 BlockState (org.spongepowered.api.block.BlockState)2 AABB (org.spongepowered.api.util.AABB)2 ArrayMutableBlockBuffer (org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer)2 ObjectArrayMutableEntityBuffer (org.spongepowered.common.world.volume.buffer.entity.ObjectArrayMutableEntityBuffer)2 ImmutableList (com.google.common.collect.ImmutableList)1 Inject (com.google.inject.Inject)1 Arrays (java.util.Arrays)1