Hello, I'm kinda new to modding, and I'm trying to make a wrench that can cycle though every facing of a block.
I came up with a way to do so for every vanilla block (by editing the game code via MCP, without forge), but I had some troubles with the new launcher and so I tried creating a forge mod.
What I have now do work for every block in vanilla but I'm experiencing a very weird thing for all rails : it just flaches to it's next possible facing and then back to it's previous state.
Also every rail that I place faces the north_south direction, I can't place them to the east/west.
My code doesn't change the way rails behave and no other block do the flash thing, I tried multiple ways of doing it and it is the only thing that happen.
My code :
public EnumActionResult onItemUse(EntityPlayer stack, World playerIn, BlockPos worldIn, EnumHand pos, EnumFacing hand, float facing, float hitX, float hitY)
{
ItemStack itemstack = stack.getHeldItem(pos);
if (!stack.canPlayerEdit(worldIn.offset(hand), hand, itemstack))
{
return EnumActionResult.FAIL;
}
else
{
IBlockState iblockstate = playerIn.getBlockState(worldIn);
iblockstate = rotateState(iblockstate, iblockstate.getBlock(), playerIn, worldIn);
if (playerIn.getBlockState(worldIn) != iblockstate)
{
setBlock(itemstack, stack, playerIn, worldIn, iblockstate);
System.out.println("Rotation with wrench succeded!");
return EnumActionResult.SUCCESS;
}
else
{
System.out.println("Rotation with wrench failed!");
return EnumActionResult.FAIL;
}
}
}
public IBlockState rotateState(IBlockState iblockstate, Block block, World world, BlockPos blockPos)
{
if (!(block instanceof BlockRailBase) && !(block instanceof BlockLever))
{
// rotating other blocks ...
}
else if (block instanceof BlockRailBase)
{
// here I am using the 'withRotation' method, but 'cycleProperty' outputs the same thing
Rotation[] arotation = Rotation.values();
Rotation rotate = arotation[1];
System.out.println("old state :" + iblockstate);
iblockstate = block.withRotation(iblockstate, rotate);
System.out.println("new state :" + iblockstate);
return iblockstate;
}
else
{
// rotating a lever...
}
// by default:
return iblockstate;
}
protected void setBlock(ItemStack stack, EntityPlayer player, World worldIn, BlockPos pos, IBlockState state)
{
worldIn.playSound(player, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 0.3F, 1.0F);
if (!worldIn.isRemote || true) // always true to have both Client and Server feedback on screen, else the rail stays the same on screen and doesn't flash to it's next facing and back again
{
System.out.println("Set Block result : " + worldIn.setBlockState(pos, state, 11));
if (MathHelper.getInt(worldIn.rand, 0, 4) == 0)
{
stack.damageItem(1, player); // 1 in 4 chance to damage the wrench.
}
}
}
The output on this when is right click a rail is :
old state :minecraft:rail[shape=north_south]
new state :minecraft:rail[shape=east_west]
Set Block result : true
Rotation with wrench succeded!
On screen, the rail rotates but imediatly after it changes back to how it was before.
Since this code works on vanilla but not on forge, is it a problem from forge or in my code ?