ItemGun.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package me.km.items.weapons;
  2. import java.util.List;
  3. import me.km.api.Utils;
  4. import net.minecraft.client.util.ITooltipFlag;
  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.nbt.NBTTagCompound;
  10. import net.minecraft.stats.StatList;
  11. import net.minecraft.util.ActionResult;
  12. import net.minecraft.util.DamageSource;
  13. import net.minecraft.util.EnumActionResult;
  14. import net.minecraft.util.EnumHand;
  15. import net.minecraft.util.SoundCategory;
  16. import net.minecraft.util.SoundEvent;
  17. import net.minecraft.util.text.TextFormatting;
  18. import net.minecraft.world.World;
  19. import net.minecraftforge.common.capabilities.ICapabilityProvider;
  20. import net.minecraftforge.fml.relauncher.Side;
  21. import net.minecraftforge.fml.relauncher.SideOnly;
  22. public class ItemGun extends ItemWeapon
  23. {
  24. private static final double STANDARD_AIM = 0.0625d;
  25. private final Item ammo;
  26. private final int damage;
  27. private final double missAim;
  28. private final int cooldown;
  29. private final int maxLoad;
  30. private SoundEvent soundShot;
  31. private SoundEvent soundReload;
  32. private SoundEvent soundCrit;
  33. public ItemGun(String name, String local, ToolMaterial material, int durability,
  34. float attackDamage, Item ammo, int damage, double missAim, int cooldown, int maxLoad,
  35. SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  36. {
  37. super(name, local, material, attackDamage);
  38. super.setMaxDamage(durability);
  39. this.ammo = ammo;
  40. this.damage = damage;
  41. this.missAim = missAim;
  42. this.cooldown = cooldown;
  43. this.maxLoad = maxLoad;
  44. this.soundShot = soundShot;
  45. this.soundReload = soundReload;
  46. this.soundCrit = soundCrit;
  47. }
  48. public ItemGun(String name, String local, ToolMaterial material, int durability,
  49. Item ammo, int damage, double missAim, int cooldown, int maxLoad,
  50. SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  51. {
  52. this(name, local, material, durability, 0, ammo, damage, missAim,
  53. cooldown, maxLoad, soundShot, soundReload, soundCrit);
  54. }
  55. public ItemGun(String name, String local, ToolMaterial material, int durability,
  56. Item ammo, int damage, int cooldown, int maxLoad,
  57. SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  58. {
  59. this(name, local, material, durability, ammo, damage, STANDARD_AIM,
  60. cooldown, maxLoad, soundShot, soundReload, soundCrit);
  61. }
  62. public void fixSounds(SoundEvent soundShot, SoundEvent soundReload, SoundEvent soundCrit)
  63. {
  64. this.soundShot = soundShot;
  65. this.soundReload = soundReload;
  66. this.soundCrit = soundCrit;
  67. }
  68. @Override
  69. public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt)
  70. {
  71. getTagCompound(stack);
  72. return null;
  73. }
  74. private NBTTagCompound getTagCompound(ItemStack stack)
  75. {
  76. NBTTagCompound com = stack.getTagCompound();
  77. if(com == null)
  78. {
  79. com = new NBTTagCompound();
  80. com.setInteger("load", 0);
  81. stack.setTagCompound(com);
  82. }
  83. else if(!com.hasKey("load"))
  84. {
  85. com.setInteger("load", 0);
  86. }
  87. return com;
  88. }
  89. private int getLoad(ItemStack stack)
  90. {
  91. return getTagCompound(stack).getInteger("load");
  92. }
  93. private void setLoad(ItemStack stack, int load)
  94. {
  95. getTagCompound(stack).setInteger("load", load);
  96. }
  97. @SideOnly(Side.CLIENT)
  98. @Override
  99. public void addInformation(ItemStack stack, World w, List<String> lore, ITooltipFlag flag)
  100. {
  101. int load = getLoad(stack);
  102. if(load >= 0)
  103. {
  104. if(load == 1)
  105. {
  106. lore.add(1, TextFormatting.GOLD + "1 shot loaded");
  107. }
  108. else
  109. {
  110. lore.add(1, TextFormatting.GOLD + "" + load + " shots loaded");
  111. }
  112. }
  113. }
  114. @Override
  115. public void onPlayerStoppedUsing(ItemStack stack, World w, EntityLivingBase liv, int timeLeft)
  116. {
  117. if(liv instanceof EntityPlayer)
  118. {
  119. EntityPlayer p = (EntityPlayer) liv;
  120. int load = getLoad(stack);
  121. boolean creative = p.capabilities.isCreativeMode;
  122. if(load > 0 || creative)
  123. {
  124. p.getCooldownTracker().setCooldown(this, 10);
  125. if (!w.isRemote)
  126. {
  127. stack.damageItem(1, p);
  128. double d = missAim;
  129. if(d > STANDARD_AIM)
  130. {
  131. int time = ((this.getMaxItemUseDuration(stack) - timeLeft) / 20) + 1;
  132. d = Math.max(STANDARD_AIM, d / time);
  133. }
  134. EntityLivingBase target = Utils.getTargetedEntity(p, 128, d, d, d, EntityLivingBase.class);
  135. if(target != null)
  136. {
  137. target.attackEntityFrom(DamageSource.causePlayerDamage(p), damage);
  138. }
  139. }
  140. if(!creative)
  141. {
  142. setLoad(stack, load - 1);
  143. }
  144. playSound(p, w, soundShot);
  145. p.addStat(StatList.getObjectUseStats(this));
  146. }
  147. }
  148. }
  149. private void playSound(EntityPlayer p, World w, SoundEvent e)
  150. {
  151. w.playSound(null, p.posX, p.posY, p.posZ, e, SoundCategory.PLAYERS, 1.0F, 1.0f / (itemRand.nextFloat() * 0.4f + 1.2f));
  152. }
  153. @Override
  154. public int getMaxItemUseDuration(ItemStack stack)
  155. {
  156. return 72000;
  157. }
  158. private boolean isAmmo(ItemStack stack)
  159. {
  160. return stack.getItem() == ammo;
  161. }
  162. private ItemStack findAmmo(EntityPlayer p)
  163. {
  164. if(this.isAmmo(p.getHeldItem(EnumHand.OFF_HAND)))
  165. {
  166. return p.getHeldItem(EnumHand.OFF_HAND);
  167. }
  168. else if(this.isAmmo(p.getHeldItem(EnumHand.MAIN_HAND)))
  169. {
  170. return p.getHeldItem(EnumHand.MAIN_HAND);
  171. }
  172. else
  173. {
  174. ItemStack stack;
  175. for(int i = 0; i < p.inventory.getSizeInventory(); i++)
  176. {
  177. stack = p.inventory.getStackInSlot(i);
  178. if(this.isAmmo(stack))
  179. {
  180. return stack;
  181. }
  182. }
  183. return ItemStack.EMPTY;
  184. }
  185. }
  186. @Override
  187. public ActionResult<ItemStack> onItemRightClick(World w, EntityPlayer p, EnumHand hand)
  188. {
  189. if(p.capabilities.isCreativeMode)
  190. {
  191. setLoad(p.getHeldItem(hand), maxLoad);
  192. playSound(p, w, soundReload);
  193. return new ActionResult(EnumActionResult.SUCCESS, p.getHeldItem(hand));
  194. }
  195. ItemStack stack = p.getHeldItem(hand);
  196. int load = getLoad(stack);
  197. if(load <= 0)
  198. {
  199. if(!p.isSneaking())
  200. {
  201. return new ActionResult<>(EnumActionResult.FAIL, p.getHeldItem(hand));
  202. }
  203. ItemStack ammoStack = findAmmo(p);
  204. if(!ammoStack.isEmpty())
  205. {
  206. int newLoad = Math.min(ammoStack.getCount(), maxLoad);
  207. setLoad(stack, newLoad);
  208. p.getCooldownTracker().setCooldown(this, cooldown * newLoad);
  209. ammoStack.shrink(newLoad);
  210. if(ammoStack.isEmpty())
  211. {
  212. p.inventory.deleteStack(ammoStack);
  213. }
  214. playSound(p, w, soundReload);
  215. }
  216. return new ActionResult(EnumActionResult.FAIL, stack);
  217. }
  218. p.setActiveHand(hand);
  219. return new ActionResult<>(EnumActionResult.SUCCESS, p.getHeldItem(hand));
  220. }
  221. }