diff --git a/src/main/java/com/minelittlepony/unicopia/Race.java b/src/main/java/com/minelittlepony/unicopia/Race.java index 0786da63..6e15ede9 100644 --- a/src/main/java/com/minelittlepony/unicopia/Race.java +++ b/src/main/java/com/minelittlepony/unicopia/Race.java @@ -9,7 +9,7 @@ import org.spongepowered.include.com.google.common.base.Objects; import com.google.common.base.Strings; import com.minelittlepony.unicopia.ability.magic.Affine; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; @@ -24,7 +24,7 @@ import net.minecraft.registry.RegistryKey; public final class Race implements Affine { public static final String DEFAULT_ID = "unicopia:human"; - public static final Registry REGISTRY = Registries.createDefaulted(Unicopia.id("race"), DEFAULT_ID); + public static final Registry REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID); public static final RegistryKey> REGISTRY_KEY = REGISTRY.getKey(); private static final DynamicCommandExceptionType UNKNOWN_RACE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("race.unknown", id)); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java b/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java index 91679d56..94134e78 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java @@ -9,13 +9,13 @@ import java.util.function.BiFunction; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Unicopia; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import net.minecraft.util.*; import net.minecraft.registry.Registry; public interface Abilities { - Registry> REGISTRY = Registries.createSimple(Unicopia.id("abilities")); + Registry> REGISTRY = RegistryUtils.createSimple(Unicopia.id("abilities")); Map>> BY_SLOT = new EnumMap<>(AbilitySlot.class); BiFunction>> BY_SLOT_AND_COMPOSITE_RACE = Util.memoize((slot, race) -> { return BY_SLOT.computeIfAbsent(slot, s -> new LinkedHashSet<>()) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java index a6ac55cf..d457fc53 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java @@ -19,7 +19,7 @@ import com.minelittlepony.unicopia.ability.magic.spell.ThrowableSpell; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.item.GemstoneItem; import com.minelittlepony.unicopia.item.UItems; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; @@ -33,8 +33,8 @@ public final class SpellType implements Affine, SpellPredicate< public static final Identifier EMPTY_ID = Unicopia.id("none"); public static final SpellType EMPTY_KEY = new SpellType<>(EMPTY_ID, Affinity.NEUTRAL, 0xFFFFFF, false, SpellTraits.EMPTY, t -> null); - public static final Registry> REGISTRY = Registries.createSimple(Unicopia.id("spells")); - private static final Map>> BY_AFFINITY = new EnumMap<>(Affinity.class); + public static final Registry> REGISTRY = RegistryUtils.createSimple(Unicopia.id("spells")); + public static final Map>> BY_AFFINITY = new EnumMap<>(Affinity.class); public static final SpellType PLACED_SPELL = register("placed", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, PlaceableSpell::new); public static final SpellType THROWN_SPELL = register("thrown", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, ThrowableSpell::new); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/TransformationSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/TransformationSpell.java index 3e0efcc9..237d2f79 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/TransformationSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/TransformationSpell.java @@ -13,7 +13,7 @@ import com.minelittlepony.unicopia.entity.UEntities; import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.projectile.MagicProjectileEntity; import com.minelittlepony.unicopia.projectile.ProjectileDelegate; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; @@ -66,7 +66,7 @@ public class TransformationSpell extends AbstractSpell implements ProjectileDele @SuppressWarnings("unchecked") private Optional> pickType(EntityType except, World world) { - Set> options = Registries.valuesForTag(world, UTags.TRANSFORMABLE_ENTITIES).collect(Collectors.toSet()); + Set> options = RegistryUtils.valuesForTag(world, UTags.TRANSFORMABLE_ENTITIES).collect(Collectors.toSet()); if (except.getSpawnGroup() == SpawnGroup.MONSTER) { options.removeIf(t -> t.getSpawnGroup() == SpawnGroup.MONSTER); } else { diff --git a/src/main/java/com/minelittlepony/unicopia/block/Tree.java b/src/main/java/com/minelittlepony/unicopia/block/Tree.java index d9c7831b..8ee606c2 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/Tree.java +++ b/src/main/java/com/minelittlepony/unicopia/block/Tree.java @@ -5,6 +5,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import net.fabricmc.fabric.api.biome.v1.*; +import net.fabricmc.fabric.api.event.registry.DynamicRegistrySetupCallback; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.block.*; import net.minecraft.block.sapling.SaplingGenerator; @@ -13,7 +14,6 @@ import net.minecraft.util.Identifier; import net.minecraft.util.math.random.Random; import net.minecraft.registry.*; import net.minecraft.registry.tag.BiomeTags; -import net.minecraft.registry.entry.*; import net.minecraft.world.gen.GenerationStep; import net.minecraft.world.gen.feature.*; import net.minecraft.world.gen.feature.size.TwoLayersFeatureSize; @@ -23,12 +23,36 @@ import net.minecraft.world.gen.stateprovider.BlockStateProvider; import net.minecraft.world.gen.trunk.TrunkPlacer; public record Tree ( - RegistryEntry> configuredFeature, - Optional> placedFeature, - Optional sapling + Identifier id, + TreeFeatureConfig.Builder config, + RegistryKey> configuredFeatureId, + Optional> placedFeatureId, + Optional sapling, + Optional placement ) { public static final List REGISTRY = new ArrayList<>(); + private static void bootstrap() { + DynamicRegistrySetupCallback.EVENT.register(registries -> { + registries.getOptional(RegistryKeys.CONFIGURED_FEATURE).ifPresent(registry -> { + REGISTRY.forEach(tree -> { + Registry.register(registry, tree.id(), new ConfiguredFeature<>(Feature.TREE, tree.config.build())); + }); + }); + registries.getOptional(RegistryKeys.PLACED_FEATURE).ifPresent(registry -> { + var reg = registries.asDynamicRegistryManager().createRegistryLookup().getOrThrow(RegistryKeys.CONFIGURED_FEATURE); + REGISTRY.stream().filter(tree -> tree.placedFeatureId().isPresent()).forEach(tree -> { + var placedFeature = new PlacedFeature(reg.getOrThrow(tree.configuredFeatureId()), + VegetationPlacedFeatures.modifiersWithWouldSurvive(tree.placement().orElseThrow(), tree.sapling().orElse(Blocks.OAK_SAPLING)) + ); + + Registry.register(registry, tree.id, placedFeature); + }); + }); + }); + + } + public static class Builder { public static final Predicate IS_FOREST = BiomeSelectors.foundInOverworld().and(BiomeSelectors.tag(BiomeTags.IS_FOREST)); @@ -92,31 +116,28 @@ public record Tree ( } public Tree build() { - RegistryEntry> configuredFeature = ConfiguredFeatures.register(id.toString(), Feature.TREE, configSupplier.map(Supplier::get) + RegistryKey> configuredFeatureId = RegistryKey.of(RegistryKeys.CONFIGURED_FEATURE, id); + Tree tree = new Tree(id, configSupplier.map(Supplier::get) .orElseGet(() -> new TreeFeatureConfig.Builder( - BlockStateProvider.of(logType), - trunkPlacer, - BlockStateProvider.of(leavesType), - foliagePlacer, - size.get() - ).forceDirt()).build()); - - Optional sapling = saplingId.map(id -> UBlocks.register(id, new SaplingBlock(new SaplingGenerator() { + BlockStateProvider.of(logType), + trunkPlacer, + BlockStateProvider.of(leavesType), + foliagePlacer, + size.get() + ).forceDirt()), configuredFeatureId, selector.map(selector -> { + RegistryKey i = RegistryKey.of(RegistryKeys.PLACED_FEATURE, id); + BiomeModifications.addFeature(selector, GenerationStep.Feature.VEGETAL_DECORATION, i); + return i; + }), saplingId.map(id -> UBlocks.register(id, new SaplingBlock(new SaplingGenerator() { @Override - protected RegistryEntry> getTreeFeature(Random rng, boolean flowersNearby) { - return configuredFeature; + protected RegistryKey> getTreeFeature(Random rng, boolean flowersNearby) { + return configuredFeatureId; } - }, FabricBlockSettings.copy(Blocks.OAK_SAPLING)), ItemGroup.DECORATIONS)); + }, FabricBlockSettings.copy(Blocks.OAK_SAPLING)), ItemGroups.NATURAL)), countModifier); - Optional> placedFeature = selector.map(selector -> { - var pf = PlacedFeatures.register(id.toString() + "_checked", configuredFeature, - VegetationPlacedFeatures.modifiersWithWouldSurvive(countModifier.orElseThrow(), sapling.orElse(Blocks.OAK_SAPLING)) - ); - BiomeModifications.addFeature(selector, GenerationStep.Feature.VEGETAL_DECORATION, pf.getKey().get()); - return pf; - }); - - Tree tree = new Tree(configuredFeature, placedFeature, sapling); + if (REGISTRY.isEmpty()) { + bootstrap(); + } REGISTRY.add(tree); return tree; diff --git a/src/main/java/com/minelittlepony/unicopia/block/UBlocks.java b/src/main/java/com/minelittlepony/unicopia/block/UBlocks.java index 06aa3de0..58ebcd5d 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/UBlocks.java +++ b/src/main/java/com/minelittlepony/unicopia/block/UBlocks.java @@ -5,6 +5,7 @@ import java.util.List; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.item.UItems; +import com.minelittlepony.unicopia.item.group.ItemGroupRegistry; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricMaterialBuilder; @@ -33,23 +34,23 @@ public interface UBlocks { Block FROSTED_OBSIDIAN = register("frosted_obsidian", new FrostedObsidianBlock(FabricBlockSettings.copy(Blocks.OBSIDIAN).ticksRandomly())); - Block ZAP_LOG = register("zap_log", new ZapAppleLogBlock(Blocks.OAK_LOG, MapColor.GRAY, MapColor.DEEPSLATE_GRAY), ItemGroup.MATERIALS); - Block ZAP_WOOD = register("zap_wood", new ZapAppleLogBlock(Blocks.OAK_WOOD, MapColor.DEEPSLATE_GRAY, MapColor.DEEPSLATE_GRAY), ItemGroup.MATERIALS); + Block ZAP_LOG = register("zap_log", new ZapAppleLogBlock(Blocks.OAK_LOG, MapColor.GRAY, MapColor.DEEPSLATE_GRAY), ItemGroups.BUILDING_BLOCKS); + Block ZAP_WOOD = register("zap_wood", new ZapAppleLogBlock(Blocks.OAK_WOOD, MapColor.DEEPSLATE_GRAY, MapColor.DEEPSLATE_GRAY), ItemGroups.BUILDING_BLOCKS); - Block STRIPPED_ZAP_LOG = register("stripped_zap_log", new ZapAppleLogBlock(Blocks.STRIPPED_OAK_LOG, MapColor.LIGHT_GRAY, MapColor.GRAY), ItemGroup.MATERIALS); - Block STRIPPED_ZAP_WOOD = register("stripped_zap_wood", new ZapAppleLogBlock(Blocks.STRIPPED_OAK_WOOD, MapColor.GRAY, MapColor.GRAY), ItemGroup.MATERIALS); + Block STRIPPED_ZAP_LOG = register("stripped_zap_log", new ZapAppleLogBlock(Blocks.STRIPPED_OAK_LOG, MapColor.LIGHT_GRAY, MapColor.GRAY), ItemGroups.BUILDING_BLOCKS); + Block STRIPPED_ZAP_WOOD = register("stripped_zap_wood", new ZapAppleLogBlock(Blocks.STRIPPED_OAK_WOOD, MapColor.GRAY, MapColor.GRAY), ItemGroups.BUILDING_BLOCKS); - Block ZAP_LEAVES = register("zap_leaves", new ZapAppleLeavesBlock(), ItemGroup.DECORATIONS); + Block ZAP_LEAVES = register("zap_leaves", new ZapAppleLeavesBlock(), ItemGroups.NATURAL); Block ZAP_BULB = register("zap_bulb", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GRAY).strength(500, 1200).sounds(BlockSoundGroup.AZALEA_LEAVES), Direction.DOWN, ZAP_LEAVES, FruitBlock.DEFAULT_SHAPE, false)); Block ZAP_APPLE = register("zap_apple", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GRAY).sounds(BlockSoundGroup.AZALEA_LEAVES), Direction.DOWN, ZAP_LEAVES, FruitBlock.DEFAULT_SHAPE, false)); - Block WEATHER_VANE = register("weather_vane", new WeatherVaneBlock(FabricBlockSettings.of(Material.METAL, MapColor.BLACK).requiresTool().strength(3.0f, 6.0f).sounds(BlockSoundGroup.METAL).nonOpaque()), ItemGroup.DECORATIONS); + Block WEATHER_VANE = register("weather_vane", new WeatherVaneBlock(FabricBlockSettings.of(Material.METAL, MapColor.BLACK).requiresTool().strength(3.0f, 6.0f).sounds(BlockSoundGroup.METAL).nonOpaque()), ItemGroups.TOOLS); Block GREEN_APPLE_LEAVES = register("green_apple_leaves", new FruitBearingBlock(FabricBlockSettings.copy(Blocks.OAK_LEAVES), 0xE5FFFF88, () -> UBlocks.GREEN_APPLE, () -> UItems.GREEN_APPLE.getDefaultStack() - ), ItemGroup.DECORATIONS); + ), ItemGroups.NATURAL); Block GREEN_APPLE = register("green_apple", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GREEN).sounds(BlockSoundGroup.WOOD), Direction.DOWN, GREEN_APPLE_LEAVES, FruitBlock.DEFAULT_SHAPE)); Block GREEN_APPLE_SPROUT = register("green_apple_sprout", new SproutBlock(0xE5FFFF88, () -> UItems.GREEN_APPLE_SEEDS, () -> UTreeGen.GREEN_APPLE_TREE.sapling().map(Block::getDefaultState).get())); @@ -57,7 +58,7 @@ public interface UBlocks { 0xE5FFCC88, () -> UBlocks.SWEET_APPLE, () -> UItems.SWEET_APPLE.getDefaultStack() - ), ItemGroup.DECORATIONS); + ), ItemGroups.NATURAL); Block SWEET_APPLE = register("sweet_apple", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GREEN).sounds(BlockSoundGroup.WOOD), Direction.DOWN, SWEET_APPLE_LEAVES, FruitBlock.DEFAULT_SHAPE)); Block SWEET_APPLE_SPROUT = register("sweet_apple_sprout", new SproutBlock(0xE5FFCC88, () -> UItems.SWEET_APPLE_SEEDS, () -> UTreeGen.SWEET_APPLE_TREE.sapling().map(Block::getDefaultState).get())); @@ -65,7 +66,7 @@ public interface UBlocks { 0xE5FFCCCC, () -> UBlocks.SOUR_APPLE, () -> UItems.SOUR_APPLE.getDefaultStack() - ), ItemGroup.DECORATIONS); + ), ItemGroups.NATURAL); Block SOUR_APPLE = register("sour_apple", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GREEN).sounds(BlockSoundGroup.WOOD), Direction.DOWN, SOUR_APPLE_LEAVES, FruitBlock.DEFAULT_SHAPE)); Block SOUR_APPLE_SPROUT = register("sour_apple_sprout", new SproutBlock(0xE5FFCC88, () -> UItems.SOUR_APPLE_SEEDS, () -> UTreeGen.SOUR_APPLE_TREE.sapling().map(Block::getDefaultState).get())); @@ -84,7 +85,7 @@ public interface UBlocks { } static T register(Identifier id, T block, ItemGroup group) { - UItems.register(id, new BlockItem(block, new Item.Settings().group(group))); + UItems.register(id, ItemGroupRegistry.register(new BlockItem(block, new Item.Settings()), group)); return register(id, block); } @@ -95,7 +96,7 @@ public interface UBlocks { if (block instanceof SaplingBlock || block instanceof SproutBlock || block instanceof FruitBlock || block instanceof CropBlock) { TRANSLUCENT_BLOCKS.add(block); } - return Registry.register(Registry.BLOCK, id, block); + return Registry.register(Registries.BLOCK, id, block); } static void bootstrap() { diff --git a/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java b/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java index c0cb3977..7614746c 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java +++ b/src/main/java/com/minelittlepony/unicopia/block/state/StatePredicate.java @@ -19,7 +19,6 @@ import net.minecraft.util.JsonHelper; import net.minecraft.registry.Registries; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.entry.RegistryEntry; -import net.minecraft.registry.tag.FluidTags; import net.minecraft.registry.tag.TagKey; import net.minecraft.world.World; diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java index e8f088e3..5f759bb9 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/SpellbookTraitDexPageContent.java @@ -12,6 +12,7 @@ import com.minelittlepony.unicopia.client.gui.spellbook.SpellbookChapterList.Cha import com.minelittlepony.unicopia.client.gui.spellbook.SpellbookScreen.ImageButton; import com.minelittlepony.unicopia.container.SpellbookState; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.item.group.ItemGroupRegistry; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.MinecraftClient; @@ -22,10 +23,8 @@ import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.Util; -import net.minecraft.util.collection.DefaultedList; public class SpellbookTraitDexPageContent extends DrawableHelper implements SpellbookChapterList.Content, SpellbookScreen.RecipesChangedListener { - private final Trait[] traits = Trait.values(); private SpellbookState.PageState state = new SpellbookState.PageState(); @@ -127,8 +126,7 @@ public class SpellbookTraitDexPageContent extends DrawableHelper implements Spel SpellTraits.getItems(trait) .sorted(Comparator.comparing(u -> knownItems.contains(u) ? 0 : 1)) .forEach(i -> { - DefaultedList stacks = DefaultedList.of(); - i.appendStacks(ItemGroups.SEARCH, stacks); + List stacks = ItemGroupRegistry.getVariations(i); if (knownItems.contains(i)) { tree.input(stacks); } else { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java b/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java index ab9f2783..1b0a7af9 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java @@ -6,7 +6,7 @@ import com.minelittlepony.unicopia.UTags; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.item.GemstoneItem; import com.minelittlepony.unicopia.item.UItems; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper; import net.minecraft.entity.Entity; @@ -80,7 +80,7 @@ public interface UTradeOffers { } private static Item random(Entity e, TagKey item, Random rng) { - return Registries.entriesForTag(e.world, item).getRandom(rng).get().value(); + return RegistryUtils.entriesForTag(e.world, item).getRandom(rng).get().value(); } static class JarredItemTradeOfferFactory implements TradeOffers.Factory { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityBehaviour.java b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityBehaviour.java index a3c16c10..457eec1e 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityBehaviour.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/behaviour/EntityBehaviour.java @@ -10,7 +10,7 @@ import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.entity.duck.LivingEntityDuck; import com.minelittlepony.unicopia.entity.duck.EntityDuck; import com.minelittlepony.unicopia.entity.player.Pony; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import net.minecraft.entity.Entity; import net.minecraft.entity.Entity.RemovalReason; @@ -38,7 +38,7 @@ import net.minecraft.registry.Registry; public class EntityBehaviour { private static final EntityBehaviour DEFAULT = new EntityBehaviour<>(); - private static final Registry> REGISTRY = Registries.createSimple(Unicopia.id("entity_behaviour")); + private static final Registry> REGISTRY = RegistryUtils.createSimple(Unicopia.id("entity_behaviour")); /** * Equivalent of the entity#tick method. Called every tick to update th logic for a disguise. diff --git a/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java b/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java index 3fef3ea8..4d04759e 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java @@ -1,5 +1,6 @@ package com.minelittlepony.unicopia.item; +import java.util.Arrays; import java.util.List; import java.util.function.Predicate; @@ -14,11 +15,11 @@ import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.client.FlowingText; import com.minelittlepony.unicopia.entity.player.PlayerCharmTracker; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.item.group.MultiItem; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; -import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.text.MutableText; import net.minecraft.text.Text; @@ -26,10 +27,9 @@ import net.minecraft.util.Formatting; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; import net.minecraft.util.TypedActionResult; -import net.minecraft.util.collection.DefaultedList; import net.minecraft.world.World; -public class GemstoneItem extends Item { +public class GemstoneItem extends Item implements MultiItem { public GemstoneItem(Settings settings) { super(settings); @@ -90,17 +90,13 @@ public class GemstoneItem extends Item { } @Override - public void appendStacks(ItemGroup tab, DefaultedList items) { - super.appendStacks(tab, items); - if (isIn(tab)) { - for (Affinity i : Affinity.VALUES) { - SpellType.byAffinity(i).forEach(type -> { - if (type.isObtainable()) { - items.add(enchant(getDefaultStack(), type, i)); - } - }); - } - } + public List getDefaultStacks() { + return Arrays.stream(Affinity.VALUES) + .flatMap(i -> SpellType.byAffinity(i).stream() + .filter(type -> type.isObtainable()) + .map(type -> enchant(getDefaultStack(), type, i)) + ) + .toList(); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/item/UItemGroups.java b/src/main/java/com/minelittlepony/unicopia/item/UItemGroups.java index 0280a792..734cf2fe 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/UItemGroups.java +++ b/src/main/java/com/minelittlepony/unicopia/item/UItemGroups.java @@ -1,52 +1,29 @@ package com.minelittlepony.unicopia.item; -import java.util.function.Supplier; import java.util.stream.Stream; -import com.minelittlepony.unicopia.UTags; -import com.minelittlepony.unicopia.Unicopia; +import com.minelittlepony.unicopia.item.group.ItemGroupRegistry; import com.minelittlepony.unicopia.item.toxin.Toxic; import com.minelittlepony.unicopia.item.toxin.ToxicHolder; -import net.minecraft.item.Item; -import net.minecraft.item.ItemGroup; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.tag.TagKey; -import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; -import net.minecraft.util.collection.DefaultedList; -import net.minecraft.util.registry.Registry; + +import net.minecraft.item.*; +import net.minecraft.registry.Registries; public interface UItemGroups { - ItemGroup ALL_ITEMS = create("items", UItems.EMPTY_JAR::getDefaultStack, () -> { + ItemGroup ALL_ITEMS = ItemGroupRegistry.createDynamic("items", UItems.EMPTY_JAR::getDefaultStack, () -> { return Stream.concat(Stream.of(Items.APPLE), UItems.ITEMS.stream().filter(item -> !(item instanceof ChameleonItem) || ((ChameleonItem)item).isFullyDisguised())); }); - ItemGroup HORSE_FEED = create("horsefeed", UItems.ZAP_APPLE::getDefaultStack, () -> { - return Registry.ITEM.stream().filter(item -> ((ToxicHolder)item).getToxic(item.getDefaultStack()) != Toxic.EMPTY); + ItemGroup HORSE_FEED = ItemGroupRegistry.createDynamic("horsefeed", UItems.ZAP_APPLE::getDefaultStack, () -> { + return Registries.ITEM.stream().filter(item -> ((ToxicHolder)item).getToxic(item.getDefaultStack()) != Toxic.EMPTY); }); - ItemGroup EARTH_PONY_ITEMS = forTag("earth_pony", UItems.APPLE_PIE::getDefaultStack); - ItemGroup UNICORN_ITEMS = forTag("unicorn", UItems.SPELLBOOK::getDefaultStack); - ItemGroup PEGASUS_ITEMS = forTag("pegasus", UItems.PEGASUS_FEATHER::getDefaultStack); - ItemGroup BAT_PONY_ITEMS = forTag("bat_pony", UItems.SUNGLASSES::getDefaultStack); - ItemGroup CHANGELING_ITEMS = forTag("changeling", UItems.LOVE_BOTTLE::getDefaultStack); + ItemGroup EARTH_PONY_ITEMS = ItemGroupRegistry.createGroupFromTag("earth_pony", UItems.APPLE_PIE::getDefaultStack); + ItemGroup UNICORN_ITEMS = ItemGroupRegistry.createGroupFromTag("unicorn", UItems.SPELLBOOK::getDefaultStack); + ItemGroup PEGASUS_ITEMS = ItemGroupRegistry.createGroupFromTag("pegasus", UItems.PEGASUS_FEATHER::getDefaultStack); + ItemGroup BAT_PONY_ITEMS = ItemGroupRegistry.createGroupFromTag("bat_pony", UItems.SUNGLASSES::getDefaultStack); + ItemGroup CHANGELING_ITEMS = ItemGroupRegistry.createGroupFromTag("changeling", UItems.LOVE_BOTTLE::getDefaultStack); - static ItemGroup forTag(String name, Supplier icon) { - TagKey key = UTags.item("groups/" + name); - return create(name, icon, () -> { - return Registry.ITEM.getEntryList(key) - .stream() - .flatMap(named -> named.stream()) - .map(entry -> entry.value()); - }); + static void bootstrap() { + UItems.bootstrap(); } - - static ItemGroup create(String name, Supplier icon, Supplier> items) { - return FabricItemGroupBuilder.create(Unicopia.id(name)).appendItems(list -> { - DefaultedList defs = DefaultedList.of(); - items.get().forEach(item -> item.appendStacks(ItemGroup.SEARCH, defs)); - list.addAll(defs); - }).icon(icon).build(); - } - - static void bootstrap() {} } diff --git a/src/main/java/com/minelittlepony/unicopia/item/UItems.java b/src/main/java/com/minelittlepony/unicopia/item/UItems.java index 1518001a..8bccb065 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/UItems.java +++ b/src/main/java/com/minelittlepony/unicopia/item/UItems.java @@ -7,6 +7,7 @@ import com.minelittlepony.unicopia.*; import com.minelittlepony.unicopia.block.UBlocks; import com.minelittlepony.unicopia.entity.UEntities; import com.minelittlepony.unicopia.item.enchantment.UEnchantments; +import com.minelittlepony.unicopia.item.group.ItemGroupRegistry; import com.minelittlepony.unicopia.item.toxin.UFoodComponents; import net.minecraft.item.*; @@ -24,15 +25,15 @@ public interface UItems { List ITEMS = new ArrayList<>(); - Item GREEN_APPLE = register("green_apple", AppleItem.registerTickCallback(new Item(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE)))); - Item SWEET_APPLE = register("sweet_apple", AppleItem.registerTickCallback(new Item(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE)))); - Item SOUR_APPLE = register("sour_apple", AppleItem.registerTickCallback(new Item(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE)))); + Item GREEN_APPLE = register("green_apple", AppleItem.registerTickCallback(new Item(new Item.Settings().food(FoodComponents.APPLE))), ItemGroups.FOOD_AND_DRINK); + Item SWEET_APPLE = register("sweet_apple", AppleItem.registerTickCallback(new Item(new Item.Settings().food(FoodComponents.APPLE))), ItemGroups.FOOD_AND_DRINK); + Item SOUR_APPLE = register("sour_apple", AppleItem.registerTickCallback(new Item(new Item.Settings().food(FoodComponents.APPLE))), ItemGroups.FOOD_AND_DRINK); - ZapAppleItem ZAP_APPLE = register("zap_apple", AppleItem.registerTickCallback(new ZapAppleItem(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.ZAP_APPLE)))); - Item ZAP_BULB = register("zap_bulb", new Item(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.ZAP_BULB))); + ZapAppleItem ZAP_APPLE = register("zap_apple", AppleItem.registerTickCallback(new ZapAppleItem(new Item.Settings().food(UFoodComponents.ZAP_APPLE))), ItemGroups.FOOD_AND_DRINK); + Item ZAP_BULB = register("zap_bulb", new Item(new Item.Settings().food(UFoodComponents.ZAP_BULB)), ItemGroups.FOOD_AND_DRINK); - Item ROTTEN_APPLE = register("rotten_apple", new RottenAppleItem(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE))); - Item COOKED_ZAP_APPLE = register("cooked_zap_apple", new Item(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.APPLE))); + Item ROTTEN_APPLE = register("rotten_apple", new RottenAppleItem(new Item.Settings().food(FoodComponents.APPLE)), ItemGroups.FOOD_AND_DRINK); + Item COOKED_ZAP_APPLE = register("cooked_zap_apple", new Item(new Item.Settings().food(FoodComponents.APPLE)), ItemGroups.FOOD_AND_DRINK); Item MUSIC_DISC_CRUSADE = register("music_disc_crusade", USounds.RECORD_CRUSADE, 181); Item MUSIC_DISC_PET = register("music_disc_pet", USounds.RECORD_PET, 221); @@ -42,89 +43,91 @@ public interface UItems { FriendshipBraceletItem FRIENDSHIP_BRACELET = register("friendship_bracelet", new FriendshipBraceletItem( new FabricItemSettings() .rarity(Rarity.UNCOMMON) - .group(ItemGroup.TOOLS) - )); + ), ItemGroups.TOOLS); - Item EMPTY_JAR = register("empty_jar", new JarItem(new Item.Settings().group(ItemGroup.DECORATIONS).maxCount(16).fireproof(), false, false, false)); + Item EMPTY_JAR = register("empty_jar", new JarItem(new Item.Settings().maxCount(16).fireproof(), false, false, false), ItemGroups.FUNCTIONAL); FilledJarItem FILLED_JAR = register("filled_jar", new FilledJarItem(new Item.Settings().maxCount(1))); - Item RAIN_CLOUD_JAR = register("rain_cloud_jar", new JarItem(new Item.Settings().group(ItemGroup.DECORATIONS).maxCount(1).fireproof(), true, false, false)); - Item STORM_CLOUD_JAR = register("storm_cloud_jar", new JarItem(new Item.Settings().group(ItemGroup.DECORATIONS).maxCount(1).fireproof(), true, true, false)); - Item LIGHTNING_JAR = register("lightning_jar", new JarItem(new Item.Settings().group(ItemGroup.DECORATIONS).maxCount(1).fireproof(), false, false, true)); - Item ZAP_APPLE_JAM_JAR = register("zap_apple_jam_jar", new JarItem(new Item.Settings().group(ItemGroup.DECORATIONS).maxCount(1).fireproof(), false, false, true)); + Item RAIN_CLOUD_JAR = register("rain_cloud_jar", new JarItem(new Item.Settings().maxCount(1).fireproof(), true, false, false), ItemGroups.FUNCTIONAL); + Item STORM_CLOUD_JAR = register("storm_cloud_jar", new JarItem(new Item.Settings().maxCount(1).fireproof(), true, true, false), ItemGroups.FUNCTIONAL); + Item LIGHTNING_JAR = register("lightning_jar", new JarItem(new Item.Settings().maxCount(1).fireproof(), false, false, true), ItemGroups.FUNCTIONAL); + Item ZAP_APPLE_JAM_JAR = register("zap_apple_jam_jar", new JarItem(new Item.Settings().maxCount(1).fireproof(), false, false, true), ItemGroups.FUNCTIONAL); - Item CRYSTAL_HEART = register("crystal_heart", new CrystalHeartItem(new Item.Settings().group(ItemGroup.DECORATIONS).maxCount(1))); - Item CRYSTAL_SHARD = register("crystal_shard", new Item(new Item.Settings().group(ItemGroup.MATERIALS))); + Item CRYSTAL_HEART = register("crystal_heart", new CrystalHeartItem(new Item.Settings().maxCount(1)), ItemGroups.TOOLS); + Item CRYSTAL_SHARD = register("crystal_shard", new Item(new Item.Settings()), ItemGroups.NATURAL); - Item GEMSTONE = register("gemstone", new GemstoneItem(new Item.Settings().group(ItemGroup.MATERIALS))); - Item BOTCHED_GEM = register("botched_gem", new Item(new Item.Settings().group(ItemGroup.MATERIALS))); + Item GEMSTONE = register("gemstone", new GemstoneItem(new Item.Settings()), ItemGroups.NATURAL); + Item BOTCHED_GEM = register("botched_gem", new Item(new Item.Settings()), ItemGroups.NATURAL); - Item PEGASUS_FEATHER = register("pegasus_feather", new Item(new Item.Settings().group(ItemGroup.MATERIALS))); - Item GRYPHON_FEATHER = register("gryphon_feather", new Item(new Item.Settings().group(ItemGroup.MATERIALS))); + Item PEGASUS_FEATHER = register("pegasus_feather", new Item(new Item.Settings()), ItemGroups.NATURAL); + Item GRYPHON_FEATHER = register("gryphon_feather", new Item(new Item.Settings()), ItemGroups.NATURAL); - Item OAT_SEEDS = register("oat_seeds", new AliasedBlockItem(UBlocks.OATS, new Item.Settings().group(ItemGroup.MATERIALS))); - Item OATS = register("oats", new Item(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.OATS))); - Item IMPORTED_OATS = register("imported_oats", new Item(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.IMPORTED_OATS))); - Item OATMEAL = register("oatmeal", new OatmealItem(new Item.Settings().recipeRemainder(Items.BOWL).maxCount(1).group(ItemGroup.FOOD).food(UFoodComponents.OATMEAL))); + Item OAT_SEEDS = register("oat_seeds", new AliasedBlockItem(UBlocks.OATS, new Item.Settings()), ItemGroups.NATURAL); + Item OATS = register("oats", new Item(new Item.Settings().food(UFoodComponents.OATS)), ItemGroups.FOOD_AND_DRINK); + Item IMPORTED_OATS = register("imported_oats", new Item(new Item.Settings().food(UFoodComponents.IMPORTED_OATS)), ItemGroups.FOOD_AND_DRINK); + Item OATMEAL = register("oatmeal", new OatmealItem(new Item.Settings().recipeRemainder(Items.BOWL).maxCount(1).food(UFoodComponents.OATMEAL)), ItemGroups.FOOD_AND_DRINK); - Item DAFFODIL_DAISY_SANDWICH = register("daffodil_daisy_sandwich", new Item(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.DAFODIL_DAISY_SANDWICH))); - Item HAY_BURGER = register("hay_burger", new Item(new Item.Settings().group(ItemGroup.FOOD).maxCount(1).food(UFoodComponents.HAY_BURGER))); - Item HAY_FRIES = register("hay_fries", new Item(new Item.Settings().group(ItemGroup.FOOD).maxCount(16).food(UFoodComponents.HAY_FRIES))); - Item WHEAT_WORMS = register("wheat_worms", new Item(new Item.Settings().group(ItemGroup.MISC).maxCount(16).food(UFoodComponents.INSECTS))); - Item MUFFIN = register("muffin", new MuffinItem(new Item.Settings().group(ItemGroup.FOOD).maxCount(32).food(FoodComponents.BREAD), 0)); - Item PINECONE = register("pinecone", new Item(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.PINECONE).maxCount(3))); - Item ACORN = register("acorn", new Item(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.ACORN).maxCount(16))); + Item DAFFODIL_DAISY_SANDWICH = register("daffodil_daisy_sandwich", new Item(new Item.Settings().food(UFoodComponents.DAFODIL_DAISY_SANDWICH)), ItemGroups.FOOD_AND_DRINK); + Item HAY_BURGER = register("hay_burger", new Item(new Item.Settings().maxCount(1).food(UFoodComponents.HAY_BURGER)), ItemGroups.FOOD_AND_DRINK); + Item HAY_FRIES = register("hay_fries", new Item(new Item.Settings().maxCount(16).food(UFoodComponents.HAY_FRIES)), ItemGroups.FOOD_AND_DRINK); + Item WHEAT_WORMS = register("wheat_worms", new Item(new Item.Settings().maxCount(16).food(UFoodComponents.INSECTS)), ItemGroups.NATURAL); + Item MUFFIN = register("muffin", new MuffinItem(new Item.Settings().maxCount(32).food(FoodComponents.BREAD), 0), ItemGroups.FOOD_AND_DRINK); + Item PINECONE = register("pinecone", new Item(new Item.Settings().food(UFoodComponents.PINECONE).maxCount(3)), ItemGroups.FOOD_AND_DRINK); + Item ACORN = register("acorn", new Item(new Item.Settings().food(UFoodComponents.ACORN).maxCount(16)), ItemGroups.FOOD_AND_DRINK); - Item PEBBLES = register("pebbles", new RacePredicatedAliasedBlockItem(UBlocks.ROCKS, new Item.Settings().group(ItemGroup.MATERIALS), Race::canUseEarth)); - Item ROCK = register("rock", new HeavyProjectileItem(new Item.Settings().group(ItemGroup.MATERIALS), 3)); - Item WEIRD_ROCK = register("weird_rock", new Item(new Item.Settings().group(ItemGroup.MATERIALS))); - Item ROCK_STEW = register("rock_stew", new Item(new Item.Settings().group(ItemGroup.FOOD).food(FoodComponents.MUSHROOM_STEW))); + Item PEBBLES = register("pebbles", new RacePredicatedAliasedBlockItem(UBlocks.ROCKS, new Item.Settings(), Race::canUseEarth), ItemGroups.NATURAL); + Item ROCK = register("rock", new HeavyProjectileItem(new Item.Settings(), 3), ItemGroups.NATURAL); + Item WEIRD_ROCK = register("weird_rock", new Item(new Item.Settings()), ItemGroups.NATURAL); + Item ROCK_STEW = register("rock_stew", new Item(new Item.Settings().food(FoodComponents.MUSHROOM_STEW)), ItemGroups.FOOD_AND_DRINK); - Item GREEN_APPLE_SEEDS = register("green_apple_seeds", new AliasedBlockItem(UBlocks.GREEN_APPLE_SPROUT, new Item.Settings().group(ItemGroup.MATERIALS))); - Item SWEET_APPLE_SEEDS = register("sweet_apple_seeds", new AliasedBlockItem(UBlocks.SWEET_APPLE_SPROUT, new Item.Settings().group(ItemGroup.MATERIALS))); - Item SOUR_APPLE_SEEDS = register("sour_apple_seeds", new AliasedBlockItem(UBlocks.SOUR_APPLE_SPROUT, new Item.Settings().group(ItemGroup.MATERIALS))); + Item GREEN_APPLE_SEEDS = register("green_apple_seeds", new AliasedBlockItem(UBlocks.GREEN_APPLE_SPROUT, new Item.Settings()), ItemGroups.NATURAL); + Item SWEET_APPLE_SEEDS = register("sweet_apple_seeds", new AliasedBlockItem(UBlocks.SWEET_APPLE_SPROUT, new Item.Settings()), ItemGroups.NATURAL); + Item SOUR_APPLE_SEEDS = register("sour_apple_seeds", new AliasedBlockItem(UBlocks.SOUR_APPLE_SPROUT, new Item.Settings()), ItemGroups.NATURAL); - Item MUG = register("mug", new Item(new Settings().group(ItemGroup.MATERIALS).maxCount(16))); - Item CIDER = register("cider", new DrinkableItem(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.CIDER).maxCount(1).recipeRemainder(MUG))); - Item JUICE = register("juice", new DrinkableItem(new Item.Settings().group(ItemGroup.FOOD).recipeRemainder(Items.GLASS_BOTTLE).maxCount(1).food(UFoodComponents.JUICE))); - Item BURNED_JUICE = register("burned_juice", new DrinkableItem(new Item.Settings().group(ItemGroup.FOOD).recipeRemainder(Items.GLASS_BOTTLE).maxCount(1).food(UFoodComponents.BURNED_JUICE))); - Item APPLE_PIE = register("apple_pie", new BlockItem(UBlocks.APPLE_PIE, new Item.Settings().group(ItemGroup.FOOD).maxCount(1))); - Item APPLE_PIE_SLICE = register("apple_pie_slice", new Item(new Item.Settings().group(ItemGroup.FOOD).maxCount(16).food(UFoodComponents.PIE))); + Item MUG = register("mug", new Item(new Settings().maxCount(16)), ItemGroups.TOOLS); + Item CIDER = register("cider", new DrinkableItem(new Item.Settings().food(UFoodComponents.CIDER).maxCount(1).recipeRemainder(MUG)), ItemGroups.FOOD_AND_DRINK); + Item JUICE = register("juice", new DrinkableItem(new Item.Settings().recipeRemainder(Items.GLASS_BOTTLE).maxCount(1).food(UFoodComponents.JUICE)), ItemGroups.FOOD_AND_DRINK); + Item BURNED_JUICE = register("burned_juice", new DrinkableItem(new Item.Settings().recipeRemainder(Items.GLASS_BOTTLE).maxCount(1).food(UFoodComponents.BURNED_JUICE)), ItemGroups.FOOD_AND_DRINK); + Item APPLE_PIE = register("apple_pie", new BlockItem(UBlocks.APPLE_PIE, new Item.Settings().maxCount(1)), ItemGroups.FOOD_AND_DRINK); + Item APPLE_PIE_SLICE = register("apple_pie_slice", new Item(new Item.Settings().maxCount(16).food(UFoodComponents.PIE)), ItemGroups.FOOD_AND_DRINK); - Item LOVE_BOTTLE = register("love_bottle", new DrinkableItem(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.LOVE_BOTTLE).maxCount(1).recipeRemainder(Items.GLASS_BOTTLE))); - Item LOVE_BUCKET = register("love_bucket", new DrinkableItem(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.LOVE_BUCKET).recipeRemainder(Items.BUCKET))); - Item LOVE_MUG = register("love_mug", new DrinkableItem(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.LOVE_MUG).recipeRemainder(MUG))); + Item LOVE_BOTTLE = register("love_bottle", new DrinkableItem(new Item.Settings().food(UFoodComponents.LOVE_BOTTLE).maxCount(1).recipeRemainder(Items.GLASS_BOTTLE)), ItemGroups.FOOD_AND_DRINK); + Item LOVE_BUCKET = register("love_bucket", new DrinkableItem(new Item.Settings().food(UFoodComponents.LOVE_BUCKET).recipeRemainder(Items.BUCKET)), ItemGroups.FOOD_AND_DRINK); + Item LOVE_MUG = register("love_mug", new DrinkableItem(new Item.Settings().food(UFoodComponents.LOVE_MUG).recipeRemainder(MUG)), ItemGroups.FOOD_AND_DRINK); - Item GOLDEN_FEATHER = register("golden_feather", new Item(new Item.Settings().rarity(Rarity.UNCOMMON).group(ItemGroup.MATERIALS))); - Item GOLDEN_WING = register("golden_wing", new Item(new Item.Settings().rarity(Rarity.UNCOMMON).group(ItemGroup.MATERIALS))); + Item GOLDEN_FEATHER = register("golden_feather", new Item(new Item.Settings().rarity(Rarity.UNCOMMON)), ItemGroups.NATURAL); + Item GOLDEN_WING = register("golden_wing", new Item(new Item.Settings().rarity(Rarity.UNCOMMON)), ItemGroups.NATURAL); - Item DRAGON_BREATH_SCROLL = register("dragon_breath_scroll", new DragonBreathScrollItem(new Item.Settings().rarity(Rarity.UNCOMMON).group(ItemGroup.TOOLS))); + Item DRAGON_BREATH_SCROLL = register("dragon_breath_scroll", new DragonBreathScrollItem(new Item.Settings().rarity(Rarity.UNCOMMON)), ItemGroups.TOOLS); - Item WOODEN_POLEARM = register("wooden_polearm", new PolearmItem(ToolMaterials.WOOD, 2, -3.6F, 2, new Item.Settings().group(ItemGroup.COMBAT))); - Item STONE_POLEARM = register("stone_polearm", new PolearmItem(ToolMaterials.STONE, 2, -3.6F, 2, new Item.Settings().group(ItemGroup.COMBAT))); - Item IRON_POLEARM = register("iron_polearm", new PolearmItem(ToolMaterials.IRON, 2, -3.6F, 3, new Item.Settings().group(ItemGroup.COMBAT))); - Item GOLDEN_POLEARM = register("golden_polearm", new PolearmItem(ToolMaterials.GOLD, 2, -3.6F, 4, new Item.Settings().group(ItemGroup.COMBAT))); - Item DIAMOND_POLEARM = register("diamond_polearm", new PolearmItem(ToolMaterials.DIAMOND, 2, -3.6F, 5, new Item.Settings().group(ItemGroup.COMBAT))); - Item NETHERITE_POLEARM = register("netherite_polearm", new PolearmItem(ToolMaterials.NETHERITE, 2, -3.6F, 5, new Item.Settings().group(ItemGroup.COMBAT).fireproof())); + Item WOODEN_POLEARM = register("wooden_polearm", new PolearmItem(ToolMaterials.WOOD, 2, -3.6F, 2, new Item.Settings()), ItemGroups.COMBAT); + Item STONE_POLEARM = register("stone_polearm", new PolearmItem(ToolMaterials.STONE, 2, -3.6F, 2, new Item.Settings()), ItemGroups.COMBAT); + Item IRON_POLEARM = register("iron_polearm", new PolearmItem(ToolMaterials.IRON, 2, -3.6F, 3, new Item.Settings()), ItemGroups.COMBAT); + Item GOLDEN_POLEARM = register("golden_polearm", new PolearmItem(ToolMaterials.GOLD, 2, -3.6F, 4, new Item.Settings()), ItemGroups.COMBAT); + Item DIAMOND_POLEARM = register("diamond_polearm", new PolearmItem(ToolMaterials.DIAMOND, 2, -3.6F, 5, new Item.Settings()), ItemGroups.COMBAT); + Item NETHERITE_POLEARM = register("netherite_polearm", new PolearmItem(ToolMaterials.NETHERITE, 2, -3.6F, 5, new Item.Settings().fireproof()), ItemGroups.COMBAT); - Item BUTTERFLY_SPAWN_EGG = register("butterfly_spawn_egg", new SpawnEggItem(UEntities.BUTTERFLY, 0x222200, 0xaaeeff, new Item.Settings().group(ItemGroup.MISC))); - Item BUTTERFLY = register("butterfly", new Item(new Item.Settings().group(ItemGroup.FOOD).food(UFoodComponents.INSECTS))); + Item BUTTERFLY_SPAWN_EGG = register("butterfly_spawn_egg", new SpawnEggItem(UEntities.BUTTERFLY, 0x222200, 0xaaeeff, new Item.Settings()), ItemGroups.SPAWN_EGGS); + Item BUTTERFLY = register("butterfly", new Item(new Item.Settings().food(UFoodComponents.INSECTS)), ItemGroups.FOOD_AND_DRINK); - Item SPELLBOOK = register("spellbook", new SpellbookItem(new Item.Settings().maxCount(1).rarity(Rarity.UNCOMMON).group(ItemGroup.TOOLS))); + Item SPELLBOOK = register("spellbook", new SpellbookItem(new Item.Settings().maxCount(1).rarity(Rarity.UNCOMMON)), ItemGroups.TOOLS); AmuletItem PEGASUS_AMULET = register("pegasus_amulet", new PegasusAmuletItem(new FabricItemSettings() .maxCount(1) .maxDamage(890) - .rarity(Rarity.UNCOMMON) - .group(ItemGroup.DECORATIONS), 900)); + .rarity(Rarity.UNCOMMON), 900), ItemGroups.TOOLS); AlicornAmuletItem ALICORN_AMULET = register("alicorn_amulet", new AlicornAmuletItem(new FabricItemSettings() .maxCount(1) .maxDamage(1000) - .rarity(Rarity.RARE) - .group(ItemGroup.DECORATIONS))); + .rarity(Rarity.RARE)), ItemGroups.TOOLS); - GlassesItem SUNGLASSES = register("sunglasses", new GlassesItem(new FabricItemSettings().maxCount(1).group(ItemGroup.COMBAT))); - GlassesItem BROKEN_SUNGLASSES = register("broken_sunglasses", new GlassesItem(new FabricItemSettings().maxCount(1).group(ItemGroup.COMBAT))); + GlassesItem SUNGLASSES = register("sunglasses", new GlassesItem(new FabricItemSettings().maxCount(1)), ItemGroups.COMBAT); + GlassesItem BROKEN_SUNGLASSES = register("broken_sunglasses", new GlassesItem(new FabricItemSettings().maxCount(1)), ItemGroups.COMBAT); + static T register(String name, T item, ItemGroup group) { + return ItemGroupRegistry.register(register(name, item), group); + } + + @Deprecated static T register(String name, T item) { return register(Unicopia.id(name), item); } @@ -134,15 +137,14 @@ public interface UItems { if (item instanceof BlockItem) { ((BlockItem)item).appendBlocks(Item.BLOCK_ITEMS, item); } - return Registry.register(Registry.ITEM, id, item); + return Registry.register(Registries.ITEM, id, item); } static MusicDiscItem register(String name, SoundEvent sound, int seconds) { return register(name, new MusicDiscItem(1, sound, new Settings() .maxCount(1) - .group(ItemGroup.MISC) .rarity(Rarity.RARE), seconds - ) {}); + ) {}, ItemGroups.TOOLS); } static void bootstrap() { diff --git a/src/main/java/com/minelittlepony/unicopia/item/ZapAppleItem.java b/src/main/java/com/minelittlepony/unicopia/item/ZapAppleItem.java index f9c7b261..bf349032 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/ZapAppleItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/ZapAppleItem.java @@ -1,15 +1,18 @@ package com.minelittlepony.unicopia.item; +import java.util.List; + import com.minelittlepony.unicopia.UTags; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.advancement.UCriteria; import com.minelittlepony.unicopia.entity.player.Pony; +import com.minelittlepony.unicopia.item.group.MultiItem; import com.minelittlepony.unicopia.item.toxin.*; import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.UParticles; import com.minelittlepony.unicopia.util.MagicalDamageSource; import com.minelittlepony.unicopia.util.TraceHelper; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; @@ -24,7 +27,6 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; -import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.Vec3d; import net.minecraft.util.Hand; import net.minecraft.util.Rarity; @@ -33,7 +35,7 @@ import net.minecraft.registry.Registries; import net.minecraft.world.World; import net.minecraft.world.event.GameEvent; -public class ZapAppleItem extends Item implements ChameleonItem, ToxicHolder { +public class ZapAppleItem extends Item implements ChameleonItem, ToxicHolder, MultiItem { public ZapAppleItem(Settings settings) { super(settings); } @@ -98,18 +100,15 @@ public class ZapAppleItem extends Item implements ChameleonItem, ToxicHolder { } @Override - public void appendStacks(ItemGroup tab, DefaultedList items) { - super.appendStacks(tab, items); - if (tab.contains(getDefaultStack())) { - Unicopia.SIDE.getPony().map(Pony::getReferenceWorld) - .stream() - .flatMap(world -> Registries.valuesForTag(world, UTags.APPLES)) - .filter(a -> a != this).forEach(item -> { - ItemStack stack = new ItemStack(this); - stack.getOrCreateNbt().putString("appearance", Registries.ITEM.getId(item).toString()); - items.add(stack); - }); - } + public List getDefaultStacks() { + return Unicopia.SIDE.getPony().map(Pony::getReferenceWorld) + .stream() + .flatMap(world -> RegistryUtils.valuesForTag(world, UTags.APPLES)) + .filter(a -> a != this).map(item -> { + ItemStack stack = new ItemStack(this); + stack.getOrCreateNbt().putString("appearance", Registries.ITEM.getId(item).toString()); + return stack; + }).toList(); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/item/group/ItemGroupRegistry.java b/src/main/java/com/minelittlepony/unicopia/item/group/ItemGroupRegistry.java new file mode 100644 index 00000000..e6651005 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/item/group/ItemGroupRegistry.java @@ -0,0 +1,70 @@ +package com.minelittlepony.unicopia.item.group; + +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import java.util.stream.Stream; + +import com.google.common.cache.*; +import com.minelittlepony.unicopia.UTags; +import com.minelittlepony.unicopia.Unicopia; + +import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; +import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; +import net.minecraft.item.*; +import net.minecraft.registry.Registries; +import net.minecraft.registry.tag.TagKey; + +public interface ItemGroupRegistry { + LoadingCache> STACK_VARIANCES_CACHE = CacheBuilder.newBuilder() + .expireAfterAccess(30, TimeUnit.SECONDS) + .build(CacheLoader.from(i -> { + if (i instanceof MultiItem m) { + return m.getDefaultStacks(); + } + return ItemGroups.SEARCH.getSearchTabStacks().stream().filter(s -> s.getItem() == i).toList(); + })); + + static List getVariations(Item item) { + return STACK_VARIANCES_CACHE.getUnchecked(item); + } + + Map> REGISTRY = new HashMap<>(); + + static T register(T item, ItemGroup group) { + REGISTRY.computeIfAbsent(group, g -> new HashSet<>()).add(item); + return item; + } + + static ItemGroup createDynamic(String name, Supplier icon, Supplier> items) { + boolean[] reloading = new boolean[1]; + return FabricItemGroup.builder(Unicopia.id(name)).entries((features, list, k) -> { + if (reloading[0]) { + return; + } + reloading[0] = true; + items.get().forEach(item -> { + list.addAll(ItemGroupRegistry.getVariations(item)); + }); + reloading[0] = false; + }).icon(icon).build(); + } + + static ItemGroup createGroupFromTag(String name, Supplier icon) { + TagKey key = UTags.item("groups/" + name); + return createDynamic(name, icon, () -> { + return Registries.ITEM.getEntryList(key) + .stream() + .flatMap(named -> named.stream()) + .map(entry -> entry.value()); + }); + } + + static void bootstrap() { + REGISTRY.forEach((group, items) -> { + ItemGroupEvents.modifyEntriesEvent(group).register(event -> { + event.addAll(items.stream().map(Item::getDefaultStack).toList()); + }); + }); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/item/group/MultiItem.java b/src/main/java/com/minelittlepony/unicopia/item/group/MultiItem.java new file mode 100644 index 00000000..ac8b4aea --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/item/group/MultiItem.java @@ -0,0 +1,9 @@ +package com.minelittlepony.unicopia.item.group; + +import java.util.List; + +import net.minecraft.item.ItemStack; + +public interface MultiItem { + List getDefaultStacks(); +} diff --git a/src/main/java/com/minelittlepony/unicopia/item/group/package-info.java b/src/main/java/com/minelittlepony/unicopia/item/group/package-info.java new file mode 100644 index 00000000..5e530073 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/item/group/package-info.java @@ -0,0 +1 @@ +package com.minelittlepony.unicopia.item.group; \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java index 8dbe1f4a..43331309 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java +++ b/src/main/java/com/minelittlepony/unicopia/item/toxin/Toxics.java @@ -1,7 +1,7 @@ package com.minelittlepony.unicopia.item.toxin; import com.minelittlepony.unicopia.*; -import com.minelittlepony.unicopia.util.Registries; +import com.minelittlepony.unicopia.util.RegistryUtils; import net.minecraft.registry.Registry; @@ -10,7 +10,7 @@ import static com.minelittlepony.unicopia.item.toxin.Ailment.*; import static com.minelittlepony.unicopia.item.toxin.Toxin.*; public interface Toxics { - Registry REGISTRY = Registries.createSimple(Unicopia.id("toxic")); + Registry REGISTRY = RegistryUtils.createSimple(Unicopia.id("toxic")); Toxic SEVERE_INNERT = Toxic.innert(Toxicity.SEVERE); diff --git a/src/main/java/com/minelittlepony/unicopia/util/Registries.java b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java similarity index 97% rename from src/main/java/com/minelittlepony/unicopia/util/Registries.java rename to src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java index 73dc07e0..0ddf531e 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/Registries.java +++ b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java @@ -12,7 +12,7 @@ import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.registry.entry.RegistryEntryList; import net.minecraft.world.World; -public interface Registries { +public interface RegistryUtils { static Registry createSimple(Identifier id) { return FabricRegistryBuilder.from(new SimpleRegistry(RegistryKey.ofRegistry(id), Lifecycle.stable())).buildAndRegister(); }