diff --git a/src/main/java/com/simibubi/create/content/logistics/filter/ItemAttribute.java b/src/main/java/com/simibubi/create/content/logistics/filter/ItemAttribute.java index 2a7f9aa4a..bdb1a442f 100644 --- a/src/main/java/com/simibubi/create/content/logistics/filter/ItemAttribute.java +++ b/src/main/java/com/simibubi/create/content/logistics/filter/ItemAttribute.java @@ -40,6 +40,8 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeType; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.CreativeModeTabs; import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.ComposterBlock; @@ -47,6 +49,7 @@ import net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.common.capabilities.ForgeCapabilities; +import net.minecraftforge.common.CreativeModeTabRegistry; import net.minecraftforge.fml.ModContainer; import net.minecraftforge.fml.ModList; import net.minecraftforge.forgespi.language.IModInfo; @@ -59,6 +62,7 @@ public interface ItemAttribute { static ItemAttribute standard = register(StandardTraits.DUMMY); static ItemAttribute inTag = register(new InTag(ItemTags.LOGS)); + static ItemAttribute inItemGroup = register(InItemGroup.EMPTY); static ItemAttribute addedBy = register(new AddedBy("dummy")); static ItemAttribute hasEnchant = register(EnchantAttribute.EMPTY); static ItemAttribute shulkerFillLevel = register(ShulkerFillLevelAttribute.EMPTY); @@ -273,6 +277,55 @@ public interface ItemAttribute { } + public static class InItemGroup implements ItemAttribute { + public static final InItemGroup EMPTY = new InItemGroup(null); + + private CreativeModeTab group; + + public InItemGroup(CreativeModeTab group) { + this.group = group; + } + + @Override + public boolean appliesTo(ItemStack stack) { + return group != null && group.contains(stack); + } + + @Override + public List listAttributesOf(ItemStack stack) { + return CreativeModeTabs.tabs().stream() + .filter(tab -> !tab.hasSearchBar() && tab.contains(stack)) + .map(tab -> (ItemAttribute)new InItemGroup(tab)) + .toList(); + } + + @Override + public String getTranslationKey() { + return "in_item_group"; + } + + @Override + public Object[] getTranslationParameters() { + return new Object[] { group == null ? "" : group.getDisplayName().getString() }; + } + + @Override + public void writeNBT(CompoundTag nbt) { + if (group != null) { + ResourceLocation groupId = CreativeModeTabRegistry.getName(group); + + if (groupId != null) { + nbt.putString("group", groupId.toString()); + } + } + } + + @Override + public ItemAttribute readNBT(CompoundTag nbt) { + return nbt.contains("group") ? new InItemGroup(CreativeModeTabRegistry.getTab(new ResourceLocation(nbt.getString("group")))) : EMPTY; + } + } + public static class AddedBy implements ItemAttribute { private String modId;