Search in sources :

Example 1 with BlockState

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

the class BlockInfo method updateLightMatrix.

public void updateLightMatrix() {
    for (int x = 0; x <= 2; x++) {
        for (int y = 0; y <= 2; y++) {
            for (int z = 0; z <= 2; z++) {
                BlockPos pos = blockPos.offset(x - 1, y - 1, z - 1);
                BlockState state = level.getBlockState(pos);
                t[x][y][z] = state.getLightBlock(level, pos) < 15;
                int brightness = LevelRenderer.getLightColor(level, pos);
                s[x][y][z] = LightTexture.sky(brightness);
                b[x][y][z] = LightTexture.block(brightness);
                ao[x][y][z] = state.getShadeBrightness(level, pos);
            }
        }
    }
    for (Direction side : SIDES) {
        BlockPos pos = blockPos.relative(side);
        BlockState state = level.getBlockState(pos);
        BlockState thisStateShape = this.state.canOcclude() && this.state.useShapeForLightOcclusion() ? this.state : Blocks.AIR.defaultBlockState();
        BlockState otherStateShape = state.canOcclude() && state.useShapeForLightOcclusion() ? state : Blocks.AIR.defaultBlockState();
        if (state.getLightBlock(level, pos) == 15 || Shapes.faceShapeOccludes(thisStateShape.getFaceOcclusionShape(level, blockPos, side), otherStateShape.getFaceOcclusionShape(level, pos, side.getOpposite()))) {
            int x = side.getStepX() + 1;
            int y = side.getStepY() + 1;
            int z = side.getStepZ() + 1;
            s[x][y][z] = Math.max(s[1][1][1] - 1, s[x][y][z]);
            b[x][y][z] = Math.max(b[1][1][1] - 1, b[x][y][z]);
        }
    }
    for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 2; y++) {
            for (int z = 0; z < 2; z++) {
                int x1 = x * 2;
                int y1 = y * 2;
                int z1 = z * 2;
                int sxyz = s[x1][y1][z1];
                int bxyz = b[x1][y1][z1];
                boolean txyz = t[x1][y1][z1];
                int sxz = s[x1][1][z1], sxy = s[x1][y1][1], syz = s[1][y1][z1];
                int bxz = b[x1][1][z1], bxy = b[x1][y1][1], byz = b[1][y1][z1];
                boolean txz = t[x1][1][z1], txy = t[x1][y1][1], tyz = t[1][y1][z1];
                int sx = s[x1][1][1], sy = s[1][y1][1], sz = s[1][1][z1];
                int bx = b[x1][1][1], by = b[1][y1][1], bz = b[1][1][z1];
                boolean tx = t[x1][1][1], ty = t[1][y1][1], tz = t[1][1][z1];
                skyLight[0][x][y][z] = combine(sx, sxz, sxy, txz || txy ? sxyz : sx, tx, txz, txy, txz || txy ? txyz : tx);
                blockLight[0][x][y][z] = combine(bx, bxz, bxy, txz || txy ? bxyz : bx, tx, txz, txy, txz || txy ? txyz : tx);
                skyLight[1][x][y][z] = combine(sy, sxy, syz, txy || tyz ? sxyz : sy, ty, txy, tyz, txy || tyz ? txyz : ty);
                blockLight[1][x][y][z] = combine(by, bxy, byz, txy || tyz ? bxyz : by, ty, txy, tyz, txy || tyz ? txyz : ty);
                skyLight[2][x][y][z] = combine(sz, syz, sxz, tyz || txz ? sxyz : sz, tz, tyz, txz, tyz || txz ? txyz : tz);
                blockLight[2][x][y][z] = combine(bz, byz, bxz, tyz || txz ? bxyz : bz, tz, tyz, txz, tyz || txz ? txyz : tz);
            }
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) Direction(net.minecraft.core.Direction)

Example 2 with BlockState

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

the class FluidUtil method tryPickUpFluid.

