Jump to content

[SOLVED] SetBlockState doesn't works


JTK222

Recommended Posts

Hey guys I have searched already a few forum topics but none helped me.

Here is my Code and the BlockState just don't changes.

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    	if (((Boolean)state.getValue(POWERED)).booleanValue())
        {
            return true;
        }
    	else if(!worldIn.isRemote)
    	{	
    		worldIn.setBlockState(pos, worldIn.getBlockState(pos).withProperty(POWERED, Boolean.valueOf(true)),2);
            worldIn.markBlockForUpdate(pos);
    		//worldIn.scheduleUpdate(pos, this, 60);
    		worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, ((Boolean)state.getValue(POWERED)).booleanValue() ? 0.6F : 0.5F);

    		return true;
    	}
    	return true;
    }

 

Thanks in Advance

Link to comment
Share on other sites

Remove some stuff like setBlockBoundsOnState

public class WallTorchLit extends BlockContainer{
public static Block blockWallTorchLit;
public static Object instance;

public static final PropertyInteger DIR = PropertyInteger.create("dir", 0, 3);
public static final PropertyBool AddonLighter = PropertyBool.create("addonLighter");
public static final PropertyBool AddonTrap = PropertyBool.create("addonTrap");
public static final PropertyBool POWERED = PropertyBool.create("powered");

public WallTorchLit() {
	super(Material.wood);
	this.setDefaultState(this.blockState.getBaseState().withProperty(DIR, Integer.valueOf(0)).withProperty(AddonLighter, Boolean.valueOf(false)).withProperty(AddonTrap, Boolean.valueOf(false)).withProperty(POWERED, Boolean.valueOf(true)));
}

static {

	blockWallTorchLit = new WallTorchLit().setHardness(2.0F)
			.setResistance(10.0F).setLightLevel(0.0F)
			.setUnlocalizedName("blockWallTorchLit")
			.setStepSound(Block.soundTypeWood)
			.setCreativeTab(DarkRoleplayTabs.drBlocksTab);
	blockWallTorchLit.setBlockBounds(1.0F / 16F * 0F, 0.0F, 1.0F / 16F * 0F,
			1.0F / 16F * 16F, 1.0F / 16F * 2F, 1.0F / 16F * 16F);
}

public IBlockState getStateFromMeta(int meta)
    {
	int i = meta;
	if(i < 4){
		return this.getDefaultState().withProperty(DIR, Integer.valueOf(meta));
	}else if(i >= 4 && i < {
		return this.getDefaultState().withProperty(DIR, Integer.valueOf(meta-4)).withProperty(AddonLighter, true);
	}else if(i >= 8 && i < 12){
		return this.getDefaultState().withProperty(DIR, Integer.valueOf(meta-).withProperty(AddonTrap,true);
	}else if(i >= 12 && i < 16){
		return this.getDefaultState().withProperty(DIR, Integer.valueOf(meta-12)).withProperty(AddonTrap,true).withProperty(POWERED, true);
	}

	return this.getDefaultState().withProperty(DIR, Integer.valueOf(meta));
    }

public int getMetaFromState(IBlockState state)
    {
	int i = (Integer) state.getValue(DIR);
	if((Boolean) state.getValue(AddonLighter))
	{
		i = i + 4;
	}else if((Boolean) state.getValue(AddonTrap)){
		i = i + 8;
		if((Boolean) state.getValue(POWERED)){
			i = i +4;
		}
	}
	return i;
    }

 protected BlockState createBlockState()
    {
        return new BlockState(this, new IProperty[] {DIR,AddonLighter,AddonTrap,POWERED});
    }
    
    @Override
	public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack){
    	EntityPlayer entity =  (EntityPlayer) placer;
		if(entity!=null){int i = MathHelper.floor_double((double)(entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
		world.setBlockState(pos, state.withProperty(DIR, Integer.valueOf(i)), 3);
		}
		world.markBlockForUpdate(pos);
    }
    
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    	if (((Boolean)state.getValue(POWERED)).booleanValue())
        {
            return true;
        }
    	else if(!worldIn.isRemote)
    	{	
    		worldIn.setBlockState(pos, worldIn.getBlockState(pos).withProperty(POWERED, Boolean.valueOf(true)),2);
            worldIn.markBlockForUpdate(pos);
    		//worldIn.scheduleUpdate(pos, this, 60);
    		worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, "random.click", 0.3F, ((Boolean)state.getValue(POWERED)).booleanValue() ? 0.6F : 0.5F);

    		return true;
    	}
    	return true;
    }
    
    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
    	
    	if(worldIn.isRemote)
    	{
    		
    	}
    	else
    	{
    		worldIn.setBlockState(pos, state.withProperty(POWERED, Boolean.valueOf(false)), 3);
    		 worldIn.notifyNeighborsOfStateChange(pos, this);
    	}
    	
    }

