Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

Could you please provide a useful context? Which exact method are you talking about? Where is the method located?

Please post your code.

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

 

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.

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

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?

  • 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);
            }
        }

 

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.

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.

  • 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

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

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.