Jump to content

Level.setBlock doesn't work on client


_p1nero

Recommended Posts

I created a item when you click on block it will generate a skyisland. But it just generate one dirt on where I click. I'm sure it generated because there is an invisible barrel in which the dirt should be. And when I restart the game, the invisible dirt appear.

level.setBlockAndUpdate() is the same.

 

Item.java

package net.p1nero.skyislandbuilder.item;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.p1nero.skyislandbuilder.utils.SkyIslandGenerator;


public class PerlinSkyIslandBuilderItem extends Item {

    public PerlinSkyIslandBuilderItem(Properties properties) {
        super(properties);
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level p_41432_, Player p_41433_, InteractionHand p_41434_) {
        //TODO: Open setting window
        return super.use(p_41432_, p_41433_, p_41434_);
    }

    @Override
    public InteractionResult useOn(UseOnContext context) {
        SkyIslandGenerator skyIslandGenerator = new SkyIslandGenerator(context.getClickedPos(),context.getLevel());
        skyIslandGenerator.printSkyIsland();
        return super.useOn(context);
    }
}

 

SkyIslandGenerator.java

public void printSkyIsland() {
        double[][] skyIsland = generateSkyIsland(width, height, scale, octaves, persistence, lacunarity, seed, maxHeight*10);
        int maxHeight = -1;
        int max_x = 0, max_z = 0;
        for (int x = 0; x < width; x++) {
            for (int z = 0; z < height; z++) {
                if (skyIsland[x][z] > maxHeight) {
                    maxHeight = (int)skyIsland[x][z];
                    max_x = x;
                    max_z = z;
                }
                System.out.print("("+x+','+z+"):"+(int)skyIsland[x][z]+" ");
            }
            System.out.println();
        }

        for (int x = 0; x < width; x++) {
            for (int z = 0; z < height; z++) {
                for(int y = bottom.getY()+maxHeight ; y>bottom.getY()+maxHeight-skyIsland[x][z] ; y--){
                   level.setBlock(new BlockPos(bottom.getX()+x-max_x,y,bottom.getZ()+z-max_z), Blocks.DIRT.defaultBlockState(),3);
                }
            }
        }
    }

 

Edited by _p1nero
Link to comment
Share on other sites

you did it on client only. all changes need to happen on the server. client only actions can be particles, toasts, etc.

in useOn(), first check the side - if not client then printBlocks. outside of the check, return proper value (see below).

do not call super.useOn - let's say you right-clicked a lever. you do not want to generate your island in front of the lever and then flip it as a bonus. have one or the other. you may want to support both via crouching check but i wouldn't. return InteractionResult.sidedSuccess(level_is_client). that will give you a hand animation on client.

if you're in a good mood, don't make a hundred BlockPos objects. make one BlockPos.Mutable, call set to move it around before use.

Edited by MFMods
Link to comment
Share on other sites

1 hour ago, MFMods said:

you did it on client only. all changes need to happen on the server. client only actions can be particles, toasts, etc.

in useOn(), first check the side - if not client then printBlocks. outside of the check, return proper value (see below).

do not call super.useOn - let's say you right-clicked a lever. you do not want to generate your island in front of the lever and then flip it as a bonus. have one or the other. you may want to support both via crouching check but i wouldn't. return InteractionResult.sidedSuccess(level_is_client). that will give you a hand animation on client.

if you're in a good mood, don't make a hundred BlockPos objects. make one BlockPos.Mutable, call set to move it around before use.

Thanks bro, I add the check side code and now when I right click it just give me a hand animation and generate nothing,  even an invisible barrier...
 


    @Override
    public InteractionResult useOn(UseOnContext context) {
        Level level = context.getLevel();
        if(!level.isClientSide){
            SkyIslandGenerator skyIslandGenerator = new SkyIslandGenerator();
            skyIslandGenerator.printSkyIsland(context.getClickedPos(),context.getLevel());
        }
        return InteractionResult.sidedSuccess(level.isClientSide);
    }
}
 public void printSkyIsland(BlockPos bottom, Level level) {
        double[][] skyIsland = generateSkyIsland(width, length, scale, octaves, persistence, lacunarity, seed, maxHeight*10);
        int maxHeight = -1;
        int max_x = 0, max_z = 0;
        for (int x = 0; x < width; x++) {
            for (int z = 0; z < length; z++) {
                if (skyIsland[x][z] > maxHeight) {
                    maxHeight = (int)skyIsland[x][z];
                    max_x = x;
                    max_z = z;
                }
                System.out.print("("+x+','+z+"):"+(int)skyIsland[x][z]+" ");
            }
            System.out.println();
        }

        BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos(0,0,0);
        for (int x = 0; x < width; x++) {
            for (int z = 0; z < length; z++) {
                for(int y = bottom.getY()+maxHeight ; y>bottom.getY()+maxHeight-skyIsland[x][z] ; y--){
                    blockPos.set(bottom.getX()+x-max_x,y,bottom.getZ()+z-max_z);
                    level.setBlock(blockPos, Blocks.DIRT.defaultBlockState(), 3);
                }
            }
        }
    }

 

Link to comment
Share on other sites

4 hours ago, _p1nero said:

Thanks bro, I add the check side code and now when I right click it just give me a hand animation and generate nothing,  even an invisible barrier...
 


    @Override
    public InteractionResult useOn(UseOnContext context) {
        Level level = context.getLevel();
        if(!level.isClientSide){
            SkyIslandGenerator skyIslandGenerator = new SkyIslandGenerator();
            skyIslandGenerator.printSkyIsland(context.getClickedPos(),context.getLevel());
        }
        return InteractionResult.sidedSuccess(level.isClientSide);
    }
}
 public void printSkyIsland(BlockPos bottom, Level level) {
        double[][] skyIsland = generateSkyIsland(width, length, scale, octaves, persistence, lacunarity, seed, maxHeight*10);
        int maxHeight = -1;
        int max_x = 0, max_z = 0;
        for (int x = 0; x < width; x++) {
            for (int z = 0; z < length; z++) {
                if (skyIsland[x][z] > maxHeight) {
                    maxHeight = (int)skyIsland[x][z];
                    max_x = x;
                    max_z = z;
                }
                System.out.print("("+x+','+z+"):"+(int)skyIsland[x][z]+" ");
            }
            System.out.println();
        }

        BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos(0,0,0);
        for (int x = 0; x < width; x++) {
            for (int z = 0; z < length; z++) {
                for(int y = bottom.getY()+maxHeight ; y>bottom.getY()+maxHeight-skyIsland[x][z] ; y--){
                    blockPos.set(bottom.getX()+x-max_x,y,bottom.getZ()+z-max_z);
                    level.setBlock(blockPos, Blocks.DIRT.defaultBlockState(), 3);
                }
            }
        }
    }

 

Thanks bro, It work well now! I found that it's due to my island generate algorithm. It's possible to generated an island with a height of 0 lol

Edited by _p1nero
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.



×
×
  • Create New...

Important Information

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