Skip to content

Commit 2c4610b

Browse files
committed
add a bit of encapsulation and simplification here so we can add more parameters and not have to really worry
1 parent 1f0c19b commit 2c4610b

File tree

8 files changed

+169
-70
lines changed

8 files changed

+169
-70
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.mcmoddev.orespawn.api;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.mcmoddev.orespawn.util.OreList;
7+
import net.minecraft.block.state.IBlockState;
8+
import net.minecraft.util.math.ChunkPos;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class GeneratorParameters {
14+
15+
private final ChunkPos chunk;
16+
private final OreList ores;
17+
private final ImmutableList<IBlockState> replacements;
18+
private final JsonObject parameters;
19+
private final BiomeLocation biomes;
20+
21+
public GeneratorParameters( ChunkPos chunkPos, OreList oreList, List<IBlockState> replacementBlocks,
22+
BiomeLocation biomes, JsonObject generatorParameters) {
23+
this.parameters = new JsonObject();
24+
for ( Map.Entry<String, JsonElement> stringJsonElementEntry : generatorParameters.entrySet() ) {
25+
this.parameters.add(stringJsonElementEntry.getKey(), stringJsonElementEntry.getValue());
26+
}
27+
28+
this.chunk = new ChunkPos( chunkPos.x, chunkPos.z );
29+
this.ores = oreList;
30+
this.replacements = ImmutableList.copyOf( replacementBlocks );
31+
this.biomes = biomes;
32+
}
33+
34+
public ChunkPos getChunk () {
35+
return chunk;
36+
}
37+
38+
public BiomeLocation getBiomes () {
39+
return biomes;
40+
}
41+
42+
public ImmutableList<IBlockState> getReplacements () {
43+
return replacements;
44+
}
45+
46+
public JsonObject getParameters () {
47+
return parameters;
48+
}
49+
50+
public OreList getOres () {
51+
return ores;
52+
}
53+
54+
}

src/main/java/com/mcmoddev/orespawn/api/IFeature.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import net.minecraft.world.chunk.IChunkProvider;
1414

1515
public interface IFeature {
16-
void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator,
17-
IChunkProvider chunkProvider, JsonObject parameters, OreList ores, List<IBlockState> blockReplace,
18-
BiomeLocation biomes );
16+
void generate( World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
17+
GeneratorParameters parameters );
1918

2019
void setRandom(Random rand);
2120

src/main/java/com/mcmoddev/orespawn/impl/features/ClusterGenerator.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.mcmoddev.orespawn.impl.features;
22

3+
import java.util.Collections;
4+
import java.util.LinkedList;
35
import java.util.List;
46
import java.util.Random;
57

68
import com.google.gson.JsonObject;
79
import com.mcmoddev.orespawn.OreSpawn;
810
import com.mcmoddev.orespawn.api.BiomeLocation;
911
import com.mcmoddev.orespawn.api.FeatureBase;
12+
import com.mcmoddev.orespawn.api.GeneratorParameters;
1013
import com.mcmoddev.orespawn.api.IFeature;
1114
import com.mcmoddev.orespawn.data.Constants;
1215
import com.mcmoddev.orespawn.util.OreList;
@@ -30,13 +33,20 @@ public ClusterGenerator() {
3033
}
3134

