ItemGun.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package me.km.items;
  2. import me.km.api.Utils;
  3. import me.km.capabilities.GunLoadProvider;
  4. import me.km.capabilities.IGunLoad;
  5. import net.minecraft.entity.EntityLivingBase;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.item.Item;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.stats.StatList;
  10. import net.minecraft.util.ActionResult;
  11. import net.minecraft.util.DamageSource;
  12. import net.minecraft.util.EnumActionResult;
  13. import net.minecraft.util.EnumHand;
  14. import net.minecraft.util.SoundCategory;
  15. import net.minecraft.util.SoundEvent;
  16. import net.minecraft.world.World;
  17. public class ItemGun extends ItemWeapon
  18. {
  19. private static final double STANDARD_AIM = 0.0625d;
  20. private final Item ammo;
  21. private final int damage;
  22. private final double missAim;
  23. private final int cooldown;
  24. private final int maxLoad;
  25. private SoundEvent soundShot;
  26. private SoundEvent soundReload;
  27. private SoundEvent soundCrit;
  28. public ItemGun(String name, String local, ToolMaterial material, int durability,
  29. float attackDamage, Item ammo, int damage, double missAim, int cooldown, int maxLoad,
  30. SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  31. {
  32. super(name, local, material, attackDamage);
  33. super.setMaxDamage(durability);
  34. this.ammo = ammo;
  35. this.damage = damage;
  36. this.missAim = missAim;
  37. this.cooldown = cooldown;
  38. this.maxLoad = maxLoad;
  39. this.soundShot = soundShot;
  40. this.soundReload = soundReload;
  41. this.soundCrit = soundCrit;
  42. }
  43. public ItemGun(String name, String local, ToolMaterial material, int durability,
  44. Item ammo, int damage, double missAim, int cooldown, int maxLoad,
  45. SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  46. {
  47. this(name, local, material, durability, 0, ammo, damage, missAim,
  48. cooldown, maxLoad, soundShot, soundReload, soundCrit);
  49. }
  50. public ItemGun(String name, String local, ToolMaterial material, int durability,
  51. Item ammo, int damage, int cooldown, int maxLoad,
  52. SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  53. {
  54. this(name, local, material, durability, ammo, damage, STANDARD_AIM,
  55. cooldown, maxLoad, soundShot, soundReload, soundCrit);
  56. }
  57. public void fixSounds(SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  58. {
  59. this.soundShot = soundShot;
  60. this.soundReload = soundReload;
  61. this.soundCrit = soundCrit;
  62. }
  63. @Override
  64. public void onPlayerStoppedUsing(ItemStack stack, World w, EntityLivingBase liv, int timeLeft)
  65. {
  66. if(liv instanceof EntityPlayer)
  67. {
  68. EntityPlayer p = (EntityPlayer) liv;
  69. IGunLoad gunLoad = stack.getCapability(GunLoadProvider.GUN_LOAD_CAP, null);
  70. int load = gunLoad.getCurrentLoad();
  71. boolean creative = p.capabilities.isCreativeMode;
  72. if(load > 0 || creative)
  73. {
  74. if (!w.isRemote)
  75. {
  76. stack.damageItem(1, p);
  77. double d = missAim;
  78. if(d > STANDARD_AIM)
  79. {
  80. int time = ((this.getMaxItemUseDuration(stack) - timeLeft) / 20) + 1;
  81. d = Math.max(STANDARD_AIM, d / time);
  82. }
  83. EntityLivingBase target = Utils.getTargetedEntity(p, 128, d, d, d, EntityLivingBase.class);
  84. if(target != null)
  85. {
  86. target.attackEntityFrom(DamageSource.causeMobDamage(p), damage);
  87. }
  88. }
  89. playSound(p, w, soundShot);
  90. if(!creative)
  91. {
  92. gunLoad.setCurrentLoad(load - 1);
  93. }
  94. p.addStat(StatList.getObjectUseStats(this));
  95. }
  96. }
  97. }
  98. private void playSound(EntityPlayer p, World w, SoundEvent e)
  99. {
  100. w.playSound(null, p.posX, p.posY, p.posZ, e, SoundCategory.PLAYERS, 1.0F, 1.0f / (itemRand.nextFloat() * 0.4f + 1.2f));
  101. }
  102. @Override
  103. public int getMaxItemUseDuration(ItemStack stack)
  104. {
  105. return 72000;
  106. }
  107. private boolean isAmmo(ItemStack stack)
  108. {
  109. return stack.getItem() == ammo;
  110. }
  111. private ItemStack findAmmo(EntityPlayer p)
  112. {
  113. if(this.isAmmo(p.getHeldItem(EnumHand.OFF_HAND)))
  114. {
  115. return p.getHeldItem(EnumHand.OFF_HAND);
  116. }
  117. else if(this.isAmmo(p.getHeldItem(EnumHand.MAIN_HAND)))
  118. {
  119. return p.getHeldItem(EnumHand.MAIN_HAND);
  120. }
  121. else
  122. {
  123. ItemStack stack;
  124. for(int i = 0; i < p.inventory.getSizeInventory(); i++)
  125. {
  126. stack = p.inventory.getStackInSlot(i);
  127. if(this.isAmmo(stack))
  128. {
  129. return stack;
  130. }
  131. }
  132. return ItemStack.EMPTY;
  133. }
  134. }
  135. @Override
  136. public ActionResult<ItemStack> onItemRightClick(World w, EntityPlayer p, EnumHand hand)
  137. {
  138. if(p.capabilities.isCreativeMode)
  139. {
  140. p.getHeldItem(hand).getCapability(GunLoadProvider.GUN_LOAD_CAP, null).setCurrentLoad(maxLoad);
  141. playSound(p, w, soundReload);
  142. return new ActionResult(EnumActionResult.FAIL, p.getHeldItem(hand));
  143. }
  144. ItemStack stack = p.getHeldItem(hand);
  145. IGunLoad gunLoad = stack.getCapability(GunLoadProvider.GUN_LOAD_CAP, null);
  146. int load = gunLoad.getCurrentLoad();
  147. if(load <= 0)
  148. {
  149. ItemStack ammoStack = findAmmo(p);
  150. if(!ammoStack.isEmpty())
  151. {
  152. int newLoad = Math.min(ammoStack.getCount(), maxLoad);
  153. gunLoad.setCurrentLoad(newLoad);
  154. ammoStack.shrink(newLoad);
  155. if(ammoStack.isEmpty())
  156. {
  157. p.inventory.deleteStack(ammoStack);
  158. }
  159. p.getCooldownTracker().setCooldown(this, cooldown * newLoad);
  160. playSound(p, w, soundReload);
  161. }
  162. return new ActionResult(EnumActionResult.FAIL, stack);
  163. }
  164. p.setActiveHand(hand);
  165. return new ActionResult<>(EnumActionResult.SUCCESS, p.getHeldItem(hand));
  166. }
  167. }