Posted August 24, 20223 yr Overrides the onContentChanged method. When I put an item, it calls the apply method, but I would like to call remove when I take item out of the container, but I don't think onContentChanged will let me do that.
August 25, 20223 yr Could you please provide a useful context? Which exact method are you talking about? Where is the method located? Please post your code.
August 25, 20223 yr Author public class ItemStackHandlerMod extends ItemStackHandler { private Player player; public ItemStackHandlerMod(int size) { super(size); } public ItemStackHandlerMod(int size, Player player) { super(size); this.player = player; } @Override protected void onContentsChanged(int slot) { if(player != null){ if(getStackInSlot(slot).getItem() instanceof BaseAmulet amulet){ amulet.apply(player); } } } } public class HealthAmulet extends BaseAmulet { public static final UUID HEALTH = UUID.fromString("c2d8616a-c58a-4d99-aec9-faad03858302"); public HealthAmulet(Properties pProperties) { super(pProperties); } @Override public void apply(Player player) { AttributeInstance instance = player.getAttribute(Attributes.MAX_HEALTH); instance.addPermanentModifier(new AttributeModifier(HEALTH, "Health Amulet", 5, AttributeModifier.Operation.ADDITION)); } @Override public void remove(Player player) { AttributeInstance instance = player.getAttribute(Attributes.MAX_HEALTH); instance.removeModifier(HEALTH); } } 6 hours ago, Luis_ST said: Could you please provide a useful context? Which exact method are you talking about? Where is the method located? Please post your code.
August 25, 20223 yr ItemStackHandler#onContentsChanged is called whenever a Item is added (via #insertItem), a Item is removed (via #extractItem) or a Item is set (via #setStackInSlot). You should be able to run code after the Item is removed, by checking in #onContentsChanged if the Slot is empty or if there is a other Item in it.
August 25, 20223 yr Author 42 minutes ago, Luis_ST said: ItemStackHandler#onContentsChanged is called whenever a Item is added (via #insertItem), a Item is removed (via #extractItem) or a Item is set (via #setStackInSlot). You should be able to run code after the Item is removed, by checking in #onContentsChanged if the Slot is empty or if there is a other Item in it. I don't think it will work. I want to do it in a similar way to the inventory when I put my boots on, the attributes from it are added to the player when I take them off they are removed.
August 25, 20223 yr 21 minutes ago, Squander said: I don't think it will work. Why did you think that, what did you tried to test if this would work or not?
August 25, 20223 yr Author I'm not sure if I did it right if(player != null){ if(getStackInSlot(slot).isEmpty()){ ((BaseAmulet)getStackInSlot(slot).getItem()).remove(player); } if(getStackInSlot(slot).getItem() instanceof BaseAmulet amulet){ amulet.apply(player); } }
August 25, 20223 yr Why doesn't something like this work? WARNING: This code is not tested, it just shows the idea. And see the comment in the code. @Override public void setStackInSlot(int slot, @NotNull ItemStack stack) { ItemStack previous = getStackInSlot(slot); super.setStackInSlot(slot, stack); // Probably needs some check here to see if "previous" and "stack" are the same item? // i.e. item swap of two different amulets onRemoved(previous); } @Override public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) { ItemStack extracted = super.extractItem(slot, amount, simulate); if (!simulate) { onRemoved(extracted); } return extracted; } protected void onRemoved(ItemStack itemStack) { // Your code here } Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
August 25, 20223 yr Note: That code above only works if the item stacks are always amount=1 like armor or tools. It doesn't take into account removing part of stacked items. Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
August 25, 20223 yr Author 1 hour ago, warjort said: Note: That code above only works if the item stacks are always amount=1 like armor or tools. It doesn't take into account removing part of stacked items. Ok it works, but i don't know how i can prevent amulet of the same kind from being added to a container
August 25, 20223 yr Quote Ok it works, but i don't know how i can prevent amulet of the same kind from being added to a container One way would be to check if the stacks are the same (same item). Then skip the onRemove() call. You might want a different check if you have custom nbt or something that means just checking the item is not enough. if (!previous.isSame(itemStack)) { onRemoved(previous); } Edited August 25, 20223 yr by warjort Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
August 25, 20223 yr Author 50 minutes ago, warjort said: One way would be to check if the stacks are the same (same item). Then skip the onRemove() call. You might want a different check if you have custom nbt or something that means just checking the item is not enough. if (!previous.isSame(itemStack)) { onRemoved(previous); } i did something like this and it works too. @Override public boolean isItemValid(int slot, @NotNull ItemStack stack) { for (int i = 0; i < getSlots(); i++) { ItemStack stack1 = getStackInSlot(i); if(stack.getItem() == stack1.getItem()){ return false; } } return super.isItemValid(slot, stack); }
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.