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