Jump to content

[Solved][1.7.10] Entity Cannot be Interacted With


nerdboy64

Recommended Posts

EDIT: I've solved the problem now. If you have a similar problem, see my comment below.

 

I'm trying to add a custom entity to my mod, and it's supposed to perform an action when right clicked by the player while holding a specific item. However, for some reason the game doesn't think the entity exists as far as right clicking goes. I've tried using EntityInteractEvent, which works for vanilla entities but not mine, as well as Minecraft.getMinecraft().objectMouseOver.entityHit, which has similar results. I've also tried overriding interactFirst() in my entity's class, which does absolutely nothing.

 

The entity is an artillery cannon, and the item is a pair of binoculars which sets the target coordinates for the cannon. My two theories are that either (A) the entity's bounding box is in the wrong place or nonexistent, or (B) the entity is not registered properly so the game ignores it. Any ideas as to what could be causing this and how to fix it?

 

EntityArtillery.java:

 

 

package com.nerdboy64.sg2;

import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

public class EntityArtillery extends Entity {

public static final double artilleryMuzzleVelocity = 500.0;
public static final double gravity = -9.81;

public int targetX, targetY, targetZ;

public static final String NBT_COORD_X = "coord_x";
public static final String NBT_COORD_Y = "coord_y";
public static final String NBT_COORD_Z = "coord_z";

public EntityArtillery(World world) {
	super(world);

	this.boundingBox.setBounds(posX - 1, posY - 1, posZ - 1, posX + 1, posY + 1, posZ + 1);
}

public EntityArtillery(World world, double posX, double posY, double posZ){
	this(world);
	this.posX = posX;
	this.posY = posY;
	this.posZ = posZ;

}

@Override
protected void entityInit() {
	// TODO Auto-generated method stub

}

@Override
protected void readEntityFromNBT(NBTTagCompound nbt) {
	targetX = nbt.getInteger(NBT_COORD_X);
	targetY = nbt.getInteger(NBT_COORD_Y);
	targetZ = nbt.getInteger(NBT_COORD_Z);
}

@Override
protected void writeEntityToNBT(NBTTagCompound nbt) {
	nbt.setInteger(NBT_COORD_X, targetX);
	nbt.setInteger(NBT_COORD_Y, targetY);
	nbt.setInteger(NBT_COORD_Z, targetZ);
}

public boolean updateLaunchAngle(int x, int y, int z){
	targetX = x;
	targetY = y;
	targetZ = z;

	double xDist = x - posX;
	double yDist = y - posY;
	double zDist = z - posZ;
	double hDist = Math.sqrt(Math.pow(xDist, 2) + Math.pow(zDist, 2));
	double vSquared = Math.pow(artilleryMuzzleVelocity, 2);

	this.rotationYaw = (float)Math.toDegrees(Math.atan2(xDist, zDist));

	double d = Math.pow(vSquared, 2) - gravity * (gravity * Math.pow(hDist, 2) + 2 * yDist * vSquared);
	if(d < 0) return false;

	double angle = Math.toDegrees(Math.atan(vSquared - Math.sqrt(d) / (gravity * hDist)));
	this.rotationPitch = (float) angle;

	return true;
}

@Override
public AxisAlignedBB getBoundingBox(){
	return boundingBox;
}

@Override
public AxisAlignedBB getCollisionBox(Entity entity){
	return entity.getBoundingBox();
}

@Override
public float getCollisionBorderSize(){
	return 2.0F;

}

 

 

 

Relevant parts of main mod class:

 

 

@EventHandler
    public void init(FMLInitializationEvent event){
    //lots of unrelated stuff happens

    int artilleryEntityID = EntityRegistry.findGlobalUniqueEntityId();
    	EntityRegistry.registerModEntity(EntityArtillery.class, "artillery", artilleryEntityID, this, 64, 1, false);
}

 

 

ItemBinoculars.java:

 

package com.nerdboy64.sg2.items;

import java.util.List;

import com.nerdboy64.sg2.EntityArtillery;

import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class ItemBinoculars extends Item {
public static final String NBT_COORD_X = "coord_x";
public static final String NBT_COORD_Y = "coord_y";
public static final String NBT_COORD_Z = "coord_z";

public ItemBinoculars(){
	setUnlocalizedName("binoculars");
	setCreativeTab(CreativeTabs.tabTools);
	setTextureName("sg2:binoculars");
}

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player){
	NBTTagCompound nbt = stack.getTagCompound();
	if(nbt == null) nbt = stack.stackTagCompound = new NBTTagCompound();

	if(Minecraft.getMinecraft().objectMouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY){
		player.addChatMessage(new ChatComponentText(Minecraft.getMinecraft().objectMouseOver.entityHit.getClass().getName()));
		if(Minecraft.getMinecraft().objectMouseOver.entityHit instanceof EntityArtillery){
			player.addChatMessage(new ChatComponentText("Selected an artillery thing."));
			return stack;
		}
	}

	Vec3 origin = Vec3.createVectorHelper(player.posX, player.posY + player.getEyeHeight(), player.posZ);
	Vec3 look = player.getLookVec().normalize();

	look = Vec3.createVectorHelper(look.xCoord * 100 + origin.xCoord, look.yCoord * 100 + origin.yCoord, look.zCoord * 100 + origin.zCoord);

	MovingObjectPosition mop = world.rayTraceBlocks(origin, look);

	if(mop != null && mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK){
		nbt.setInteger(NBT_COORD_X, mop.blockX);
		nbt.setInteger(NBT_COORD_Y, mop.blockY);
		nbt.setInteger(NBT_COORD_Z, mop.blockZ);
		player.addChatMessage(new ChatComponentText("Saved coordinates: " + mop.blockX + ", " + mop.blockY+  ", " + mop.blockZ));
	}

	return stack;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List info, boolean flag){
	NBTTagCompound nbt = stack.getTagCompound();
	if(nbt == null) nbt = stack.stackTagCompound = new NBTTagCompound();

	info.add("Saved Coordinates: " + nbt.getInteger(NBT_COORD_X) + ", " + nbt.getInteger(NBT_COORD_Y) + ", " + nbt.getInteger(NBT_COORD_Z));
}
}

 

 

