Skip to content

Commit

Permalink
Properly filter curios inserted directly through item handlers, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIllusiveC4 committed Aug 31, 2023
1 parent e237fae commit 8d1ed1e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,18 @@ public CurioStacksHandler(ICuriosItemHandler itemHandler, String identifier, int
this.identifier = identifier;
this.canToggleRender = canToggleRender;
this.dropRule = dropRule;
this.stackHandler = new DynamicStackHandler(size);
this.cosmeticStackHandler = new DynamicStackHandler(size);
this.stackHandler = new DynamicStackHandler(size, (slot, stack) -> {
NonNullList<Boolean> list = getRenders();
SlotContext ctx = new SlotContext(identifier, itemHandler.getWearer(), slot, false,
list.size() > slot && list.get(slot));
return CuriosApi.isStackValid(ctx, stack);
});
this.cosmeticStackHandler = new DynamicStackHandler(size, (slot, stack) -> {
NonNullList<Boolean> list = getRenders();
SlotContext ctx = new SlotContext(identifier, itemHandler.getWearer(), slot, true,
list.size() > slot && list.get(slot));
return CuriosApi.isStackValid(ctx, stack);
});
this.renderHandler = NonNullList.withSize(size, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@

package top.theillusivec4.curios.common.inventory;

import java.util.function.BiPredicate;
import javax.annotation.Nonnull;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.ItemStackHandler;
import top.theillusivec4.curios.api.type.inventory.IDynamicStackHandler;

public class DynamicStackHandler extends ItemStackHandler implements IDynamicStackHandler {

protected NonNullList<ItemStack> previousStacks;
protected BiPredicate<Integer, ItemStack> validator;

public DynamicStackHandler(int size) {
public DynamicStackHandler(int size, BiPredicate<Integer, ItemStack> validator) {
super(size);
this.previousStacks = NonNullList.withSize(size, ItemStack.EMPTY);
this.validator = validator;
}

@Override
Expand All @@ -41,18 +44,18 @@ public void setPreviousStackInSlot(int slot, @Nonnull ItemStack stack) {
this.onContentsChanged(slot);
}

@Override
public int getSlots() {
return stacks.size();
}

@Nonnull
@Override
public ItemStack getPreviousStackInSlot(int slot) {
this.validateSlotIndex(slot);
return this.previousStacks.get(slot);
}

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return this.validator.test(slot, stack);
}

@Override
public void grow(int amount) {
this.stacks = getResizedList(this.stacks.size() + amount, this.stacks);
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/top/theillusivec4/curiostest/CuriosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@

package top.theillusivec4.curiostest;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.CreativeModeTabs;
Expand All @@ -39,7 +37,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.theillusivec4.curios.api.SlotTypeMessage;
import top.theillusivec4.curios.api.SlotTypePreset;
import top.theillusivec4.curios.api.client.CuriosRendererRegistry;
import top.theillusivec4.curios.api.event.CurioAttributeModifierEvent;
import top.theillusivec4.curiostest.client.CuriosLayerDefinitions;
Expand Down

0 comments on commit 8d1ed1e

Please sign in to comment.