Search in sources :

Example 26 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method bridge$startBlockChange.

@Override
public Optional<WorldPipeline.Builder> bridge$startBlockChange(final BlockPos pos, final BlockState newState, final int flags) {
    if (net.minecraft.world.level.Level.isOutsideBuildHeight(pos)) {
        return Optional.empty();
    } else if (this.shadow$isDebug()) {
        // isClientSide is always false since this is WorldServer
        return Optional.empty();
    }
    // Sponge Start - Sanity check against the PhaseTracker for instances
    if (this.bridge$isFake()) {
        return Optional.empty();
    }
    final PhaseTracker instance = PhaseTracker.getInstance();
    if (instance.getSidedThread() != PhaseTracker.SERVER.getSidedThread() && instance != PhaseTracker.SERVER) {
        throw new UnsupportedOperationException("Cannot perform a tracked Block Change on a ServerWorld while not on the main thread!");
    }
    final SpongeBlockChangeFlag spongeFlag = BlockChangeFlagManager.fromNativeInt(flags);
    final LevelChunk chunk = this.shadow$getChunkAt(pos);
    if (chunk.isEmpty()) {
        return Optional.empty();
    }
    final net.minecraft.world.level.block.state.BlockState currentState = chunk.getBlockState(pos);
    return Optional.of(this.bridge$makePipeline(pos, currentState, newState, chunk, spongeFlag, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
}
Also used : SpongeBlockChangeFlag(org.spongepowered.common.world.SpongeBlockChangeFlag) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) BlockState(net.minecraft.world.level.block.state.BlockState)

Example 27 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method destroyBlock.

@Override
public boolean destroyBlock(final BlockPos pos, final boolean doDrops, @Nullable final Entity p_241212_3_, final int limit) {
    final BlockState currentState = this.shadow$getBlockState(pos);
    if (currentState.isAir()) {
        return false;
    } else {
        // Sponge Start - Sanity check against the PhaseTracker for instances
        if (this.bridge$isFake()) {
            return super.destroyBlock(pos, doDrops, p_241212_3_, limit);
        }
        final PhaseTracker instance = PhaseTracker.getInstance();
        if (instance.getSidedThread() != PhaseTracker.SERVER.getSidedThread() && instance != PhaseTracker.SERVER) {
            throw new UnsupportedOperationException("Cannot perform a tracked Block Change on a ServerWorld while not on the main thread!");
        }
        final FluidState fluidstate = this.shadow$getFluidState(pos);
        final BlockState emptyBlock = fluidstate.createLegacyBlock();
        final SpongeBlockChangeFlag spongeFlag = BlockChangeFlagManager.fromNativeInt(3);
        final LevelChunk chunk = this.shadow$getChunkAt(pos);
        if (chunk.isEmpty()) {
            return false;
        }
        final WorldPipeline.Builder pipelineBuilder = this.bridge$makePipeline(pos, currentState, emptyBlock, chunk, spongeFlag, limit).addEffect(WorldDestroyBlockLevelEffect.getInstance());
        if (doDrops) {
            pipelineBuilder.addEffect(PerformBlockDropsFromDestruction.getInstance());
        }
        final WorldPipeline pipeline = pipelineBuilder.addEffect(WorldBlockChangeCompleteEffect.getInstance()).build();
        return pipeline.processEffects(instance.getPhaseContext(), currentState, emptyBlock, pos, p_241212_3_, spongeFlag, limit);
    }
}
Also used : SpongeBlockChangeFlag(org.spongepowered.common.world.SpongeBlockChangeFlag) BlockState(net.minecraft.world.level.block.state.BlockState) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) WorldPipeline(org.spongepowered.common.event.tracking.context.transaction.pipeline.WorldPipeline) FluidState(net.minecraft.world.level.material.FluidState)

Example 28 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class BlockGetterMixin_RayTraceChunkLoadOptimizations method clip.

// @formatter:on
/**
 * Can be modified later when
 * <a href="https://github.com/SpongePowered/Mixin/issues/355">the mixin feature</a>
 * is resolved.
 * <p>
 * Check if a chunk is loaded before attempting to check the state of the block
 * return null if the chunk is not loaded. Based on
 * <a href="https://github.com/PaperMC/Paper/blob/ver/1.15.2/Spigot-Server-Patches/0331-Prevent-rayTrace-from-loading-chunks.patch#L16">a Paper patch</a>
 *
 * @author gabizou - Minecraft 1.15.2 - October 16th, 2020
 * @reason Because this patch requires a lambda injection, I don't want
 * to risk the breakages that can be caused by injecting into an interface.
 */
