Created
July 3, 2024 01:53
-
-
Save will-y/c0e35adcedfe80f149bb9de9fa266d46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.neoforged.neoforge.items; | |
import net.minecraft.world.item.ItemStack; | |
import org.jetbrains.annotations.Nullable; | |
public class SlotItemHandlerImmutable extends SlotItemHandler { | |
// Vanilla MC code modifies the stack returned by `getItem()` directly, but it | |
// calls `setChanged()` when that happens, so we just cache the returned stack, | |
// and set it when `setChanged()` is called. | |
@Nullable | |
private ItemStack cachedReturnedStack = null; | |
public SlotItemHandlerImmutable(IItemHandler itemHandler, int index, int xPosition, int yPosition) { | |
super(itemHandler, index, xPosition, yPosition); | |
} | |
@Override | |
public ItemStack getItem() { | |
return cachedReturnedStack = super.getItem(); | |
} | |
@Override | |
public final void set(ItemStack stack) { | |
super.set(stack); | |
cachedReturnedStack = stack; | |
} | |
@Override | |
public final void setChanged() { | |
if (cachedReturnedStack != null) { | |
set(cachedReturnedStack); | |
} | |
} | |
@Override | |
public final ItemStack remove(int amount) { | |
ItemStack stack = getItem().copy(); | |
ItemStack ret = stack.split(amount); | |
set(stack); | |
cachedReturnedStack = null; | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment