Jump to content

Recommended Posts

Posted

Hi! I'm one of those brats working on a gun mod. I'm confused about how to make the bullet entities do damage. I've been comparing my bullet entity to the arrow entity hoping to make it work in a similar way. I based it off of what I saw of Mr. Crayfish's live modding video (I'll link to it but it's 3 hours long) and tried to fill the rest in myself to put the weapons from classic Doom into the game. The bullet comes from the gun but when it makes contact with a mob or animal it does not damage them. While I'm not a Java expert I'm pretty sure that this isn't due to my ignorance of Java and is a Minecraft thing. I apologize if that is not the case. Something is weird with the BULLET_TARGETS constant. Eclipse is throwing an error on it but I"m using it the same way the EntityArrow class does so I'm confused as to why it doesn't seem to be working. Thanks for any help.

 

Here is my Bullet Entity:

package org.educraft.mymod.entities;

import com.google.common.base.Predicates;

import java.util.List;

import javax.annotation.Nullable;

import org.educraft.mymod.itemclasses.ItemGun;
import org.educraft.mymod.utilclasses.ModDamageSource;

import com.google.common.base.Predicate;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IProjectile;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EntitySelectors;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class EntityBullet extends Entity implements IProjectile
{
	
	private static final Predicate<Entity> BULLET_TARGETS = Predicates.and(new Predicate[] {EntitySelectors.NOT_SPECTATING, EntitySelectors.IS_ALIVE, new Predicate<Entity>()
    {
        public boolean apply(@Nullable Entity p_apply_1_)
        {
            return p_apply_1_.canBeCollidedWith();
        }
    }});
    
		
	private EntityLivingBase shooter;
	private int ticksAlive = 100;
	private double damage;
	//private int ticksInGround;

	public Entity shootingEntity;

	private int ticksInAir;
    /** The amount of knockback an arrow applies when it hits a mob. */


	public EntityBullet(World worldIn) 
	{
		super(worldIn);
		
	}
	
	
	public EntityBullet(World worldIn, EntityLivingBase shooter, float size, double speed, float damage)
    {
        super(worldIn);
        this.setShooter(shooter);
        this.damage = ItemGun.damage;
        this.setSize(size, size);
        this.setLocationAndAngles(shooter.posX, shooter.posY, shooter.posZ, shooter.rotationYaw, shooter.rotationPitch);
        this.setPosition(this.posX, this.posY, this.posZ);
        Vec3d dir = shooter.getLookVec();
        this.motionX = dir.xCoord;
        this.motionY = dir.yCoord;
        this.motionZ = dir.zCoord;

    }
    
    @Override
    public void onUpdate()
    {
    	super.onUpdate();
    	
    	this.posX += this.motionX;
        this.posY += this.motionY;
        this.posZ += this.motionZ;
    	this.setPosition(this.posX, this.posY, this.posZ);
    	
    	ticksAlive--;
    	if(ticksAlive <= 0) 
    	{
    		this.setDead();
    	}
    }
    
    @Nullable
    protected Entity findEntityOnPath(Vec3d start, Vec3d end)
    {
        Entity entity = null;
        List<Entity> list = this.world.getEntitiesInAABBexcluding(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expandXyz(1.0D), BULLET_TARGETS);
        double d0 = 0.0D;

        for (int i = 0; i < list.size(); ++i)
        {
            Entity entity1 = (Entity)list.get(i);

            if (entity1 != this.shootingEntity || this.ticksInAir >= 5)
            {
                AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expandXyz(0.30000001192092896D);
                RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(start, end);

                if (raytraceresult != null)
                {
                    double d1 = start.squareDistanceTo(raytraceresult.hitVec);

                    if (d1 < d0 || d0 == 0.0D)
                    {
                        entity = entity1;
                        d0 = d1;
                    }
                }
            }
        }

        return entity;
    }
    
	@Override
	public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy) 
	{
		float f = MathHelper.sqrt(x * x + y * y + z * z);
        x = x / (double)f;
        y = y / (double)f;
        z = z / (double)f;
        x = x + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy;
        y = y + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy;
        z = z + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy;
        x = x * (double)velocity;
        y = y * (double)velocity;
        z = z * (double)velocity;
        this.motionX = x;
        this.motionY = y;
        this.motionZ = z;
        float f1 = MathHelper.sqrt(x * x + z * z);
        this.rotationYaw = (float)(MathHelper.atan2(x, z) * (180D / Math.PI));
        this.rotationPitch = (float)(MathHelper.atan2(y, (double)f1) * (180D / Math.PI));
        this.prevRotationYaw = this.rotationYaw;
        this.prevRotationPitch = this.rotationPitch;
	}

	protected void onHit(RayTraceResult raytraceResultIn)
	{
		if (this.shootingEntity == null)
        {
            ModDamageSource.causeBulletDamage(this, this);
        }
        else
        {
            ModDamageSource.causeBulletDamage(this, this.shootingEntity);
        }
	}

	public EntityLivingBase getShooter() {
		return shooter;
	}

	public void setShooter(EntityLivingBase shooter) {
		this.shooter = shooter;
	}


	protected ItemStack getArrowStack() {
		return null;
	}

	@Override
	protected void writeEntityToNBT(NBTTagCompound compound) {
		compound.setDouble("damage", this.damage);
	}
	
	@Override
	public void readEntityFromNBT(NBTTagCompound compound) 
	{
		if (compound.hasKey("damage", 99))
        {
            this.damage = compound.getDouble("damage");
        }
	}


	@Override
	protected void entityInit() {}
}

 