@Overwrite
default BlockHitResult clip(final ClipContext context) {
    return BlockGetter.traverseBlocks(context, (p_217297_1_, p_217297_2_) -> {
        // Sponge start - check if the blockstate is loaded/null
        // final BlockState blockstate = this.shadow$getBlockState(p_217297_2_); // Vanilla
        @Nullable final BlockState lvt_3_1_ = ((BlockGetterBridge) this).bridge$getBlockIfLoaded(p_217297_2_);
        if (lvt_3_1_ == null) {
            // copied the last function parameter (listed below)
            final Vec3 vec3d = p_217297_1_.getFrom().subtract(p_217297_1_.getTo());
            return BlockHitResult.miss(context.getTo(), Direction.getNearest(vec3d.x, vec3d.y, vec3d.z), new BlockPos(p_217297_1_.getTo()));
        }
        // Sponge end
        final FluidState lvt_4_1_ = this.shadow$getFluidState(p_217297_2_);
        final Vec3 lvt_5_1_ = p_217297_1_.getFrom();
        final Vec3 lvt_6_1_ = p_217297_1_.getTo();
        final VoxelShape lvt_7_1_ = p_217297_1_.getBlockShape(lvt_3_1_, (BlockGetter) this, p_217297_2_);
        final BlockHitResult lvt_8_1_ = this.shadow$clipWithInteractionOverride(lvt_5_1_, lvt_6_1_, p_217297_2_, lvt_7_1_, lvt_3_1_);
        final VoxelShape lvt_9_1_ = p_217297_1_.getFluidShape(lvt_4_1_, (BlockGetter) this, p_217297_2_);
        final BlockHitResult lvt_10_1_ = lvt_9_1_.clip(lvt_5_1_, lvt_6_1_, p_217297_2_);
        final double lvt_11_1_ = lvt_8_1_ == null ? 1.7976931348623157E308D : p_217297_1_.getFrom().distanceToSqr(lvt_8_1_.getLocation());
        final double lvt_13_1_ = lvt_10_1_ == null ? 1.7976931348623157E308D : p_217297_1_.getFrom().distanceToSqr(lvt_10_1_.getLocation());
        return lvt_11_1_ <= lvt_13_1_ ? lvt_8_1_ : lvt_10_1_;
    }, (p_217302_0_) -> {
        final Vec3 lvt_1_1_ = p_217302_0_.getFrom().subtract(p_217302_0_.getTo());
        return BlockHitResult.miss(p_217302_0_.getTo(), Direction.getNearest(lvt_1_1_.x, lvt_1_1_.y, lvt_1_1_.z), new BlockPos(p_217302_0_.getTo()));
    });
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) VoxelShape(net.minecraft.world.phys.shapes.VoxelShape) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockGetterBridge(org.spongepowered.common.bridge.world.level.BlockGetterBridge) FluidState(net.minecraft.world.level.material.FluidState) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 29 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class ServerLocationData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ServerLocation.class).create(Keys.BIOME_TEMPERATURE).get(h -> {
        final Level world = (Level) h.world();
        final BlockPos pos = VecHelper.toBlockPos(h);
        final Biome biome = world.getBiome(pos);
        return (double) biome.getBaseTemperature();
    }).create(Keys.BLOCK_LIGHT).get(h -> {
        final Level world = (Level) h.world();
        return world.getBrightness(LightLayer.BLOCK, VecHelper.toBlockPos(h));
    }).create(Keys.BLOCK_TEMPERATURE).get(h -> {
        final Level world = (Level) h.world();
        final BlockPos pos = VecHelper.toBlockPos(h);
        final Biome biome = world.getBiome(pos);
        return (double) biome.getTemperature(pos);
    }).create(Keys.SKY_LIGHT).get(h -> {
        final Level world = (Level) h.world();
        final BlockPos pos = VecHelper.toBlockPos(h);
        return world.getBrightness(LightLayer.SKY, pos);
    }).create(Keys.IS_FULL_BLOCK).get(h -> {
        final BlockState block = (BlockState) h.block();
        final Level world = (Level) h.world();
        final BlockPos pos = VecHelper.toBlockPos(h.position());
        return block.isSolidRender(world, pos);
    }).create(Keys.IS_INDIRECTLY_POWERED).get(h -> {
        final Level world = (Level) h.world();
        final BlockPos pos = VecHelper.toBlockPos(h);
        return world.getBestNeighborSignal(pos) > 0;
    }).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(((Nameable) h.blockEntity().get()).getDisplayName())).supports(h -> h.blockEntity().isPresent() && h.blockEntity().get() instanceof NameableBlockEntity).create(Keys.CUSTOM_NAME).get(h -> {
        final BlockEntity blockEntity = h.blockEntity().get();
        return ((Nameable) blockEntity).hasCustomName() ? SpongeAdventure.asAdventure(((Nameable) blockEntity).getCustomName()) : null;
    }).set((h, v) -> (((CustomNameableBridge) h.blockEntity().get())).bridge$setCustomDisplayName(SpongeAdventure.asVanilla(v))).delete(h -> (((CustomNameableBridge) h.blockEntity().get())).bridge$setCustomDisplayName(null)).supports(h -> h.blockEntity().isPresent() && h.blockEntity().get() instanceof NameableBlockEntity).create(Keys.CREATOR).get(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$getBlockCreatorUUID(VecHelper.toBlockPos(h.blockPosition())).orElse(null)).set((h, v) -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockCreator(VecHelper.toBlockPos(h.blockPosition()), v)).delete(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockCreator(VecHelper.toBlockPos(h.blockPosition()), null)).create(Keys.NOTIFIER).get(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$getBlockNotifierUUID(VecHelper.toBlockPos(h.blockPosition())).orElse(null)).set((h, v) -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockNotifier(VecHelper.toBlockPos(h.blockPosition()), v)).delete(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockNotifier(VecHelper.toBlockPos(h.blockPosition()), null));
}
Also used : Nameable(net.minecraft.world.Nameable) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) BlockState(net.minecraft.world.level.block.state.BlockState) NameableBlockEntity(org.spongepowered.api.block.entity.NameableBlockEntity) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) Biome(net.minecraft.world.level.biome.Biome) Keys(org.spongepowered.api.data.Keys) BlockPos(net.minecraft.core.BlockPos) Nameable(net.minecraft.world.Nameable) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) VecHelper(org.spongepowered.common.util.VecHelper) LightLayer(net.minecraft.world.level.LightLayer) Level(net.minecraft.world.level.Level) ServerLocation(org.spongepowered.api.world.server.ServerLocation) CustomNameableBridge(org.spongepowered.common.bridge.CustomNameableBridge) CustomNameableBridge(org.spongepowered.common.bridge.CustomNameableBridge) Biome(net.minecraft.world.level.biome.Biome) BlockState(net.minecraft.world.level.block.state.BlockState) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) NameableBlockEntity(org.spongepowered.api.block.entity.NameableBlockEntity) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) NameableBlockEntity(org.spongepowered.api.block.entity.NameableBlockEntity)

