BloodMagic/src/main/java/WayofTime/bloodmagic/item/soul/ItemSentientBow.java

168 lines
6.4 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.item.soul;
import net.minecraft.enchantment.EnchantmentHelper;
2016-03-18 18:54:31 +00:00
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
2016-03-18 18:54:31 +00:00
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items;
2016-03-18 18:54:31 +00:00
import net.minecraft.init.SoundEvents;
import net.minecraft.item.IItemPropertyGetter;
2016-03-18 18:54:31 +00:00
import net.minecraft.item.ItemArrow;
import net.minecraft.item.ItemBow;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
2016-03-18 18:54:31 +00:00
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
2016-03-18 18:54:31 +00:00
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-03-18 18:54:31 +00:00
import WayofTime.bloodmagic.BloodMagic;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.registry.ModItems;
public class ItemSentientBow extends ItemBow
{
public ItemSentientBow()
{
super();
setUnlocalizedName(Constants.Mod.MODID + ".sentientBow");
setRegistryName(Constants.BloodMagicItem.SENTIENT_BOW.getRegName());
setCreativeTab(BloodMagic.tabBloodMagic);
this.addPropertyOverride(new ResourceLocation("pull"), new IItemPropertyGetter()
{
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack, World worldIn, EntityLivingBase entityIn)
{
if (entityIn == null)
{
return 0.0F;
} else
{
ItemStack itemstack = entityIn.getActiveItemStack();
return itemstack != null && itemstack.getItem() == ModItems.sentientBow ? (float) (stack.getMaxItemUseDuration() - entityIn.getItemInUseCount()) / 20.0F : 0.0F;
}
}
});
this.addPropertyOverride(new ResourceLocation("pulling"), new IItemPropertyGetter()
{
@SideOnly(Side.CLIENT)
public float apply(ItemStack stack, World worldIn, EntityLivingBase entityIn)
{
return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;
}
});
}
@Override
2016-03-18 18:54:31 +00:00
public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
{
2016-03-18 18:54:31 +00:00
if (entityLiving instanceof EntityPlayer)
{
2016-03-18 18:54:31 +00:00
EntityPlayer entityplayer = (EntityPlayer) entityLiving;
boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.infinity, stack) > 0;
ItemStack itemstack = this.getFiredArrow(entityplayer);
int i = this.getMaxItemUseDuration(stack) - timeLeft;
2016-03-18 18:54:31 +00:00
i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, (EntityPlayer) entityLiving, i, itemstack != null || flag);
if (i < 0)
return;
2016-03-18 18:54:31 +00:00
if (itemstack != null || flag)
{
2016-03-18 18:54:31 +00:00
if (itemstack == null)
{
itemstack = new ItemStack(Items.arrow);
}
2016-03-21 19:55:36 +00:00
float arrowVelocity = getArrowVelocity(i);
2016-03-21 19:55:36 +00:00
if ((double) arrowVelocity >= 0.1D)
2016-03-18 18:54:31 +00:00
{
boolean flag1 = flag && itemstack.getItem() instanceof ItemArrow; //Forge: Fix consuming custom arrows.
2016-03-18 18:54:31 +00:00
if (!worldIn.isRemote)
{
//Need to do some stuffs
2016-03-21 19:55:36 +00:00
ItemArrow itemarrow = ((ItemArrow) (itemstack.getItem() instanceof ItemArrow ? itemstack.getItem() : Items.arrow));
EntityArrow entityarrow = itemarrow.createArrow(worldIn, itemstack, entityplayer);
entityarrow.func_184547_a(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, arrowVelocity * 3.0F, 1.0F);
2016-03-21 19:55:36 +00:00
if (arrowVelocity == 1.0F)
2016-03-18 18:54:31 +00:00
{
entityarrow.setIsCritical(true);
}
2016-03-18 18:54:31 +00:00
int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.power, stack);
2016-03-18 18:54:31 +00:00
if (j > 0)
{
entityarrow.setDamage(entityarrow.getDamage() + (double) j * 0.5D + 0.5D);
}
2016-03-18 18:54:31 +00:00
int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.punch, stack);
2016-03-18 18:54:31 +00:00
if (k > 0)
{
entityarrow.setKnockbackStrength(k);
}
2016-03-18 18:54:31 +00:00
if (EnchantmentHelper.getEnchantmentLevel(Enchantments.flame, stack) > 0)
{
entityarrow.setFire(100);
}
2016-03-18 18:54:31 +00:00
stack.damageItem(1, entityplayer);
2016-03-18 18:54:31 +00:00
if (flag1)
{
entityarrow.canBePickedUp = EntityArrow.PickupStatus.CREATIVE_ONLY;
}
2016-03-18 18:54:31 +00:00
worldIn.spawnEntityInWorld(entityarrow);
}
2016-03-21 19:55:36 +00:00
worldIn.playSound(null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.entity_arrow_shoot, SoundCategory.NEUTRAL, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + arrowVelocity * 0.5F);
2016-03-18 18:54:31 +00:00
if (!flag1)
{
--itemstack.stackSize;
if (itemstack.stackSize == 0)
{
entityplayer.inventory.deleteStack(itemstack);
}
}
2016-03-21 19:55:36 +00:00
entityplayer.addStat(StatList.getObjectUseStats(this));
2016-03-18 18:54:31 +00:00
}
}
}
}
2016-03-18 18:54:31 +00:00
protected ItemStack getFiredArrow(EntityPlayer player)
{
2016-03-21 19:55:36 +00:00
if (this.isArrow(player.getHeldItem(EnumHand.OFF_HAND)))
2016-03-18 18:54:31 +00:00
{
return player.getHeldItem(EnumHand.OFF_HAND);
2016-03-21 19:55:36 +00:00
} else if (this.isArrow(player.getHeldItem(EnumHand.MAIN_HAND)))
2016-03-18 18:54:31 +00:00
{
return player.getHeldItem(EnumHand.MAIN_HAND);
} else
{
for (int i = 0; i < player.inventory.getSizeInventory(); ++i)
{
ItemStack itemstack = player.inventory.getStackInSlot(i);
2016-03-21 19:55:36 +00:00
if (this.isArrow(itemstack))
2016-03-18 18:54:31 +00:00
{
return itemstack;
}
}
return null;
}
}
}