Merge pull request #6389 from HateMeLikeAPro/item-group-attribute-filter

Added InItemGroup attribute filter
This commit is contained in:
simibubi 2024-08-07 17:26:05 +02:00 committed by GitHub
commit 3a3ded66d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<ItemAttribute> 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 ? "<none>" : 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;