Skip to content

Commit

Permalink
feat(utils): OrderedForgeRegistries uses LazyObjectHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Sep 17, 2022
1 parent b8f485b commit 7c8f1c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 73 deletions.
Expand Up @@ -2,15 +2,15 @@

import java.util.ArrayList;
import java.util.List;
import org.auioc.mcmod.arnicalib.server.loot.AHLootItemFunctions;
import org.auioc.mcmod.arnicalib.utils.game.OrderedForgeRegistries;
import org.auioc.mcmod.arnicalib.utils.java.RandomUtils;
import org.auioc.mcmod.arnicalib.utils.java.Validate;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import org.auioc.mcmod.arnicalib.server.loot.AHLootItemFunctions;
import org.auioc.mcmod.arnicalib.utils.game.OrderedForgeRegistries;
import org.auioc.mcmod.arnicalib.utils.java.RandomUtils;
import org.auioc.mcmod.arnicalib.utils.java.Validate;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected ItemStack run(ItemStack stack, LootContext ctx) {
}

private static Potion getRandomPotion() {
return RandomUtils.pickOneFromList(OrderedForgeRegistries.Potions.get()).getValue();
return RandomUtils.pickOneFromList(OrderedForgeRegistries.POTIONS.get()).getValue();
}


Expand Down
Expand Up @@ -82,7 +82,7 @@ static List<MobEffect> getAllEffects() {

static MobEffect getRandomEffect(boolean useOrderedRegestry) {
if (useOrderedRegestry) {
return RandomUtils.pickOneFromList(OrderedForgeRegistries.MobEffects.get()).getValue();
return RandomUtils.pickOneFromList(OrderedForgeRegistries.MOB_EFFECTS.get()).getValue();
}
return RandomUtils.pickOneFromCollection(ForgeRegistries.MOB_EFFECTS.getValues());
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.Marker;
import org.auioc.mcmod.arnicalib.api.java.holder.LazyObjectHolder;
import org.auioc.mcmod.arnicalib.utils.LogUtil;
import org.auioc.mcmod.arnicalib.utils.java.Validate;
import net.minecraft.core.particles.ParticleType;
Expand All @@ -19,6 +20,7 @@
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
Expand All @@ -30,13 +32,24 @@ public class OrderedForgeRegistries {

private static final Marker MARKER = LogUtil.getMarker("OrderedForgeRegistries");

public static final LazyObjectHolder<List<Entry<ResourceLocation, Block>>> BLOCKS = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.BLOCKS));
public static final LazyObjectHolder<List<Entry<ResourceLocation, Fluid>>> FLUIDS = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.FLUIDS));
public static final LazyObjectHolder<List<Entry<ResourceLocation, Item>>> ITEMS = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.ITEMS));
public static final LazyObjectHolder<List<Entry<ResourceLocation, SoundEvent>>> SOUND_EVENTS = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.SOUND_EVENTS));
public static final LazyObjectHolder<List<Entry<ResourceLocation, MobEffect>>> MOB_EFFECTS = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.MOB_EFFECTS));
public static final LazyObjectHolder<List<Entry<ResourceLocation, Potion>>> POTIONS = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.POTIONS));
public static final LazyObjectHolder<List<Entry<ResourceLocation, Enchantment>>> ENCHANTMENTS = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.ENCHANTMENTS));
public static final LazyObjectHolder<List<Entry<ResourceLocation, EntityType<?>>>> ENTITIES = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.ENTITIES));
public static final LazyObjectHolder<List<Entry<ResourceLocation, BlockEntityType<?>>>> BLOCK_ENTITIES = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.BLOCK_ENTITIES));
public static final LazyObjectHolder<List<Entry<ResourceLocation, ParticleType<?>>>> PARTICLE_TYPES = new LazyObjectHolder<>(() -> createEntryList(ForgeRegistries.PARTICLE_TYPES));

private static <V extends IForgeRegistryEntry<V>> List<Entry<ResourceLocation, V>> createEntryList(IForgeRegistry<V> iRegistry) {
Validate.isTrue(iRegistry instanceof IForgeRegistryModifiable, "Only supports IForgeRegistryModifiable");

IForgeRegistryModifiable<V> registry = (IForgeRegistryModifiable<V>) iRegistry;

ResourceLocation name = registry.getRegistryName();
LOGGER.info(MARKER, "Creating entry list of registry " + name);
LOGGER.info(MARKER, "Creating entry list of registry '" + name + "'");

Validate.isTrue(registry.isLocked(), "The entry list can only be created from a frozen registry");

Expand All @@ -48,73 +61,8 @@ private static <V extends IForgeRegistryEntry<V>> List<Entry<ResourceLocation, V
list.add(Map.entry(entry.getKey().location(), entry.getValue()));
}

LOGGER.info(MARKER, "Created entry list of registry " + name + " with " + list.size() + " entries");
LOGGER.info(MARKER, "Created entry list of registry '" + name + "'' with " + list.size() + " entries");
return List.copyOf(list);
}


public static class Blocks {
private static final List<Entry<ResourceLocation, Block>> ENTRIES = createEntryList(ForgeRegistries.BLOCKS);

public static List<Entry<ResourceLocation, Block>> get() {
return ENTRIES;
}
}
public static class Fluids {
private static final List<Entry<ResourceLocation, Fluid>> ENTRIES = createEntryList(ForgeRegistries.FLUIDS);

public static List<Entry<ResourceLocation, Fluid>> get() {
return ENTRIES;
}
}
public static class Items {
private static final List<Entry<ResourceLocation, Item>> ENTRIES = createEntryList(ForgeRegistries.ITEMS);

public static List<Entry<ResourceLocation, Item>> get() {
return ENTRIES;
}
}
public static class MobEffects {
private static final List<Entry<ResourceLocation, MobEffect>> ENTRIES = createEntryList(ForgeRegistries.MOB_EFFECTS);

public static List<Entry<ResourceLocation, MobEffect>> get() {
return ENTRIES;
}
}
public static class SoundEvents {
private static final List<Entry<ResourceLocation, SoundEvent>> ENTRIES = createEntryList(ForgeRegistries.SOUND_EVENTS);

public static List<Entry<ResourceLocation, SoundEvent>> get() {
return ENTRIES;
}
}
public static class Potions {
private static final List<Entry<ResourceLocation, Potion>> ENTRIES = createEntryList(ForgeRegistries.POTIONS);

public static List<Entry<ResourceLocation, Potion>> get() {
return ENTRIES;
}
}
public static class Enchantments {
private static final List<Entry<ResourceLocation, Enchantment>> ENTRIES = createEntryList(ForgeRegistries.ENCHANTMENTS);

public static List<Entry<ResourceLocation, Enchantment>> get() {
return ENTRIES;
}
}
public static class Entities {
private static final List<Entry<ResourceLocation, EntityType<?>>> ENTRIES = createEntryList(ForgeRegistries.ENTITIES);

public static List<Entry<ResourceLocation, EntityType<?>>> get() {
return ENTRIES;
}
}
public static class ParticleTypes {
private static final List<Entry<ResourceLocation, ParticleType<?>>> ENTRIES = createEntryList(ForgeRegistries.PARTICLE_TYPES);

public static List<Entry<ResourceLocation, ParticleType<?>>> get() {
return ENTRIES;
}
}

}

0 comments on commit 7c8f1c3

Please sign in to comment.