Jump to content

[SOLVED] [1.11.2] Emit a redstone signal from a TileEntity capability


Franckyi

Recommended Posts

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 by Franckyi
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

×
×
  • Create New...

Important Information

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