Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.9.4] Creating a custom bow


Bloopers
 Share

Recommended Posts

I'd like to create an item that is meant to charge up like a bow, and then shoot an enderpearl(meant to be able to shoot enderpearls really far). I've taken a look at the bow class file and I don't understand any of it...

 

Any help is appreciated.

Link to comment
Share on other sites

Override the following methods:

  • Item#onItemRightClick

    to set the item in use by calling

    EntityLivingBase#setActiveHand


  • Item#getMaxItemUseDuration

    to return the maximum use duration of your item

  • Item#getItemUseAction

    to return

    EnumAction.BOW

    so the entity draws the item back like a bow

  • Item#onPlayerStoppedUsing

    or

    Item#onItemUseFinish

    to do something (e.g. spawn a projectile entity) when the entity stops or finishes using item

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Override the following methods:

  • Item#onItemRightClick

    to set the item in use by calling

    EntityLivingBase#setActiveHand


  • Item#getMaxItemUseDuration

    to return the maximum use duration of your item

  • Item#getItemUseAction

    to return

    EnumAction.BOW

    so the entity draws the item back like a bow

  • Item#onPlayerStoppedUsing

    or

    Item#onItemUseFinish

    to do something (e.g. spawn a projectile entity) when the entity stops or finishes using item

 

Thank you for the quick response, I have this now:

 

 

package bloopers.trinkets.items;

 

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.item.EntityEnderPearl;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.SoundEvents;

import net.minecraft.item.EnumAction;

import net.minecraft.item.Item;

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.SoundCategory;

import net.minecraft.world.World;

 

public class ItemEnderRifle extends Item

{

    public ItemEnderRifle()

    {

        this.maxStackSize = 1;

        this.setMaxDamage(32);

        this.setCreativeTab(CreativeTabs.MISC);

    }

 

   

    public EnumAction getItemUseAction(ItemStack stack)

    {

        return EnumAction.BOW;

    }

   

   

   

    public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)

    {

 

            playerIn.setActiveHand(hand);

            return new ActionResult(EnumActionResult.SUCCESS, itemStackIn);

    }

   

    public void onPlayerStoppedUsing(ItemStack itemStackIn, World worldIn, EntityLivingBase entityLiving, int timeLeft, EntityPlayer playerIn)

    {

   

        if (!worldIn.isRemote)

        {

            EntityEnderPearl entityenderpearl = new EntityEnderPearl(worldIn, playerIn);

            entityenderpearl.setHeadingFromThrower(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 1.5F, 1.0F);

            worldIn.spawnEntityInWorld(entityenderpearl);

            itemStackIn.damageItem(1, playerIn);

        }

   

    }

   

}

 

 

 

But it doesn't do anything ingame(like a blank item)

Any idea what I've done wrong?

Link to comment
Share on other sites

You didn't override

Item#getMaxItemUseDuration

.

 

Side note: Always annotate override methods with

@Override

so you get a compilation error if they don't override a super method.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

You didn't override

Item#getMaxItemUseDuration

.

 

Side note: Always annotate override methods with

@Override

so you get a compilation error if they don't override a super method.

The item will charge up now like a bow, but it doesn't do anything on release, even though I have written down to throw an enderpearl in the onPlayerStoppedUsing. This is what I have now:

 

 

package bloopers.trinkets.items;

 

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.item.EntityEnderPearl;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.SoundEvents;

import net.minecraft.item.EnumAction;

import net.minecraft.item.Item;

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.SoundCategory;

import net.minecraft.world.World;

 

public class ItemEnderRifle extends Item

{

    public ItemEnderRifle()

    {

        this.maxStackSize = 1;

        this.setMaxDamage(32);

        this.setCreativeTab(CreativeTabs.MISC);

    }

 

    @Override

    public EnumAction getItemUseAction(ItemStack stack)

    {

        return EnumAction.BOW;

    }

   

    /**

    * How long it takes to use or consume an item

    */

    @Override

    public int getMaxItemUseDuration(ItemStack stack)

    {

        return 5000;

    }

   

    @Override

    public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)

    {

 

            playerIn.setActiveHand(hand);

            return new ActionResult(EnumActionResult.SUCCESS, itemStackIn);

    }

   

    public void onPlayerStoppedUsing(ItemStack itemStackIn, World worldIn, EntityLivingBase entityLiving, int timeLeft, EntityPlayer playerIn)

    {

   

            EntityEnderPearl entityenderpearl = new EntityEnderPearl(worldIn, playerIn);

            entityenderpearl.setHeadingFromThrower(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 1.5F, 1.0F);

            worldIn.spawnEntityInWorld(entityenderpearl);

            itemStackIn.damageItem(1, playerIn);

           

    }

   

}

 

 

 

I couldn't put @Override ontop of onPlayerStoppedUsing(it gave me an error and told me to remove the @Override annotation)

Link to comment
Share on other sites

I couldn't put @Override ontop of onPlayerStoppedUsing(it gave me an error and told me to remove the @Override annotation)

 

If

@Override

gives you a compilation error, it's doing its job and telling you that the method doesn't override a super method. The solution is to fix the method signature, not remove the annotation.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Didn't know what method signature was before, did a search and it told me that it's the parameters before the method like public, static and void. (is that correct?)

I tried multiple combinations of them and I'm still getting the error.

Link to comment
Share on other sites

Didn't know what method signature was before, did a search and it told me that it's the parameters before the method like public, static and void. (is that correct?)

I tried multiple combinations of them and I'm still getting the error.

 

That's not correct. This tutorial explains method overriding and method signatures.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

What why?

 

The issue was that the OP had the wrong method signature (i.e. arguments), not a missing annotation.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Didn't know what method signature was before, did a search and it told me that it's the parameters before the method like public, static and void. (is that correct?)

I tried multiple combinations of them and I'm still getting the error.

 

That's not correct. This tutorial explains method overriding and method signatures.

I'm not sure what I am supposed to do from here still, sorry to bother you again.

Link to comment
Share on other sites

I'm not sure what I am supposed to do from here still, sorry to bother you again.

 

Override the method by creating a method with the same name, return type and parameter types and annotating it with

@Override

. Your IDE should be able to do this for you.

 

It's not strictly necessary, but you should also annotate your override method with the same annotations as the super method.

 

If you've managed to override the method, post your latest code and describe what the current problem is.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I'm not sure what I am supposed to do from here still, sorry to bother you again.

 

Override the method by creating a method with the same name, return type and parameter types and annotating it with

@Override

. Your IDE should be able to do this for you.

 

It's not strictly necessary, but you should also annotate your override method with the same annotations as the super method.

 

If you've managed to override the method, post your latest code and describe what the current problem is.

It's no longer giving me an error when overriding, but when I finish charging up the game crashes. I think I know the problem too. Here's the code first:

    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
    {
    	
    	EntityEnderPearl entityenderpearl = new EntityEnderPearl(worldIn, player);
        entityenderpearl.setHeadingFromThrower(player, player.rotationPitch, player.rotationYaw, 0.0F, 1.5F, 1.0F);
        worldIn.spawnEntityInWorld(entityenderpearl);
        stack.damageItem(1, player);
    	
    }

 

So, I noticed this now, is the onPlayerStoppedUsing method supposed to return something?

    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
    {
    	
    	EntityEnderPearl entityenderpearl = new EntityEnderPearl(worldIn, player);
        entityenderpearl.setHeadingFromThrower(player, player.rotationPitch, player.rotationYaw, 0.0F, 1.5F, 1.0F);
        worldIn.spawnEntityInWorld(entityenderpearl);
        stack.damageItem(1, player);
    	
    }

 

Because onItemRightClick returns EnumActionResult.SUCCESS - I wonder if onPlayerStoppedUsing is supposed to return something.

Link to comment
Share on other sites

onPlayerStoppedUsing

is a

void

method. It doesn't return anything.

 

In the code you posted, you're referencing a

player

variable. Where is this coming from? You should use the

EntityLivingBase

argument.

 

Only create and spawn entities on the server (when

World#isRemote

is

false

).

 

In future, always post the error message (if it's a compile-time error) or the FML log (if it's a runtime error). This makes it much easier to help you.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.