Advertisement
Guest User

Untitled

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