Skip to content

Commit

Permalink
Simplify ZetaRegistry's API surface
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Oct 29, 2023
1 parent 4a5bd62 commit 51c6a86
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 66 deletions.
6 changes: 2 additions & 4 deletions src/main/java/vazkii/quark/base/handler/QuarkSounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +44,7 @@ public static void addEgg(ZetaModule module, EntityType<? extends Mob> entityTyp
}

public static void addEgg(EntityType<? extends Mob> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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;
}
Expand Down
85 changes: 31 additions & 54 deletions src/main/java/vazkii/zeta/registry/ZetaRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,57 @@

//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<ResourceLocation, Supplier<Object>> defers = ArrayListMultimap.create();
private final Map<Object, ResourceLocation> internalNames = new IdentityHashMap<>();
private final Map<Item, String> itemsToColorProviderName = new HashMap<>();

// to support calling getRegistryName before the object actually gets registered for real
protected final Map<Object, ResourceLocation> 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<Block, String> blocksToColorProviderName = new HashMap<>();
private final Map<Item, String> itemsToColorProviderName = new HashMap<>();

// creative tab haxx
private final Map<ResourceLocation, CreativeModeTab> groups = new LinkedHashMap<>();

public ZetaRegistry(Zeta z) {
this.z = z;
}

public <T> ResourceLocation getRegistryName(T obj, Registry<T> 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
public <T> void register(T obj, ResourceLocation id, ResourceKey<Registry<T>> registry) {
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);
}

public <T> void register(T obj, String resloc, ResourceKey<Registry<T>> 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 <T> void register(T obj, ResourceKey<Registry<T>> 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);
}
Expand All @@ -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");

Expand All @@ -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 ///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private <T> void register(ResourceKey<? extends Registry<?>> key, IForgeRegistry

for(Supplier<Object> 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);
}
Expand Down

0 comments on commit 51c6a86

Please sign in to comment.