Jump to content

[SOLVED] [1.11] World#setBlockState() changes block, but then REVERTS back immediately


Recommended Posts

Posted (edited)

Hey everyone,

 

So, I have a block that has a Tile Entity. When I right click this block with one of my items, I check if this block that was right clicked is an instanceof this Tile Entity. If it is, I run a getWorld().setBlockState() method inside of the Tile Entities code. All the code runs, except, as soon as it changes the block, it INSTANTLY REVERTS back to the old block. I feel this may be because the Tile Entity might still be there, or something? Anyways, here's my code.

 

(NOTE: I have shortened this code on purpose in the Tile Entity. PlaceNewModBlock actually has a bunch of other conditions in it to change the block into others. I removed these other conditions in my code to test if it was the others interfering, but they weren't.)

 

TILE ENTITY CLASS:

public class TileEntityModBlock extends TileEntity {
    public int carveAmount = 0;

    public void placeNewModBlock() {
        Block mod_block = getWorld().getBlockState(getPos()).getBlock();
        Utils.getLogger().info(mod_block.getUnlocalizedName());

        if(mod_block == ModBlocks.new_mod_block) {
            getWorld().setBlockState(getPos(), ModBlocks.new_mod_block.getDefaultState());
        }
    }
}

ITEM CLASS:

@Override
    public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        if(worldIn.isRemote) {
            if (worldIn.getTileEntity(pos) != null) {
                if (worldIn.getTileEntity(pos) instanceof TileEntityModBlock) {
                    TileEntityModBlock modBlock = (TileEntityModBlock) worldIn.getTileEntity(pos);

                    modBlock.carveAmount++;
                    if(modBlock.carveAmount >= 3){
                        modBlock.placeNewModBlock();
                    }
                }
            }
        }
        return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
    }

 

So, when the carveAmount is greater than 3 (R-Click block 3 times) The block should change to the new one, based on whatever block you're right clicking is.

It does change, but REVERTS back immediately. Again, I think it may be something to do with the Tile Entity still being there... anways, help is appreciated!

 

Thanks!

Edited by MSpace-Dev
Solved the problem
Posted

UPDATE: I've also tried adding getWorld().removeTileEntity(getPos()); just before the code that sets the block state. Still reverts back immediately.

 

I should also mention that a lot of different blocks use this Tile Entity when registered through overriding createTileEntity() in each block's class.

Posted

I have found the problem. So obvious. During trying to make sound and particles work, I changed !worldIn.isRemote to worldIn.isRemote! Changed that back, and now all's working fine! Just need to figure out how I can do particles and sound for the block and the logic server side, without having to duplicate the function

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.