mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
tryGet -> getFromID
This commit is contained in:
@@ -4,8 +4,6 @@ import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
@@ -26,7 +24,7 @@ public class BiomeDelegateLoader implements TypeLoader<BiomeDelegate> {
|
||||
public BiomeDelegate load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader) throws LoadException {
|
||||
if(c.equals("SELF")) return BiomeDelegate.self();
|
||||
return biomeRegistry
|
||||
.tryGet((String) c)
|
||||
.getFromID((String) c)
|
||||
.map(BiomeDelegate::from)
|
||||
.orElseGet(() -> BiomeDelegate.ephemeral((String) c));
|
||||
}
|
||||
|
||||
@@ -9,13 +9,10 @@ package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.script.StructureScript;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -71,7 +68,7 @@ public class StructureFunction implements Function<Boolean> {
|
||||
|
||||
|
||||
String app = id.apply(implementationArguments, variableMap);
|
||||
return registry.tryGet(app).map(script -> {
|
||||
return registry.getFromID(app).map(script -> {
|
||||
Rotation rotation1;
|
||||
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).apply(implementationArguments, variableMap);
|
||||
try {
|
||||
|
||||
@@ -79,13 +79,13 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
|
||||
TypeKey<T> getType();
|
||||
|
||||
default Optional<T> tryGet(String attempt) {
|
||||
return get(attempt, map -> {
|
||||
default Optional<T> getFromID(String id) {
|
||||
return getFromID(id, map -> {
|
||||
if(map.isEmpty()) return Optional.empty();
|
||||
if(map.size() == 1) {
|
||||
return map.values().stream().findFirst(); // only one value.
|
||||
}
|
||||
throw new IllegalArgumentException("ID \"" + attempt + "\" is ambiguous; matches: " + map
|
||||
throw new IllegalArgumentException("ID \"" + id + "\" is ambiguous; matches: " + map
|
||||
.keySet()
|
||||
.stream()
|
||||
.map(RegistryKey::toString)
|
||||
@@ -93,12 +93,12 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
});
|
||||
}
|
||||
|
||||
Map<RegistryKey, T> get(String id);
|
||||
Map<RegistryKey, T> getIDMatches(String id);
|
||||
|
||||
default Optional<T> get(String attempt, Function<Map<RegistryKey, T>, Optional<T>> reduction) {
|
||||
default Optional<T> getFromID(String attempt, Function<Map<RegistryKey, T>, Optional<T>> reduction) {
|
||||
if(attempt.contains(":")) {
|
||||
return get(RegistryKey.parse(attempt));
|
||||
}
|
||||
return reduction.apply(get(attempt));
|
||||
return reduction.apply(getIDMatches(attempt));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class GenericTemplateSupplierLoader<T> implements TypeLoader<T> {
|
||||
Map<String, Object> map = (Map<String, Object>) c;
|
||||
try {
|
||||
return loader
|
||||
.load(registry.tryGet(((String) map.get("type")))
|
||||
.load(registry.getFromID(((String) map.get("type")))
|
||||
.orElseThrow(() -> new LoadException("No such entry: " + map.get("type")))
|
||||
.get(), new MapConfiguration(map)).get();
|
||||
} catch(ConfigException e) {
|
||||
|
||||
@@ -97,8 +97,8 @@ public class CheckedRegistryImpl<T> implements CheckedRegistry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> get(String id) {
|
||||
return registry.get(id);
|
||||
public Map<RegistryKey, T> getIDMatches(String id) {
|
||||
return registry.getIDMatches(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -84,8 +84,8 @@ public class LockedRegistryImpl<T> implements Registry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> get(String id) {
|
||||
return registry.get(id);
|
||||
public Map<RegistryKey, T> getIDMatches(String id) {
|
||||
return registry.getIDMatches(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,7 +32,6 @@ import java.lang.reflect.AnnotatedType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -67,9 +66,9 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
|
||||
@Override
|
||||
public T load(@NotNull AnnotatedType type, @NotNull Object o, @NotNull ConfigLoader configLoader) throws LoadException {
|
||||
return tryGet((String) o).orElseThrow(() -> new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " +
|
||||
getItemsFormatted()));
|
||||
return getFromID((String) o).orElseThrow(() -> new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " +
|
||||
getItemsFormatted()));
|
||||
}
|
||||
|
||||
private String getItemsFormatted() {
|
||||
@@ -145,7 +144,7 @@ public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> get(String id) {
|
||||
public Map<RegistryKey, T> getIDMatches(String id) {
|
||||
return objectIDs
|
||||
.get(id)
|
||||
.stream()
|
||||
|
||||
@@ -37,7 +37,7 @@ public class RegistryTest {
|
||||
|
||||
test.register("test", "bazinga");
|
||||
|
||||
assertEquals(test.get("test").orElseThrow(), "bazinga");
|
||||
assertEquals(test.getIDMatches("test").orElseThrow(), "bazinga");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,7 +60,7 @@ public class RegistryTest {
|
||||
|
||||
test.register("test", "bazinga");
|
||||
|
||||
assertEquals(test.get("test").orElseThrow(), "bazinga");
|
||||
assertEquals(test.getIDMatches("test").orElseThrow(), "bazinga");
|
||||
|
||||
try {
|
||||
test.register("test", "bazinga2");
|
||||
|
||||
@@ -25,7 +25,6 @@ import cloud.commandframework.paper.PaperCommandManager;
|
||||
import com.dfsek.terra.api.entity.CommandSender;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.generator.ChunkGenerator;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -35,7 +34,6 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
@@ -216,9 +214,9 @@ public class TerraBukkitPlugin extends JavaPlugin {
|
||||
public @Nullable
|
||||
ChunkGenerator getDefaultWorldGenerator(@NotNull String worldName, String id) {
|
||||
return new BukkitChunkGeneratorWrapper(generatorMap.computeIfAbsent(worldName, name -> {
|
||||
ConfigPack pack = platform.getConfigRegistry().get(id).orElseThrow(
|
||||
ConfigPack pack = platform.getConfigRegistry().getIDMatches(id).orElseThrow(
|
||||
() -> new IllegalArgumentException("No such config pack \"" + id + "\""));
|
||||
return pack.getGeneratorProvider().newInstance(pack);
|
||||
}), platform.getRawConfigRegistry().get(id).orElseThrow(), platform.getWorldHandle().air());
|
||||
}), platform.getRawConfigRegistry().getIDMatches(id).orElseThrow(), platform.getWorldHandle().air());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public final class TerraCLI {
|
||||
CLIPlatform platform = new CLIPlatform();
|
||||
platform.getEventManager().callEvent(new PlatformInitializationEvent());
|
||||
|
||||
ConfigPack generate = platform.getConfigRegistry().get("OVERWORLD").orElseThrow(); // TODO: make this a cli argument
|
||||
ConfigPack generate = platform.getConfigRegistry().getIDMatches("OVERWORLD").orElseThrow(); // TODO: make this a cli argument
|
||||
|
||||
CLIWorld world = new CLIWorld(2, 2, 384, -64, generate);
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
package com.dfsek.terra.fabric.mixin.lifecycle.server;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
import net.minecraft.util.registry.DynamicRegistryManager;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.registry.SimpleRegistry;
|
||||
@@ -38,7 +37,6 @@ import java.util.function.Supplier;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.fabric.FabricEntryPoint;
|
||||
import com.dfsek.terra.fabric.PlatformImpl;
|
||||
import com.dfsek.terra.fabric.event.BiomeRegistrationEvent;
|
||||
import com.dfsek.terra.fabric.generation.FabricChunkGeneratorWrapper;
|
||||
import com.dfsek.terra.fabric.generation.TerraBiomeSource;
|
||||
|
||||
@@ -85,7 +83,7 @@ public abstract class GeneratorOptionsMixin {
|
||||
prop = prop.substring(prop.indexOf(":") + 1);
|
||||
|
||||
String finalProp = prop;
|
||||
ConfigPack config = main.getConfigRegistry().tryGet(prop).orElseThrow(() -> new IllegalArgumentException(
|
||||
ConfigPack config = main.getConfigRegistry().getFromID(prop).orElseThrow(() -> new IllegalArgumentException(
|
||||
"No such pack " + finalProp));
|
||||
|
||||
cir.setReturnValue(
|
||||
|
||||
Reference in New Issue
Block a user