Cavalry
Cavalry

Reputation: 31

Add a delay before block placement (Minecraft Forge API 1.19.2)

I have an event that is on the server which when a sponge gets placed, replaces any surrounding water blocks in its path to air.


public static void spongePlace(EntityPlaceEvent event) {
        Entity entity = event.getEntity();
        Level level = entity.level;
        if(level.isClientSide())  
            return;
        BlockState sponge = event.getState();
        if(sponge != Blocks.SPONGE.defaultBlockState()) 
            return;
        BlockPos spongePos = event.getPos();
        int spongePosY = spongePos.getY();

        List<BlockPos> pos = new ArrayList<BlockPos>();
        pos.add(spongePos);
        List<BlockPos> pos2 = new ArrayList<BlockPos>();
        
        while(pos.size()!=0) {
            for(int i=0;i<pos.size();i++) {
                
                BlockPos curPos = pos.get(i);
                int xBP = curPos.getX();
                int zBP = curPos.getZ();
                
                level.setBlock(curPos, Blocks.AIR.defaultBlockState(), 2);
                
                BlockPos BP1 = new BlockPos(xBP+1, spongePosY, zBP);
                BlockPos BP2 = new BlockPos(xBP-1, spongePosY, zBP);
                BlockPos BP3 = new BlockPos(xBP, spongePosY, zBP+1);
                BlockPos BP4 = new BlockPos(xBP, spongePosY, zBP-1);
                
                BlockState BS1 = level.getBlockState(new BlockPos(xBP+1, spongePosY, zBP));
                BlockState BS2 = level.getBlockState(new BlockPos(xBP-1, spongePosY, zBP));
                BlockState BS3 = level.getBlockState(new BlockPos(xBP, spongePosY, zBP+1));
                BlockState BS4 = level.getBlockState(new BlockPos(xBP, spongePosY, zBP-1));

                if(BS1==waterBS) {
                    pos2.add(BP1);
                }
                if(BS2==waterBS) {
                    pos2.add(BP2);
                }
                if(BS3==waterBS) {
                    pos2.add(BP3);
                }
                if(BS4==waterBS) {
                    pos2.add(BP4);
                }
            }
            pos = pos2;
            pos2 = new ArrayList<BlockPos>();
        }
        
    } // spongePlaced

Although it works, there are 2 problems that occur:

  1. The while loop overwhelms the server causing it to freeze the screen
  2. The setblock function does not appear to have a tick delay to keep up with the server

I have tried cooldowns and executors, but both have not worked. I expected those results to have a cooldoown between block updates but don't appear to help. It instead resulted in the same 2 issues of freezing my screen and updating the block after a set cooldown.

How can I add a specific tick delay every time a water block gets updated?

Upvotes: 0

Views: 165

Answers (1)

Dave
Dave

Reputation: 1

BlockPos.MutableBlockPos might help with the runtime, a lot of BlockPos are being created here.

Upvotes: 0

Related Questions