Example 30 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project SpongeCommon by SpongePowered.

the class UpdateLightSideEffect method processSideEffect.

@Override
public EffectResult processSideEffect(final BlockPipeline pipeline, final PipelineCursor oldState, final BlockState newState, final SpongeBlockChangeFlag flag, final int limit) {
    if (!flag.updateLighting()) {
        return EffectResult.NULL_PASS;
    }
    final int originalOpactiy = oldState.opacity;
    final ServerLevel serverWorld = pipeline.getServerWorld();
    final BlockState currentState = pipeline.getAffectedChunk().getBlockState(oldState.pos);
    // ) {
    if (oldState.state != currentState && (currentState.getLightBlock(serverWorld, oldState.pos) != originalOpactiy || currentState.getLightEmission() != oldState.state.getLightEmission() || currentState.useShapeForLightOcclusion() || oldState.state.useShapeForLightOcclusion())) {
        // this.profiler.startSection("queueCheckLight");
        serverWorld.getProfiler().push("queueCheckLight");
        // this.getChunkProvider().getLightManager().checkBlock(pos);
        serverWorld.getChunkSource().getLightEngine().checkBlock(oldState.pos);
        // this.profiler.endSection();
        serverWorld.getProfiler().pop();
    }
    return EffectResult.NULL_PASS;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockState(net.minecraft.world.level.block.state.BlockState)

Aggregations

BlockState (net.minecraft.world.level.block.state.BlockState)141 BlockPos (net.minecraft.core.BlockPos)75 Direction (net.minecraft.core.Direction)18 Level (net.minecraft.world.level.Level)14 Block (net.minecraft.world.level.block.Block)14 ArrayList (java.util.ArrayList)10 ServerLevel (net.minecraft.server.level.ServerLevel)10 SoundType (net.minecraft.world.level.block.SoundType)10 Vec3 (net.minecraft.world.phys.Vec3)10 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)9 Random (java.util.Random)8 ItemStack (net.minecraft.world.item.ItemStack)7 FluidState (net.minecraft.world.level.material.FluidState)7 AABB (net.minecraft.world.phys.AABB)6 BuildBlock (com.wuest.prefab.structures.base.BuildBlock)5 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)5 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)5 LocationTag (com.denizenscript.denizen.objects.LocationTag)4 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)4 HashMap (java.util.HashMap)4