package net.xevianlight.aphelion.planet; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.Level; import net.xevianlight.aphelion.Aphelion; import net.xevianlight.aphelion.util.registries.ModRegistries; import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; public final class PlanetCache { public static final Map PLANETS = new HashMap<>(); public static final Map, ResourceLocation> PLANET_BY_DIMENSION = new HashMap<>(); public static final Planet DEFAULT = new Planet( ResourceKey.create(Registries.DIMENSION, ResourceLocation.withDefaultNamespace("overworld")), ResourceKey.create(ModRegistries.ORBIT, Aphelion.id("orbit/overworld")), 1, ResourceKey.create(ModRegistries.STAR_SYSTEM, Aphelion.id("sol")), true, 1 ); public static void registerPlanets(Map planets) { PLANETS.clear(); PLANET_BY_DIMENSION.clear(); PLANETS.putAll(planets); planets.forEach((planetId, planet) -> { var dim = planet.dimension(); var prev = PLANET_BY_DIMENSION.put(dim, planetId); if (prev != null) { Aphelion.LOGGER.warn( "Dimension {} is claimed by multiple planets: {} and {}. Keeping latest: {}", dim.location(), prev, planetId, planetId ); } }); Aphelion.LOGGER.info("Loaded {} planets; {} dimension mappings", PLANETS.size(), PLANET_BY_DIMENSION.size()); } public static Planet getOrDefault(ResourceLocation id) { return PLANETS.getOrDefault(id, DEFAULT); } public static @Nullable Planet getOrNull(ResourceLocation id) { return PLANETS.getOrDefault(id, null); } public static @Nullable Planet getByOrbitOrNull(ResourceLocation id) { return PLANETS.values().stream() .filter(planet -> planet.orbit().location().equals(id)) .findFirst() .orElse(null); } public static Planet getByDimensionOrNull(ResourceKey dimension) { ResourceLocation planetId = PLANET_BY_DIMENSION.get(dimension); return planetId == null ? null : PLANETS.get(planetId); } }