Jump to content

How can I get a playerEntity in the #getContainerItem method?


middangeardim

Recommended Posts

I'm a beginner of Modding, and I'm really confusing about how to get a playerEntity

here is my code

PlayerEntity player = [???]

@Override public ItemStack getContainerItem(ItemStack itemStack) { itemStack.damageItem(1,player,null); return new ItemStack(Items.BUCKET); }

 

what should I enter in the [???],I used to enter Minecraft.getInstance().player; but the game crushed because of null pointer

Link to comment
Share on other sites

53 minutes ago, middangeardim said:

I'm a beginner of Modding, and I'm really confusing about how to get a playerEntity

here is my code

PlayerEntity player = [???]

@Override public ItemStack getContainerItem(ItemStack itemStack) { itemStack.damageItem(1,player,null); return new ItemStack(Items.BUCKET); }

 

what should I enter in the [???],I used to enter Minecraft.getInstance().player; but the game crushed because of null pointer

Show the whole code or none can help

Link to comment
Share on other sites

4 minutes ago, eggpasta said:

Show the whole code or none can help

sorry about that.

I want to make a item which will damage when reciping

public class carrot_jam extends TieredItem {

    public carrot_jam() {
        super(ModItemTier.PUMPKINS,new Item.Properties().group(ModGroup.toolsGroup));
        this.getItemStackTileEntityRenderer()
    }
    

    @Override
    public boolean hasContainerItem(ItemStack itemStack) {
        return true;
    }
    
//here is the problem
    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        itemStack.damageItem(1,player,null);
        return new ItemStack(Items.BUCKET);
    }

}

 

Link to comment
Share on other sites

3 minutes ago, middangeardim said:

sorry about that.

I want to make a item which will damage when reciping


public class carrot_jam extends TieredItem {

    public carrot_jam() {
        super(ModItemTier.PUMPKINS,new Item.Properties().group(ModGroup.toolsGroup));
        this.getItemStackTileEntityRenderer()
    }
    

    @Override
    public boolean hasContainerItem(ItemStack itemStack) {
        return true;
    }
    
//here is the problem
    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        itemStack.damageItem(1,player,null);
        return new ItemStack(Items.BUCKET);
    }

}
 

 

So you want them to get damaged when they craft it ?

if so override

 public void onCraftedBy(ItemStack p_77622_1_, World p_77622_2_, PlayerEntity p_77622_3_) {
   }

 

Link to comment
Share on other sites

7 minutes ago, eggpasta said:

So you want them to get damaged when they craft it ?

if so override


 public void onCraftedBy(ItemStack p_77622_1_, World p_77622_2_, PlayerEntity p_77622_3_) {
   }

 

no I mean, I want use this item to recipe another thing, and when recipe success,this item will reduce 1 durablity

Link to comment
Share on other sites

3 minutes ago, poopoodice said:

This does not really make sense as you are damaging the carrot jam stack, and then replace it with a bucket in the crafting table.

well...I want to return a bucket after the carrot jam used up, need I add an "if" to check whether the item was used up? Which method should I use to check?

Link to comment
Share on other sites

47 minutes ago, middangeardim said:

well...I want to return a bucket after the carrot jam used up, need I add an "if" to check whether the item was used up? Which method should I use to check?

So you want to use an Item(Carrot Jam) In a crafting recipe and then it loses 1 durability?

If so what is the crafting recipe?

and you would have to add the carrot jam to the players inventory because a vanilla crafting recipe can't return 2 items

Link to comment
Share on other sites

2 minutes ago, eggpasta said:

So you want to use an Item(Carrot Jam) In a crafting recipe and then it loses 1 durability?

If so what is the crafting recipe?

and you would have to add the carrot jam to the players inventory because a vanilla crafting recipe can't return 2 items

for example,the recipe is carrot jam + bread = carrot jam bread

it will recipe a carrot jam bread and return me a carrot jam which reduce 1 reduablity

I need to use itemStack.damageItem(count,LivingEntity,null) to do it,but i have no idea how to get a playerEntity in this method

Link to comment
Share on other sites

1 hour ago, middangeardim said:

sorry about that.

I want to make a item which will damage when reciping



public class carrot_jam extends TieredItem {

    public carrot_jam() {
        super(ModItemTier.PUMPKINS,new Item.Properties().group(ModGroup.toolsGroup));
        this.getItemStackTileEntityRenderer()
    }
    

    @Override
    public boolean hasContainerItem(ItemStack itemStack) {
        return true;
    }
    
//here is the problem
    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        itemStack.damageItem(1,player,null);
        return new ItemStack(Items.BUCKET);
    }

}
 

 

You would want to set a default durability for the item(amount of uses) and also have you tried using null in the place of player you could try :

        @Override
        public ItemStack getContainerItem(ItemStack itemStack) {
            itemStack.getItem().damageItem(itemStack, 1, null,null)
            return new ItemStack(Items.BUCKET);
        }

Or try what poopoodice just suggested

Edited by eggpasta
IncorrectCode
Link to comment
Share on other sites

43 minutes ago, eggpasta said:

You would want to set a default durability for the item(amount of uses) and also have you tried using null in the place of player you could try :



        @Override
        public ItemStack getContainerItem(ItemStack itemStack) {
            itemStack.getItem().damageItem(itemStack, 1, null,null)
            return new ItemStack(Items.BUCKET);
        }

Or try what poopoodice just suggested

well....it doesn't work,but I success finally using the setDamge(),here is the final code

    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {

        if(itemStack.getDamage() < itemStack.getMaxDamage()){
            ItemStack item = itemStack.copy();
            item.setDamage(item.getDamage() + 1);
            return item;
        }

        return new ItemStack(Items.BUCKET);
    }

 

by the way, thank you very much, both you and poopoodice😀

Edited by middangeardim
Link to comment
Share on other sites

This does, of course, ignore the Unbreaking enchantment if your item is allowed to have it.

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.

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