    public int isProvidingWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
    {
        return !((Boolean)state.getValue(POWERED)).booleanValue() ? 15 : 0;
    }

    public int isProvidingStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
    {
    	EnumFacing Facing = EnumFacing.SOUTH;
    	switch((Integer)state.getValue(DIR)){
    		case 0: Facing = EnumFacing.NORTH;
    		break;
    		case 1: Facing = EnumFacing.EAST;
    		break;
    		case 2: Facing = EnumFacing.SOUTH;
    		break;
    		case 3: Facing = EnumFacing.WEST;
    		break;
    		default:
    			break;
    	}
    			
        return ((Boolean)state.getValue(POWERED)).booleanValue() ? 0 : (Facing == side ? 15 : 0);
    }
    
    public boolean canProvidePower()
    {
        return true;
    }
    
	public int getRenderType() {
		return -1;
	}

	public boolean isOpaqueCube() {
		return false;
	}

	public boolean renderAsNormalBlock() {
		return false;
	}

	@Override
	public TileEntity createNewTileEntity(World world, int meta) {
		return new TileEntityCustomWallTorchLit();
	}
	@Override
	public boolean isFullCube()
    {
        return false;
    }

 public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
 {
	 return worldIn.isSideSolid(pos.west(), EnumFacing.EAST, true) ||
			 worldIn.isSideSolid(pos.east(), EnumFacing.WEST, true) ||
			 worldIn.isSideSolid(pos.north(), EnumFacing.SOUTH, true) ||
			 worldIn.isSideSolid(pos.south(), EnumFacing.NORTH, true);
 }
}

 

The Block is still not Done but I can't place any Blocks Vanilla blocks are also not working... :/

Link to comment
Share on other sites

- Don't create your block in a static initializer, do it in preInit.

Could this cause this problem?

If not I would keep it because I like it to have everything from block in one Class or at least in one package.

- Your getMetaFromState and getStateFromMeta are quite weird, are you sure they work?

It should, it's the same as in the Minecraft Vanilla code but a little bit simpler to understand and with more data.

(Vanilla store for default just the facing and the Powered Boolean)

- You say vanilla blocks don't work either, can you show you trying that?

Figured out that they are working but not from every block for example they aren't working from my unlit variant of this block.

Could make a Video but could take some time.

Link to comment
Share on other sites

Here is the Block which can't place event Vanilla blocks:

import net.jtk.darkroleplay.blocks.ShipSteeringWheel.ShipSteeringWheel;
import net.jtk.darkroleplay.blocks.ShipSteeringWheel.TileEntityCustomShipSteeringWheel;
import net.jtk.darkroleplay.main.DarkRoleplayTabs;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class WallTorchEmpty extends BlockContainer{
public static Block blockWallTorchEmpty;
public static Object instance;

public static PropertyInteger DIR = PropertyInteger.create("dir", 0, 3);
public static PropertyBool AddonLighter = PropertyBool.create("addonLighter");
public static PropertyBool AddonTrap = PropertyBool.create("addonTrap");

public WallTorchEmpty() {
	super(Material.wood);
	this.setDefaultState(this.blockState.getBaseState().withProperty(DIR, Integer.valueOf(0)));
	this.setDefaultState(this.blockState.getBaseState().withProperty(AddonLighter, false));
	this.setDefaultState(this.blockState.getBaseState().withProperty(AddonTrap, false));
}

static {

	blockWallTorchEmpty = new WallTorchEmpty().setHardness(2.0F)
			.setResistance(10.0F).setLightLevel(0.0F)
			.setUnlocalizedName("blockWallTorchEmpty")
			.setStepSound(Block.soundTypeWood)
			.setCreativeTab(DarkRoleplayTabs.drBlocksTab);
	blockWallTorchEmpty.setBlockBounds(1.0F / 16F * 0F, 0.0F, 1.0F / 16F * 0F,
			1.0F / 16F * 16F, 1.0F / 16F * 2F, 1.0F / 16F * 16F);
}

public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos){
	 IBlockState iblockstate = worldIn.getBlockState(pos);
	 if (iblockstate.getBlock() == this){
		 if (iblockstate.getValue(DIR).equals(1))
             {
                 this.setBlockBounds(0.0F, 0.2F, 0.375F, 0.25F, 0.8F, 0.625F);
             }
		 else if(iblockstate.getValue(DIR).equals(2))
		 {
			 this.setBlockBounds(0.375F, 0.2F, 0.0F,0.625F, 0.8F, 0.25F);
		 }
		 else if(iblockstate.getValue(DIR).equals(3))
		 {
			 this.setBlockBounds(0.75F, 0.2F, 0.375F, 1.0F, 0.8F, 0.625F);
		 }
             else
             {
                 this.setBlockBounds(0.375F, 0.2F, 0.75F,  0.625F, 0.8F, 1.0F);
             }
	 }
}

