Jump to content

Recommended Posts

Posted

Hi,

 

I have been creating a mod with a custom entity, and I wanted to teleport it when I clicked a specific block to that block. I have tried to get it to work, but it seems that I don't exactly know how to initialize the entity properly so that it is recognized by the onItemUse() method. This is the code I have been trying with fail:

 

public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
    {
	EntityTameable entity = new EntityRobot(worldIn); //my custom entity that I attempt to initialize
	if(stack.getItem() == MyMod.bucket_oil) { //bucket_oil is just the name for an item

		entity.setLocationAndAngles((double)pos.getX(), (double)pos.getY(), (double)pos.getZ(), 1.0F, 1.0F); //attempts to teleport entity but does not do anything
		System.out.println(entity.getEntityId()); //check to see if my entity is "seen"


		return true;
	}
	return false;
    }

 

Thanks!

Posted

You can't just create a new entity whenever you want - you need the instance of the entity that already exists in the world. Luckily, there is an Item method called whenever you right-click on an entity with your item, which you can override to do whatever you want, e.g. teleport:

@Override
public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase entity) {
    if (entity instanceof YourTargetEntityClass) {
          // put teleportation code or whatever here
          return true;
    }
    return false;
}

Posted

You can't just create a new entity whenever you want - you need the instance of the entity that already exists in the world. Luckily, there is an Item method called whenever you right-click on an entity with your item, which you can override to do whatever you want, e.g. teleport:

@Override
public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase entity) {
    if (entity instanceof YourTargetEntityClass) {
          // put teleportation code or whatever here
          return true;
    }
    return false;
}

 

That isn't exactly what I am looking for. I don't really want the entity to teleport when I right click on it. I want it to teleport to a block when I right click an item on that block. I'm just wondering if this is possible. If it is, how would I go about doing it? If it isn't, then I will just have to find another way to do something similar.

Posted

Is it YOUR block, a vanilla block, any block? If it's your block, you can override the onBlockClicked method (or whatever it's called) and you'll have the coordinates there; if it can by any block, but your Item, you can do what you did above and use onItemUse. Either way, that will give you the Block's position.

 

How are you going to find the entity to teleport? Do you have some kind of system to track the entity, e.g. if the entity's unique ID is stored in the ItemStack NBT, the Block's TileEntity, or even the player's IEEP? Without such a system, it will be impossible to know which entity to look for and teleport to the block position.

 

If you describe how your item/block/entity teleportation should work, not in code but from the player's perspective in game, then I could give you better advice.

Posted

Is it YOUR block, a vanilla block, any block? If it's your block, you can override the onBlockClicked method (or whatever it's called) and you'll have the coordinates there; if it can by any block, but your Item, you can do what you did above and use onItemUse. Either way, that will give you the Block's position.

 

How are you going to find the entity to teleport? Do you have some kind of system to track the entity, e.g. if the entity's unique ID is stored in the ItemStack NBT, the Block's TileEntity, or even the player's IEEP? Without such a system, it will be impossible to know which entity to look for and teleport to the block position.

 

If you describe how your item/block/entity teleportation should work, not in code but from the player's perspective in game, then I could give you better advice.

 

It is any block.

 

So let's assume this:

My custom item will be called test_item

My custom entity will be called EntityTest

 

Now for what I want to happen:

From a player's perspective, the player has item test_item in their hand. With this, they can right click any block, and all of their tamed entity/entities EntityTest teleport to that clicked block.

 

I don't know if this is possible. But if it is, could you please give me a walkthrough of what I would have to do? It doesn't have to be the entire code; I just want the concepts that I would have to apply to make this work.

Posted

Possible, yes, but to get every tamed entity... it's not going to be pretty.

 

Basically, you need a way to know which entities to fetch, but there isn't any 'getAllTamedEntities' method. The way I see it, you have 2 options:

 

1. Use World#getLoadedEntityList and iterate through each entry checking if a. is it an EntityTameable, and b. does it belong to the player; teleport them as they are found.

 

2. Store a list of every entity the player tames in IExtendedEntityProperties; you'll have to write code to find out when an entity is being tamed, add it to the list, and then, since the player may log out and back in, be able to find the entities from their unique ID (which is what you should store).

 

Both of those have a problem: if the entity is in a chunk that is not currently loaded, it will not be found. The first method, there is no way around this; the second method, it's possible, but starts to get clunky - you'd have to track the last known location of each entity, then you could try to force load that chunk (and possibly nearby chunks) if not loaded and see if the entity is still there. Even then, there is no guarantee, as the entity may have been killed, or it may have wandered / been lead off while the owner was away, and there is not really any way for you to know this.

 

Another option, then, would be WorldSavedData with a server-tick handler that updates each tamed entities' last position, but as you can see, things have gotten far more complicated than one would like.

 

So, you can easily teleport MOST of the player's tamed entities, but to guarantee ALL of them, you've got a lot of work ahead of you.

Posted

1. Use World#getLoadedEntityList and iterate through each entry checking if a. is it an EntityTameable, and b. does it belong to the player; teleport them as they are found.

 

I tried using that, but when I do, nothing happens. This is the code I'm using:

 

@Override
public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
    {

	if(stack.getItem() == RobotPetItems.bucket_oil) {
		       
		Object o = worldIn.getLoadedEntityList();
		if (o instanceof EntityTameable) {
			if (((EntityTameable) o).isOwner(playerIn)) {
				System.out.println("Found Entity");
				return true;
			}
			else {
				System.out.println("No entity found");
				return false;
			}
		}

		//playerIn.setPosition(pos.getX(), pos.getY() + 1, pos.getZ());

		//return true;
	}
	return false;
    }

 

 

 

I sorry but I'm actually new to this.

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.