/**
 * Attempts to pick up a fluid in the world and put it in an empty container item.
 *
 * @param emptyContainer The empty container to fill.
 *                       Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
 * @param playerIn       The player filling the container. Optional.
 * @param worldIn        The world the fluid is in.
 * @param pos            The position of the fluid in the world.
 * @param side           The side of the fluid that is being drained.
 * @return a {@link FluidActionResult} holding the result and the resulting container.
 */
@Nonnull
public static FluidActionResult tryPickUpFluid(@Nonnull ItemStack emptyContainer, @Nullable Player playerIn, Level worldIn, BlockPos pos, Direction side) {
    if (emptyContainer.isEmpty() || worldIn == null || pos == null) {
        return FluidActionResult.FAILURE;
    }
    BlockState state = worldIn.getBlockState(pos);
    Block block = state.getBlock();
    IFluidHandler targetFluidHandler;
    if (block instanceof IFluidBlock) {
        targetFluidHandler = new FluidBlockWrapper((IFluidBlock) block, worldIn, pos);
    } else if (block instanceof BucketPickup) {
        targetFluidHandler = new BucketPickupHandlerWrapper((BucketPickup) block, worldIn, pos);
    } else {
        Optional<IFluidHandler> fluidHandler = getFluidHandler(worldIn, pos, side).resolve();
        if (!fluidHandler.isPresent()) {
            return FluidActionResult.FAILURE;
        }
        targetFluidHandler = fluidHandler.get();
    }
    return tryFillContainer(emptyContainer, targetFluidHandler, Integer.MAX_VALUE, playerIn, true);
}
Also used : FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BucketPickup(net.minecraft.world.level.block.BucketPickup) BucketPickupHandlerWrapper(net.minecraftforge.fluids.capability.wrappers.BucketPickupHandlerWrapper) BlockState(net.minecraft.world.level.block.state.BlockState) LazyOptional(net.minecraftforge.common.util.LazyOptional) Optional(java.util.Optional) Block(net.minecraft.world.level.block.Block) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) Nonnull(javax.annotation.Nonnull)

Example 3 with BlockState

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

the class FluidUtil method tryPlaceFluid.

/**
 * Tries to place a fluid resource into the world as a block and drains the fluidSource.
 * Makes a fluid emptying or vaporization sound when successful.
 * Honors the amount of fluid contained by the used container.
 * Checks if water-like fluids should vaporize like in the nether.
 *
 * Modeled after {@link BucketItem#emptyContents(Player, Level, BlockPos, BlockHitResult)}
 *
 * @param player      Player who places the fluid. May be null for blocks like dispensers.
 * @param world       Level to place the fluid in
 * @param hand
 * @param pos         The position in the world to place the fluid block
 * @param fluidSource The fluid source holding the fluidStack to place
 * @param resource    The fluidStack to place.
 * @return true if the placement was successful, false otherwise
 */