Here is my gun item:

package org.educraft.mymod.itemclasses;

import org.educraft.mymod.entities.EntityBullet;
import org.educraft.mymod.init.ModItems;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;

public class ItemGun extends Item
{
	public static float damage;
	private final float spread;
	private final double speed;
	
	public ItemGun(float damage, float spread, double speed)
	{
		ItemGun.damage = damage;
		this.spread = spread;
		this.speed = speed;
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
	{
		ItemStack stack = this.findAmmo(playerIn);
		if(stack != ItemStack.EMPTY)
		{
			worldIn.playSound(playerIn, playerIn.getPosition(), SoundEvents.BLOCK_ANVIL_BREAK, SoundCategory.PLAYERS, 1.0F, 0.5F + (itemRand.nextFloat()));
			if(!worldIn.isRemote)
			{
				EntityBullet bullet = new EntityBullet(worldIn, playerIn, spread, speed, damage);
				worldIn.spawnEntity(bullet);
			}
			else
			{
				playerIn.rotationPitch -= 5f;
			}
			stack.shrink(1);
			
			if(stack.isEmpty())
			{
				playerIn.inventory.deleteStack(stack);
			}
			
		}
		return super.onItemRightClick(worldIn, playerIn, handIn);
	}
	
	private ItemStack findAmmo(EntityPlayer player)
    {
        if (this.isAmmo(player.getHeldItem(EnumHand.OFF_HAND)))
        {
            return player.getHeldItem(EnumHand.OFF_HAND);
        }
        else if (this.isAmmo(player.getHeldItem(EnumHand.MAIN_HAND)))
        {
            return player.getHeldItem(EnumHand.MAIN_HAND);
        }
        else
        {
            for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
            {
                ItemStack itemstack = player.inventory.getStackInSlot(i);

                if (this.isAmmo(itemstack))
                {
                    return itemstack;
                }
            }

            return ItemStack.EMPTY;
        }
    }
	
	protected boolean isAmmo(ItemStack stack)
    {
        return stack.getItem() == ModItems.shotgun_shell;
    }
}

 

Posted

This isn't realted to your problem but,

	public EntityBullet(World worldIn, EntityLivingBase shooter, float size, double speed, float damage) {
        this.damage = ItemGun.damage;

Why the hell do you pass the damage to the constructor and then get the damage from the item?

	public static float damage;
	
	public ItemGun(float damage, float spread, double speed) {
		ItemGun.damage = damage;

Second, why the hell is the variable static?  As soon as you try to create two guns with different damage values, the second one will overwrite the first.

 

Now:

protected void onHit(RayTraceResult raytraceResultIn)

This method is protected meaning that it can't be called by any class outside of this class (and any subclasses). You are not calling this method meaning that it is never called.  What did you expect to happen?

  • Like 1

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.

Posted

Take a closer look at EntityArrows onUpdate() method, your missing a lot of stuff in yours, for example that would be where you call onHit() after doing some raytracing and/or calling findEntityOnPath(). You should also check out EntityFireball, mainly how onUpdate() is written in there and how its different than EntityArrows onUpdate().

You could also just extend EntityThrowable and change some stuff in it to suit your needs, like getGravityVelocity to something smaller and override onImpact() to handle what it does on impact.

  • Like 1
Posted

Oh wow Draco18s! I got so deep into trying everything I lost track of some of that stuff! Thanks for pointing it out. Turns out I forgot to call the onHit method in the onUpdate method and that is a necessary step.

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.