middangeardim Posted May 3, 2021 Posted May 3, 2021 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 Quote
eggpasta Posted May 3, 2021 Posted May 3, 2021 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 Quote
middangeardim Posted May 3, 2021 Author Posted May 3, 2021 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); } } Quote
eggpasta Posted May 3, 2021 Posted May 3, 2021 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_) { } Quote
poopoodice Posted May 3, 2021 Posted May 3, 2021 9 minutes ago, middangeardim said: itemStack.damageItem(1,player,null); return new ItemStack(Items.BUCKET); 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. Quote
middangeardim Posted May 3, 2021 Author Posted May 3, 2021 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 Quote
middangeardim Posted May 3, 2021 Author Posted May 3, 2021 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? Quote
eggpasta Posted May 3, 2021 Posted May 3, 2021 49 minutes ago, middangeardim said: no I mean, I want use this item to recipe another thing, and when recipe success,this item will reduce 1 durablity So you don't want to get the playerEntity? Quote
eggpasta Posted May 3, 2021 Posted May 3, 2021 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 Quote
middangeardim Posted May 3, 2021 Author Posted May 3, 2021 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 Quote
poopoodice Posted May 3, 2021 Posted May 3, 2021 Or just call ItemStack#setDamage since ItemStack#damageItem is more likely to be used on other purposes. Quote
eggpasta Posted May 3, 2021 Posted May 3, 2021 (edited) 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 May 3, 2021 by eggpasta IncorrectCode Quote
middangeardim Posted May 4, 2021 Author Posted May 4, 2021 (edited) 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 May 4, 2021 by middangeardim Quote
Draco18s Posted May 4, 2021 Posted May 4, 2021 This does, of course, ignore the Unbreaking enchantment if your item is allowed to have it. Quote 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.
Recommended Posts
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.