public static boolean tryPlaceFluid(@Nullable Player player, Level world, InteractionHand hand, BlockPos pos, IFluidHandler fluidSource, FluidStack resource) {
    if (world == null || pos == null) {
        return false;
    }
    Fluid fluid = resource.getFluid();
    if (fluid == Fluids.EMPTY || !fluid.getAttributes().canBePlacedInWorld(world, pos, resource)) {
        return false;
    }
    if (fluidSource.drain(resource, IFluidHandler.FluidAction.SIMULATE).isEmpty()) {
        return false;
    }
    BlockPlaceContext context = new BlockPlaceContext(world, player, hand, player == null ? ItemStack.EMPTY : player.getItemInHand(hand), new BlockHitResult(Vec3.ZERO, Direction.UP, pos, false));
    // check that we can place the fluid at the destination
    BlockState destBlockState = world.getBlockState(pos);
    Material destMaterial = destBlockState.getMaterial();
    boolean isDestNonSolid = !destMaterial.isSolid();
    boolean isDestReplaceable = destBlockState.canBeReplaced(context);
    boolean canDestContainFluid = destBlockState.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer) destBlockState.getBlock()).canPlaceLiquid(world, pos, destBlockState, fluid);
    if (!world.isEmptyBlock(pos) && !isDestNonSolid && !isDestReplaceable && !canDestContainFluid) {
        // Non-air, solid, unreplacable block. We can't put fluid here.
        return false;
    }
    if (world.dimensionType().ultraWarm() && fluid.getAttributes().doesVaporize(world, pos, resource)) {
        FluidStack result = fluidSource.drain(resource, IFluidHandler.FluidAction.EXECUTE);
        if (!result.isEmpty()) {
            result.getFluid().getAttributes().vaporize(player, world, pos, result);
            return true;
        }
    } else {
        // This fluid handler places the fluid block when filled
        IFluidHandler handler;
        if (canDestContainFluid) {
            handler = new BlockWrapper.LiquidContainerBlockWrapper((LiquidBlockContainer) destBlockState.getBlock(), world, pos);
        } else {
            handler = getFluidBlockHandler(fluid, world, pos);
        }
        FluidStack result = tryFluidTransfer(handler, fluidSource, resource, true);
        if (!result.isEmpty()) {
            SoundEvent soundevent = resource.getFluid().getAttributes().getEmptySound(resource);
            world.playSound(player, pos, soundevent, SoundSource.BLOCKS, 1.0F, 1.0F);
            return true;
        }
    }
    return false;
}
Also used : LiquidBlockContainer(net.minecraft.world.level.block.LiquidBlockContainer) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) SoundEvent(net.minecraft.sounds.SoundEvent) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Fluid(net.minecraft.world.level.material.Fluid) Material(net.minecraft.world.level.material.Material) BlockHitResult(net.minecraft.world.phys.BlockHitResult) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 4 with BlockState

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

the class BlockSnapshot method restoreToLocation.

public boolean restoreToLocation(LevelAccessor world, BlockPos pos, boolean force, boolean notifyNeighbors) {
    BlockState current = getCurrentBlock();
    BlockState replaced = getReplacedBlock();
    int flags = notifyNeighbors ? Block.UPDATE_ALL : Block.UPDATE_CLIENTS;
    if (current != replaced) {
        if (force)
            world.setBlock(pos, replaced, flags);
        else
            return false;
    }
    world.setBlock(pos, replaced, flags);
    if (world instanceof Level)
        ((Level) world).sendBlockUpdated(pos, current, replaced, flags);
    BlockEntity te = null;
    if (getTag() != null) {
        te = world.getBlockEntity(pos);
        if (te != null) {
            te.load(getTag());
            te.setChanged();
        }
    }
    if (DEBUG)
        System.out.println("Restored " + this.toString());
    return true;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) Level(net.minecraft.world.level.Level) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 5 with BlockState

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

the class ForgeHooksClient method renderPistonMovedBlocks.

public static void renderPistonMovedBlocks(BlockPos pos, BlockState state, PoseStack stack, MultiBufferSource buffer, Level world, boolean checkSides, int combinedOverlay, BlockRenderDispatcher blockRenderer) {
    RenderType.chunkBufferLayers().stream().filter(t -> ItemBlockRenderTypes.canRenderInLayer(state, t)).forEach(rendertype -> {
        setRenderType(rendertype);
        VertexConsumer ivertexbuilder = buffer.getBuffer(rendertype == RenderType.translucent() ? RenderType.translucentMovingBlock() : rendertype);
        blockRenderer.getModelRenderer().tesselateBlock(world, blockRenderer.getBlockModel(state), state, pos, stack, ivertexbuilder, checkSides, new Random(), state.getSeed(pos), combinedOverlay);
    });
    setRenderType(null);
}
Also used : Camera(net.minecraft.client.Camera) Font(net.minecraft.client.gui.Font) Arrays(java.util.Arrays) Connection(net.minecraft.network.Connection) ForgeMod(net.minecraftforge.common.ForgeMod) Dist(net.minecraftforge.api.distmarker.Dist) net.minecraftforge.fml(net.minecraftforge.fml) Pair(org.apache.commons.lang3.tuple.Pair) MultiPlayerGameMode(net.minecraft.client.multiplayer.MultiPlayerGameMode) Map(java.util.Map) AbstractClientPlayer(net.minecraft.client.player.AbstractClientPlayer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) TooltipComponent(net.minecraft.world.inventory.tooltip.TooltipComponent) WorldPreset(net.minecraft.client.gui.screens.worldselection.WorldPreset) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Window(com.mojang.blaze3d.platform.Window) ForgeVersion(net.minecraftforge.versions.forge.ForgeVersion) SoundInstance(net.minecraft.client.resources.sounds.SoundInstance) Screen(net.minecraft.client.gui.screens.Screen) ResourceManager(net.minecraft.server.packs.resources.ResourceManager) Set(java.util.Set) Language(net.minecraft.locale.Language) NetworkRegistry(net.minecraftforge.network.NetworkRegistry) TextComponent(net.minecraft.network.chat.TextComponent) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) FormattedText(net.minecraft.network.chat.FormattedText) FluidState(net.minecraft.world.level.material.FluidState) ForgeI18n(net.minecraftforge.common.ForgeI18n) ItemStack(net.minecraft.world.item.ItemStack) GameType(net.minecraft.world.level.GameType) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) BETA(net.minecraftforge.fml.VersionChecker.Status.BETA) Resource(net.minecraft.server.packs.resources.Resource) WorldGenSettings(net.minecraft.world.level.levelgen.WorldGenSettings) com.mojang.blaze3d.vertex(com.mojang.blaze3d.vertex) HumanoidArm(net.minecraft.world.entity.HumanoidArm) BlockState(net.minecraft.world.level.block.state.BlockState) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) Supplier(java.util.function.Supplier) Vector3f(com.mojang.math.Vector3f) ArrayList(java.util.ArrayList) I18n(net.minecraft.client.resources.language.I18n) MouseHandler(net.minecraft.client.MouseHandler) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) ModelManager(net.minecraft.client.resources.model.ModelManager) TitleScreen(net.minecraft.client.gui.screens.TitleScreen) Marker(org.apache.logging.log4j.Marker) Nullable(javax.annotation.Nullable) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) BlockAndTintGetter(net.minecraft.world.level.BlockAndTintGetter) ServerData(net.minecraft.client.multiplayer.ServerData) BakedModel(net.minecraft.client.resources.model.BakedModel) Transformation(com.mojang.math.Transformation) ItemTransforms(net.minecraft.client.renderer.block.model.ItemTransforms) Component(net.minecraft.network.chat.Component) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) TransformationHelper(net.minecraftforge.common.model.TransformationHelper) IOException(java.io.IOException) NarratorChatListener(net.minecraft.client.gui.chat.NarratorChatListener) File(java.io.File) ItemRenderer(net.minecraft.client.renderer.entity.ItemRenderer) LerpingBossEvent(net.minecraft.client.gui.components.LerpingBossEvent) MinecraftForge(net.minecraftforge.common.MinecraftForge) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) RenderSystem(com.mojang.blaze3d.systems.RenderSystem) ClientTooltipComponent(net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent) InteractionHand(net.minecraft.world.InteractionHand) Material(net.minecraft.client.resources.model.Material) net.minecraftforge.client.event(net.minecraftforge.client.event) ResourceLocation(net.minecraft.resources.ResourceLocation) Either(com.mojang.datafixers.util.Either) LivingEntity(net.minecraft.world.entity.LivingEntity) TextureAtlas(net.minecraft.client.renderer.texture.TextureAtlas) Direction(net.minecraft.core.Direction) Matrix4f(com.mojang.math.Matrix4f) Random(java.util.Random) ServerStatus(net.minecraft.network.protocol.status.ServerStatus) ForgeTextureMetadata(net.minecraftforge.client.textures.ForgeTextureMetadata) GameData(net.minecraftforge.registries.GameData) ChatFormatting(net.minecraft.ChatFormatting) BETA_OUTDATED(net.minecraftforge.fml.VersionChecker.Status.BETA_OUTDATED) FogMode(net.minecraft.client.renderer.FogRenderer.FogMode) Event(net.minecraftforge.eventbus.api.Event) Input(net.minecraft.client.player.Input) ImmutableMap(com.google.common.collect.ImmutableMap) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Matrix3f(com.mojang.math.Matrix3f) Collectors(java.util.stream.Collectors) Player(net.minecraft.world.entity.player.Player) Objects(java.util.Objects) MarkerManager(org.apache.logging.log4j.MarkerManager) List(java.util.List) RecipeManager(net.minecraft.world.item.crafting.RecipeManager) BlockPos(net.minecraft.core.BlockPos) Optional(java.util.Optional) BOSSINFO(net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType.BOSSINFO) Level(net.minecraft.world.level.Level) net.minecraft.client.renderer(net.minecraft.client.renderer) PlayerInfo(net.minecraft.client.multiplayer.PlayerInfo) GuiComponent(net.minecraft.client.gui.GuiComponent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) SoundEngine(net.minecraft.client.sounds.SoundEngine) PlaySoundEvent(net.minecraftforge.client.event.sound.PlaySoundEvent) Function(java.util.function.Function) Stack(java.util.Stack) ItemColors(net.minecraft.client.color.item.ItemColors) BlockRenderDispatcher(net.minecraft.client.renderer.block.BlockRenderDispatcher) ParticleRenderType(net.minecraft.client.particle.ParticleRenderType) Minecraft(net.minecraft.client.Minecraft) JoinMultiplayerScreen(net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen) Fluid(net.minecraft.world.level.material.Fluid) Mod(net.minecraftforge.fml.common.Mod) HumanoidModel(net.minecraft.client.model.HumanoidModel) NativeImage(com.mojang.blaze3d.platform.NativeImage) Nonnull(javax.annotation.Nonnull) EntityHitResult(net.minecraft.world.phys.EntityHitResult) NetworkConstants(net.minecraftforge.network.NetworkConstants) LayerDefinition(net.minecraft.client.model.geom.builders.LayerDefinition) LocalPlayer(net.minecraft.client.player.LocalPlayer) ModelLayerLocation(net.minecraft.client.model.geom.ModelLayerLocation) HitResult(net.minecraft.world.phys.HitResult) Entity(net.minecraft.world.entity.Entity) Model(net.minecraft.client.model.Model) BlockColors(net.minecraft.client.color.block.BlockColors) KeyMapping(net.minecraft.client.KeyMapping) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) ForgeModelBakery(net.minecraftforge.client.model.ForgeModelBakery) Random(java.util.Random)

Aggregations

BlockState (net.minecraft.world.level.block.state.BlockState)38 BlockPos (net.minecraft.core.BlockPos)10 SoundType (net.minecraft.world.level.block.SoundType)10 Level (net.minecraft.world.level.Level)6 LocationTag (com.denizenscript.denizen.objects.LocationTag)4 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)4 ArrayList (java.util.ArrayList)3 Nonnull (javax.annotation.Nonnull)3 Direction (net.minecraft.core.Direction)3 ChunkPos (net.minecraft.world.level.ChunkPos)3 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)3 ChunkCoordinate (com.denizenscript.denizen.utilities.blocks.ChunkCoordinate)2 File (java.io.File)2 List (java.util.List)2 Optional (java.util.Optional)2 TextComponent (net.minecraft.network.chat.TextComponent)2 Player (net.minecraft.world.entity.player.Player)2 ItemStack (net.minecraft.world.item.ItemStack)2 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)2 Fluid (net.minecraft.world.level.material.Fluid)2