mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-18 14:20:02 +00:00
make registry return optional for get operations
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user