|
@@ -0,0 +1,184 @@
|
|
|
+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));
|
|
|
+ }
|
|
|
+}
|