Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Solved] Changing/setting blockstates 1.16.4


Recommended Posts

Hi
I have one problem I dont know why but for some reason I cant change blockstate.

Here is the LeadCoating.java:

package me.gleep.oreganized.blocks;

import me.gleep.oreganized.util.RegistryHandler;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.IntegerProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.ToolType;
import org.jetbrains.annotations.Nullable;

import java.util.Random;

public class LeadCoating extends Block {

    public static final int RANGE = 4;
    public static final IntegerProperty LEVEL = IntegerProperty.create("water_level", 0, 3);
    public static final IntegerProperty HEIGHT =  IntegerProperty.create("base_height", 0, 255);
    public static final BooleanProperty IS_BASE = BooleanProperty.create("is_base");

    public LeadCoating() {
        super(Properties.create(Material.IRON)
                .hardnessAndResistance(4.0F, 5.0F)
                .harvestTool(ToolType.PICKAXE)
                .setRequiresTool()
                .harvestLevel(1)
                .sound(SoundType.METAL));

        this.setDefaultState(this.stateContainer.getBaseState().with(LEVEL, Integer.valueOf(0)).with(IS_BASE, false).with(HEIGHT, Integer.valueOf(0)));
    }

    /**
     * Called by ItemBlocks after a block is set in the world, to allow post-place logic
     */
    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
        BlockPos basePos = pos.down();
        while (worldIn.getBlockState(basePos).getBlock().equals(RegistryHandler.LEAD_COATING.get())) basePos = basePos.down();
        basePos = basePos.up();

        BlockPos topPos = basePos.up();
        while (worldIn.getBlockState(topPos).getBlock().equals(RegistryHandler.LEAD_COATING.get())) {
            worldIn.setBlockState(topPos, worldIn.getBlockState(topPos).with(IS_BASE, false).with(HEIGHT, basePos.getY()), 2);
            topPos = topPos.up();
        }
        topPos = topPos.down();

        BlockState baseState = worldIn.getBlockState(basePos).with(IS_BASE, true).with(HEIGHT, topPos.getY());
        worldIn.setBlockState(basePos, baseState, 2);

    }

    /**
     * Called after a player destroys this Block - the position pos may no longer hold the state indicated.
     */
    @Override
    public void onPlayerDestroy(IWorld worldIn, BlockPos pos, BlockState state) {
        if (worldIn.getBlockState(pos.up()).getBlock().equals(RegistryHandler.LEAD_COATING.get())) {
            worldIn.getBlockState(pos.up()).getBlock().onBlockPlacedBy((World)worldIn, pos.up(), worldIn.getBlockState(pos.up()), null, ItemStack.EMPTY);
        }

        if (worldIn.getBlockState(pos.down()).getBlock().equals(RegistryHandler.LEAD_COATING.get())) {
            worldIn.getBlockState(pos.down()).getBlock().onBlockPlacedBy((World)worldIn, pos.down(), worldIn.getBlockState(pos.down()), null, ItemStack.EMPTY);
        }
    }

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(LEVEL).add(IS_BASE).add(HEIGHT);
    }

    @Override
    public boolean ticksRandomly(BlockState state) {
        return true;
    }

    /**
     * Performs a random tick on a block.
     */
    @Override
    public void randomTick(BlockState state, ServerWorld worldIn, BlockPos pos, Random random) {
        this.tick(state, worldIn, pos, random);
    }

    @Override
    public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
        if (state.get(IS_BASE) && state.get(LEVEL) == 3) {
            fertilize((World) worldIn, pos, state);
        }
    }

    @Override
    public void fillWithRain(World worldIn, BlockPos pos) {
        float f = worldIn.getBiome(pos).getTemperature(pos);
        if (!(f < 0.15F)) {
            BlockState state = worldIn.getBlockState(pos);

            BlockPos basePos = new BlockPos(pos.getX(), state.get(HEIGHT), pos.getZ());
            BlockState baseState = worldIn.getBlockState(basePos);
            if (!flow(worldIn, basePos, baseState)) {
                worldIn.setBlockState(pos, state.with(LEVEL, Integer.valueOf(state.get(LEVEL) + 1)));
            }
        }
    }

    public void fertilize(World world, BlockPos pos, BlockState blockState) {
        int minY = pos.getY();
        int maxY = blockState.get(HEIGHT);

        for (int i = minY; i <= maxY; i++) {
            int rndX = world.getRandom().nextInt(RANGE * 2) - 4;
            int rndZ = world.getRandom().nextInt(RANGE * 2) - 4;
            BlockPos pos1 = new BlockPos(pos.getX() + rndX, i - 1, pos.getZ() + rndZ);
            BlockState state1 = world.getBlockState(pos1);

            BlockPos leadPos = new BlockPos(pos.getX(), i, pos.getZ());
            BlockState leadState = world.getBlockState(leadPos);

            for (int j = leadState.get(LEVEL); j > 0; j--) {
                if (state1.getBlock() instanceof IGrowable) {
                    IGrowable igrowable = (IGrowable) state1.getBlock();
                    if (igrowable.canGrow(world, pos1, state1, world.isRemote)) {
                        if (world instanceof ServerWorld) {
                            if (igrowable.canUseBonemeal(world, world.rand, pos1, state1)) {
                                igrowable.grow((ServerWorld) world, world.rand, pos1, state1);
                                world.setBlockState(leadPos, leadState.with(LEVEL, Integer.valueOf(leadState.get(LEVEL) - 1)), 2);
                            }
                        }
                    }
                }
            }
        }
    }

    public boolean flow(World world, BlockPos pos, BlockState blockState) {
        if (blockState.get(IS_BASE)) {
            if (pos.getY() > blockState.get(HEIGHT)) return false;

            if (world.getBlockState(pos).get(LEVEL) == 3) {
                return flow(world, pos.up(), blockState);
            } else {
                BlockState prevState = world.getBlockState(pos);
                world.setBlockState(pos, prevState.with(LEVEL, Integer.valueOf(prevState.get(LEVEL) + 1)), 2);
                return true;
            }
        }

        return false;
    }

    /**
     * @deprecated call via {@link BlockState#hasComparatorInputOverride()} whenever possible. Implementing/overriding
     * is fine.
     */
    @Override
    public boolean hasComparatorInputOverride(BlockState state) {
        return true;
    }

    /**
     * @deprecated call via {@link IBlockState#getComparatorInputOverride(World, BlockPos)} whenever possible.
     * Implementing/overriding is fine.
     */
    @Override
    public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
        return blockState.get(LEVEL);
    }
}

The block has three properties:

height (if is_base == true then its the height of the most top block in the pillar else its the height of the base block);

is_base (if this is the base block);

water_level (amount of water in the block);

 

And the problem is that it change the water level of base block and the top block but I cant change the water level of the blocks between. So my guess is that i have something wrong in the flow function but I tried almost everything and it just doesnt work.

 

2021-06-19_10.01.10.png

EDIT: Well I found out that it always bonemeals the grass and that means it seemed to not work. Sorry for bothering.

Edited by <Gl33p_0r4nge>
Link to post
Share on other sites
  • <Gl33p_0r4nge> changed the title to [Solved] Changing/setting blockstates 1.16.4

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.