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

Hello, so i've been trying to create a custom chair that i can sit on, after many attempts I created it but there seems to be an issue when I try to sit on it. Whenever i right click the chair, it teleports me into it and then pushes me out and nothing happens afterwards. However if I log off and log back in I find myself sitting on the chair.

 

Here's the code for the chair block

 

public class Phoerite_Chair extends Block {

 

public static PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);

 

public Phoerite_Chair(Material materialIn) {

super(materialIn);

this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));

super.setBlockBounds( 0.0F, 0F, 0.0F, 1.0F, 1.35F, 1.0F);

}

 

@Override

public boolean isOpaqueCube() {

return false;

}

 

@Override

public EnumWorldBlockLayer getBlockLayer() {

return EnumWorldBlockLayer.CUTOUT;

}

 

@Override

public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,

ItemStack stack) {

 

        if (!worldIn.isRemote)

        {

worldIn.spawnEntityInWorld(new EntityArrow(worldIn, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D));

        }

super.onBlockPlacedBy(worldIn, pos, state, placer, stack);

}

@Override

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,EnumFacing side, float hitX, float hitY, float hitZ) {

 

if (!worldIn.isRemote)

{

playerIn.mountEntity(new EntityArrow(worldIn, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D));

}

return super.onBlockActivated(worldIn, pos, state, playerIn, side, hitX, hitY, hitZ);

}

 

@Override

public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state) {

        Entity entity;

        entity = new EntityArrow(worldIn, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D);

        if (!worldIn.isRemote)

        {

        entity.setDead();

        }

super.onBlockDestroyedByPlayer(worldIn, pos, state);

}

 

@Override

public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {

IBlockState state = super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer);

return state.withProperty(FACING, placer.getHorizontalFacing());

}

 

   

    @Override

    public int getMetaFromState(IBlockState state) {

   

    return ((EnumFacing) state.getValue(FACING)).getHorizontalIndex();

    }

   

    @Override

    public IBlockState getStateFromMeta(int meta) {

   

    return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));

    }

   

    @Override

    protected BlockState createBlockState() {

    return new BlockState(this, new IProperty[] {FACING});

    }

}

 

 

First of all, you need a custom entity as it needs to have a hitbox of size 0 to not be pushed out of the block. Secondly, you are spawning a NEW entity every time you want to interact with it. This means that you are creating more and more entities but never actually removing them. Instead you need to create a TileEntity and store the entity's UUID in it and retrieve the from the world with the help of that UUID every time you need to interact with the entity.

  • Author

Sorry, but how could I store the entity UUID? I'm not quite sure how to do this as i'm still a newbie..

Entities have a UUID field.

Get it.

Save it into a field of your TE.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author

Thanks again! But there still seems to be an issue... None of the interractions with the entity work.

Here is the current chair block code :

public class Phoerite_Chair extends Block {

public static PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);

public Phoerite_Chair(Material materialIn) {
	super(materialIn);
	this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
	super.setBlockBounds( 0.0F, 0F, 0.0F, 1.0F, 1.35F, 1.0F);
}

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

@Override
public EnumWorldBlockLayer getBlockLayer() {
	return EnumWorldBlockLayer.CUTOUT;
}

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        if (!worldIn.isRemote)
        {
        WorldServer worldserver = (WorldServer) worldIn;	
	worldIn.spawnEntityInWorld(worldserver.getEntityFromUuid(TileEntityChair.ChairUUID));
        }

	super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,EnumFacing side, float hitX, float hitY, float hitZ) {

	if (!worldIn.isRemote)
	{
		WorldServer worldserver = (WorldServer) worldIn;
		playerIn.mountEntity(worldserver.getEntityFromUuid(TileEntityChair.ChairUUID));
	}
		return super.onBlockActivated(worldIn, pos, state, playerIn, side, hitX, hitY, hitZ);
}

