simplified filters and enhanced error reporting

master
Stefan Dollase 2016-07-03 22:01:47 +02:00
parent a75626cc9c
commit 0b904acd5e
5 changed files with 47 additions and 56 deletions

View File

@ -1,10 +1,13 @@
package amidst.mojangapi.file.json.filter;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import amidst.documentation.GsonConstructor;
import amidst.documentation.Immutable;
import amidst.mojangapi.world.biome.Biome;
import amidst.mojangapi.world.filter.WorldFilter_Biome;
@Immutable
@ -19,10 +22,24 @@ public class WorldFilterJson_Biome {
public void validate(List<String> notifications) {
if (biomes.isEmpty()) {
notifications.add("No biomes for filter");
} else {
for (String name : biomes) {
if (Biome.exists(name)) {
notifications.add("invalid biome name: '" + name + "'");
}
}
}
}
public WorldFilter_Biome createBiomeFilter() {
return new WorldFilter_Biome(distance, biomes);
return new WorldFilter_Biome(distance, createValidBiomeIndexes());
}
private Set<Short> createValidBiomeIndexes() {
Set<Short> result = new HashSet<>();
for (String name : biomes) {
result.add((short) Biome.getByName(name).getIndex());
}
return result;
}
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import amidst.documentation.GsonConstructor;
import amidst.documentation.Immutable;
import amidst.mojangapi.world.filter.WorldFilter_Structure;
import amidst.mojangapi.world.icon.type.DefaultWorldIconTypes;
@Immutable
public class WorldFilterJson_Structure {
@ -17,10 +18,15 @@ public class WorldFilterJson_Structure {
}
public void validate(List<String> notifications) {
// noop
if (!DefaultWorldIconTypes.exists(structure)) {
notifications.add("invalid structure: '" + structure + "'");
}
if (minimum <= 0) {
notifications.add("invalid minimum: " + minimum);
}
}
public WorldFilter_Structure createStructureFilter() {
return new WorldFilter_Structure(distance, structure, minimum);
return new WorldFilter_Structure(distance, DefaultWorldIconTypes.getByName(structure), minimum);
}
}

View File

@ -1,66 +1,31 @@
package amidst.mojangapi.world.filter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import amidst.documentation.NotThreadSafe;
import amidst.mojangapi.world.World;
import amidst.mojangapi.world.biome.Biome;
@NotThreadSafe
public class WorldFilter_Biome extends WorldFilter {
private short[][] evaluatedRegion = null;
private final List<Biome> biomes;
private final Set<Short> validBiomeIndexes;
private short[][] region;
public WorldFilter_Biome(long worldFilterSize, List<String> biomeNames) {
public WorldFilter_Biome(long worldFilterSize, Set<Short> validBiomeIndexes) {
super(worldFilterSize);
List<Biome> biomes = new ArrayList<>();
for (String name : biomeNames) {
Biome biome = Biome.getByName(name);
if (biome == null) {
List<String> possibleNames = new ArrayList<String>();
for (Biome possibleBiome : Biome.allBiomes()) {
possibleNames.add(possibleBiome.getName());
}
throw new IllegalArgumentException("Biome name '" + name + "' should be one of: "
+ String.join(", ", possibleNames));
}
biomes.add(biome);
}
this.biomes = biomes;
this.validBiomeIndexes = validBiomeIndexes;
this.region = new short[(int) this.quarterFilterSize * 2][(int) this.quarterFilterSize * 2];
}
@Override
public boolean isValid(World world) {
return isValid(world, getUpdatedRegion(world));
}
protected short[][] getUpdatedRegion(World world) {
if (this.evaluatedRegion == null) {
this.evaluatedRegion = new short[(int) this.quarterFilterSize * 2][(int) this.quarterFilterSize * 2];
}
world.getBiomeDataOracle().populateArray(corner, evaluatedRegion, true);
return evaluatedRegion;
}
protected boolean isValid(World world, short[][] region) {
world.getBiomeDataOracle().populateArray(corner, region, true);
for (short[] row : region) {
for (short entry : row) {
if (isValidBiome(entry)) {
if (validBiomeIndexes.contains(entry)) {
return true;
}
}
}
return false;
}
private boolean isValidBiome(short biomeIndex) {
for (Biome biome : biomes) {
if (biomeIndex == biome.getIndex()) {
return true;
}
}
return false;
}
}

View File

@ -13,26 +13,25 @@ public class WorldFilter_Structure extends WorldFilter {
private final DefaultWorldIconTypes structure;
private final int count;
public WorldFilter_Structure(long worldFilterSize, String structureName, int count) {
public WorldFilter_Structure(long worldFilterSize, DefaultWorldIconTypes structure, int count) {
super(worldFilterSize);
this.structure = DefaultWorldIconTypes.getByName(structureName);
this.structure = structure;
this.count = count;
}
@Override
public boolean isValid(World world) {
WorldIconCollector structureCollector = getCollector();
WorldIconProducer<Void> structureProducer = getProducer(world);
procudeAndCollect(getProducer(world), structureCollector);
return structureCollector.get().size() > count;
}
private void procudeAndCollect(WorldIconProducer<Void> structureProducer, WorldIconCollector structureCollector) {
for (long x = 0; x < 2 * worldFilterSize; x += 512) {
for (long y = 0; y < 2 * worldFilterSize; y += 512) {
CoordinatesInWorld subCorner = CoordinatesInWorld.from(x, y).add(corner);
structureProducer.produce(subCorner, structureCollector, null);
structureProducer.produce(CoordinatesInWorld.from(x, y).add(corner), structureCollector, null);
}
}
return structureCollector.get().size() > count;
}
private WorldIconProducer<Void> getProducer(World world) {

View File

@ -45,6 +45,10 @@ public enum DefaultWorldIconTypes {
return typeMap.get(name);
}
public static boolean exists(String name) {
return typeMap.containsKey(name);
}
private static String getFilename(String name) {
return "/amidst/gui/main/icon/" + name + ".png";
}