Jump to content

Spreading with a capacity


bosko2

Recommended Posts

So I would like to have infestation(spreading) with capacity. The way it would work is by having a source block(or entity is also fine) that is going to spread blocks. Each of those blocks will implement spread more blocks. All blocks will stop spreading when capacity in source block is equal to zero. Ways I tried:

  • Using non-Static variable in every spreading block that is linked to the Source block - ended up finding out that I can't do that
  • Using Blockstates(Source will have capacity, and every new block will have its coordinates saved in its own blockstate, way I changed the capacity is just by replacing source blocks with a new block), problem here is that number of coordinates it to big to be handled by blockstates so java runs out of memory that way.

For the first try I have no code. NO_AIR is a register for a block that this infesting block can replace. CLEAR_AIR is a register for the clear air. For the second one here it is:

CleanAir block:

package; //has a proper package name

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.Property;
import org.jetbrains.annotations.NotNull;
import java.util.Random;

import static /*pkgname*/.BlockInit.NO_AIR;

public class CleanAir extends Block {
    public static final Property<Integer> SOURCE_X = IntegerProperty.create("sourcex",0,29999984);
    public static final Property<Boolean> SOURCE_X_DIR = BooleanProperty.create("sourcexdir");
    public static final Property<Integer> SOURCE_Y = IntegerProperty.create("sourcey",0,29999984);
    public static final Property<Boolean> SOURCE_Y_DIR = BooleanProperty.create("sourceydir");
    public static final Property<Integer> SOURCE_Z = IntegerProperty.create("sourcez",0,29999984);
    public static final Property<Boolean> SOURCE_Z_DIR = BooleanProperty.create("sourceydir");
    private static BlockPos SourcePos;

    public CleanAir(Properties p_49795_) {
        super(Properties.copy(Blocks.AIR));
    }


    @Override
    public void onPlace(@NotNull BlockState blockstate, @NotNull Level world, @NotNull BlockPos pos, @NotNull BlockState oldState, boolean moving) {
        super.onPlace(blockstate,world,pos,oldState,moving);
        SourcePos = new BlockPos(
                blockstate.getValue(SOURCE_X)*( (blockstate.getValue(SOURCE_X_DIR))?1:-1),
                blockstate.getValue(SOURCE_X)*( (blockstate.getValue(SOURCE_Y_DIR))?1:-1),
                blockstate.getValue(SOURCE_X)*( (blockstate.getValue(SOURCE_Y_DIR))?1:-1));
        world.scheduleTick(pos,this,10);
    }

    @Override
    public void tick(@NotNull BlockState pState, @NotNull ServerLevel pLevel, @NotNull BlockPos pPos, @NotNull Random pRandom) {
        super.tick(pState, pLevel, pPos, pRandom);
        for(int[] coord: CleanAirSource.COORDINATES)
        {
            if(pLevel.getBlockState(SourcePos).getValue(CleanAirSource.CAPACITY)==0)
                break;
            int x = coord[0];
            int y = coord[1];
            int z = coord[2];
            BlockPos position = new BlockPos(pPos.getX() + x, pPos.getY() + y, pPos.getZ() + z);
            if(pLevel.getBlockState(position).is(NO_AIR.get())) {
                CleanAir temp = this;
                temp.defaultBlockState().setValue(SOURCE_X,pState.getValue(SOURCE_X));
                temp.defaultBlockState().setValue(SOURCE_Y,pState.getValue(SOURCE_Y));
                temp.defaultBlockState().setValue(SOURCE_Z,pState.getValue(SOURCE_Z));
                temp.defaultBlockState().setValue(SOURCE_X_DIR,pState.getValue(SOURCE_X_DIR));
                temp.defaultBlockState().setValue(SOURCE_Y_DIR,pState.getValue(SOURCE_Y_DIR));
                temp.defaultBlockState().setValue(SOURCE_Z_DIR,pState.getValue(SOURCE_Z_DIR));
                pLevel.setBlockAndUpdate(position, temp.defaultBlockState());
                pLevel.setBlockAndUpdate(SourcePos,pLevel.getBlockState(SourcePos).setValue(CleanAirSource.CAPACITY,pLevel.getBlockState(SourcePos).getValue(CleanAirSource.CAPACITY)+1));
            }
        }
        pLevel.scheduleTick(pPos,this,10);
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
        super.createBlockStateDefinition(pBuilder);
        pBuilder.add(SOURCE_X);
        pBuilder.add(SOURCE_Y);
        pBuilder.add(SOURCE_Z);
        pBuilder.add(SOURCE_X_DIR);
        pBuilder.add(SOURCE_Y_DIR);
        pBuilder.add(SOURCE_Z_DIR);

    }
}

CleanAirSource block:

package; //proper package name

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.Property;
import org.jetbrains.annotations.NotNull;

import java.util.Random;

import static /*pkgname*/.BlockInit.NO_AIR;

public class CleanAirSource extends Block {

    public static final Property<Integer> CAPACITY= IntegerProperty.create("capacity",0,256);
    public CleanAirSource(Properties p_49795_) {
        super(Properties.copy(Blocks.IRON_BLOCK));
        this.registerDefaultState(this.getStateDefinition().any().setValue(CAPACITY,10));
    }
    public static final int[][] COORDINATES={
            {-1,0,0},
            {0,-1,0},
            {0,0,-1},
            { 1,0,0},
            {0, 1,0},
            {0,0, 1}
    };
    @Override
    public void onPlace(BlockState pState, Level pLevel, BlockPos pPos, BlockState pOldState, boolean pIsMoving) {
        super.onPlace(pState, pLevel, pPos, pOldState, pIsMoving);
        pLevel.scheduleTick(pPos,this,10);
    }

    public void tick(@NotNull BlockState pState, @NotNull ServerLevel pLevel, @NotNull BlockPos pPos, @NotNull Random pRandom) {
        super.tick(pState, pLevel, pPos, pRandom);
        for(int[] coord: COORDINATES)
        {
            int x = coord[0];
            int y = coord[1];
            int z = coord[2];
            BlockPos position = new BlockPos(pPos.getX() + x, pPos.getY() + y, pPos.getZ() + z);
            if(pLevel.getBlockState(position).is(NO_AIR.get()) ) {
                CleanAir temp = (CleanAir) BlockInit.CLEAR_AIR.get();
                pLevel.setBlockAndUpdate(position, temp.defaultBlockState()
                        .setValue(CleanAir.SOURCE_X,java.lang.Math.abs( pPos.getX()))
                        .setValue(CleanAir.SOURCE_Y,java.lang.Math.abs( pPos.getY()))
                        .setValue(CleanAir.SOURCE_Z,java.lang.Math.abs( pPos.getZ()))
                        .setValue(CleanAir.SOURCE_X_DIR,pPos.getX()>=0)
                        .setValue(CleanAir.SOURCE_Y_DIR,pPos.getY()>=0)
                        .setValue(CleanAir.SOURCE_Z_DIR,pPos.getZ()>=0));
            }
        }
        pLevel.scheduleTick(pPos,this,10);
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
        super.createBlockStateDefinition(pBuilder);
        pBuilder.add(CAPACITY);
    }



}

 

Link to comment
Share on other sites

I would like it to spread to multiple chunks. Source will be some sort of machine that does this. Would it be better to map out NO_AIR around the source and then setting it to clear air from no air directly from Source(not from CleanAir) and doing that every 10 ticks. Also there can be a possibility that two sources are in one chunk.

Edited by bosko2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.