Posted May 28, 20169 yr 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); } }
May 28, 20169 yr Author 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); } }
May 28, 20169 yr I think this is caused by your getMetaFromState method. Metadata should simply be radius - 1. Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
May 28, 20169 yr I think this is caused by your getMetaFromState method. Metadata should simply be radius - 1. Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
May 28, 20169 yr Author I think this is caused by your getMetaFromState method. Metadata should simply be radius - 1. Yep, that was it. Thanks.
May 28, 20169 yr Author I think this is caused by your getMetaFromState method. Metadata should simply be radius - 1. Yep, that was it. Thanks.
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.