- 1,031
- 19
- 87
Java:
public class OrchiteLauncher extends ItemBow {
public OrchiteLauncher() {
this.setRegistryName("orchite_launcher");
this.maxStackSize = 1;
this.setMaxDamage(99);
this.setCreativeTab(CreativeTabs.COMBAT);
this.addPropertyOverride(new ResourceLocation("orchite_pull"), new IItemPropertyGetter() {
public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
if (entityIn == null) {
return 0.0F;
}
else {
return entityIn.getActiveItemStack().getItem() != RegisterItem.ORCHITE_LAUNCHER ? 0.0F : (float)(stack.getMaxItemUseDuration() - entityIn.getItemInUseCount()) / 20.0F;
}
}
});
this.addPropertyOverride(new ResourceLocation("orchite_pulling"), new IItemPropertyGetter()
{
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 ItemStack.EMPTY;
}
}
protected boolean isArrow(ItemStack stack)
{
return stack.getItem() instanceof ItemArrow;
}
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, i, !itemstack.isEmpty() || flag);
if (i < 0) return;
if (!itemstack.isEmpty() || flag)
{
if (itemstack.isEmpty())
{
itemstack = new ItemStack(RegisterItem.ORCHITE_SPIKE);
}
float f = getArrowVelocity(i);
if ((double)f >= 0.1D)
{
boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ItemArrow && ((ItemArrow) itemstack.getItem()).isInfinite(itemstack, stack, entityplayer));
if (!worldIn.isRemote)
{
ItemArrow itemarrow = (ItemArrow)(itemstack.getItem() instanceof ItemArrow ? itemstack.getItem() : RegisterItem.ORCHITE_SPIKE);
EntityArrow entityarrow = itemarrow.createArrow(worldIn, itemstack, entityplayer);
entityarrow.shoot(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 || entityplayer.capabilities.isCreativeMode && (itemstack.getItem() == RegisterItem.ORCHITE_SPIKE || itemstack.getItem() == RegisterItem.ORCHITE_SPIKE))
{
entityarrow.pickupStatus = EntityArrow.PickupStatus.CREATIVE_ONLY;
}
worldIn.spawnEntity(entityarrow);
}
worldIn.playSound((EntityPlayer)null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.BLOCK_ENCHANTMENT_TABLE_USE, SoundCategory.PLAYERS, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);
if (!flag1 && !entityplayer.capabilities.isCreativeMode)
{
itemstack.shrink(1);
if (itemstack.isEmpty())
{
entityplayer.inventory.deleteStack(itemstack);
}
}
entityplayer.addStat(StatList.getObjectUseStats(this));
}
}
}
}
/**
* Gets the velocity of the arrow entity from the bow's charge
*/
public static float getArrowVelocity(int charge)
{
float f = (float)charge / 20.0F;
f = (f * f + f * 2.0F) / 3.0F;
if (f > 1.0F)
{
f = 1.0F;
}
return f;
}
/**
* How long it takes to use or consume an item
*/
public int getMaxItemUseDuration(ItemStack stack)
{
return 72000;
}
/**
* returns the action that specifies what animation to play when the items is being used
*/
public EnumAction getItemUseAction(ItemStack stack)
{
return RegisterEnumAction.LAUNCHER;
}
/**
* Called when the equipped item is right clicked.
*/
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
{
ItemStack itemstack = playerIn.getHeldItem(handIn);
boolean flag = !this.findAmmo(playerIn).isEmpty();
ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, worldIn, playerIn, handIn, flag);
if (ret != null) return ret;
if (!playerIn.capabilities.isCreativeMode && !flag)
{
return flag ? new ActionResult<ItemStack>(EnumActionResult.PASS, itemstack) : new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
}
else
{
playerIn.setActiveHand(handIn);
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
}
}
/**
* Return the enchantability factor of the item, most of the time is based on material.
*/
public int getItemEnchantability()
{
return 1;
}
}