EDIT: I solved the problem, I didn't have sendsVelocityUpdates on in the init() section
So I have a gun code which is supposed to fire an entity towards where I am looking at, but it doesn't work. I have tried using motionX/Y/Z and setVelocity but none of them have any effect on the bullet entity, it just falls onto the ground.
//Called when right clicked
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer){
if(par1ItemStack.getItemDamage() < 8){
//If in survival, damage item and throw bullet
if (!par3EntityPlayer.capabilities.isCreativeMode){
par1ItemStack.damageItem(1, par3EntityPlayer);
Fire(par2World, par3EntityPlayer);
}
//If in creative, don't damage item but throw the bullet
if (par3EntityPlayer.capabilities.isCreativeMode){
Fire(par2World, par3EntityPlayer);
}
}
//If there are no bullets left in the gun, find and reload new ones!
if(par1ItemStack.getItemDamage() == 8){
if(!par3EntityPlayer.capabilities.isCreativeMode && par3EntityPlayer.inventory.hasItem(JItems.cartridge)){
par3EntityPlayer.inventory.consumeInventoryItem(JItems.cartridge);
par1ItemStack.setItemDamage(0);
}
//If in creative, don't consume bullets
if (par3EntityPlayer.capabilities.isCreativeMode){
Fire(par2World, par3EntityPlayer);
}
So I have a gun code which is supposed to fire an entity towards where I am looking at, but it doesn't work. I have tried using motionX/Y/Z and setVelocity but none of them have any effect on the bullet entity, it just falls onto the ground.
Gun code:
import com.jamesscn.jamesscnmod.Main;
import com.jamesscn.jamesscnmod.entity.EntityBullet;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
public class ItemGun extends Item{
//Defines basics
public ItemGun() {
super();
this.maxStackSize = 1;
this.setMaxDamage(8);
}
//Called when right clicked
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer){
if(par1ItemStack.getItemDamage() < 8){
//If in survival, damage item and throw bullet
if (!par3EntityPlayer.capabilities.isCreativeMode){
par1ItemStack.damageItem(1, par3EntityPlayer);
Fire(par2World, par3EntityPlayer);
}
//If in creative, don't damage item but throw the bullet
if (par3EntityPlayer.capabilities.isCreativeMode){
Fire(par2World, par3EntityPlayer);
}
}
//If there are no bullets left in the gun, find and reload new ones!
if(par1ItemStack.getItemDamage() == 8){
if(!par3EntityPlayer.capabilities.isCreativeMode && par3EntityPlayer.inventory.hasItem(JItems.cartridge)){
par3EntityPlayer.inventory.consumeInventoryItem(JItems.cartridge);
par1ItemStack.setItemDamage(0);
}
//If in creative, don't consume bullets
if (par3EntityPlayer.capabilities.isCreativeMode){
Fire(par2World, par3EntityPlayer);
}
}
return par1ItemStack;
}
public void Fire(World par1World, EntityPlayer par2EntityPlayer) {
EntityBullet entityBullet = new EntityBullet(par1World);
if (!par1World.isRemote){
par1World.playSoundAtEntity(par2EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
entityBullet.setLocationAndAngles(par2EntityPlayer.posX, par2EntityPlayer.posY, par2EntityPlayer.posZ, par2EntityPlayer.rotationYaw, par2EntityPlayer.rotationPitch);
par1World.spawnEntityInWorld(entityBullet);
}
}
}
Entity code:
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
public class EntityBullet extends EntityThrowable{
public EntityBullet(World par1World){
super(par1World);
}
//Sets speed of the thrown bullet and gravity
public void setVelocity(){
//old throw code
/*
this.motionX *= 0.0D;
this.motionY *= -0.0D;
this.motionZ *= -0.0D;
*/
this.setVelocity(10.0D, 0.0D, 0.0D);
}
//If bullet hits something, hurt it
protected void onImpact(MovingObjectPosition par1MovingObjectPosition){
if (par1MovingObjectPosition.entityHit != null){
par1MovingObjectPosition.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), 10);
}
/*
//If the bullet hits something, despawn the bullet
if (!this.worldObj.isRemote){
this.setDead();
}
*/
}
}