From 08d1ca02f9f4dfa0cd34a06e6df765503cb3f7e1 Mon Sep 17 00:00:00 2001 From: dfsek Date: Fri, 13 Nov 2020 12:01:20 -0700 Subject: [PATCH] Move StructureSpawnRequirements to separate classes. --- .../terra/command/structure/SpawnCommand.java | 6 +- .../com/dfsek/terra/structure/Structure.java | 2 +- .../structure/StructureSpawnRequirement.java | 65 ++++--------------- .../dfsek/terra/structure/spawn/AirSpawn.java | 24 +++++++ .../terra/structure/spawn/BlankSpawn.java | 12 ++++ .../terra/structure/spawn/LandSpawn.java | 19 ++++++ .../terra/structure/spawn/OceanSpawn.java | 22 +++++++ .../terra/structure/spawn/Requirement.java | 25 +++++++ 8 files changed, 120 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java create mode 100644 src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java create mode 100644 src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java create mode 100644 src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java create mode 100644 src/main/java/com/dfsek/terra/structure/spawn/Requirement.java diff --git a/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java b/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java index 8553ad148..7e9243cdf 100644 --- a/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java +++ b/src/main/java/com/dfsek/terra/command/structure/SpawnCommand.java @@ -24,9 +24,9 @@ public class SpawnCommand extends WorldCommand implements DebugCommand { int x = p.getBlockX(); int y = p.getBlockY(); int z = p.getBlockZ(); - boolean air = StructureSpawnRequirement.AIR.matches(world, x, y, z); - boolean ground = StructureSpawnRequirement.LAND.matches(world, x, y, z); - boolean sea = StructureSpawnRequirement.OCEAN.matches(world, x, y, z); + boolean air = StructureSpawnRequirement.AIR.getInstance(world).matches(x, y, z); + boolean ground = StructureSpawnRequirement.LAND.getInstance(world).matches(x, y, z); + boolean sea = StructureSpawnRequirement.OCEAN.getInstance(world).matches(x, y, z); sender.sendMessage("AIR: " + air + "\nLAND: " + ground + "\nOCEAN: " + sea); return true; diff --git a/src/main/java/com/dfsek/terra/structure/Structure.java b/src/main/java/com/dfsek/terra/structure/Structure.java index e7d041e60..bb5e0f0d0 100644 --- a/src/main/java/com/dfsek/terra/structure/Structure.java +++ b/src/main/java/com/dfsek/terra/structure/Structure.java @@ -277,7 +277,7 @@ public class Structure implements Serializable { public boolean checkSpawns(Location origin, Rotation r) { for(StructureContainedBlock b : spawns) { 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 true; diff --git a/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java b/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java index 812a36c21..c1c55dc08 100644 --- a/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java +++ b/src/main/java/com/dfsek/terra/structure/StructureSpawnRequirement.java @@ -1,74 +1,37 @@ package com.dfsek.terra.structure; -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 com.dfsek.terra.structure.spawn.AirSpawn; +import com.dfsek.terra.structure.spawn.BlankSpawn; +import com.dfsek.terra.structure.spawn.LandSpawn; +import com.dfsek.terra.structure.spawn.OceanSpawn; +import com.dfsek.terra.structure.spawn.Requirement; import org.bukkit.World; -import org.polydev.gaea.generation.GenerationPhase; -import org.polydev.gaea.math.FastNoiseLite; import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; public enum StructureSpawnRequirement implements Serializable { AIR { @Override - public boolean matches(World w, int x, int y, int z) { - setNoise(w, x, y, z); - 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; + public Requirement getInstance(World world) { + return new AirSpawn(world); } }, OCEAN { @Override - public boolean matches(World w, int x, int y, int z) { - setNoise(w, x, y, z); - 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; + public Requirement getInstance(World world) { + return new OceanSpawn(world); } }, LAND { @Override - public boolean matches(World w, int x, int y, int z) { - setNoise(w, x, y, z); - UserDefinedBiome b = (UserDefinedBiome) TerraWorld.getWorld(w).getGrid().getBiome(x, z, GenerationPhase.POPULATE); - return b.getGenerator().getNoise(getNoise(w), w, x, y, z) > 0; + public Requirement getInstance(World world) { + return new LandSpawn(world); } }, BLANK { @Override - public boolean matches(World w, int x, int y, int z) { - return true; + public Requirement getInstance(World world) { + return new BlankSpawn(); } }; private static final long serialVersionUID = -175639605885943679L; - private static final transient Map noiseMap = new HashMap<>(); - private static void setNoise(World w, int x, int y, int z) { - 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); + public abstract Requirement getInstance(World world); } diff --git a/src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java new file mode 100644 index 000000000..a90b29a9d --- /dev/null +++ b/src/main/java/com/dfsek/terra/structure/spawn/AirSpawn.java @@ -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; + } +} diff --git a/src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java new file mode 100644 index 000000000..f943f08da --- /dev/null +++ b/src/main/java/com/dfsek/terra/structure/spawn/BlankSpawn.java @@ -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; + } +} diff --git a/src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java new file mode 100644 index 000000000..361c17ab4 --- /dev/null +++ b/src/main/java/com/dfsek/terra/structure/spawn/LandSpawn.java @@ -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; + } +} diff --git a/src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java b/src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java new file mode 100644 index 000000000..d417d3b5c --- /dev/null +++ b/src/main/java/com/dfsek/terra/structure/spawn/OceanSpawn.java @@ -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; + } +} diff --git a/src/main/java/com/dfsek/terra/structure/spawn/Requirement.java b/src/main/java/com/dfsek/terra/structure/spawn/Requirement.java new file mode 100644 index 000000000..5c4a64248 --- /dev/null +++ b/src/main/java/com/dfsek/terra/structure/spawn/Requirement.java @@ -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; + } +}