Franckyi Posted June 21, 2017 Share Posted June 21, 2017 (edited) Hi ! I'm working on a custom block that can emit redstone signals. The block has a TileEntity. A Capability is attached to this TileEntity. It contains informations like if the block should be sending redstone and with how much power. So consider that my Capability can return a boolean (enabled ? (sending redstone ?)) and an integer (how much power ?). This is my custom block class : public class BlockRedstoneSwitch extends Block { public static final PropertyBool ENABLED = PropertyBool.create("enabled"); public static final PropertyInteger POWER = PropertyInteger.create("power", 0, 15); public BlockRedstoneSwitch(String name, Material mat, CreativeTabs tab, float hardness, float resistance, String tool, int harvest, float light) { super(mat); setUnlocalizedName(name); setRegistryName(name); setCreativeTab(tab); setHardness(hardness); setResistance(resistance); setHarvestLevel(tool, harvest); setLightLevel(light); setDefaultState(this.blockState.getBaseState().withProperty(ENABLED, false).withProperty(POWER, 0)); isBlockContainer = true; } @Override public int getWeakPower(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side) { if (state.getValue(ENABLED)) return state.getValue(POWER); return 0; } @Override public boolean canProvidePower(IBlockState state) { return true; } @Override public boolean canConnectRedstone(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side) { return state.getValue(ENABLED); } @Override public boolean shouldCheckWeakPower(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side) { return false; } @Override public IBlockState getStateFromMeta(int meta) { if (meta > 15) return getDefaultState().withProperty(ENABLED, true).withProperty(POWER, meta - 16); return getDefaultState().withProperty(ENABLED, false).withProperty(POWER, meta); } @Override public int getMetaFromState(IBlockState state) { if(state.getValue(ENABLED)) return state.getValue(POWER) + 16; return state.getValue(POWER); } @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] { ENABLED, POWER }); } @Override public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { // internal } @Override public boolean hasTileEntity(IBlockState state) { return true; } @Override public TileEntity createTileEntity(World world, IBlockState state) { return new TileEntitySwitch(); } } But I really don't know how to link the Capability data to the BlockState property... It could be updated every tick (is it possible ?) or after a certain method is called on the TileEntity. If someone can help me Edited June 21, 2017 by Franckyi Quote Link to comment Share on other sites More sharing options...
V0idWa1k3r Posted June 21, 2017 Share Posted June 21, 2017 34 minutes ago, Franckyi said: return state.getValue(POWER) + 16; 36 minutes ago, Franckyi said: if (meta > 15) metadata can only go up to 16(actual values [0 - 15]). Apart from that the power your block sends is defined in Block::getWeakPower method, you can access your tile from it, access the capability and get the power you are emitting. You might need to notify neightbour blocks when you are changing that value with World::notifyNeighborsOfStateChange (see how BlockLever does that). You can do that directly from your tileentity. 1 Quote Link to comment Share on other sites More sharing options...
Franckyi Posted June 21, 2017 Author Share Posted June 21, 2017 (edited) Thanks, I'll try that. Does that means that my BlockState "power" property is useless because I'll directly check the data in the capability ? [EDIT] Yeah, in fact it's not useful to have BlockState properties in my case. I tested and it works very well ! I've overriden TileEntity::markDirty and called World::notifyNeighborsOfStateChange just after that. It should be alright for me, because I always call markDirty() when I change the capability data. Thanks a lot ! Edited June 21, 2017 by Franckyi Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.