@Override
public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state) {

        if (!worldIn.isRemote)
        {
        	WorldServer worldserver = (WorldServer) worldIn;
        	worldserver.getEntityFromUuid(TileEntityChair.ChairUUID).setDead();
        }
	super.onBlockDestroyedByPlayer(worldIn, pos, state);
}

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

    @Override
    public int getMetaFromState(IBlockState state) {
    	
    	return ((EnumFacing) state.getValue(FACING)).getHorizontalIndex();
    }
    
    @Override
    public IBlockState getStateFromMeta(int meta) {
    	
    	return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
    }
    
    @Override
    protected BlockState createBlockState() {
    	return new BlockState(this, new IProperty[] {FACING});
    }
}

  • Author

I'm setting the UUID Field inside the TE, It is now no longer static but it seems like there is still the same issue, so i'm guessing i'm doing something wrong with setting the UUID field.

 

Here is one of the interractions with the entity :

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        if (!worldIn.isRemote)
        {
        TileEntityChair id = new TileEntityChair();
        WorldServer worldserver = (WorldServer) worldIn;
	worldIn.spawnEntityInWorld(worldserver.getEntityFromUuid(id.ChairUUID));
        }

	super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
}

 

And this is the TE :

public class TileEntityChair extends TileEntity {

public UUID ChairUUID;

public void TileEntityChairID(UUID id)
    {
        id = new EntityChair(worldObj).getPersistentID();
        ChairUUID = id;
       
    }

}

Why the hell do you create a new

TileEntity

and a new

Entity

? Get the existing ones from the world.

 

Also:

You can cache this value in your TE in a field, too, if you want, but you'll have to use a

WeakReference

to avoid memory leaks.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

  • Author

Get the existing ones from the world.

Sorry, I don't think i know how to do that..

world.getTileEntity for one....

You were told already how to do the other...

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author

Okay, this is embarassing but it still doesn't work  ._.

 

Here's the TE:

public class TileEntityChair extends TileEntity {

public UUID ChairUUID;

public void TileEntityChairID(UUID uuid, World world, int id)
    {
	EntityChair entity = (EntityChair) world.getEntityByID(id);
        uuid = entity.getPersistentID();
        ChairUUID = uuid;
    }

And here's one of the interractions :

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        if (!worldIn.isRemote)
        {
        TileEntityChair id = (TileEntityChair) worldIn.getTileEntity(pos);
        WorldServer worldserver = (WorldServer) worldIn;
	worldIn.spawnEntityInWorld(worldserver.getEntityFromUuid(id.ChairUUID));
        }

	super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
}

You need to create the entity when the block is placed, save its uuid to the tile entity by getting that from the world and retrieve the entity with the uuid from the world when a player sits down or when you need to kill the entity when the block is destroyed.

  • Author

u_u

 

Interractions:

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        if (!worldIn.isRemote)
        {
        Entity entity = new EntityChair(worldIn, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D);
	worldIn.spawnEntityInWorld(entity);
        }

	super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,EnumFacing side, float hitX, float hitY, float hitZ) {
	if (!worldIn.isRemote)
	{
		TileEntityChair id = (TileEntityChair) worldIn.getTileEntity(pos);
		WorldServer worldserver = (WorldServer) worldIn;
		playerIn.mountEntity(worldserver.getEntityFromUuid(id.ChairUUID));
	}
		return super.onBlockActivated(worldIn, pos, state, playerIn, side, hitX, hitY, hitZ);
}

@Override
public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state) {

        if (!worldIn.isRemote)
        {
        	TileEntityChair id = (TileEntityChair) worldIn.getTileEntity(pos);
        	WorldServer worldserver = (WorldServer) worldIn;
        	worldserver.getEntityFromUuid(id.ChairUUID).setDead();
        }
	super.onBlockDestroyedByPlayer(worldIn, pos, state);
}

 

TE:

public class TileEntityChair extends TileEntity {

public UUID ChairUUID;

public void TileEntityChairID(UUID uuid, World world, int id)
    {
	EntityChair entity = (EntityChair) world.getEntityByID(id);
        uuid = entity.getPersistentID();
        ChairUUID = uuid;
    }

}

After you spawned the entity, you need to put its uuid into the tile entity. And why the hell do you have that uuid stuff in the constructor?

Your TE's constructor must take 0 parameters or it will be unable to be recreated when the game is loaded from disk.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.