make registry return optional for get operations

This commit is contained in:
dfsek
2021-12-01 17:48:41 -07:00
parent 4cc07a7b02
commit a69be58b58
17 changed files with 73 additions and 86 deletions

View File

@@ -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;
}

View File

@@ -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