Move StructureSpawnRequirements to separate classes.

This commit is contained in:
dfsek 2020-11-13 12:01:20 -07:00
parent 658ed01055
commit 08d1ca02f9
8 changed files with 120 additions and 55 deletions

View File

@ -24,9 +24,9 @@ public class SpawnCommand extends WorldCommand implements DebugCommand {
int x = p.getBlockX(); int x = p.getBlockX();
int y = p.getBlockY(); int y = p.getBlockY();
int z = p.getBlockZ(); int z = p.getBlockZ();
boolean air = StructureSpawnRequirement.AIR.matches(world, x, y, z); boolean air = StructureSpawnRequirement.AIR.getInstance(world).matches(x, y, z);
boolean ground = StructureSpawnRequirement.LAND.matches(world, x, y, z); boolean ground = StructureSpawnRequirement.LAND.getInstance(world).matches(x, y, z);
boolean sea = StructureSpawnRequirement.OCEAN.matches(world, x, y, z); boolean sea = StructureSpawnRequirement.OCEAN.getInstance(world).matches(x, y, z);
sender.sendMessage("AIR: " + air + "\nLAND: " + ground + "\nOCEAN: " + sea); sender.sendMessage("AIR: " + air + "\nLAND: " + ground + "\nOCEAN: " + sea);
return true; return true;

View File

@ -277,7 +277,7 @@ public class Structure implements Serializable {
public boolean checkSpawns(Location origin, Rotation r) { public boolean checkSpawns(Location origin, Rotation r) {
for(StructureContainedBlock b : spawns) { for(StructureContainedBlock b : spawns) {
Vector2 rot = getRotatedCoords(new Vector2(b.getX() - structureInfo.getCenterX(), b.getZ() - structureInfo.getCenterZ()), r); Vector2 rot = getRotatedCoords(new Vector2(b.getX() - structureInfo.getCenterX(), b.getZ() - structureInfo.getCenterZ()), r);
if(!b.getRequirement().matches(origin.getWorld(), (int) rot.getX() + origin.getBlockX(), origin.getBlockY() + b.getY(), (int) rot.getZ() + origin.getBlockZ())) if(!b.getRequirement().getInstance(origin.getWorld()).matches((int) rot.getX() + origin.getBlockX(), origin.getBlockY() + b.getY(), (int) rot.getZ() + origin.getBlockZ()))
return false; return false;
} }
return true; return true;

View File

@ -1,74 +1,37 @@
package com.dfsek.terra.structure; package com.dfsek.terra.structure;
import com.dfsek.terra.TerraWorld; import com.dfsek.terra.structure.spawn.AirSpawn;
import com.dfsek.terra.biome.UserDefinedBiome; import com.dfsek.terra.structure.spawn.BlankSpawn;
import com.dfsek.terra.config.base.ConfigPack; import com.dfsek.terra.structure.spawn.LandSpawn;
import com.dfsek.terra.config.genconfig.biome.BiomeConfig; import com.dfsek.terra.structure.spawn.OceanSpawn;
import com.dfsek.terra.structure.spawn.Requirement;
import org.bukkit.World; import org.bukkit.World;
import org.polydev.gaea.generation.GenerationPhase;
import org.polydev.gaea.math.FastNoiseLite;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public enum StructureSpawnRequirement implements Serializable { public enum StructureSpawnRequirement implements Serializable {
AIR { AIR {
@Override @Override
public boolean matches(World w, int x, int y, int z) { public Requirement getInstance(World world) {
setNoise(w, x, y, z); return new AirSpawn(world);
TerraWorld tw = TerraWorld.getWorld(w);
ConfigPack wc = tw.getConfig();
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeConfig c = wc.getBiome(b);
if(y <= c.getOcean().getSeaLevel()) return false;
return b.getGenerator().getNoise(getNoise(w), w, x, y, z) <= 0;
} }
}, OCEAN { }, OCEAN {
@Override @Override
public boolean matches(World w, int x, int y, int z) { public Requirement getInstance(World world) {
setNoise(w, x, y, z); return new OceanSpawn(world);
UserDefinedBiome b = (UserDefinedBiome) TerraWorld.getWorld(w).getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeConfig c = TerraWorld.getWorld(w).getConfig().getBiome(b);
if(y > c.getOcean().getSeaLevel()) return false;
return b.getGenerator().getNoise(getNoise(w), w, x, y, z) <= 0;
} }
}, LAND { }, LAND {
@Override @Override
public boolean matches(World w, int x, int y, int z) { public Requirement getInstance(World world) {
setNoise(w, x, y, z); return new LandSpawn(world);
UserDefinedBiome b = (UserDefinedBiome) TerraWorld.getWorld(w).getGrid().getBiome(x, z, GenerationPhase.POPULATE);
return b.getGenerator().getNoise(getNoise(w), w, x, y, z) > 0;
} }
}, BLANK { }, BLANK {
@Override @Override
public boolean matches(World w, int x, int y, int z) { public Requirement getInstance(World world) {
return true; return new BlankSpawn();
} }
}; };
private static final long serialVersionUID = -175639605885943679L; private static final long serialVersionUID = -175639605885943679L;
private static final transient Map<World, FastNoiseLite> noiseMap = new HashMap<>();
private static void setNoise(World w, int x, int y, int z) { public abstract Requirement getInstance(World world);
TerraWorld tw = TerraWorld.getWorld(w);
ConfigPack wc = tw.getConfig();
if(getNoise(w) == null) {
FastNoiseLite gen = new FastNoiseLite((int) w.getSeed());
gen.setNoiseType(FastNoiseLite.NoiseType.OpenSimplex2);
gen.setFractalType(FastNoiseLite.FractalType.FBm);
gen.setFractalOctaves(wc.octaves);
gen.setFrequency(wc.frequency);
putNoise(w, gen);
}
}
public static void putNoise(World w, FastNoiseLite noise) {
noiseMap.putIfAbsent(w, noise);
}
private static FastNoiseLite getNoise(World w) {
return noiseMap.get(w);
}
public abstract boolean matches(World w, int x, int y, int z);
} }

View File

@ -0,0 +1,24 @@
package com.dfsek.terra.structure.spawn;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.genconfig.biome.BiomeConfig;
import org.bukkit.World;
import org.polydev.gaea.generation.GenerationPhase;
public class AirSpawn extends Requirement {
public AirSpawn(World world) {
super(world);
}
@Override
public boolean matches(int x, int y, int z) {
TerraWorld tw = TerraWorld.getWorld(getWorld());
ConfigPack wc = tw.getConfig();
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeConfig c = wc.getBiome(b);
if(y <= c.getOcean().getSeaLevel()) return false;
return b.getGenerator().getNoise(getNoise(), getWorld(), x, y, z) <= 0;
}
}

View File

@ -0,0 +1,12 @@
package com.dfsek.terra.structure.spawn;
public class BlankSpawn extends Requirement {
public BlankSpawn() {
super(null);
}
@Override
public boolean matches(int x, int y, int z) {
return true;
}
}

View File

@ -0,0 +1,19 @@
package com.dfsek.terra.structure.spawn;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.UserDefinedBiome;
import org.bukkit.World;
import org.polydev.gaea.generation.GenerationPhase;
public class LandSpawn extends Requirement {
public LandSpawn(World world) {
super(world);
}
@Override
public boolean matches(int x, int y, int z) {
TerraWorld tw = TerraWorld.getWorld(getWorld());
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
return b.getGenerator().getNoise(getNoise(), getWorld(), x, y, z) > 0;
}
}

View File

@ -0,0 +1,22 @@
package com.dfsek.terra.structure.spawn;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.genconfig.biome.BiomeConfig;
import org.bukkit.World;
import org.polydev.gaea.generation.GenerationPhase;
public class OceanSpawn extends Requirement {
public OceanSpawn(World world) {
super(world);
}
@Override
public boolean matches(int x, int y, int z) {
TerraWorld tw = TerraWorld.getWorld(getWorld());
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeConfig c = tw.getConfig().getBiome(b);
if(y > c.getOcean().getSeaLevel()) return false;
return b.getGenerator().getNoise(getNoise(), getWorld(), x, y, z) <= 0;
}
}

View File

@ -0,0 +1,25 @@
package com.dfsek.terra.structure.spawn;
import com.dfsek.terra.generation.TerraChunkGenerator;
import org.bukkit.World;
import org.polydev.gaea.math.FastNoiseLite;
import java.util.Objects;
public abstract class Requirement {
private final World world;
public Requirement(World world) {
this.world = world;
}
public abstract boolean matches(int x, int y, int z);
protected FastNoiseLite getNoise() {
return ((TerraChunkGenerator) Objects.requireNonNull(world.getGenerator())).getNoiseGenerator();
}
public World getWorld() {
return world;
}
}