123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- package me.km.items;
- import me.km.api.Utils;
- import me.km.capabilities.GunLoadProvider;
- import me.km.capabilities.IGunLoad;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- 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.world.World;
- 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 void onPlayerStoppedUsing(ItemStack stack, World w, EntityLivingBase liv, int timeLeft)
- {
- if(liv instanceof EntityPlayer)
- {
- EntityPlayer p = (EntityPlayer) liv;
- IGunLoad gunLoad = stack.getCapability(GunLoadProvider.GUN_LOAD_CAP, null);
- int load = gunLoad.getCurrentLoad();
- boolean creative = p.capabilities.isCreativeMode;
-
- if(load > 0 || creative)
- {
- 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.causeMobDamage(p), damage);
- }
- }
- playSound(p, w, soundShot);
-
- if(!creative)
- {
- gunLoad.setCurrentLoad(load - 1);
- }
- 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<ItemStack> onItemRightClick(World w, EntityPlayer p, EnumHand hand)
- {
- if(p.capabilities.isCreativeMode)
- {
- p.getHeldItem(hand).getCapability(GunLoadProvider.GUN_LOAD_CAP, null).setCurrentLoad(maxLoad);
- playSound(p, w, soundReload);
- return new ActionResult(EnumActionResult.FAIL, p.getHeldItem(hand));
- }
- ItemStack stack = p.getHeldItem(hand);
- IGunLoad gunLoad = stack.getCapability(GunLoadProvider.GUN_LOAD_CAP, null);
- int load = gunLoad.getCurrentLoad();
- if(load <= 0)
- {
- ItemStack ammoStack = findAmmo(p);
- if(!ammoStack.isEmpty())
- {
- int newLoad = Math.min(ammoStack.getCount(), maxLoad);
- gunLoad.setCurrentLoad(newLoad);
- ammoStack.shrink(newLoad);
- if(ammoStack.isEmpty())
- {
- p.inventory.deleteStack(ammoStack);
- }
- p.getCooldownTracker().setCooldown(this, cooldown * newLoad);
- playSound(p, w, soundReload);
- }
- return new ActionResult(EnumActionResult.FAIL, stack);
- }
- p.setActiveHand(hand);
- return new ActionResult<>(EnumActionResult.SUCCESS, p.getHeldItem(hand));
- }
- }
|