Advertisement
Guest User

Untitled

a guest
Mar 5th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.00 KB | None | 0 0
  1. package net.theviolentsquirrels.questsystem.item;
  2.  
  3. import net.minecraft.enchantment.Enchantment;
  4. import net.minecraft.enchantment.EnchantmentHelper;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.projectile.EntityArrow;
  7. import net.minecraft.init.Items;
  8. import net.minecraft.item.EnumAction;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemArmor;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.stats.StatList;
  13. import net.minecraft.world.World;
  14. import net.minecraftforge.common.MinecraftForge;
  15. import net.minecraftforge.event.entity.player.ArrowLooseEvent;
  16. import net.minecraftforge.event.entity.player.ArrowNockEvent;
  17. import net.minecraftforge.fml.relauncher.Side;
  18. import net.minecraftforge.fml.relauncher.SideOnly;
  19. import net.theviolentsquirrels.questsystem.Main;
  20.  
  21. public class                        ItemBow extends Item {
  22.     public static final String[]    bowPullIconNameArray = new String[] { "questsystem:pulling_0", "questsystem:pulling_1", "questsystem:pulling_2" };
  23.  
  24.     private final int               renderIndex;
  25.     private final float             damageCoefficient;
  26.     private final float             velocityCoefficient;
  27.     private final BowMaterial       material;
  28.  
  29.     public                          ItemBow(String unlocalizedName, BowMaterial material, int renderIndex) {
  30.         this.setUnlocalizedName(unlocalizedName);
  31.         this.setMaxStackSize(1);
  32.         this.setMaxDamage(material.getBaseDurability());
  33.         this.setCreativeTab(Main.tab);
  34.         this.renderIndex = renderIndex;
  35.         this.damageCoefficient = material.getDamageCoefficient();
  36.         this.velocityCoefficient = material.getVelocityCoefficient();
  37.         this.material = material;
  38.     }
  39.  
  40.     /**
  41.      * Called when the player stops using an Item (stops holding the right mouse button).
  42.      */
  43.     @Override
  44.     public void                     onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityPlayer playerIn, int timeLeft) {
  45.         boolean                     creativeflag;
  46.  
  47.         creativeflag = playerIn.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, stack) > 0;
  48.         if (creativeflag || playerIn.inventory.hasItem(Items.arrow)) {
  49.             int                     bowCharge = this.getMaxItemUseDuration(stack) - timeLeft;
  50.             ArrowLooseEvent         event = new ArrowLooseEvent(playerIn, stack, bowCharge);
  51.  
  52.             if (MinecraftForge.EVENT_BUS.post(event)) return;
  53.  
  54.             bowCharge = event.charge;
  55.             float                   velocity = (float) bowCharge / 20.0F;
  56.  
  57.             velocity = (velocity * velocity + velocity * 2.0F) / 3.0F;
  58.             if ((double) velocity < 0.1D) return;
  59.             if (velocity > 1.0F) velocity = 1.0F;
  60.  
  61.             EntityArrow             entityarrow = new EntityArrow(worldIn, playerIn, velocity * 2.0F);
  62.  
  63.             if (velocity == 1.0F) entityarrow.setIsCritical(true);
  64.  
  65.             int                     powerEnchantmentLevel = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, stack);
  66.  
  67.             if (powerEnchantmentLevel > 0)
  68.                 entityarrow.setDamage(entityarrow.getDamage() + (double) powerEnchantmentLevel * 0.5D + 0.5D);
  69.  
  70.             int                     punchEnchantmentLevel = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, stack);
  71.  
  72.             if (punchEnchantmentLevel > 0)
  73.                 entityarrow.setKnockbackStrength(punchEnchantmentLevel);
  74.  
  75.             if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, stack) > 0)
  76.                 entityarrow.setFire(100);
  77.  
  78.             stack.damageItem(1, playerIn);
  79.             worldIn.playSoundAtEntity(playerIn, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + velocity * 0.5F);
  80.  
  81.             if (creativeflag)
  82.                 entityarrow.canBePickedUp = 2;
  83.             else
  84.                 playerIn.inventory.consumeInventoryItem(Items.arrow);
  85.             playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this)]);
  86.  
  87.             if (!worldIn.isRemote)
  88.                 worldIn.spawnEntityInWorld(entityarrow);
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Called when the player finishes using this Item (E.g. finishes eating.). Not called when the player stops using
  94.      * the Item before the action is complete.
  95.      */
  96.     @Override
  97.     public ItemStack                onItemUseFinish(ItemStack stack, World worldIn, EntityPlayer playerIn) {
  98.         return (stack);
  99.     }
  100.  
  101.     /**
  102.      * How long it takes to use or consume an item
  103.      */
  104.     @Override
  105.     public int                      getMaxItemUseDuration(ItemStack stack) {
  106.         return (72000);
  107.     }
  108.  
  109.     /**
  110.      * returns the action that specifies what animation to play when the items is being used
  111.      */
  112.     @Override
  113.     public EnumAction               getItemUseAction(ItemStack stack) {
  114.         return (EnumAction.BOW);
  115.     }
  116.  
  117.     /**
  118.      * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
  119.      */
  120.     @Override
  121.     public ItemStack                onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn) {
  122.         ArrowNockEvent              event = new ArrowNockEvent(playerIn, itemStackIn);
  123.  
  124.         if (MinecraftForge.EVENT_BUS.post(event)) return (event.result);
  125.  
  126.         if (playerIn.capabilities.isCreativeMode || playerIn.inventory.hasItem(Items.arrow))
  127.             playerIn.setItemInUse(itemStackIn, this.getMaxItemUseDuration(itemStackIn));
  128.         return (itemStackIn);
  129.     }
  130.  
  131.     /**
  132.      * Return the enchantability factor of the item, most of the time is based on material.
  133.      */
  134.     @Override
  135.     public int                      getItemEnchantability() {
  136.         return (1);
  137.     }
  138.  
  139.     /**
  140.      * Return whether this item is repairable in an anvil.
  141.      * TVS-NOTE : taken from {@link net.minecraft.item.ItemBow#getIsRepairable(ItemStack, ItemStack)} !
  142.      */
  143.     @Override
  144.     public boolean                  getIsRepairable(ItemStack toRepair, ItemStack repair) {
  145.         return (this.material.getRepairItem() == repair.getItem() || super.getIsRepairable(toRepair, repair));
  146.     }
  147.  
  148.     public enum                     BowMaterial {
  149.         BASE("wood", 3, 0.4f, 2),
  150.         IRON("iron", 4, 0.7f, 2),
  151.         GOLD("gold", 5, 1.0f, 2),
  152.         DIAMOND("diamond", 6, 1.3f, 2);
  153.  
  154.         private final String        materialName;
  155.         private final int           baseDurability;
  156.         private final float         damageCoefficient;
  157.         private final float         velocityCoefficient;
  158.  
  159.         public Item                 customCraftingMaterial = null;
  160.  
  161.         BowMaterial(String name, int durability, float damageCoeff, int velocity) {
  162.             this.materialName = name;
  163.             this.baseDurability = durability;
  164.             this.damageCoefficient = damageCoeff;
  165.             this.velocityCoefficient = velocity;
  166.         }
  167.  
  168.         @SideOnly(Side.CLIENT)
  169.         public String               getName() {
  170.             return (this.materialName);
  171.         }
  172.  
  173.         public int                  getBaseDurability() {
  174.             return (this.baseDurability);
  175.         }
  176.  
  177.         public float                getVelocityCoefficient() {
  178.             return (this.velocityCoefficient);
  179.         }
  180.  
  181.         public float                getDamageCoefficient() {
  182.             return (this.damageCoefficient);
  183.         }
  184.  
  185.         /**
  186.          * Get a main crafting component of this Armor Material (example is Items.iron_ingot)
  187.          * TVS-NOTE : taken from {@link ItemArmor.ArmorMaterial#getRepairItem()} !
  188.          */
  189.         public Item                 getRepairItem() {
  190.             switch (this) {
  191.                 case IRON:      return (Items.iron_ingot);
  192.                 case GOLD:      return (Items.gold_ingot);
  193.                 case DIAMOND:   return (Items.diamond);
  194.                 default:        return (this.customCraftingMaterial);
  195.             }
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement