Guest User

Untitled

a guest
Dec 11th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. public abstract class ItemGun extends Item
  2. {
  3. public ItemGun()
  4. {
  5. this.setMaxStackSize(1);
  6.  
  7. MainRegistry.content().addGun(this);
  8. }
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. public abstract void onGunFired(ItemStack paramItemStack, World paramWorld, EntityPlayer paramEntityPlayer);
  16.  
  17. public abstract String getRendererTexture();
  18.  
  19. public abstract String getFireSound();
  20.  
  21. public abstract String getSilencedSound();
  22.  
  23. public abstract Item getMagazineItem();
  24.  
  25. public abstract EnumGunType getGunType();
  26.  
  27. public abstract int getFireRate();
  28.  
  29. public String getGunTypeDescription()
  30. {
  31. return StatCollector.translateToLocal("gun.type." + getGunType().name().toLowerCase());
  32. }
  33.  
  34. public void addAttachment(ItemStack stack, ItemAttachment attachment)
  35. {
  36. NBTTagCompound nbt = stack.stackTagCompound != null ? stack.stackTagCompound : new NBTTagCompound();
  37.  
  38. ItemAttachment[] attachments = getAttachments(stack);
  39. ItemAttachment[] newAttachments = new ItemAttachment[attachments.length + 1];
  40. for (ItemAttachment att : attachments) {
  41. if (att.getName().equals(attachment)) {
  42. return;
  43. }
  44. }
  45. for (int i = 0; i < attachments.length; i++) {
  46. newAttachments[i] = attachments[i];
  47. }
  48. if (attachment.canBeUsedWithGun(this, attachments)) {
  49. newAttachments[(newAttachments.length - 1)] = attachment;
  50. }
  51. NBTTagList list = new NBTTagList();
  52. for (ItemAttachment attach : newAttachments)
  53. {
  54. NBTTagString tag = new NBTTagString(attach.getName());
  55. list.appendTag(tag);
  56. }
  57. nbt.setTag("Attachments", list);
  58. stack.stackTagCompound = nbt;
  59. }
  60.  
  61. public void removeAttachment(String name, ItemStack stack)
  62. {
  63. NBTTagCompound nbt = stack.stackTagCompound;
  64. if (nbt == null) {
  65. return;
  66. }
  67. ArrayList<ItemAttachment> arr = new ArrayList();
  68. for (ItemAttachment attach : getAttachments(stack)) {
  69. if (!attach.getName().equals(name)) {
  70. arr.add(attach);
  71. }
  72. }
  73. NBTTagList list = new NBTTagList();
  74. for (ItemAttachment attach : (ItemAttachment[])arr.toArray(new ItemAttachment[0])) {
  75. if (attach != null)
  76. {
  77. NBTTagString tag = new NBTTagString(attach.getName());
  78. list.appendTag(tag);
  79. }
  80. }
  81. nbt.setTag("Attachments", list);
  82. stack.stackTagCompound = nbt;
  83. }
  84.  
  85. public void fireRaycastBullet(EntityLivingBase living, float spread)
  86. {
  87. float f = spread;
  88. double motionX = -MathHelper.sin(living.rotationYaw / 180.0F * 3.1415927F) * MathHelper.cos(living.rotationPitch / 180.0F * 3.1415927F) * f;
  89. double motionZ = MathHelper.cos(living.rotationYaw / 180.0F * 3.1415927F) * MathHelper.cos(living.rotationPitch / 180.0F * 3.1415927F) * f;
  90. double motionY = -MathHelper.sin(living.rotationPitch / 180.0F * 3.1415927F) * f;
  91.  
  92.  
  93. }
  94.  
  95.  
  96.  
  97. public boolean hasAttachment(ItemStack stack, String name)
  98. {
  99. for (ItemAttachment attach : getAttachments(stack)) {
  100. if (attach.getName().equals(name)) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106.  
  107. public ItemAttachment[] getAttachments(ItemStack stack)
  108. {
  109. ArrayList<ItemAttachment> arr = new ArrayList();
  110. if ((stack != null) && (stack.stackTagCompound != null))
  111. {
  112. NBTTagList list = stack.stackTagCompound.getTagList("Attachments", 8);
  113. for (int i = 0; i < list.tagCount(); i++)
  114. {
  115. String att = list.getStringTagAt(i);
  116. if (att != null)
  117. {
  118. ItemAttachment attach = MainRegistry.content().getAttachmentFromName(att);
  119. if (attach != null) {
  120. arr.add(attach);
  121. }
  122. }
  123. }
  124. }
  125. return (ItemAttachment[])arr.toArray(new ItemAttachment[0]);
  126. }
  127.  
  128. public boolean hasAmmo(ItemStack stack)
  129. {
  130. NBTTagCompound nbt = stack.stackTagCompound != null ? stack.stackTagCompound : new NBTTagCompound();
  131. if (!nbt.hasKey("remainingAmmo"))
  132. {
  133. nbt.setInteger("remainingAmmo", 0);
  134. stack.stackTagCompound = nbt;
  135. }
  136. return stack.stackTagCompound.getInteger("remainingAmmo") > 0;
  137. }
  138.  
  139.  
  140.  
  141. @SideOnly(Side.CLIENT)
  142. public abstract ModelBase getRendererModel();
  143.  
  144. public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
  145. {
  146. if ((!par3EntityPlayer.capabilities.isCreativeMode) && (par1ItemStack.stackTagCompound != null) && (par1ItemStack.stackTagCompound.getInteger("remainingAmmo") <= 0) && (!par3EntityPlayer.inventory.consumeInventoryItem(getMagazineItem()))) {
  147. return par1ItemStack;
  148. }
  149. par3EntityPlayer.setItemInUse(par1ItemStack, getMaxItemUseDuration(par1ItemStack));
  150.  
  151. return par1ItemStack;
  152. }
  153.  
  154. public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5)
  155. {
  156. if (par1ItemStack.stackTagCompound != null)
  157. {
  158. NBTTagCompound nbt = par1ItemStack.stackTagCompound;
  159. if (nbt.getInteger("fireCooldown") > 0)
  160. {
  161. nbt.setInteger("fireCooldown", nbt.getInteger("fireCooldown") - 1);
  162. par1ItemStack.stackTagCompound = nbt;
  163.  
  164. return;
  165. }
  166. }
  167. MainRegistry.proxy.fireGun(par1ItemStack, par2World, par3Entity, this);
  168. }
  169.  
  170. public void fireGun(EntityPlayer player, ItemStack stack, World world)
  171. {
  172. if ((!world.isRemote) && (stack != null))
  173. {
  174. boolean silencerAttached = hasAttachment(stack, MainRegistry.content().silencer.getName());
  175. String sound = silencerAttached ? getSilencedSound() : getFireSound();
  176. if (sound != null) {
  177. world.playSoundAtEntity(player, sound, silencerAttached ? 0.5F : 1.0F, 1.0F);
  178. }
  179. onGunFired(stack, world, player);
  180. performRecoilAnimation(stack, player);
  181.  
  182. NBTTagCompound nbt = stack.stackTagCompound != null ? stack.stackTagCompound : new NBTTagCompound();
  183. nbt.setInteger("fireCooldown", getFireRate());
  184.  
  185. consumeAmmo(player, nbt);
  186.  
  187. stack.stackTagCompound = nbt;
  188. }
  189. }
  190.  
  191. public void consumeAmmo(EntityPlayer player, NBTTagCompound nbt)
  192. {
  193. if (!player.capabilities.isCreativeMode) {
  194. nbt.setInteger("remainingAmmo", nbt.getInteger("remainingAmmo") - 1);
  195. }
  196. if (nbt.getInteger("remainingAmmo") < 0) {
  197. nbt.setInteger("remainingAmmo", 0);
  198. }
  199. }
  200.  
  201. public void performRecoilAnimation(ItemStack stack, EntityPlayer player)
  202. {
  203. if ((stack != null) && (stack.getItem() == MainRegistry.content().crossbow)) {
  204. return;
  205. }
  206. float power = getRecoilPower();
  207. if (player.rotationPitch > -90.0F) {
  208. player.rotationPitch -= power;
  209. }
  210. }
  211.  
  212. public boolean canUseScope(ItemStack stack)
  213. {
  214. if (hasAttachment(stack, MainRegistry.content().ironSights.getName())) {
  215. return true;
  216. }
  217. return false;
  218. }
  219.  
  220. public boolean canContinueFiring(EntityPlayer player)
  221. {
  222. if ((player.capabilities.isCreativeMode) || (hasAmmo(player.getHeldItem()))) {
  223. return true;
  224. }
  225. return false;
  226. }
  227.  
  228. public ItemStack onEaten(ItemStack is, World world, EntityPlayer player)
  229. {
  230. return is;
  231. }
  232.  
  233. public int getMaxItemUseDuration(ItemStack is)
  234. {
  235. return 7200000;
  236. }
  237.  
  238. @SideOnly(Side.CLIENT)
  239. public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
  240. {
  241. par3List.add(StatCollector.translateToLocalFormatted("gun.info.type", new Object[] { getGunTypeDescription() }));
  242. if (getMagazineItem() != null) {
  243. par3List.add(StatCollector.translateToLocalFormatted("gun.info.ammo", new Object[] { StatCollector.translateToLocal(getMagazineItem().getUnlocalizedName() + ".name") }));
  244. }
  245. ItemAttachment[] attachments = getAttachments(par1ItemStack);
  246. if (attachments.length > 0)
  247. {
  248. par3List.add(StatCollector.translateToLocal("item.daystomine.gun.attachlist"));
  249. for (ItemAttachment attach : attachments) {
  250. par3List.add(StatCollector.translateToLocal(attach.getUnlocalizedName() + ".name"));
  251. }
  252. }
  253. }
  254.  
  255.  
  256.  
  257. public float getRecoilPower()
  258. {
  259. return 0.7F;
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment