Jump to content

BlockState behaving oddly[Solved]


Leviathan143

Recommended Posts

I have a block with a PropertyInteger called radius, when the block is right-clicked radius should be incremented by one, unless it is 8; if it is 8, it should be set to 1. This works perfectly except when radius is set to 2, instead of being set to 2, it is set to 8.

 

package com.leviathan143.ellipsis.common.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.audio.ISound;
import net.minecraft.client.audio.SoundCategory;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

import com.leviathan143.ellipsis.common.data.RegionalMufflerMap;

public class BlockRegionalMuffler extends Block
{	
private static final PropertyInteger REGION_RADIUS = PropertyInteger.create("radius", 1, ; 

public BlockRegionalMuffler() 
{
	super(Material.iron);
	setDefaultState(this.blockState.getBaseState().withProperty(REGION_RADIUS, 1));
}

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state,
		EntityLivingBase placer, ItemStack stack) 
{
	RegionalMufflerMap.get(worldIn).addMuffler(worldIn, pos);
}

@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) 
{
	RegionalMufflerMap.get(worldIn).removeMuffler(worldIn, pos);
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side,
		float hitX, float hitY, float hitZ) 
{
	if(playerIn.getHeldItem() != null) return false;
	int radius = state.getValue(REGION_RADIUS);
	radius = radius < 8 ? radius + 1 : 1;
	worldIn.setBlockState(pos, this.getDefaultState().withProperty(REGION_RADIUS, radius));
	return true;
}

@Override
protected BlockState createBlockState() 
{
	return new BlockState(this, new IProperty[]{REGION_RADIUS});
}

@Override
public IBlockState getStateFromMeta(int meta) 
{
	return this.getDefaultState().withProperty(REGION_RADIUS, meta + 1);
}

@Override
public int getMetaFromState(IBlockState state) 
{
	int radius = state.getValue(REGION_RADIUS);
	return radius <= 7 ? radius - 1 : 1;
}

public boolean shouldMuffleSound(World world, BlockPos mufflerPos,
		ISound sound, SoundCategory category) 
{
	if(category.equals(SoundCategory.RECORDS)) return false;
	return Math.sqrt(mufflerPos.distanceSqToCenter(sound.getXPosF(), sound.getYPosF(), sound.getZPosF())) <= world.getBlockState(mufflerPos).getValue(REGION_RADIUS);
}
}

Link to comment
Share on other sites

I have a block with a PropertyInteger called radius, when the block is right-clicked radius should be incremented by one, unless it is 8; if it is 8, it should be set to 1. This works perfectly except when radius is set to 2, instead of being set to 2, it is set to 8.

 

package com.leviathan143.ellipsis.common.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.audio.ISound;
import net.minecraft.client.audio.SoundCategory;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

import com.leviathan143.ellipsis.common.data.RegionalMufflerMap;

public class BlockRegionalMuffler extends Block
{	
private static final PropertyInteger REGION_RADIUS = PropertyInteger.create("radius", 1, ; 

public BlockRegionalMuffler() 
{
	super(Material.iron);
	setDefaultState(this.blockState.getBaseState().withProperty(REGION_RADIUS, 1));
}

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state,
		EntityLivingBase placer, ItemStack stack) 
{
	RegionalMufflerMap.get(worldIn).addMuffler(worldIn, pos);
}

@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) 
{
	RegionalMufflerMap.get(worldIn).removeMuffler(worldIn, pos);
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side,
		float hitX, float hitY, float hitZ) 
{
	if(playerIn.getHeldItem() != null) return false;
	int radius = state.getValue(REGION_RADIUS);
	radius = radius < 8 ? radius + 1 : 1;
	worldIn.setBlockState(pos, this.getDefaultState().withProperty(REGION_RADIUS, radius));
	return true;
}

@Override
protected BlockState createBlockState() 
{
	return new BlockState(this, new IProperty[]{REGION_RADIUS});
}

@Override
public IBlockState getStateFromMeta(int meta) 
{
	return this.getDefaultState().withProperty(REGION_RADIUS, meta + 1);
}

@Override
public int getMetaFromState(IBlockState state) 
{
	int radius = state.getValue(REGION_RADIUS);
	return radius <= 7 ? radius - 1 : 1;
}

public boolean shouldMuffleSound(World world, BlockPos mufflerPos,
		ISound sound, SoundCategory category) 
{
	if(category.equals(SoundCategory.RECORDS)) return false;
	return Math.sqrt(mufflerPos.distanceSqToCenter(sound.getXPosF(), sound.getYPosF(), sound.getZPosF())) <= world.getBlockState(mufflerPos).getValue(REGION_RADIUS);
}
}

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.