From 51c6a8638fa7fabcbe3cd10ce834cd42d865bb5d Mon Sep 17 00:00:00 2001 From: quat1024 Date: Sun, 29 Oct 2023 01:33:46 -0400 Subject: [PATCH] Simplify ZetaRegistry's API surface --- .../quark/base/handler/QuarkSounds.java | 6 +- .../quark/base/world/EntitySpawnHandler.java | 3 +- .../tools/module/AmbientDiscsModule.java | 2 +- .../world/module/AncientWoodModule.java | 3 +- .../world/module/BlossomTreesModule.java | 3 +- .../world/module/GlimmeringWealdModule.java | 4 +- .../vazkii/zeta/registry/ZetaRegistry.java | 85 +++++++------------ .../registry/ForgeZetaRegistry.java | 2 +- 8 files changed, 42 insertions(+), 66 deletions(-) diff --git a/src/main/java/vazkii/quark/base/handler/QuarkSounds.java b/src/main/java/vazkii/quark/base/handler/QuarkSounds.java index 0705bbb25d..b1245fe4f5 100644 --- a/src/main/java/vazkii/quark/base/handler/QuarkSounds.java +++ b/src/main/java/vazkii/quark/base/handler/QuarkSounds.java @@ -99,14 +99,12 @@ public class QuarkSounds { @LoadEvent public static void start(ZRegister e) { for (SoundEvent event : REGISTRY_DEFERENCE) - Quark.ZETA.registry.register(event, Registry.SOUND_EVENT_REGISTRY); + Quark.ZETA.registry.register(event, event.getLocation(), Registry.SOUND_EVENT_REGISTRY); REGISTRY_DEFERENCE.clear(); } public static SoundEvent register(String name) { - ResourceLocation loc = GameData.checkPrefix(name, false); - SoundEvent event = new SoundEvent(loc); - Quark.ZETA.registry.setInternalName(event, loc); + SoundEvent event = new SoundEvent(new ResourceLocation(Quark.MOD_ID, name)); REGISTRY_DEFERENCE.add(event); return event; } diff --git a/src/main/java/vazkii/quark/base/world/EntitySpawnHandler.java b/src/main/java/vazkii/quark/base/world/EntitySpawnHandler.java index c67e0ee818..6a2a99dbec 100644 --- a/src/main/java/vazkii/quark/base/world/EntitySpawnHandler.java +++ b/src/main/java/vazkii/quark/base/world/EntitySpawnHandler.java @@ -5,6 +5,7 @@ import java.util.function.BooleanSupplier; import net.minecraft.core.Holder; +import net.minecraft.core.Registry; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.MobCategory; @@ -43,7 +44,7 @@ public static void addEgg(ZetaModule module, EntityType entityTyp } public static void addEgg(EntityType entityType, int color1, int color2, ZetaModule module, BooleanSupplier enabledSupplier) { - new QuarkSpawnEggItem(() -> entityType, color1, color2, Quark.ZETA.registry.getInternalName(entityType) + "_spawn_egg", module, + new QuarkSpawnEggItem(() -> entityType, color1, color2, Quark.ZETA.registry.getRegistryName(entityType, Registry.ENTITY_TYPE) + "_spawn_egg", module, new Item.Properties().tab(CreativeModeTab.TAB_MISC)) .setCondition(enabledSupplier); } diff --git a/src/main/java/vazkii/quark/content/tools/module/AmbientDiscsModule.java b/src/main/java/vazkii/quark/content/tools/module/AmbientDiscsModule.java index 6f0da7e6eb..467f8e6ca8 100644 --- a/src/main/java/vazkii/quark/content/tools/module/AmbientDiscsModule.java +++ b/src/main/java/vazkii/quark/content/tools/module/AmbientDiscsModule.java @@ -49,7 +49,7 @@ public void register(ZRegister event) { } private void disc(SoundEvent sound) { - String name = Quark.ZETA.registry.getRegistryName(sound, Registry.SOUND_EVENT).getPath().replaceAll(".+\\.", ""); + String name = sound.getLocation().getPath().replaceAll(".+\\.", ""); discs.add(new QuarkMusicDiscItem(15, () -> sound, name, this, Integer.MAX_VALUE)); } diff --git a/src/main/java/vazkii/quark/content/world/module/AncientWoodModule.java b/src/main/java/vazkii/quark/content/world/module/AncientWoodModule.java index da653ceae9..0e57a26224 100644 --- a/src/main/java/vazkii/quark/content/world/module/AncientWoodModule.java +++ b/src/main/java/vazkii/quark/content/world/module/AncientWoodModule.java @@ -3,6 +3,7 @@ import com.google.common.base.Functions; import com.google.common.collect.ImmutableSet; +import net.minecraft.core.Registry; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.ComposterBlock; @@ -72,7 +73,7 @@ public void register(ZRegister event) { ancient_sapling = new AncientSaplingBlock(this); ancient_fruit = new AncientFruitItem(this); - VariantHandler.addFlowerPot(ancient_sapling, Quark.ZETA.registry.getInternalName(ancient_sapling).getPath(), Functions.identity()); + VariantHandler.addFlowerPot(ancient_sapling, Quark.ZETA.registry.getRegistryName(ancient_sapling, Registry.BLOCK).getPath(), Functions.identity()); QuarkAdvancementHandler.addModifier(new BalancedDietModifier(this, ImmutableSet.of(ancient_fruit))); diff --git a/src/main/java/vazkii/quark/content/world/module/BlossomTreesModule.java b/src/main/java/vazkii/quark/content/world/module/BlossomTreesModule.java index 50f9cf88ae..af11772493 100644 --- a/src/main/java/vazkii/quark/content/world/module/BlossomTreesModule.java +++ b/src/main/java/vazkii/quark/content/world/module/BlossomTreesModule.java @@ -1,6 +1,7 @@ package vazkii.quark.content.world.module; import com.google.common.base.Functions; +import net.minecraft.core.Registry; import net.minecraft.tags.BiomeTags; import net.minecraft.world.level.block.ComposterBlock; import net.minecraft.world.level.levelgen.GenerationStep.Decoration; @@ -83,7 +84,7 @@ private void add(String colorName, MaterialColor color, BlossomTreeConfig config BlossomLeavesBlock leaves = new BlossomLeavesBlock(colorName, this, color); BlossomTree tree = new BlossomTree(leaves); BlossomSaplingBlock sapling = new BlossomSaplingBlock(colorName, this, tree); - VariantHandler.addFlowerPot(sapling, Quark.ZETA.registry.getInternalName(sapling).getPath(), Functions.identity()); + VariantHandler.addFlowerPot(sapling, zeta.registry.getRegistryName(sapling, Registry.BLOCK).getPath(), Functions.identity()); trees.put(tree, config); } diff --git a/src/main/java/vazkii/quark/content/world/module/GlimmeringWealdModule.java b/src/main/java/vazkii/quark/content/world/module/GlimmeringWealdModule.java index d249266102..658516aa98 100644 --- a/src/main/java/vazkii/quark/content/world/module/GlimmeringWealdModule.java +++ b/src/main/java/vazkii/quark/content/world/module/GlimmeringWealdModule.java @@ -105,8 +105,7 @@ public final void register(ZRegister event) { @LoadEvent public void postRegister(ZRegister.Post e) { - Biome obj = makeBiome(); - Quark.ZETA.registry.register(obj, Registry.BIOME_REGISTRY); + Quark.ZETA.registry.register(makeBiome(), BIOME_NAME, Registry.BIOME_REGISTRY); float wmin = (float) minDepthRange; float wmax = (float) maxDepthRange; if(wmin >= wmax){ @@ -171,7 +170,6 @@ private static Biome makeBiome() { Music music = Musics.createGameMusic(QuarkSounds.MUSIC_GLIMMERING_WEALD); Biome biome = OverworldBiomes.biome(Biome.Precipitation.RAIN, 0.8F, 0.4F, mobs, settings, music); - Quark.ZETA.registry.setInternalName(biome, BIOME_NAME); return biome; } diff --git a/src/main/java/vazkii/zeta/registry/ZetaRegistry.java b/src/main/java/vazkii/zeta/registry/ZetaRegistry.java index 4ce84327bc..fbf7c21039 100644 --- a/src/main/java/vazkii/zeta/registry/ZetaRegistry.java +++ b/src/main/java/vazkii/zeta/registry/ZetaRegistry.java @@ -21,16 +21,20 @@ //Mash of arl's RegistryHelper and its ModData innerclass. //You're expected to create one of these per modid instead, avoiding a dependency on Forge's "current mod id" notion. -// -//TODO: Tidy up this API a bit - it might be nice to use a "proper" deferredregister on Forge public abstract class ZetaRegistry { protected final Zeta z; - //the keys of this are things like "minecraft:block", "minecraft:item" and so on + // the keys of this are things like "minecraft:block", "minecraft:item" and so on private final Multimap> defers = ArrayListMultimap.create(); - private final Map internalNames = new IdentityHashMap<>(); - private final Map itemsToColorProviderName = new HashMap<>(); + + // to support calling getRegistryName before the object actually gets registered for real + protected final Map internalNames = new IdentityHashMap<>(); + + // "named color provider" system allows blocks and items to choose their own color providers in a side-safe way private final Map blocksToColorProviderName = new HashMap<>(); + private final Map itemsToColorProviderName = new HashMap<>(); + + // creative tab haxx private final Map groups = new LinkedHashMap<>(); public ZetaRegistry(Zeta z) { @@ -38,18 +42,15 @@ public ZetaRegistry(Zeta z) { } public ResourceLocation getRegistryName(T obj, Registry registry) { - if(internalNames.containsKey(obj)) - return getInternalName(obj); - - return registry.getKey(obj); + ResourceLocation internal = internalNames.get(obj); + return internal == null ? registry.getKey(obj) : internal; } - public void setInternalName(Object obj, ResourceLocation name) { - internalNames.put(obj, name); - } - - public ResourceLocation getInternalName(Object obj) { - return internalNames.get(obj); + //You know how `new ResourceLocation(String)` prepends "minecraft" if there's no prefix? + //This method is like that, except it prepends *your* modid + public ResourceLocation newResourceLocation(String in) { + if(in.indexOf(':') == -1) return new ResourceLocation(z.modid, in); + else return new ResourceLocation(in); } //Root registration method @@ -57,8 +58,13 @@ public void register(T obj, ResourceLocation id, ResourceKey> re if(obj == null) throw new IllegalArgumentException("Can't register null object."); - handleColors(obj, obj); - setInternalName(obj, id); + if(obj instanceof Block block && obj instanceof IZetaBlockColorProvider provider && provider.getBlockColorProviderName() != null) + blocksToColorProviderName.put(block, provider.getBlockColorProviderName()); + + if(obj instanceof Item item && obj instanceof IZetaItemColorProvider provider && provider.getItemColorProviderName() != null) + itemsToColorProviderName.put(item, provider.getItemColorProviderName()); + + internalNames.put(obj, id); defers.put(registry.location(), () -> obj); } @@ -66,23 +72,6 @@ public void register(T obj, String resloc, ResourceKey> registry register(obj, newResourceLocation(resloc), registry); } - //You know how `new ResourceLocation(String)` prepends "minecraft" if there's no prefix? - //This method is like that, except it prepends *your* modid - public ResourceLocation newResourceLocation(String in) { - if(in.indexOf(':') == -1) return new ResourceLocation(z.modid, in); - else return new ResourceLocation(in); - } - - //TODO ZETA: what's up with this? - public void register(T obj, ResourceKey> registry) { - if(obj == null) - throw new IllegalArgumentException("Can't register null object."); - if(getInternalName(obj) == null) - throw new IllegalArgumentException("Can't register object without registry name."); - - register(obj, getInternalName(obj), registry); - } - public void registerItem(Item item, String resloc) { register(item, resloc, Registry.ITEM_REGISTRY); } @@ -101,7 +90,7 @@ public void registerBlock(Block block, String resloc, boolean hasBlockItem) { } public void setCreativeTab(Block block, CreativeModeTab group) { - ResourceLocation res = getInternalName(block); + ResourceLocation res = internalNames.get(block); if(res == null) throw new IllegalArgumentException("Can't set the creative tab for a block without a registry name yet"); @@ -110,37 +99,25 @@ public void setCreativeTab(Block block, CreativeModeTab group) { private Item createItemBlock(Block block) { Item.Properties props = new Item.Properties(); - ResourceLocation registryName = getInternalName(block); + ResourceLocation registryName = internalNames.get(block); CreativeModeTab group = groups.get(registryName); if(group != null) props = props.tab(group); - if(block instanceof IZetaItemPropertiesFiller) - ((IZetaItemPropertiesFiller) block).fillItemProperties(props); + if(block instanceof IZetaItemPropertiesFiller filler) + filler.fillItemProperties(props); BlockItem blockitem; if(block instanceof IZetaBlockItemProvider) blockitem = ((IZetaBlockItemProvider) block).provideItemBlock(block, props); else blockitem = new BlockItem(block, props); - handleColors(blockitem, block); - setInternalName(blockitem, registryName); - return blockitem; - } + if(block instanceof IZetaItemColorProvider prov && prov.getItemColorProviderName() != null) + itemsToColorProviderName.put(blockitem, prov.getItemColorProviderName()); - private void handleColors(Object thing, Object prov) { - if(thing instanceof Block block && prov instanceof IZetaBlockColorProvider provider) { - String blockColorProviderName = provider.getBlockColorProviderName(); - if(blockColorProviderName != null) - blocksToColorProviderName.put(block, blockColorProviderName); - } - - if(thing instanceof Item item && prov instanceof IZetaItemColorProvider provider) { - String itemColorProviderName = provider.getItemColorProviderName(); - if(itemColorProviderName != null) - itemsToColorProviderName.put(item, itemColorProviderName); - } + internalNames.put(blockitem, registryName); + return blockitem; } /// performing registration /// diff --git a/src/main/java/vazkii/zetaimplforge/registry/ForgeZetaRegistry.java b/src/main/java/vazkii/zetaimplforge/registry/ForgeZetaRegistry.java index c42f87570d..5736fac37f 100644 --- a/src/main/java/vazkii/zetaimplforge/registry/ForgeZetaRegistry.java +++ b/src/main/java/vazkii/zetaimplforge/registry/ForgeZetaRegistry.java @@ -33,7 +33,7 @@ private void register(ResourceKey> key, IForgeRegistry for(Supplier supplier : ourEntries) { Object entry = supplier.get(); - ResourceLocation name = getInternalName(entry); + ResourceLocation name = internalNames.get(entry); z.log.debug("Registering to " + registryRes + " - " + name); registry.register(name, (T) entry); }