reimplement loot tables

This commit is contained in:
dfsek
2020-12-31 23:20:16 -07:00
parent 356771bcea
commit a9df684b80
6 changed files with 34 additions and 47 deletions

View File

@@ -32,7 +32,6 @@ import com.dfsek.terra.config.files.FolderLoader;
import com.dfsek.terra.config.files.Loader;
import com.dfsek.terra.config.files.ZIPLoader;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.loaders.LootTableLoader;
import com.dfsek.terra.config.templates.AbstractableTemplate;
import com.dfsek.terra.config.templates.BiomeGridTemplate;
import com.dfsek.terra.config.templates.BiomeTemplate;
@@ -48,12 +47,15 @@ import com.dfsek.terra.registry.BiomeGridRegistry;
import com.dfsek.terra.registry.BiomeRegistry;
import com.dfsek.terra.registry.CarverRegistry;
import com.dfsek.terra.registry.FloraRegistry;
import com.dfsek.terra.registry.LootRegistry;
import com.dfsek.terra.registry.OreRegistry;
import com.dfsek.terra.registry.PaletteRegistry;
import com.dfsek.terra.registry.ScriptRegistry;
import com.dfsek.terra.registry.StructureRegistry;
import com.dfsek.terra.registry.TerraRegistry;
import com.dfsek.terra.registry.TreeRegistry;
import org.apache.commons.io.IOUtils;
import org.json.simple.parser.ParseException;
import parsii.eval.Scope;
import java.io.File;
@@ -61,6 +63,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
@@ -86,6 +89,7 @@ public class ConfigPack implements LoaderRegistrar {
private final OreRegistry oreRegistry = new OreRegistry();
private final TreeRegistry treeRegistry;
private final ScriptRegistry scriptRegistry = new ScriptRegistry();
private final LootRegistry lootRegistry = new LootRegistry();
private final AbstractConfigLoader abstractConfigLoader = new AbstractConfigLoader();
private final ConfigLoader selfLoader = new ConfigLoader();
@@ -149,13 +153,19 @@ public class ConfigPack implements LoaderRegistrar {
for(Map.Entry<String, Double> var : template.getVariables().entrySet()) {
varScope.create(var.getKey()).setValue(var.getValue());
}
abstractConfigLoader
.registerLoader(LootTable.class, new LootTableLoader(loader, main)); // These loaders need access to the Loader instance to get files.
loader.open("structures/data", ".tesf").then(streams -> streams.forEach(stream -> {
StructureScript structureScript = new StructureScript(stream, main, scriptRegistry, checkCache);
scriptRegistry.add(structureScript.getId(), structureScript);
})).close();
})).close().open("structures/loot", ".json").thenEntries(entries -> {
for(Map.Entry<String, InputStream> entry : entries) {
try {
lootRegistry.add(entry.getKey(), new LootTable(IOUtils.toString(entry.getValue(), StandardCharsets.UTF_8), main));
} catch(ParseException | IOException | NullPointerException e) {
throw new LoadException("Unable to load loot", e);
}
}
}).close();
loader
.open("palettes", ".yml").then(streams -> buildAll(new PaletteFactory(), paletteRegistry, abstractConfigLoader.load(streams, PaletteTemplate::new), main)).close()
@@ -250,7 +260,8 @@ public class ConfigPack implements LoaderRegistrar {
.registerLoader(Ore.class, oreRegistry)
.registerLoader(Tree.class, treeRegistry)
.registerLoader(StructureScript.class, scriptRegistry)
.registerLoader(TerraStructure.class, structureRegistry);
.registerLoader(TerraStructure.class, structureRegistry)
.registerLoader(LootTable.class, lootRegistry);
}
public ScriptRegistry getScriptRegistry() {

View File

@@ -8,7 +8,7 @@ import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.Set;
public abstract class Loader {
protected final Map<String, InputStream> streams = new HashMap<>();
@@ -23,11 +23,16 @@ public abstract class Loader {
return this;
}
public Loader thenNames(Consumer<List<String>> consumer) {
public Loader thenNames(ExceptionalConsumer<List<String>> consumer) throws ConfigException {
consumer.accept(new GlueList<>(streams.keySet()));
return this;
}
public Loader thenEntries(ExceptionalConsumer<Set<Map.Entry<String, InputStream>>> consumer) throws ConfigException {
consumer.accept(streams.entrySet());
return this;
}
/**
* Get a single file from this Loader.
*

View File

@@ -1,34 +0,0 @@
package com.dfsek.terra.config.loaders;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeLoader;
import com.dfsek.terra.api.loot.LootTable;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.config.files.Loader;
import org.apache.commons.io.IOUtils;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
public class LootTableLoader implements TypeLoader<LootTable> {
private final Loader loader;
private final TerraPlugin main;
public LootTableLoader(Loader loader, TerraPlugin main) {
this.loader = loader;
this.main = main;
}
@Override
public LootTable load(Type type, Object o, ConfigLoader configLoader) throws LoadException {
try(InputStream stream = loader.get("structures/loot/" + o + ".json")) {
return new LootTable(IOUtils.toString(stream, StandardCharsets.UTF_8), main);
} catch(IOException | ParseException | NullPointerException e) {
throw new LoadException("Unable to load loot", e);
}
}
}

View File

@@ -33,14 +33,14 @@ public class StructureTemplate extends AbstractableTemplate implements ConfigTem
@Value("loot")
@Abstractable
private Map<Integer, LootTable> loot;
private Map<String, LootTable> loot;
@Value("features")
@Abstractable
@Default
private List<Void> features = new GlueList<>();
public Map<Integer, LootTable> getLoot() {
public Map<String, LootTable> getLoot() {
return loot;
}

View File

@@ -9,15 +9,14 @@ import com.dfsek.terra.procgen.GridSpawn;
import java.util.Map;
// TODO: implementation
public class TerraStructure {
private final ProbabilityCollection<StructureScript> structure;
private final Range spawnStart;
private final GridSpawn spawn;
private final Map<Integer, LootTable> loot;
private final Map<String, LootTable> loot;
private final StructureTemplate template;
public TerraStructure(ProbabilityCollection<StructureScript> structures, Range spawnStart, GridSpawn spawn, Map<Integer, LootTable> loot, StructureTemplate template) {
public TerraStructure(ProbabilityCollection<StructureScript> structures, Range spawnStart, GridSpawn spawn, Map<String, LootTable> loot, StructureTemplate template) {
this.structure = structures;
this.spawnStart = spawnStart;
this.spawn = spawn;
@@ -41,7 +40,7 @@ public class TerraStructure {
return spawn;
}
public Map<Integer, LootTable> getLoot() {
public Map<String, LootTable> getLoot() {
return loot;
}
}

View File

@@ -0,0 +1,6 @@
package com.dfsek.terra.registry;
import com.dfsek.terra.api.loot.LootTable;
public class LootRegistry extends TerraRegistry<LootTable> {
}