Jump to content

[1.10.2] [Solved] Need help with mountable chair block


Erfurt

Recommended Posts

Hey guys, I trying to make some chairs for my mod, and at the moment most of my code seems to be working fine. I can mount the chairs but not dismount them. I have been looking at how other mods are making their chairs just for reference, no copy paste, and there seems to not be any difference, other than some variable naming.

 

Code can be found in the spoiler

 

 

 

EntityMountableBlock class

 

 

 



public class EntityMountableBlock extends Entity
{
public int blockPosX;
public int blockPosY;
public int blockPosZ;

public EntityMountableBlock(World world)
{
	super(world);
	this.noClip = true;
	this.height = 0.01F;
	this.width = 0.01F;
}

public EntityMountableBlock(World world, double x, double y, double z, double y0ffset)
{
	this(world);
	this.blockPosX = (int) x;
	this.blockPosY = (int) y;
	this.blockPosZ = (int) z;
	setPosition(x + 0.5D, y + y0ffset, z + 0.5D);
}

public EntityMountableBlock(World world, double x, double y, double z, double y0ffset, int rotation, double rotationOffset)
{
	this(world);
	this.blockPosX = (int) x;
	this.blockPosY = (int) y;
	this.blockPosZ = (int) z;
	setPostionConsideringRotation(x + 0.5D, y + y0ffset, z + 0.5D, rotation, rotationOffset);
}

public void setPostionConsideringRotation(double x, double y, double z, int rotation, double rotationOffset)
{
	switch (rotation)
	{
	case 2:
		z += rotationOffset;
		break;
	case 0:
		z -= rotationOffset;
		break;
	case 3:
		x -= rotationOffset;
		break;
	case 1:
		x += rotationOffset;
		break;
	}
	setPosition(x, y, z);
}

@Override
public double getMountedYOffset()
{
	return this.height * 0.0D;
}

@Override
protected boolean shouldSetPosAfterLoading()
{
	return false;
}

@Override
public void onEntityUpdate()
{
	if (!this.worldObj.isRemote)
	{
		if (this.isBeingRidden() || this.worldObj.isAirBlock(new BlockPos(blockPosX, blockPosY, blockPosZ)))
		{
			this.setDead();
			worldObj.updateComparatorOutputLevel(getPosition(), worldObj.getBlockState(getPosition()).getBlock());
		}
	}
}

@Override
protected void entityInit()
{
}

@Override
public void readEntityFromNBT(NBTTagCompound nbttagcompound)
{
}

@Override
public void writeEntityToNBT(NBTTagCompound nbttagcompound)
{
}

}
 

 

 

I register the entity like this in my base mod class

 

 

 



@EventHandler
public void init(FMLInitializationEvent event)
{
	proxy.init();

	CraftingRegistry.init();

	EntityRegistry.registerModEntity(EntityMountableBlock.class, "MountableBlock", 0, this, 80, 1, false);
}
 

 

 

MountableUtil class

 

 

 



public class MountableUtil
{
public static boolean sitOnBlock(World par1World, double x, double y, double z, EntityPlayer par5EntityPlayer, double par6)
{
	if (!checkForExistingEntity(par1World, x, y, z, par5EntityPlayer))
	{
		EntityMountableBlock nemb = new EntityMountableBlock(par1World, x, y, z, par6);
		par1World.spawnEntityInWorld(nemb);
		par5EntityPlayer.startRiding(nemb);
	}
	return true;
}

public static boolean sitOnBlockWithRotationOffset(World par1World, double x, double y, double z, EntityPlayer par5EntityPlayer, double par6, int metadata, double offset)
{
	if (!checkForExistingEntity(par1World, x, y, z, par5EntityPlayer))
	{
		EntityMountableBlock nemb = new EntityMountableBlock(par1World, x, y, z, par6, metadata, offset);
		par1World.spawnEntityInWorld(nemb);
		par5EntityPlayer.startRiding(nemb);
	}
	return true;
}

public static boolean checkForExistingEntity(World par1World, double x, double y, double z, EntityPlayer par5EntityPlayer)
{
	List<EntityMountableBlock> listEMB = par1World.getEntitiesWithinAABB(EntityMountableBlock.class, new AxisAlignedBB(x, y, z, x + 1.0D, y + 1.0D, z + 1.0D).expand(1D, 1D, 1D));
	for (EntityMountableBlock mount : listEMB)
	{
		if (mount.blockPosX == x && mount.blockPosY == y && mount.blockPosZ == z)
		{
			if (!mount.isBeingRidden())
			{
				par5EntityPlayer.startRiding(mount);
			}
			return true;
		}
	}
	return false;
}

public static boolean isSomeoneSitting(World world, double x, double y, double z)
{
	List<EntityMountableBlock> listEMB = world.getEntitiesWithinAABB(EntityMountableBlock.class, new AxisAlignedBB(x, y, z, x + 1.0D, y + 1.0D, z + 1.0D).expand(1D, 1D, 1D));
	for (EntityMountableBlock mount : listEMB)
	{
		if (mount.blockPosX == x && mount.blockPosY == y && mount.blockPosZ == z)
		{
			return mount.isBeingRidden();
		}
	}
	return false;
}
}
 

 

 