Relevant method from event handler class:

 

@EventHandler @SubscribeEvent()
   public void onEntityInteract(EntityInteractEvent evt){
//this works perfectly for all vanilla entities but not EntityArtillery
   evt.entityPlayer.addChatMessage(new ChatComponentText(evt.target.getClass().getName()));
   
//this does not work at all
   if(evt.target.getClass() == EntityArtillery.class){
	   ItemStack stack = evt.entityPlayer.getCurrentEquippedItem();
	   if(stack != null && stack.getItem() instanceof ItemBinoculars){
		   NBTTagCompound nbt = stack.getTagCompound();
		   if(((EntityArtillery)evt.target).updateLaunchAngle(
				   nbt.getInteger(NBT_COORD_X),
				   nbt.getInteger(NBT_COORD_Y),
				   nbt.getInteger(NBT_COORD_Z))){
			   evt.entityPlayer.addChatMessage(new ChatComponentText(
					   "Set target to: " + nbt.getInteger(NBT_COORD_X) + ", " + nbt.getInteger(NBT_COORD_Y) + ", " + nbt.getInteger(NBT_COORD_Z)
					   ));
		   }
	   }
   }
   }

 

Link to comment
Share on other sites

  • 2 weeks later...

In case anyone else finds this thread and has a similar problem, I've found the solution. There is a method in Entity called canBeCollidedWith(), which is set by default to return false. The name is somewhat misleading, however, as it actually determines whether or not the entity will respond to interactions such as right-clicking. Overriding it to return true (see below) causes the entity to work properly.

 

@Override
public boolean canBeCollidedWith(){
    return true;
}

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.