package xjon.jum.items; import javax.annotation.Nullable; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Enchantments; import net.minecraft.init.Items; import net.minecraft.init.SoundEvents; import net.minecraft.item.EnumAction; import net.minecraft.item.IItemPropertyGetter; import net.minecraft.item.ItemArrow; import net.minecraft.item.ItemBow; import net.minecraft.item.ItemStack; import net.minecraft.stats.StatList; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundCategory; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import xjon.jum.entity.projectile.EntityUselessArrow; import xjon.jum.init.UselessItems; import xjon.jum.util.Log; public class ItemUselessBow extends ItemBow { public ItemUselessBow() { this.maxStackSize = 1; this.setMaxDamage(764); this.setFull3D(); this.addPropertyOverride(new ResourceLocation("pull"), new IItemPropertyGetter() { @SideOnly(Side.CLIENT) public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) { if (entityIn == null) { return 0.0F; } else { ItemStack itemstack = entityIn.getActiveItemStack(); return itemstack != null && itemstack.getItem() == UselessItems.useless_bow ? (float)(stack.getMaxItemUseDuration() - entityIn.getItemInUseCount()) / 20.0F : 0.0F; } } }); this.addPropertyOverride(new ResourceLocation("pulling"), new IItemPropertyGetter() { @SideOnly(Side.CLIENT) public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) { return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F; } }); } private ItemStack findAmmo(EntityPlayer player) { if (this.isArrow(player.getHeldItem(EnumHand.OFF_HAND))) { return player.getHeldItem(EnumHand.OFF_HAND); } else if (this.isArrow(player.getHeldItem(EnumHand.MAIN_HAND))) { return player.getHeldItem(EnumHand.MAIN_HAND); } else { for (int i = 0; i < player.inventory.getSizeInventory(); ++i) { ItemStack itemstack = player.inventory.getStackInSlot(i); if (this.isArrow(itemstack)) { return itemstack; } } return null; } } protected boolean isArrow(@Nullable ItemStack stack) { boolean b = stack != null && stack.getItem() instanceof ItemUselessArrow; return b; } @Override public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) { if (entityLiving instanceof EntityPlayer) { EntityPlayer entityplayer = (EntityPlayer)entityLiving; boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0; ItemStack itemstack = this.findAmmo(entityplayer); int i = this.getMaxItemUseDuration(stack) - timeLeft; i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, (EntityPlayer)entityLiving, i, itemstack != null || flag); if (i < 0) return; if (itemstack != null || flag) { if (itemstack == null) { itemstack = new ItemStack(UselessItems.useless_arrow); } float f = getArrowVelocity(i); if ((double)f >= 0.1D) { boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ItemUselessArrow ? ((ItemUselessArrow)itemstack.getItem()).isInfinite(itemstack, stack, entityplayer) : false); if (!worldIn.isRemote) { ItemUselessArrow itemarrow = (ItemUselessArrow)((ItemUselessArrow)(itemstack.getItem() instanceof ItemUselessArrow ? itemstack.getItem() : UselessItems.useless_arrow)); EntityUselessArrow entityarrow = itemarrow.createArrow(worldIn, itemstack, entityplayer); entityarrow.setAim(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, f * 3.0F, 1.0F); if (f == 1.0F) { entityarrow.setIsCritical(true); } int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, stack); if (j > 0) { entityarrow.setDamage(entityarrow.getDamage() + (double)j * 0.5D + 0.5D); } int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, stack); if (k > 0) { entityarrow.setKnockbackStrength(k); } if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, stack) > 0) { entityarrow.setFire(100); } stack.damageItem(1, entityplayer); if (flag1) { entityarrow.pickupStatus = EntityUselessArrow.PickupStatus.CREATIVE_ONLY; } worldIn.spawnEntityInWorld(entityarrow); } worldIn.playSound((EntityPlayer)null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.NEUTRAL, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F); if (!flag1) { --itemstack.stackSize; if (itemstack.stackSize == 0) { entityplayer.inventory.deleteStack(itemstack); } } entityplayer.addStat(StatList.getObjectUseStats(this)); } } } } @Override public int getItemEnchantability() { return 10; } }