mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-03 08:25:31 +00:00
make registry return optional for get operations
This commit is contained in:
parent
4cc07a7b02
commit
a69be58b58
@ -34,10 +34,8 @@ public class BiomeConfigType implements ConfigType<BiomeTemplate, TerraBiome> {
|
||||
public Supplier<OpenRegistry<TerraBiome>> registrySupplier(ConfigPack pack) {
|
||||
return () -> pack.getRegistryFactory().create(registry -> (TypeLoader<TerraBiome>) (t, c, loader) -> {
|
||||
if(c.equals("SELF")) return null;
|
||||
TerraBiome obj = registry.get((String) c);
|
||||
if(obj == null)
|
||||
throw new LoadException("No such " + t.getType().getTypeName() + " matching \"" + c + "\" was found in this registry.");
|
||||
return obj;
|
||||
return registry.get((String) c).orElseThrow(() -> new LoadException(
|
||||
"No such " + t.getType().getTypeName() + " matching \"" + c + "\" was found in this registry."));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,6 @@ public class BiomeArgumentParser implements ArgumentParser<TerraBiome> {
|
||||
@Override
|
||||
public TerraBiome parse(CommandSender sender, String arg) {
|
||||
Player player = (Player) sender;
|
||||
return player.world().getConfig().getRegistry(TerraBiome.class).get(arg);
|
||||
return player.world().getConfig().getRegistry(TerraBiome.class).get(arg).orElse(null);
|
||||
}
|
||||
}
|
||||
|
@ -38,10 +38,8 @@ public class PaletteConfigType implements ConfigType<PaletteTemplate, Palette> {
|
||||
if(((String) c).startsWith("BLOCK:"))
|
||||
return new PaletteImpl.Singleton(
|
||||
platform.getWorldHandle().createBlockData(((String) c).substring(6))); // Return single palette for BLOCK: shortcut.
|
||||
Palette obj = registry.get((String) c);
|
||||
if(obj == null)
|
||||
throw new LoadException("No such " + t.getType().getTypeName() + " matching \"" + c + "\" was found in this registry.");
|
||||
return obj;
|
||||
return registry.get((String) c).orElseThrow(() -> new LoadException(
|
||||
"No such " + t.getType().getTypeName() + " matching \"" + c + "\" was found in this registry."));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,6 @@ public class ScriptArgumentParser implements ArgumentParser<Structure> {
|
||||
|
||||
@Override
|
||||
public Structure parse(CommandSender sender, String arg) {
|
||||
return ((Player) sender).world().getConfig().getRegistry(Structure.class).get(arg);
|
||||
return ((Player) sender).world().getConfig().getRegistry(Structure.class).get(arg).orElse(null);
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,6 @@ public class StructureArgumentParser implements ArgumentParser<ConfiguredStructu
|
||||
|
||||
@Override
|
||||
public ConfiguredStructure parse(CommandSender sender, String arg) {
|
||||
return ((Player) sender).world().getConfig().getRegistry(ConfiguredStructure.class).get(arg);
|
||||
return ((Player) sender).world().getConfig().getRegistry(ConfiguredStructure.class).get(arg).orElse(null);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,8 @@
|
||||
package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -26,9 +28,6 @@ import com.dfsek.terra.api.util.RotationUtil;
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class LootFunction implements Function<Void> {
|
||||
private final Registry<LootTable> registry;
|
||||
@ -61,16 +60,12 @@ public class LootFunction implements Function<Void> {
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
String id = data.apply(implementationArguments, variableMap);
|
||||
LootTable table = registry.get(id);
|
||||
|
||||
if(table == null) {
|
||||
LOGGER.error("No such loot table {}", id);
|
||||
return null;
|
||||
}
|
||||
|
||||
arguments.getBuffer().addItem(new BufferedLootApplication(table, platform, script),
|
||||
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())));
|
||||
registry.get(id).ifPresentOrElse(table -> arguments.getBuffer().addItem(new BufferedLootApplication(table, platform, script),
|
||||
new Vector3(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, variableMap)
|
||||
.intValue(),
|
||||
FastMath.roundToInt(xz.getZ()))),
|
||||
() -> LOGGER.error("No such loot table {}", id));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -70,26 +70,25 @@ public class StructureFunction implements Function<Boolean> {
|
||||
RotationUtil.rotateVector(xz, arguments.getRotation());
|
||||
|
||||
String app = id.apply(implementationArguments, variableMap);
|
||||
Structure script = registry.get(app);
|
||||
if(script == null) {
|
||||
LOGGER.warn("No such structure {}", app);
|
||||
return null;
|
||||
}
|
||||
|
||||
Rotation rotation1;
|
||||
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).apply(implementationArguments, variableMap);
|
||||
try {
|
||||
rotation1 = Rotation.valueOf(rotString);
|
||||
} catch(IllegalArgumentException e) {
|
||||
LOGGER.warn("Invalid rotation {}", rotString);
|
||||
return null;
|
||||
}
|
||||
|
||||
Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
|
||||
FastMath.roundToInt(xz.getZ()));
|
||||
|
||||
return script.generate(new IntermediateBuffer(arguments.getBuffer(), offset), arguments.getWorld(), arguments.getRandom(),
|
||||
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
|
||||
return registry.get(app).map(script -> {
|
||||
Rotation rotation1;
|
||||
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).apply(implementationArguments, variableMap);
|
||||
try {
|
||||
rotation1 = Rotation.valueOf(rotString);
|
||||
} catch(IllegalArgumentException e) {
|
||||
LOGGER.warn("Invalid rotation {}", rotString);
|
||||
return null;
|
||||
}
|
||||
|
||||
Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
|
||||
FastMath.roundToInt(xz.getZ()));
|
||||
|
||||
return script.generate(new IntermediateBuffer(arguments.getBuffer(), offset), arguments.getWorld(), arguments.getRandom(),
|
||||
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
|
||||
}).orElseGet(() -> {
|
||||
LOGGER.error("No such structure {}", app);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -15,6 +15,7 @@ import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
@ -28,9 +29,8 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
*
|
||||
* @return Value matching the identifier, {@code null} if no value is present.
|
||||
*/
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
T get(@NotNull String identifier) throws NoSuchEntryException;
|
||||
Optional<T> get(@NotNull String identifier);
|
||||
|
||||
/**
|
||||
* Check if the registry contains a value.
|
||||
|
@ -5,7 +5,4 @@ public class NoSuchEntryException extends RuntimeException {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public NoSuchEntryException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
@ -43,12 +43,11 @@ public class GenericTemplateSupplierLoader<T> implements TypeLoader<T> {
|
||||
public T load(AnnotatedType t, Object c, ConfigLoader loader) throws LoadException {
|
||||
Map<String, Object> map = (Map<String, Object>) c;
|
||||
try {
|
||||
if(!registry.contains((String) map.get("type"))) {
|
||||
throw new LoadException("No such entry: " + map.get("type"));
|
||||
}
|
||||
ObjectTemplate<T> template = registry.get(((String) map.get("type"))).get();
|
||||
loader.load(template, new MapConfiguration(map));
|
||||
return template.get();
|
||||
return loader
|
||||
.load(registry
|
||||
.get(((String) map.get("type")))
|
||||
.orElseThrow(() -> new LoadException("No such entry: " + map.get("type")))
|
||||
.get(), new MapConfiguration(map)).get();
|
||||
} catch(ConfigException e) {
|
||||
throw new LoadException("Unable to load object: ", e);
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
@ -50,7 +51,7 @@ public class CheckedRegistryImpl<T> implements CheckedRegistry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T get(@NotNull String identifier) {
|
||||
public Optional<T> get(@NotNull String identifier) {
|
||||
return registry.get(identifier);
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
@ -44,7 +45,7 @@ public class LockedRegistryImpl<T> implements Registry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T get(@NotNull String identifier) {
|
||||
public Optional<T> get(@NotNull String identifier) {
|
||||
return registry.get(identifier);
|
||||
}
|
||||
|
||||
|
@ -19,11 +19,13 @@ package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.BiConsumer;
|
||||
@ -34,10 +36,6 @@ import java.util.stream.Collectors;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
|
||||
import com.dfsek.terra.api.registry.exception.NoSuchEntryException;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
* Registry implementation with read/write access. For internal use only.
|
||||
@ -59,17 +57,12 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
|
||||
@Override
|
||||
public T load(AnnotatedType type, Object o, ConfigLoader configLoader) throws LoadException {
|
||||
T obj;
|
||||
try {
|
||||
obj = get((String) o);
|
||||
} catch(NoSuchEntryException e) {
|
||||
return get((String) o).orElseThrow(() -> {
|
||||
String list = objects.keySet().stream().sorted().reduce("", (a, b) -> a + "\n - " + b);
|
||||
if(objects.isEmpty()) list = "[ ]";
|
||||
throw new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " + list);
|
||||
}
|
||||
|
||||
return obj;
|
||||
return new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " + list);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -101,12 +94,8 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public @NotNull T get(@NotNull String identifier) {
|
||||
T value = objects.getOrDefault(identifier, (Entry<T>) NULL).getValue();
|
||||
if(value == null) {
|
||||
throw new NoSuchEntryException("Entry " + identifier + " is not present in registry.");
|
||||
}
|
||||
return value;
|
||||
public Optional<T> get(@NotNull String identifier) {
|
||||
return Optional.ofNullable(objects.getOrDefault(identifier, (Entry<T>) NULL).getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -72,7 +72,7 @@ public class PlatformImpl extends AbstractPlatform {
|
||||
|
||||
worlds.forEach(world -> {
|
||||
FabricChunkGeneratorWrapper chunkGeneratorWrapper = ((FabricChunkGeneratorWrapper) world.getChunkManager().getChunkGenerator());
|
||||
chunkGeneratorWrapper.setPack(getConfigRegistry().get(chunkGeneratorWrapper.getPack().getID()));
|
||||
chunkGeneratorWrapper.setPack(getConfigRegistry().get(chunkGeneratorWrapper.getPack().getID()).orElseThrow());
|
||||
});
|
||||
|
||||
return succeed;
|
||||
|
@ -72,7 +72,13 @@ public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.C
|
||||
config -> config.group(
|
||||
Codec.STRING.fieldOf("pack")
|
||||
.forGetter(ConfigPack::getID)
|
||||
).apply(config, config.stable(FabricEntryPoint.getPlatform().getConfigRegistry()::get)));
|
||||
).apply(config, config.stable(id -> FabricEntryPoint.getPlatform()
|
||||
.getConfigRegistry()
|
||||
.get(id)
|
||||
.orElseThrow(
|
||||
() -> new IllegalArgumentException(
|
||||
"No such config pack " +
|
||||
id)))));
|
||||
|
||||
public static final Codec<FabricChunkGeneratorWrapper> CODEC = RecordCodecBuilder.create(
|
||||
instance -> instance.group(
|
||||
|
@ -24,6 +24,7 @@ import net.minecraft.util.dynamic.RegistryLookupCodec;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
import net.minecraft.world.biome.source.BiomeSource;
|
||||
import net.minecraft.world.biome.source.util.MultiNoiseUtil.MultiNoiseSampler;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
@ -34,16 +35,19 @@ import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.fabric.FabricEntryPoint;
|
||||
import com.dfsek.terra.fabric.util.FabricUtil;
|
||||
|
||||
import net.minecraft.world.biome.source.util.MultiNoiseUtil.MultiNoiseSampler;
|
||||
|
||||
|
||||
public class TerraBiomeSource extends BiomeSource {
|
||||
public static final Codec<ConfigPack> PACK_CODEC = (RecordCodecBuilder.create(config -> config.group(
|
||||
Codec.STRING.fieldOf("pack").forGetter(ConfigPack::getID)
|
||||
)
|
||||
.apply(config, config.stable(
|
||||
FabricEntryPoint.getPlatform()
|
||||
.getConfigRegistry()::get))));
|
||||
id -> FabricEntryPoint.getPlatform()
|
||||
.getConfigRegistry()
|
||||
.get(id)
|
||||
.orElseThrow(
|
||||
() -> new IllegalArgumentException(
|
||||
"No such config pack " +
|
||||
id))))));
|
||||
public static final Codec<TerraBiomeSource> CODEC = RecordCodecBuilder.create(instance -> instance.group(
|
||||
RegistryLookupCodec.of(Registry.BIOME_KEY).forGetter(source -> source.biomeRegistry),
|
||||
Codec.LONG.fieldOf("seed").stable().forGetter(source -> source.seed),
|
||||
|
@ -78,10 +78,10 @@ public abstract class GeneratorOptionsMixin {
|
||||
l, false);
|
||||
|
||||
prop = prop.substring(prop.indexOf(":") + 1);
|
||||
|
||||
ConfigPack config = main.getConfigRegistry().get(prop);
|
||||
|
||||
if(config == null) throw new IllegalArgumentException("No such pack " + prop);
|
||||
|
||||
String finalProp = prop;
|
||||
ConfigPack config = main.getConfigRegistry().get(prop).orElseThrow(() -> new IllegalArgumentException(
|
||||
"No such pack " + finalProp));
|
||||
|
||||
main.getEventManager().callEvent(new BiomeRegistrationEvent(registryManager)); // register biomes
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user