Jump to content

[1.8] Teleporting entity with onItemUse()


plr123

Recommended Posts

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!

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
  • Topics

×
×
  • Create New...

Important Information

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