Greetings everyone,
I've been trying to code an automatic shooting weapon recently, and encountered a problem.
My weapon uses an NBT integer tag to store the current magazine contents, and should fire as long as the RMB is held and there is ammo in the magazine.
I use this code:
public void onUsingTick(ItemStack stack, EntityLivingBase player, int count)
{//Every 3rd tick...
if (count % 3 == 0)
{//...get the world we are in...
World world = player.world;
//...get magazine contents...
int magCount = stack.getTagCompound().getInteger("magazine");
//...if the magazine has ammo...
if (magCount > 0)
{
if (!world.isRemote)
{
//decrease the ammo...
stack.getTagCompound().setInteger("magazine", magCount - 1);
//...create and spawn the bullet, play shot sound.
EntityGenericBullet bullet = new EntityGenericBullet(world, player, 6.0D, 0.5D, false);
bullet.shoot(player, player.rotationPitch, player.rotationYaw, 5.0F, 1.0F);
world.spawnEntity(bullet);
world.playSound(null, player.posX, player.posY, player.posZ, ModSounds.submachinegunfire, SoundCategory.PLAYERS, 6.0F, 1.0F / (itemRand.nextFloat() * 0.2F + 0.8F));
}
//Small recoil
player.setLocationAndAngles(player.posX, player.posY, player.posZ, player.rotationYaw, player.rotationPitch - (itemRand.nextFloat() + 1.0F));
}
}
}
And for some reason, it gives a weird bug: sometimes upon a short RMB press, instead of a short burst, the weapon empties the entire magazine. That does not always happen though.
What am I doing wrong?
Thanks in advance,
Dan