package me.km.items.weapons; import java.util.List; import me.km.api.Utils; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.stats.StatList; import net.minecraft.util.ActionResult; import net.minecraft.util.DamageSource; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundEvent; import net.minecraft.util.text.TextFormatting; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class ItemGun extends ItemWeapon { private static final double STANDARD_AIM = 0.0625d; private final Item ammo; private final int damage; private final double missAim; private final int cooldown; private final int maxLoad; private SoundEvent soundShot; private SoundEvent soundReload; private SoundEvent soundCrit; public ItemGun(String name, String local, ToolMaterial material, int durability, float attackDamage, Item ammo, int damage, double missAim, int cooldown, int maxLoad, SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit) { super(name, local, material, attackDamage); super.setMaxDamage(durability); this.ammo = ammo; this.damage = damage; this.missAim = missAim; this.cooldown = cooldown; this.maxLoad = maxLoad; this.soundShot = soundShot; this.soundReload = soundReload; this.soundCrit = soundCrit; } public ItemGun(String name, String local, ToolMaterial material, int durability, Item ammo, int damage, double missAim, int cooldown, int maxLoad, SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit) { this(name, local, material, durability, 0, ammo, damage, missAim, cooldown, maxLoad, soundShot, soundReload, soundCrit); } public ItemGun(String name, String local, ToolMaterial material, int durability, Item ammo, int damage, int cooldown, int maxLoad, SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit) { this(name, local, material, durability, ammo, damage, STANDARD_AIM, cooldown, maxLoad, soundShot, soundReload, soundCrit); } public void fixSounds(SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit) { this.soundShot = soundShot; this.soundReload = soundReload; this.soundCrit = soundCrit; } @Override public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt) { getTagCompound(stack); return null; } private NBTTagCompound getTagCompound(ItemStack stack) { NBTTagCompound com = stack.getTagCompound(); if(com == null) { com = new NBTTagCompound(); com.setInteger("load", 0); stack.setTagCompound(com); } else if(!com.hasKey("load")) { com.setInteger("load", 0); } return com; } private int getLoad(ItemStack stack) { return getTagCompound(stack).getInteger("load"); } private void setLoad(ItemStack stack, int load) { getTagCompound(stack).setInteger("load", load); } @SideOnly(Side.CLIENT) @Override public void addInformation(ItemStack stack, World w, List lore, ITooltipFlag flag) { int load = getLoad(stack); if(load >= 0) { if(load == 1) { lore.add(1, TextFormatting.GOLD + "1 shot loaded"); } else { lore.add(1, TextFormatting.GOLD + "" + load + " shots loaded"); } } } @Override public void onPlayerStoppedUsing(ItemStack stack, World w, EntityLivingBase liv, int timeLeft) { if(liv instanceof EntityPlayer) { EntityPlayer p = (EntityPlayer) liv; int load = getLoad(stack); boolean creative = p.capabilities.isCreativeMode; if(load > 0 || creative) { p.getCooldownTracker().setCooldown(this, 10); if (!w.isRemote) { stack.damageItem(1, p); double d = missAim; if(d > STANDARD_AIM) { int time = ((this.getMaxItemUseDuration(stack) - timeLeft) / 20) + 1; d = Math.max(STANDARD_AIM, d / time); } EntityLivingBase target = Utils.getTargetedEntity(p, 128, d, d, d, EntityLivingBase.class); if(target != null) { target.attackEntityFrom(DamageSource.causePlayerDamage(p), damage); } } if(!creative) { setLoad(stack, load - 1); } playSound(p, w, soundShot); p.addStat(StatList.getObjectUseStats(this)); } } } private void playSound(EntityPlayer p, World w, SoundEvent e) { w.playSound(null, p.posX, p.posY, p.posZ, e, SoundCategory.PLAYERS, 1.0F, 1.0f / (itemRand.nextFloat() * 0.4f + 1.2f)); } @Override public int getMaxItemUseDuration(ItemStack stack) { return 72000; } private boolean isAmmo(ItemStack stack) { return stack.getItem() == ammo; } private ItemStack findAmmo(EntityPlayer p) { if(this.isAmmo(p.getHeldItem(EnumHand.OFF_HAND))) { return p.getHeldItem(EnumHand.OFF_HAND); } else if(this.isAmmo(p.getHeldItem(EnumHand.MAIN_HAND))) { return p.getHeldItem(EnumHand.MAIN_HAND); } else { ItemStack stack; for(int i = 0; i < p.inventory.getSizeInventory(); i++) { stack = p.inventory.getStackInSlot(i); if(this.isAmmo(stack)) { return stack; } } return ItemStack.EMPTY; } } @Override public ActionResult onItemRightClick(World w, EntityPlayer p, EnumHand hand) { if(p.capabilities.isCreativeMode) { setLoad(p.getHeldItem(hand), maxLoad); playSound(p, w, soundReload); return new ActionResult(EnumActionResult.SUCCESS, p.getHeldItem(hand)); } ItemStack stack = p.getHeldItem(hand); int load = getLoad(stack); if(load <= 0) { if(!p.isSneaking()) { return new ActionResult<>(EnumActionResult.FAIL, p.getHeldItem(hand)); } ItemStack ammoStack = findAmmo(p); if(!ammoStack.isEmpty()) { int newLoad = Math.min(ammoStack.getCount(), maxLoad); setLoad(stack, newLoad); p.getCooldownTracker().setCooldown(this, cooldown * newLoad); ammoStack.shrink(newLoad); if(ammoStack.isEmpty()) { p.inventory.deleteStack(ammoStack); } playSound(p, w, soundReload); } return new ActionResult(EnumActionResult.FAIL, stack); } p.setActiveHand(hand); return new ActionResult<>(EnumActionResult.SUCCESS, p.getHeldItem(hand)); } }