diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index ea9cad0e7..b5404e54c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -49,6 +49,8 @@ import dev.latvian.mods.kubejs.block.custom.WallBlockBuilder; import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentRegistry; import dev.latvian.mods.kubejs.block.entity.CustomCapabilityAttachment; +import dev.latvian.mods.kubejs.block.entity.EnergyStorageAttachment; +import dev.latvian.mods.kubejs.block.entity.FluidTankAttachment; import dev.latvian.mods.kubejs.block.entity.InventoryAttachment; import dev.latvian.mods.kubejs.block.state.BlockStatePredicate; import dev.latvian.mods.kubejs.color.KubeColor; @@ -707,6 +709,8 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) { public void registerBlockEntityAttachments(BlockEntityAttachmentRegistry registry) { registry.register(CustomCapabilityAttachment.TYPE); registry.register(InventoryAttachment.TYPE); + registry.register(FluidTankAttachment.TYPE); + registry.register(EnergyStorageAttachment.TYPE); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java index 7ba7c524e..3e163c600 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java @@ -1,12 +1,15 @@ package dev.latvian.mods.kubejs.block.entity; +import net.minecraft.core.HolderLookup; +import net.minecraft.nbt.Tag; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.block.state.BlockState; import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.common.util.INBTSerializable; import org.jetbrains.annotations.Nullable; public interface BlockEntityAttachment { - default Object getExposedObject() { + default Object getWrappedObject() { return this; } @@ -15,9 +18,24 @@ default CAP getCapability(BlockCapability capability) { return null; } + @Nullable + default Tag serialize(HolderLookup.Provider registries) { + if (getWrappedObject() instanceof INBTSerializable s) { + return s.serializeNBT(registries); + } + + return null; + } + + default void deserialize(HolderLookup.Provider registries, @Nullable Tag tag) { + if (tag != null && getWrappedObject() instanceof INBTSerializable s) { + s.deserializeNBT(registries, tag); + } + } + default void onRemove(ServerLevel level, KubeBlockEntity blockEntity, BlockState newState) { } - default void tick() { + default void serverTick() { } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java index 2966be790..f61e9f0be 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java @@ -5,7 +5,7 @@ import java.util.List; public interface BlockEntityAttachmentFactory { - BlockEntityAttachment create(KubeBlockEntity entity); + BlockEntityAttachment create(BlockEntityAttachmentInfo info, KubeBlockEntity entity); default List> getCapabilities() { return List.of(); diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java index 0167cd41d..ced8dae53 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java @@ -6,6 +6,7 @@ import dev.latvian.mods.rhino.util.HideFromJS; import net.minecraft.core.Direction; import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; import org.jetbrains.annotations.Nullable; import java.util.Optional; @@ -41,4 +42,16 @@ default void inventory(String id, Set directions, int width, int heig default void inventory(String id, Set directions, int width, int height) { inventory(id, directions, width, height, null); } + + default void fluidTank(String id, Set directions, int capcity, @Nullable FluidIngredient inputFilter) { + attach(id, FluidTankAttachment.TYPE, directions, new FluidTankAttachment.Factory(capcity, Optional.ofNullable(inputFilter))); + } + + default void fluidTank(String id, Set directions, int capcity) { + fluidTank(id, directions, capcity, null); + } + + default void energyStorage(String id, Set directions, int capcity, int maxReceive, int maxExtract, int autoOutput) { + attach(id, EnergyStorageAttachment.TYPE, directions, new EnergyStorageAttachment.Factory(capcity, maxReceive <= 0 ? Optional.empty() : Optional.of(maxReceive), maxExtract <= 0 ? Optional.empty() : Optional.of(maxExtract), autoOutput <= 0 ? Optional.empty() : Optional.of(autoOutput))); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java index 208ee8cca..0fb5afaff 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java @@ -15,6 +15,8 @@ import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import net.neoforged.neoforge.fluids.FluidUtil; +import net.neoforged.neoforge.fluids.capability.IFluidHandler; import java.util.EnumSet; import java.util.HashMap; @@ -97,6 +99,14 @@ public void rightClickOpensInventory(String id) { }; } + public void rightClickFillsTank(String id) { + blockBuilder.rightClick = e -> { + if (e.getPlayer() instanceof ServerPlayerKJS && e.getBlock().getEntity() instanceof KubeBlockEntity entity && entity.attachments.get(id) instanceof IFluidHandler tank) { + FluidUtil.interactWithFluidHandler(e.getPlayer(), e.getHand(), tank); + } + }; + } + @HideFromJS public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { return new KubeBlockEntity(pos, state, this); diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java index e945a0c4d..dac92946a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java @@ -1,20 +1,17 @@ package dev.latvian.mods.kubejs.block.entity; -import net.minecraft.core.HolderLookup; -import net.minecraft.nbt.Tag; import net.neoforged.neoforge.capabilities.BlockCapability; -import net.neoforged.neoforge.common.util.INBTSerializable; import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.function.Supplier; -public record CustomCapabilityAttachment(BlockCapability capability, Object data) implements BlockEntityAttachment, INBTSerializable { +public record CustomCapabilityAttachment(BlockCapability capability, Object data) implements BlockEntityAttachment { public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("custom_capability", Factory.class); public record Factory(BlockCapability type, Supplier dataFactory) implements BlockEntityAttachmentFactory { @Override - public BlockEntityAttachment create(KubeBlockEntity entity) { + public BlockEntityAttachment create(BlockEntityAttachmentInfo info, KubeBlockEntity entity) { return new CustomCapabilityAttachment(type, dataFactory.get()); } @@ -25,7 +22,7 @@ public BlockEntityAttachment create(KubeBlockEntity entity) { } @Override - public Object getExposedObject() { + public Object getWrappedObject() { return data; } @@ -38,16 +35,4 @@ public CAP getCapability(BlockCapability c) { return null; } - - @Override - public Tag serializeNBT(HolderLookup.Provider registries) { - return data instanceof INBTSerializable s ? s.serializeNBT(registries) : null; - } - - @Override - public void deserializeNBT(HolderLookup.Provider registries, Tag tag) { - if (data instanceof INBTSerializable s) { - s.deserializeNBT(registries, tag); - } - } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/EnergyStorageAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/EnergyStorageAttachment.java new file mode 100644 index 000000000..10d19881a --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/EnergyStorageAttachment.java @@ -0,0 +1,126 @@ +package dev.latvian.mods.kubejs.block.entity; + +import dev.latvian.mods.kubejs.bindings.DirectionWrapper; +import net.minecraft.core.Direction; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.capabilities.Capabilities; +import net.neoforged.neoforge.energy.EnergyStorage; +import net.neoforged.neoforge.energy.IEnergyStorage; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class EnergyStorageAttachment implements BlockEntityAttachment { + public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("energy_storage", Factory.class); + + public record Factory(int capacity, Optional maxReceive, Optional maxExtract, Optional autoOutput) implements BlockEntityAttachmentFactory { + @Override + public BlockEntityAttachment create(BlockEntityAttachmentInfo info, KubeBlockEntity entity) { + int rx = Math.max(0, maxReceive.orElse(0)); + int tx = Math.max(0, maxExtract.orElse(0)); + int auto = Math.max(0, autoOutput.orElse(0)); + return new EnergyStorageAttachment(entity, capacity, rx, tx, auto, (auto > 0 ? info.directions() : DirectionWrapper.EMPTY_SET).toArray(new Direction[0])); + } + + @Override + public boolean isTicking() { + return autoOutput.isPresent() && autoOutput.get() > 0; + } + + @Override + public List> getCapabilities() { + return List.of(Capabilities.EnergyStorage.BLOCK); + } + } + + public static class Wrapped extends EnergyStorage { + private final EnergyStorageAttachment attachment; + + public Wrapped(EnergyStorageAttachment attachment, int capacity, int maxReceive, int maxExtract) { + super(capacity, maxReceive, maxExtract); + this.attachment = attachment; + } + + @Override + public int extractEnergy(int toExtract, boolean simulate) { + int s = super.extractEnergy(toExtract, simulate); + + if (s > 0 && !simulate && !attachment.entity.getLevel().isClientSide()) { + attachment.entity.save(); + } + + return s; + } + + @Override + public int receiveEnergy(int toReceive, boolean simulate) { + int s = super.receiveEnergy(toReceive, simulate); + + if (s > 0 && !simulate && !attachment.entity.getLevel().isClientSide()) { + attachment.entity.save(); + } + + return s; + } + } + + private final KubeBlockEntity entity; + public final Wrapped energyStorage; + public final int autoOutput; + public final Direction[] autoOutputDirections; + + public EnergyStorageAttachment(KubeBlockEntity entity, int capacity, int maxReceive, int maxExtract, int autoOutput, Direction[] autoOutputDirections) { + this.entity = entity; + this.energyStorage = new Wrapped(this, capacity, maxReceive, maxExtract); + this.autoOutput = autoOutput; + this.autoOutputDirections = autoOutputDirections; + } + + @Override + public Object getWrappedObject() { + return energyStorage; + } + + @Override + @Nullable + public CAP getCapability(BlockCapability capability) { + if (capability == Capabilities.EnergyStorage.BLOCK) { + return (CAP) energyStorage; + } + + return null; + } + + @Override + public void serverTick() { + if (autoOutputDirections.length > 0 && autoOutput > 0) { + var list = new ArrayList(1); + + for (var dir : autoOutputDirections) { + var c = Capabilities.EnergyStorage.BLOCK.getCapability(entity.getLevel(), entity.getBlockPos().relative(dir), null, null, dir.getOpposite()); + + if (c != null) { + list.add(c); + } + } + + if (!list.isEmpty()) { + int draw = Math.min(autoOutput, energyStorage.getEnergyStored()) / list.size(); + + if (draw > 0) { + for (var c : list) { + int e = energyStorage.extractEnergy(draw, true); + + if (e > 0) { + energyStorage.extractEnergy(c.receiveEnergy(e, false), false); + } else { + break; + } + } + } + } + } + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/FluidTankAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/FluidTankAttachment.java new file mode 100644 index 000000000..06add8692 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/FluidTankAttachment.java @@ -0,0 +1,80 @@ +package dev.latvian.mods.kubejs.block.entity; + +import net.minecraft.core.HolderLookup; +import net.minecraft.nbt.Tag; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.capabilities.Capabilities; +import net.neoforged.neoforge.fluids.FluidStack; +import net.neoforged.neoforge.fluids.capability.templates.FluidTank; +import net.neoforged.neoforge.fluids.crafting.FluidIngredient; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; + +public class FluidTankAttachment implements BlockEntityAttachment { + public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("fluid_tank", Factory.class); + + public record Factory(int capacity, Optional inputFilter) implements BlockEntityAttachmentFactory { + private static final Predicate ALWAYS_TRUE = stack -> true; + + @Override + public BlockEntityAttachment create(BlockEntityAttachmentInfo info, KubeBlockEntity entity) { + return new FluidTankAttachment(entity, capacity, inputFilter.isEmpty() ? ALWAYS_TRUE : inputFilter.get()); + } + + @Override + public List> getCapabilities() { + return List.of(Capabilities.EnergyStorage.BLOCK); + } + } + + public static class Wrapped extends FluidTank { + private final FluidTankAttachment attachment; + + public Wrapped(FluidTankAttachment attachment, int capacity, Predicate inputFilter) { + super(capacity, inputFilter); + this.attachment = attachment; + } + + @Override + protected void onContentsChanged() { + attachment.entity.save(); + } + } + + public final KubeBlockEntity entity; + public final Wrapped fluidTank; + + public FluidTankAttachment(KubeBlockEntity entity, int capacity, Predicate filter) { + this.entity = entity; + this.fluidTank = new Wrapped(this, capacity, filter); + } + + @Override + public Object getWrappedObject() { + return fluidTank; + } + + @Override + @Nullable + public CAP getCapability(BlockCapability capability) { + if (capability == Capabilities.FluidHandler.BLOCK) { + return (CAP) fluidTank; + } + + return null; + } + + @Override + @Nullable + public Tag serialize(HolderLookup.Provider registries) { + return fluidTank.getFluid().isEmpty() ? null : fluidTank.getFluid().save(registries); + } + + @Override + public void deserialize(HolderLookup.Provider registries, @Nullable Tag tag) { + fluidTank.setFluid(tag == null ? FluidStack.EMPTY : FluidStack.parse(registries, tag).orElse(FluidStack.EMPTY)); + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java index 31036ce28..eac6ed73b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java @@ -6,25 +6,25 @@ import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.Containers; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.state.BlockState; import net.neoforged.neoforge.capabilities.BlockCapability; import net.neoforged.neoforge.capabilities.Capabilities; -import net.neoforged.neoforge.common.util.INBTSerializable; import net.neoforged.neoforge.items.ItemStackHandler; import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Optional; -public class InventoryAttachment implements BlockEntityAttachment, INBTSerializable { +public class InventoryAttachment implements BlockEntityAttachment { public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("inventory", Factory.class); public record Factory(int width, int height, Optional inputFilter) implements BlockEntityAttachmentFactory { @Override - public BlockEntityAttachment create(KubeBlockEntity entity) { + public BlockEntityAttachment create(BlockEntityAttachmentInfo info, KubeBlockEntity entity) { return new InventoryAttachment(entity, width, height, inputFilter.orElse(null)); } @@ -34,10 +34,10 @@ public BlockEntityAttachment create(KubeBlockEntity entity) { } } - public static class ItemHandler extends ItemStackHandler implements InventoryKJS { + public static class Wrapped extends ItemStackHandler implements InventoryKJS { private final InventoryAttachment attachment; - public ItemHandler(InventoryAttachment attachment) { + public Wrapped(InventoryAttachment attachment) { super(attachment.width * attachment.height); this.attachment = attachment; } @@ -70,37 +70,37 @@ public boolean isItemValid(int slot, ItemStack stack) { public final int width, height; public final KubeBlockEntity blockEntity; public final ItemPredicate inputFilter; - public final ItemHandler itemStackHandler; + public final Wrapped inventory; public InventoryAttachment(KubeBlockEntity blockEntity, int width, int height, @Nullable ItemPredicate inputFilter) { this.width = width; this.height = height; this.blockEntity = blockEntity; this.inputFilter = inputFilter; - this.itemStackHandler = new ItemHandler(this); + this.inventory = new Wrapped(this); } @Override - public Object getExposedObject() { - return itemStackHandler; + public Object getWrappedObject() { + return inventory; } @Override @Nullable public CAP getCapability(BlockCapability capability) { if (capability == Capabilities.ItemHandler.BLOCK) { - return (CAP) itemStackHandler; + return (CAP) inventory; } return null; } @Override - public ListTag serializeNBT(HolderLookup.Provider registries) { + public ListTag serialize(HolderLookup.Provider registries) { var list = new ListTag(); for (int i = 0; i < width * height; i++) { - var stack = itemStackHandler.stacks().get(i); + var stack = inventory.stacks().get(i); if (!stack.isEmpty()) { var itemTag = (CompoundTag) stack.save(registries, new CompoundTag()); @@ -113,21 +113,23 @@ public ListTag serializeNBT(HolderLookup.Provider registries) { } @Override - public void deserializeNBT(HolderLookup.Provider registries, ListTag list) { - itemStackHandler.setSize(width * height); + public void deserialize(HolderLookup.Provider registries, Tag tag) { + inventory.setSize(width * height); - for (int i = 0; i < list.size(); i++) { - var itemTag = list.getCompound(i); - var slot = itemTag.getByte("slot"); + if (tag instanceof ListTag list) { + for (int i = 0; i < list.size(); i++) { + var itemTag = list.getCompound(i); + var slot = itemTag.getByte("slot"); - if (slot >= 0 && slot < width * height) { - itemStackHandler.stacks().set(slot, ItemStack.parse(registries, itemTag).orElse(ItemStack.EMPTY)); + if (slot >= 0 && slot < width * height) { + inventory.stacks().set(slot, ItemStack.parse(registries, itemTag).orElse(ItemStack.EMPTY)); + } } } } @Override public void onRemove(ServerLevel level, KubeBlockEntity blockEntity, BlockState newState) { - Containers.dropContents(blockEntity.getLevel(), blockEntity.getBlockPos(), itemStackHandler.stacks()); + Containers.dropContents(blockEntity.getLevel(), blockEntity.getBlockPos(), inventory.stacks()); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java index 6b4693670..8a644e4db 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java @@ -15,7 +15,6 @@ import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.state.BlockState; -import net.neoforged.neoforge.common.util.INBTSerializable; import org.jetbrains.annotations.Nullable; import java.util.HashMap; @@ -52,8 +51,8 @@ public KubeBlockEntity(BlockPos blockPos, BlockState blockState, BlockEntityInfo this.attachmentArray = new BlockEntityAttachmentHolder[entityInfo.attachments.size()]; for (var aInfo : entityInfo.attachments.values()) { - var f = aInfo.factory().create(this); - map.put(aInfo.id(), f.getExposedObject()); + var f = aInfo.factory().create(aInfo, this); + map.put(aInfo.id(), f.getWrappedObject()); this.attachmentArray[aInfo.index()] = new BlockEntityAttachmentHolder(aInfo, f); } @@ -91,12 +90,10 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) var data = new CompoundTag(); for (var entry : attachmentArray) { - if (entry.attachment() instanceof INBTSerializable s) { - var t = s.serializeNBT(registries); + var t = entry.attachment().serialize(registries); - if (t != null) { - data.put(entry.info().id(), t); - } + if (t != null) { + data.put(entry.info().id(), t); } } @@ -116,13 +113,7 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) { var data = tag.getCompound("attachments"); for (var entry : attachmentArray) { - if (entry.attachment() instanceof INBTSerializable s) { - var t = data.get(entry.info().id()); - - if (t != null) { - s.deserializeNBT(registries, t); - } - } + entry.attachment().deserialize(registries, data.get(entry.info().id())); } } } @@ -228,19 +219,21 @@ private void tick() { if (!level.isClientSide && info.attachmentsTicking) { for (var entry : attachmentArray) { - entry.attachment().tick(); + entry.attachment().serverTick(); } } - if (sync) { - level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 11); - save = true; - sync = false; - } + if ((sync || save) && level.getGameTime() % 20L == 0L) { + if (sync) { + level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 11); + save = true; + sync = false; + } - if (save) { - level.blockEntityChanged(worldPosition); - save = false; + if (save) { + level.blockEntityChanged(worldPosition); + save = false; + } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemStackSet.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemStackSet.java index 2b484320d..895d453ec 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemStackSet.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemStackSet.java @@ -9,6 +9,7 @@ import java.util.Iterator; import java.util.List; import java.util.function.Consumer; +import java.util.stream.Stream; public class ItemStackSet implements Iterable { private final HashMap map; @@ -85,4 +86,8 @@ public void forEach(Consumer action) { public ItemStack getFirst() { return map.isEmpty() ? ItemStack.EMPTY : map.values().iterator().next(); } + + public Stream stream() { + return map.isEmpty() ? Stream.of() : map.values().stream(); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabContentSupplier.java b/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabContentSupplier.java index a7e167eef..ca6264509 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabContentSupplier.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabContentSupplier.java @@ -1,11 +1,11 @@ package dev.latvian.mods.kubejs.item.creativetab; +import dev.latvian.mods.kubejs.item.ItemPredicate; import net.minecraft.network.chat.Component; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; -import java.util.Arrays; import java.util.List; @FunctionalInterface @@ -16,7 +16,7 @@ public void accept(CreativeModeTab.ItemDisplayParameters itemDisplayParameters, List items = List.of(); try { - items = Arrays.stream(supplier.getContent(itemDisplayParameters.hasPermissions())).filter(is -> !is.isEmpty()).toList(); + items = supplier.getContent(itemDisplayParameters.hasPermissions()).kjs$getDisplayStacks().stream().filter(is -> !is.isEmpty()).toList(); } catch (Exception ex) { ex.printStackTrace(); } @@ -33,7 +33,7 @@ public void accept(CreativeModeTab.ItemDisplayParameters itemDisplayParameters, } } - CreativeTabContentSupplier DEFAULT = showRestrictedItems -> new ItemStack[0]; + CreativeTabContentSupplier DEFAULT = showRestrictedItems -> ItemPredicate.NONE; - ItemStack[] getContent(boolean showRestrictedItems); + ItemPredicate getContent(boolean showRestrictedItems); }