Jump to content

Recommended Posts

Posted

Hello! I'm relatively new to modding and Java in general, but I have the basics down and understand how items/blocks/entities/etc. work. Here I am trying to make an item that takes the item in the player's offhand itemstack and launches it as an entity (that extends from SnowballEntity), and then lands as an item on the ground. It all works except that I cannot find I way for the item entity spawned on the ground at the end to detect what item was initially launched from the player's inventory.

 

Here's my "launcher" item class;

public class xplauncher extends Item {



    public xplauncher(Properties p) {
        super(p);
    }
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        ItemStack itmsk = playerIn.getHeldItem(handIn);
        ItemStack stack = playerIn.getHeldItemOffhand();
        double lox = playerIn.getPosX();
        double loy = playerIn.getPosY();
        double loz = playerIn.getPosZ();
        xpball ball = new xpball(worldIn, lox, loy, loz);
        ball.func_234612_a_(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 4.0F, 1.5F, 1.0F);
        if(!stack.isEmpty()) {
            if(!worldIn.isRemote) {
                ball.setItem(stack);
                worldIn.addEntity(ball);
                stack.shrink(1);
            }
        }
        else
        {
            System.out.println("Nothing to shoot");
        }
        return ActionResult.func_233538_a_(itmsk, worldIn.isRemote());
    }
}

 

And here's my "projectile" class;

public class xpball extends SnowballEntity {



    ItemStack it = new ItemStack();

    public xpball(World world, double x, double y, double z) {
        super(world, x, y, z);
    }

    protected void onImpact(RayTraceResult rt) {
        super.onImpact(rt);
        if (!this.world.isRemote) {
                this.world.addEntity(new ItemEntity(this.world, this.getPosX(), this.getPosY(), this.getPosZ(), it));
            }

            this.remove();
        }

    }

 

My goal is for the "it" ItemStack in the projectile class to be somehow filled with the value of the "stack" ItemStack from the launcher class. Can anyone help me out? Constructive criticism is happily taken.

Posted (edited)
7 hours ago, Giga_5 said:

ItemStack it = new ItemStack();

This is just some random ItemStack you created that contains nothing (I am not even sure that's a valid constructor...)

 

You need to actually get the itemstack in the player's hand, which you would have to do when you create your thrown entity (and appears you are already doing):

7 hours ago, Giga_5 said:

ball.setItem(stack);

 

  

4 hours ago, Beethoven92 said:

Doesn't this#getItem() work, in place of 'it' when you need to pass the ItemStack you want? At this line:


this.world.addEntity(new ItemEntity(this.world, this.getPosX(), this.getPosY(), this.getPosZ(), it));
            

He's not creating item entities, he's creating snowball like thrown entities.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
2 hours ago, Draco18s said:

 

6 hours ago, Beethoven92 said:

Doesn't this#getItem() work, in place of 'it' when you need to pass the ItemStack you want? At this line:



this.world.addEntity(new ItemEntity(this.world, this.getPosX(), this.getPosY(), this.getPosZ(), it));
            

  He's not creating item entities, he's creating snowball like thrown entities.

 

Yeah. the thrown entity is represented by is xpball class, but he also wants to generate an item entity in the world, where his projectile hits something. Doesn't the line of code above do that? He just have to retrieve the ItemStack he needs and generate the item entity from it, unless i am missing something..

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

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