public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(DIR, Integer.valueOf(meta));
    }

public int getMetaFromState(IBlockState state)
    {
        return ((Integer)state.getValue(DIR)).intValue();
    }

 protected BlockState createBlockState()
    {
        return new BlockState(this, new IProperty[] {DIR,AddonLighter,AddonTrap});
    }
    
    @Override
	public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack){
    	EntityPlayer entity =  (EntityPlayer) placer;
		if(entity!=null){int i = MathHelper.floor_double((double)(entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
		world.setBlockState(pos, state.withProperty(DIR, Integer.valueOf(i)), 3);
		}
		world.markBlockForUpdate(pos);
    }
    
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    	if (worldIn.isRemote)
        {
            return true;
        }
    	else
    	{
    		if(playerIn.getHeldItem() != null){
    			if(playerIn.getHeldItem().getItem().equals(Blocks.torch)){
    			//worldIn.setBlockState(pos, WallTorchLit.blockWallTorchLit.getDefaultState()/*.withProperty(DIR, state.getValue(DIR)).withProperty(AddonLighter, state.getValue(AddonLighter)).withProperty(AddonTrap, state.getValue(AddonTrap))*/,2);
    			worldIn.setBlockState(pos, Blocks.stone.getDefaultState());
    			worldIn.markBlockForUpdate(pos);
    			}
    		}
    		return true;
    	}
    }

	public int getRenderType() {
		return -1;
	}

	public boolean isOpaqueCube() {
		return false;
	}

	public boolean renderAsNormalBlock() {
		return false;
	}

	@Override
	public TileEntity createNewTileEntity(World world, int meta) {
		return new TileEntityCustomWallTorchEmpty();
	}
	@Override
	public boolean isFullCube()
    {
        return false;
    }

 public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
 {
	 return worldIn.isSideSolid(pos.west(), EnumFacing.EAST, true) ||
			 worldIn.isSideSolid(pos.east(), EnumFacing.WEST, true) ||
			 worldIn.isSideSolid(pos.north(), EnumFacing.SOUTH, true) ||
			 worldIn.isSideSolid(pos.south(), EnumFacing.NORTH, true);
 }
}

 

And could it be something with the TileEntity? Or because its a BlockContainer?

Because I can place a normal Block out of my mod:

public class blockPlaceholder extends Block{

public static Block blockPlaceholder;
public static Object instance;


public blockPlaceholder(){
	super(Material.piston);
}

static{
	blockPlaceholder = (blockPlaceholder) (new blockPlaceholder()
	.setUnlocalizedName("blockPlaceholder")
	.setLightLevel(1F/15F*14.0F)
	.setCreativeTab(DarkRoleplayTabs.drBlocksTab)
	.setHardness(2.0F)
	.setStepSound(Block.soundTypePiston)
	.setResistance(10.0F));
	blockPlaceholder.setHarvestLevel("pickaxe", 0);
	blockPlaceholder.setBlockBounds(0.0F,0.0F,0.0F,1.0F,1.0F,1.0F);
}	

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.