Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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);
}
}

  • 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);
}
}

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.

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.

  • Author

I think this is caused by your

getMetaFromState

method. Metadata should simply be radius - 1.

Yep, that was it. Thanks.

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.