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

Hi there,

I'm a newbie here.

 

I wanna build an entity that when I right click him, the entity moves to the certain position.

 

How can I make it?

 

Thx!

  • Author

I tried to build an AI class, but it doesn't work :'(

 

package com.practise.ai;

import com.practise.NPC.EscortRequestNPC;

import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.EntityAIMoveToBlock;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

public class EscortRequestNPCai extends EntityAIBase{

private final EscortRequestNPC entity;

public EscortRequestNPCai(EscortRequestNPC entity){
	this.entity = entity;

}

@Override
public boolean shouldExecute() {
	if(entity.ifTrigger == true)
		return true;
	else
		return false;
}

@Override
public void startExecuting() {
	BlockPos NPCpos = entity.getPosition();
	int posX = NPCpos.getX();
	int posY = NPCpos.getY();
	int posZ = NPCpos.getZ();
	posX += 200;
	posY += 200;
	entity.getNavigator().tryMoveToXYZ(posX, posY, posZ, 2);
}

}

  • Author

Thanks buddy! 8)

package com.practise.NPC;

import com.practise.ai.EscortRequestNPCai;

import net.minecraft.entity.ai.EntityAIMoveToBlock;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

public class EscortRequestNPC extends EntityVillager{


public EscortRequestNPC(World worldIn) {
	super(worldIn);	
	this.tasks.addTask(0, new EscortRequestNPCai(this));
}
public boolean ifTrigger = false;


@Override
public boolean interact(EntityPlayer player) {
	ifTrigger = true;
	return true;
}



}

  • Author

Ok, the problem is, I can enter the method startExcuting, but it doesn't work.

 

I've changed my code to this:

package com.practise.ai;

import com.practise.NPC.EscortRequestNPC;

import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.EntityAIMoveToBlock;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

public class EscortRequestNPCai extends EntityAIBase{

private final EscortRequestNPC entity;
BlockPos NPCpos;
int posX;
int posY;
int posZ;

public EscortRequestNPCai(EscortRequestNPC entity){
	this.entity = entity;	
	NPCpos = entity.getPosition();
	posX = NPCpos.getX();
	posY = NPCpos.getY();
	posZ = NPCpos.getZ();
	posX += 200;
	posY += 200;
}



@Override
public boolean shouldExecute() {
	if(entity.ifTrigger == true)
		return true;
	else
		return false;
}

@Override
public void startExecuting() {
	for(int i=0;i !=10;i++)
		System.out.println("enter startExecuting!!!");
	entity.getNavigator().tryMoveToXYZ(posX, posY, posZ, 0.5);
}


}

 

When I right click the entity, the console gave me this:

 

[13:03:53] [server thread/INFO]: Player98 joined the game

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:57] [server thread/INFO] [sTDOUT]: [com.practise.ai.EscortRequestNPCai:startExecuting:42]: enter startExecuting!!!

[13:03:58] [server thread/INFO]: Saving and pausing game...

 

 

 

 

So, it seems the "tryMoveToXYZ" method did not work.

  • Author

I changed my code to this:

package com.practise.ai;

import com.practise.NPC.EscortRequestNPC;

import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.EntityAIMoveToBlock;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

public class EscortRequestNPCai extends EntityAIBase{

private final EscortRequestNPC entity;
BlockPos NPCpos;
int posX;
int posY;
int posZ;

public EscortRequestNPCai(EscortRequestNPC entity){
	this.entity = entity;	
	NPCpos = entity.getPosition();
	posX = NPCpos.getX();
	posY = NPCpos.getY();
	posZ = NPCpos.getZ();
	posX += 5;
	posY += 5;
}



@Override
public boolean shouldExecute() {
	if(entity.ifTrigger == true)
		return true;
	else
		return false;
}

@Override
public void startExecuting() {
	System.out.println(entity.getNavigator().tryMoveToXYZ((double)posX, (double)posY, (double)posZ, 2));
}
}

 

And the console gave me "false" result.

 

So I guess there must be something wrong with parameters? :'(

BTW, In class EscortRequestNPC, is interact being called when it should be? Do you ever clear ifTrigger to false? Does anything ever test for being at the XYZ so you can stop executing?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

  • Author

is interact being called when it should be?

I believe so, with other classes I made, I used it a bunch of times, they work perfectly.

 

Do you ever clear ifTrigger to false?

Oh, I forgot about it, I'll definitely add this when the entity moves to certain position. But the problem is: it doesn't even move now.

 

Does anything ever test for being at the XYZ so you can stop executing?

The same answer with last one, it doesn't move now.

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.