ChairBlock Class

 

 

 



public class ChairBlock extends Block
{

public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
protected static final double pixel = 1/16D;
protected static final AxisAlignedBB CHAIR_BOUNDING_AABB = new AxisAlignedBB(1*pixel, 0.0D, 1*pixel, 15*pixel, 21*pixel, 15*pixel);
private static final AxisAlignedBB CHAIR_BASE = new AxisAlignedBB(1*pixel, 0.0D, 1*pixel, 15*pixel, 8*pixel, 15*pixel);
private static final AxisAlignedBB CHAIR_BACKREST_NORTH = CollisionHelper.getBlockBounds(EnumFacing.NORTH, 1*pixel, 0.0D, 1*pixel, 15*pixel, 21*pixel, 15*pixel);
private static final AxisAlignedBB CHAIR_BACKREST_EAST = CollisionHelper.getBlockBounds(EnumFacing.EAST, 1*pixel, 0.0D, 1*pixel, 15*pixel, 21*pixel, 15*pixel);
private static final AxisAlignedBB CHAIR_BACKREST_SOUTH = CollisionHelper.getBlockBounds(EnumFacing.SOUTH, 1*pixel, 0.0D, 1*pixel, 15*pixel, 21*pixel, 15*pixel);
private static final AxisAlignedBB CHAIR_BACKREST_WEST = CollisionHelper.getBlockBounds(EnumFacing.WEST, 1*pixel, 0.0D, 1*pixel, 15*pixel, 21*pixel, 15*pixel);

public ChairBlock(String name)
{
	super(Material.WOOD);
	this.setUnlocalizedName(name);
	this.setRegistryName(name);
	this.setHardness(1.0F);
	this.setSoundType(SoundType.WOOD);
	this.isToolEffective("axe", getDefaultState());
	this.setCreativeTab(CreativeTabs.DECORATIONS);
	this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}

@Override
public boolean isOpaqueCube(IBlockState state)
{
	return false;
}

@Override
public boolean isFullCube(IBlockState state)
{
	return false;
}

@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
    {
	return CHAIR_BOUNDING_AABB;
        
    }

@Override
public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB axisAligned, List<AxisAlignedBB> axisAlignedList, Entity collidingEntity) 
{
	if (!(collidingEntity instanceof EntityMountableBlock))
	{
		EnumFacing facing = state.getValue(FACING);
		switch(facing) 
		{
		case NORTH:
			super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, CHAIR_BACKREST_NORTH);
			break;
		case SOUTH:
			super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, CHAIR_BACKREST_SOUTH);
			break;
		case WEST:
			super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, CHAIR_BACKREST_WEST);
			break;
		default:
			super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, CHAIR_BACKREST_EAST);
			break;
		}
		super.addCollisionBoxToList(pos, axisAligned, axisAlignedList, CHAIR_BASE);
	}
}

@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
	return super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer).withProperty(FACING, placer.getHorizontalFacing());
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
{
	if(MountableUtil.sitOnBlock(worldIn, pos.getX(), pos.getY(), pos.getZ(), playerIn, 7*pixel))
	{
		worldIn.updateComparatorOutputLevel(pos, this);
		return true;
	}
	return false;
}

@Override
public int getMetaFromState(IBlockState state)
{
        return state.getValue(FACING).getHorizontalIndex();
}

@Override
public IBlockState getStateFromMeta(int meta)
{
	return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
}

@Override
public BlockStateContainer createBlockState()
{
	return new BlockStateContainer(this, new IProperty[] { FACING });
}


@Override
public boolean hasComparatorInputOverride(IBlockState state) 
{
	return true;
}

@Override
public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos)
{
	return MountableUtil.isSomeoneSitting(worldIn, pos.getX(), pos.getY(), pos.getZ()) ? 1 : 0;
}
}
 

 

 

 

 

Edited by Erfurt
Link to comment
Share on other sites

3 hours ago, TheSunCat said:

Is it fixed?

Yes it is

 

I had to remove this code from my EntityMountableBlock class, not really sure why, but that fixed my issue

@Override
public void onEntityUpdate()
{
	if (!this.worldObj.isRemote)
	{
		if (this.isBeingRidden() || this.worldObj.isAirBlock(new BlockPos(blockPosX, blockPosY, blockPosZ)))
		{
			this.setDead();
			worldObj.updateComparatorOutputLevel(getPosition(), worldObj.getBlockState(getPosition()).getBlock());
		}
	}
}
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.

Announcements



×
×
  • Create New...

Important Information

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