3235
@Override
33-
public void generate( ChunkPos pos, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
34-
JsonObject parameters, OreList ores, List<IBlockState> blockReplace, BiomeLocation biomes) {
36+
public void generate( World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
37+
GeneratorParameters parameters ) {
38+
ChunkPos pos = parameters.getChunk();
39+
List<IBlockState> blockReplace = new LinkedList<>();
40+
blockReplace.addAll( parameters.getReplacements() );
41+
JsonObject params = parameters.getParameters();
42+
OreList ores = parameters.getOres();
43+
BiomeLocation biomes = parameters.getBiomes();
44+
3545
// First, load cached blocks for neighboring chunk ore spawns
3646
int chunkX = pos.x;
3747
int chunkZ = pos.z;
3848

39-
mergeDefaults(parameters, getDefaultParameters());
49+
mergeDefaults(params, getDefaultParameters());
4050

4151
runCache(chunkX, chunkZ, world, blockReplace);
4252

@@ -45,14 +55,14 @@ public void generate( ChunkPos pos, World world, IChunkGenerator chunkGenerator,
4555
int blockX = chunkX * 16 + 8;
4656
int blockZ = chunkZ * 16 + 8;
4757

48-
int maxSpread = parameters.get(Constants.FormatBits.MAX_SPREAD).getAsInt();
49-
int minHeight = parameters.get(Constants.FormatBits.MIN_HEIGHT).getAsInt();
50-
int maxHeight = parameters.get(Constants.FormatBits.MAX_HEIGHT).getAsInt();
51-
int variance = parameters.get(Constants.FormatBits.VARIATION).getAsInt();
52-
int frequency = parameters.get(Constants.FormatBits.FREQUENCY).getAsInt();
53-
int tries = parameters.get(Constants.FormatBits.ATTEMPTS).getAsInt();
54-
int clusterSize = parameters.get(Constants.FormatBits.NODE_SIZE).getAsInt();
55-
int clusterCount = parameters.get(Constants.FormatBits.NODE_COUNT).getAsInt();
58+
int maxSpread = params.get(Constants.FormatBits.MAX_SPREAD).getAsInt();
59+
int minHeight = params.get(Constants.FormatBits.MIN_HEIGHT).getAsInt();
60+
int maxHeight = params.get(Constants.FormatBits.MAX_HEIGHT).getAsInt();
61+
int variance = params.get(Constants.FormatBits.VARIATION).getAsInt();
62+
int frequency = params.get(Constants.FormatBits.FREQUENCY).getAsInt();
63+
int tries = params.get(Constants.FormatBits.ATTEMPTS).getAsInt();
64+
int clusterSize = params.get(Constants.FormatBits.NODE_SIZE).getAsInt();
65+
int clusterCount = params.get(Constants.FormatBits.NODE_COUNT).getAsInt();
5666

5767
while( tries > 0 ) {
5868
if( this.random.nextInt(100) <= frequency ) {
@@ -62,9 +72,9 @@ public void generate( ChunkPos pos, World world, IChunkGenerator chunkGenerator,
6272
int x = blockX + xRand - (maxSpread / 2);
6373
int y = random.nextInt(maxHeight - minHeight) + minHeight;
6474
int z = blockZ + zRand - (maxSpread / 2);
65-
int[] params = new int[] { clusterSize, variance, clusterCount, maxSpread, minHeight, maxHeight};
75+
int[] keyParams = new int[] { clusterSize, variance, clusterCount, maxSpread, minHeight, maxHeight};
6676

67-
spawnCluster(ores, new BlockPos(x,y,z), params, random, world, blockReplace, biomes);
77+
spawnCluster(ores, new BlockPos(x,y,z), keyParams, random, world, blockReplace, biomes);
6878
}
6979
tries--;
7080
}

src/main/java/com/mcmoddev/orespawn/impl/features/DefaultFeatureGenerator.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.mcmoddev.orespawn.impl.features;
22

3+
import java.util.LinkedList;
34
import java.util.List;
45
import java.util.Random;
56

67
import com.google.gson.JsonObject;
78
import com.mcmoddev.orespawn.api.BiomeLocation;
89
import com.mcmoddev.orespawn.api.FeatureBase;
10+
import com.mcmoddev.orespawn.api.GeneratorParameters;
911
import com.mcmoddev.orespawn.api.IFeature;
1012
import com.mcmoddev.orespawn.data.Constants;
1113
import com.mcmoddev.orespawn.util.OreList;
@@ -24,17 +26,22 @@ public class DefaultFeatureGenerator extends FeatureBase implements IFeature {
2426
public DefaultFeatureGenerator() {
2527
super( new Random() );
2628
}
27-
28-
29+
2930
@Override
30-
public void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator,
31-
IChunkProvider chunkProvider, JsonObject parameters, OreList ores, List<IBlockState> replaceBlock,
32-
BiomeLocation biomes ) {
31+
public void generate( World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
32+
GeneratorParameters parameters ) {
33+
ChunkPos pos = parameters.getChunk();
34+
List<IBlockState> replaceBlock = new LinkedList<>();
35+
replaceBlock.addAll( parameters.getReplacements() );
36+
JsonObject params = parameters.getParameters();
37+
OreList ores = parameters.getOres();
38+
BiomeLocation biomes = parameters.getBiomes();
39+
3340
// First, load cached blocks for neighboring chunk ore spawns
3441
int chunkX = pos.x;
3542
int chunkZ = pos.z;
3643

37-
mergeDefaults(parameters, getDefaultParameters());
44+
mergeDefaults(params, getDefaultParameters());
3845

3946
runCache(chunkX, chunkZ, world, replaceBlock);
4047

@@ -43,11 +50,11 @@ public void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator,
4350
int blockX = chunkX * 16 + 8;
4451
int blockZ = chunkZ * 16 + 8;
4552

46-
int minY = parameters.get(Constants.FormatBits.MIN_HEIGHT).getAsInt();
47-
int maxY = parameters.get(Constants.FormatBits.MAX_HEIGHT).getAsInt();
48-
int vari = parameters.get(Constants.FormatBits.VARIATION).getAsInt();
49-
float freq = parameters.get(Constants.FormatBits.FREQUENCY).getAsFloat();
50-
int size = parameters.get(Constants.FormatBits.NODE_SIZE).getAsInt();
53+
int minY = params.get(Constants.FormatBits.MIN_HEIGHT).getAsInt();
54+
int maxY = params.get(Constants.FormatBits.MAX_HEIGHT).getAsInt();
55+
int vari = params.get(Constants.FormatBits.VARIATION).getAsInt();
56+
float freq = params.get(Constants.FormatBits.FREQUENCY).getAsFloat();
57+
int size = params.get(Constants.FormatBits.NODE_SIZE).getAsInt();
5158

5259
if(freq >= 1){
5360
for(int i = 0; i < freq; i++){
@@ -103,18 +110,18 @@ private void spawnOre ( BlockPos blockPos, OreList possibleOres, int quantity, W
103110

104111
private void doSpawnFill ( boolean nextBoolean, World world, BlockPos blockPos, int quantity, List<IBlockState> replaceBlock, OreList possibleOres, BiomeLocation biomes ) {
105112
double radius = Math.pow(quantity, 1.0/3.0) * (3.0 / 4.0 / Math.PI) + 2;
106-
int rSqr = (int)(radius * radius);
107113
if( nextBoolean ) {
108-
spawnMungeNE( world, blockPos, rSqr, radius, replaceBlock, quantity, possibleOres, biomes );
114+
spawnMungeNE( world, blockPos, radius, replaceBlock, quantity, possibleOres, biomes );
109115
} else {
110-
spawnMungeSW( world, blockPos, rSqr, radius, replaceBlock, quantity, possibleOres, biomes );
116+
spawnMungeSW( world, blockPos, radius, replaceBlock, quantity, possibleOres, biomes );
111117
}
112118
}
113119

114120

115-
private void spawnMungeSW ( World world, BlockPos blockPos, int rSqr, double radius,
121+
private void spawnMungeSW ( World world, BlockPos blockPos, double radius,
116122
List<IBlockState> replaceBlock, int count, OreList possibleOres, BiomeLocation biomes ) {
117123
Random prng = this.random;
124+
int rSqr = (int)(radius * radius);
118125
int quantity = count;
119126
for(int dy = (int)(-1 * radius); dy < radius; dy++){
120127
for(int dx = (int)(radius); dx >= (int)(-1 * radius); dx--){
@@ -133,9 +140,10 @@ private void spawnMungeSW ( World world, BlockPos blockPos, int rSqr, double rad
133140
}
134141

135142

136-
private void spawnMungeNE ( World world, BlockPos blockPos, int rSqr, double radius,
143+
private void spawnMungeNE ( World world, BlockPos blockPos, double radius,
137144
List<IBlockState> replaceBlock, int count, OreList possibleOres, BiomeLocation biomes ) {
138145
Random prng = this.random;
146+
int rSqr = (int)(radius * radius);
139147
int quantity = count;
140148
for(int dy = (int)(-1 * radius); dy < radius; dy++){
141149
for(int dz = (int)(-1 * radius); dz < radius; dz++){

src/main/java/com/mcmoddev/orespawn/impl/features/NormalCloudGenerator.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.mcmoddev.orespawn.impl.features;
22

3+
import java.util.LinkedList;
34
import java.util.List;
45
import java.util.Random;
56

67
import com.google.gson.JsonObject;
78
import com.mcmoddev.orespawn.OreSpawn;
89
import com.mcmoddev.orespawn.api.BiomeLocation;
910
import com.mcmoddev.orespawn.api.FeatureBase;
11+
import com.mcmoddev.orespawn.api.GeneratorParameters;
1012
import com.mcmoddev.orespawn.api.IFeature;
1113
import com.mcmoddev.orespawn.data.Constants;
1214
import com.mcmoddev.orespawn.util.OreList;
@@ -27,15 +29,21 @@ private NormalCloudGenerator ( Random rand ) {
2729
public NormalCloudGenerator() {
2830
this( new Random() );
2931
}
30-
32+
3133
@Override
32-
public void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
33-
JsonObject parameters, OreList ores, List<IBlockState> blockReplace, BiomeLocation biomes) {
34+
public void generate( World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
35+
GeneratorParameters parameters ) {
36+
ChunkPos pos = parameters.getChunk();
37+
List<IBlockState> blockReplace = new LinkedList<>();
38+
blockReplace.addAll( parameters.getReplacements() );
39+
JsonObject params = parameters.getParameters();
40+
OreList ores = parameters.getOres();
41+
BiomeLocation biomes = parameters.getBiomes();
3442
// First, load cached blocks for neighboring chunk ore spawns
3543
int chunkX = pos.x;
3644
int chunkZ = pos.z;
3745

38-
mergeDefaults(parameters, getDefaultParameters());
46+
mergeDefaults(params, getDefaultParameters());
3947

4048
runCache(chunkX, chunkZ, world, blockReplace);
4149

@@ -45,13 +53,13 @@ public void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator,
4553
int blockX = chunkX * 16;
4654
int blockZ = chunkZ * 16;
4755

48-
int maxSpread = parameters.get(Constants.FormatBits.MAX_SPREAD).getAsInt();
49-
int medianSize = parameters.get(Constants.FormatBits.MEDIAN_SIZE).getAsInt();
50-
int minHeight = parameters.get(Constants.FormatBits.MIN_HEIGHT).getAsInt();
51-
int maxHeight = parameters.get(Constants.FormatBits.MAX_HEIGHT).getAsInt();
52-
int variance = parameters.get(Constants.FormatBits.VARIATION).getAsInt();
53-
int frequency = parameters.get(Constants.FormatBits.FREQUENCY).getAsInt();
54-
int tries = parameters.get(Constants.FormatBits.ATTEMPTS).getAsInt();
56+
int maxSpread = params.get(Constants.FormatBits.MAX_SPREAD).getAsInt();
57+
int medianSize = params.get(Constants.FormatBits.MEDIAN_SIZE).getAsInt();
58+
int minHeight = params.get(Constants.FormatBits.MIN_HEIGHT).getAsInt();
59+
int maxHeight = params.get(Constants.FormatBits.MAX_HEIGHT).getAsInt();
60+
int variance = params.get(Constants.FormatBits.VARIATION).getAsInt();
61+
int frequency = params.get(Constants.FormatBits.FREQUENCY).getAsInt();
62+
int tries = params.get(Constants.FormatBits.ATTEMPTS).getAsInt();
5563

5664
// on the X and Z you have a possible 2-chunk range - 32 blocks - subtract the spread to get
5765
// a size that will let us insert by the radius

src/main/java/com/mcmoddev/orespawn/impl/features/PrecisionGenerator.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.mcmoddev.orespawn.impl.features;
22

33
import java.util.ArrayList;
4+
import java.util.LinkedList;
45
import java.util.List;
56
import java.util.Random;
67

78
import com.google.gson.JsonObject;
89
import com.mcmoddev.orespawn.OreSpawn;
910
import com.mcmoddev.orespawn.api.BiomeLocation;
1011
import com.mcmoddev.orespawn.api.FeatureBase;
12+
import com.mcmoddev.orespawn.api.GeneratorParameters;
1113
import com.mcmoddev.orespawn.api.IFeature;
1214
import com.mcmoddev.orespawn.data.Constants.FormatBits;
1315
import com.mcmoddev.orespawn.util.OreList;
@@ -29,24 +31,31 @@ private PrecisionGenerator(Random rand) {
2931
public PrecisionGenerator() {
3032
this( new Random() );
3133
}
32-
34+
3335

3436
@Override
35-
public void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
36-
JsonObject parameters, OreList ores, List<IBlockState> blockReplace, BiomeLocation biomes) {
37+
public void generate( World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider,
38+
GeneratorParameters parameters ) {
39+
ChunkPos pos = parameters.getChunk();
40+
List<IBlockState> blockReplace = new LinkedList<>();
41+
blockReplace.addAll( parameters.getReplacements() );
42+
JsonObject params = parameters.getParameters();
43+
OreList ores = parameters.getOres();
44+
BiomeLocation biomes = parameters.getBiomes();
45+
3746
// First, load cached blocks for neighboring chunk ore spawns
3847
int chunkX = pos.x;
3948
int chunkZ = pos.z;
4049

41-
mergeDefaults(parameters, getDefaultParameters());
50+
mergeDefaults(params, getDefaultParameters());
4251

4352
runCache(chunkX, chunkZ, world, blockReplace);
4453

4554
// extract parameters
46-
int nodeCount = parameters.get(FormatBits.NODE_COUNT).getAsInt();
47-
int maxHeight = parameters.get(FormatBits.MAX_HEIGHT).getAsInt();
48-
int minHeight = parameters.get(FormatBits.MIN_HEIGHT).getAsInt();
49-
int nodeSize = parameters.get(FormatBits.NODE_SIZE).getAsInt();
55+
int nodeCount = params.get(FormatBits.NODE_COUNT).getAsInt();
56+
int maxHeight = params.get(FormatBits.MAX_HEIGHT).getAsInt();
57+
int minHeight = params.get(FormatBits.MIN_HEIGHT).getAsInt();
58+
int nodeSize = params.get(FormatBits.NODE_SIZE).getAsInt();
5059

5160
int thisNode = nodeSize;
5261

0 commit comments

Comments
 (0)