Skip to content

Commit ef2231e

Browse files
committed
Remove save value from OreSetting, add save value check to OreGenerator and BlockSelector
Took 24 minutes
1 parent 51a7503 commit ef2231e

File tree

21 files changed

+156
-43
lines changed

21 files changed

+156
-43
lines changed

api/src/main/java/de/derfrzocker/custom/ore/generator/api/BlockSelector.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,19 @@ public interface BlockSelector {
4141
*/
4242
@NotNull
4343
String getName();
44+
45+
/**
46+
* Checks if the given value for the given oreSetting is save or not.
47+
* Save means, that passing an ore config with the given oreSetting and value to the selectBlocks method,
48+
* will not cause definitely an error.
49+
*
50+
* @param oreSetting to check
51+
* @param value to check
52+
* @param oreConfig which gets the setting set
53+
* @return true if the given value is save, otherwise return false
54+
* @throws IllegalArgumentException if oreSetting or oreConfig is null
55+
* @throws IllegalArgumentException if the BlockSelector does not need the given oreSetting
56+
*/
57+
boolean isSaveValue(@NotNull OreSetting oreSetting, double value, @NotNull OreConfig oreConfig);
58+
4459
}

api/src/main/java/de/derfrzocker/custom/ore/generator/api/OreGenerator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ public interface OreGenerator {
4545
@NotNull
4646
String getName();
4747

48+
/**
49+
* Checks if the given value for the given oreSetting is save or not.
50+
* Save means, that passing an ore config with the given oreSetting and value to the generate method,
51+
* will not cause definitely an error.
52+
*
53+
* @param oreSetting to check
54+
* @param value to check
55+
* @param oreConfig which gets the setting set
56+
* @return true if the given value is save, otherwise return false
57+
* @throws IllegalArgumentException if oreSetting or oreConfig is null
58+
* @throws IllegalArgumentException if the OreGenerator does not need the given oreSetting
59+
*/
60+
boolean isSaveValue(@NotNull OreSetting oreSetting, double value, @NotNull OreConfig oreConfig);
61+
4862
}

api/src/main/java/de/derfrzocker/custom/ore/generator/api/OreSetting.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,23 @@ public static OreSetting getOreSetting(@NotNull final String name) {
2525
}
2626

2727
/**
28-
* Create a new OreSetting with the given name and save value
28+
* Create a new OreSetting with the given name.
2929
* If a OreSetting with the same name already exist, it return's the already
3030
* existing one
3131
* The name of the OreSetting must match the following Regex: ^[A-Z_]*$
3232
* The name can be empty
3333
*
34-
* @param name of the OreSetting
35-
* @param saveValue the minimum value which the OreSetting can have
34+
* @param name of the OreSetting
3635
* @return a new OreSetting with the given name and save value, or an already existing one
3736
* @throws IllegalArgumentException if name is null
3837
* @throws IllegalArgumentException if name is empty
3938
* @throws IllegalArgumentException if the name doesn't match the following Regex: ^[A-Z_]*$
4039
*/
4140
@NotNull
42-
public static OreSetting createOreSetting(@NotNull final String name, final double saveValue) {
41+
public static OreSetting createOreSetting(@NotNull final String name) {
4342
Validate.notNull(name, "Name can't be null");
4443

45-
return ORE_SETTINGS.computeIfAbsent(name, name2 -> new OreSetting(name, saveValue));
44+
return ORE_SETTINGS.computeIfAbsent(name, name2 -> new OreSetting(name));
4645
}
4746

4847
/**
@@ -54,15 +53,13 @@ public static Set<OreSetting> getOreSettings() {
5453

5554
@NotNull
5655
private final String name;
57-
private final double saveValue;
5856

59-
private OreSetting(@NotNull final String name, final double saveValue) {
57+
private OreSetting(@NotNull final String name) {
6058
Validate.notNull(name, "Name can't be null");
6159
Validate.notEmpty(name, "Name can't be empty");
6260
Validate.isTrue(NAME_PATTER.matcher(name).matches(), "Name " + name + " doesn't match the regex: ^[A-Z_]*$");
6361

6462
this.name = name;
65-
this.saveValue = saveValue;
6663
}
6764

6865
/**
@@ -76,13 +73,6 @@ public String getName() {
7673
return name;
7774
}
7875

79-
/**
80-
* @return the save value of this OreSetting
81-
*/
82-
public double getSaveValue() {
83-
return saveValue;
84-
}
85-
8676
@Override
8777
public boolean equals(@Nullable final Object obj) {
8878
if (!(obj instanceof OreSetting))
@@ -93,12 +83,12 @@ public boolean equals(@Nullable final Object obj) {
9383

9484
final OreSetting other = (OreSetting) obj;
9585

96-
return other.getName().equals(getName()) && other.getSaveValue() == getSaveValue();
86+
return other.getName().equals(getName());
9787
}
9888

9989
@Override
10090
public int hashCode() {
101-
return Objects.hash(name, saveValue);
91+
return Objects.hash(name);
10292
}
10393

10494
}

api/src/main/java/de/derfrzocker/custom/ore/generator/api/OreSettings.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*/
66
public final class OreSettings {
77

8-
public final static OreSetting VEIN_SIZE = OreSetting.createOreSetting("VEIN_SIZE", 0);
9-
public final static OreSetting VEINS_PER_CHUNK = OreSetting.createOreSetting("VEINS_PER_CHUNK", 0);
10-
public final static OreSetting HEIGHT_RANGE = OreSetting.createOreSetting("HEIGHT_RANGE", -1);
11-
public final static OreSetting HEIGHT_CENTER = OreSetting.createOreSetting("HEIGHT_CENTER", -1);
12-
public final static OreSetting MINIMUM_ORES_PER_CHUNK = OreSetting.createOreSetting("MINIMUM_ORES_PER_CHUNK", -1);
13-
public final static OreSetting ORES_PER_CHUNK_RANGE = OreSetting.createOreSetting("ORES_PER_CHUNK_RANGE", -1);
14-
public final static OreSetting MINIMUM_HEIGHT = OreSetting.createOreSetting("MINIMUM_HEIGHT", 0);
15-
public final static OreSetting HEIGHT_SUBTRACT_VALUE = OreSetting.createOreSetting("HEIGHT_SUBTRACT_VALUE", -1);
8+
public final static OreSetting VEIN_SIZE = OreSetting.createOreSetting("VEIN_SIZE");
9+
public final static OreSetting VEINS_PER_CHUNK = OreSetting.createOreSetting("VEINS_PER_CHUNK");
10+
public final static OreSetting HEIGHT_RANGE = OreSetting.createOreSetting("HEIGHT_RANGE");
11+
public final static OreSetting HEIGHT_CENTER = OreSetting.createOreSetting("HEIGHT_CENTER");
12+
public final static OreSetting MINIMUM_ORES_PER_CHUNK = OreSetting.createOreSetting("MINIMUM_ORES_PER_CHUNK");
13+
public final static OreSetting ORES_PER_CHUNK_RANGE = OreSetting.createOreSetting("ORES_PER_CHUNK_RANGE");
14+
public final static OreSetting MINIMUM_HEIGHT = OreSetting.createOreSetting("MINIMUM_HEIGHT");
15+
public final static OreSetting HEIGHT_SUBTRACT_VALUE = OreSetting.createOreSetting("HEIGHT_SUBTRACT_VALUE");
1616

1717
private OreSettings() {
1818
}

custom-ore-generator/src/main/java/de/derfrzocker/custom/ore/generator/impl/blockselector/CountRangeBlockSelector.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.Sets;
44
import de.derfrzocker.custom.ore.generator.api.*;
55
import de.derfrzocker.spigot.utils.NumberUtil;
6+
import org.apache.commons.lang.Validate;
67
import org.bukkit.Location;
78
import org.jetbrains.annotations.NotNull;
89

@@ -20,9 +21,12 @@ public class CountRangeBlockSelector implements BlockSelector {
2021
public Set<Location> selectBlocks(@NotNull final ChunkInfo chunkInfo, @NotNull final OreConfig config, @NotNull final Random random) {
2122
final Set<Location> locations = new HashSet<>();
2223

23-
final int heightRange = NumberUtil.getInt(config.getValue(OreSettings.HEIGHT_RANGE).orElse(OreSettings.HEIGHT_RANGE.getSaveValue()), random);
24-
final int minimumHeight = NumberUtil.getInt(config.getValue(OreSettings.MINIMUM_HEIGHT).orElse(OreSettings.MINIMUM_HEIGHT.getSaveValue()), random);
25-
final int veinsPerChunk = NumberUtil.getInt(config.getValue(OreSettings.VEINS_PER_CHUNK).orElse(OreSettings.VEINS_PER_CHUNK.getSaveValue()), random);
24+
final int heightRange = NumberUtil.getInt(config.getValue(OreSettings.HEIGHT_RANGE).orElse(0d), random);
25+
final int minimumHeight = NumberUtil.getInt(config.getValue(OreSettings.MINIMUM_HEIGHT).orElse(0d), random);
26+
final int veinsPerChunk = NumberUtil.getInt(config.getValue(OreSettings.VEINS_PER_CHUNK).orElse(0d), random);
27+
28+
if(heightRange == 0)
29+
return locations;
2630

2731
for (int i = 0; i < veinsPerChunk; i++) {
2832
final int x = random.nextInt(16);
@@ -47,4 +51,22 @@ public String getName() {
4751
return "COUNT_RANGE";
4852
}
4953

54+
@Override
55+
public boolean isSaveValue(@NotNull final OreSetting oreSetting, final double value, @NotNull final OreConfig oreConfig) {
56+
Validate.notNull(oreSetting, "OreSetting can not be null");
57+
Validate.notNull(oreConfig, "OreConfig can not be null");
58+
Validate.isTrue(neededOreSettings.contains(oreSetting), "The BlockSelector '" + getName() + "' does not need the OreSetting '" + oreSetting.getName() + "'");
59+
60+
if(oreSetting == OreSettings.HEIGHT_RANGE)
61+
return value >= 0;
62+
63+
if(oreSetting == OreSettings.MINIMUM_HEIGHT)
64+
return value >= 0;
65+
66+
if(oreSetting == OreSettings.VEINS_PER_CHUNK)
67+
return value >= 0;
68+
69+
throw new RuntimeException("Wtf?");
70+
}
71+
5072
}

custom-ore-generator/src/main/java/de/derfrzocker/custom/ore/generator/impl/blockselector/HighestBlockBlockSelector.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.Sets;
44
import de.derfrzocker.custom.ore.generator.api.*;
55
import de.derfrzocker.spigot.utils.NumberUtil;
6+
import org.apache.commons.lang.Validate;
67
import org.bukkit.Location;
78
import org.jetbrains.annotations.NotNull;
89

@@ -20,7 +21,7 @@ public class HighestBlockBlockSelector implements BlockSelector {
2021
public Set<Location> selectBlocks(@NotNull final ChunkInfo chunkInfo, @NotNull final OreConfig config, @NotNull final Random random) {
2122
final Set<Location> locations = new HashSet<>();
2223

23-
final int veinsPerChunk = NumberUtil.getInt(config.getValue(OreSettings.VEINS_PER_CHUNK).orElse(OreSettings.VEINS_PER_CHUNK.getSaveValue()), random);
24+
final int veinsPerChunk = NumberUtil.getInt(config.getValue(OreSettings.VEINS_PER_CHUNK).orElse(0d), random);
2425

2526
for (int i = 0; i < veinsPerChunk; i++) {
2627
final int x = random.nextInt(16);
@@ -44,4 +45,13 @@ public String getName() {
4445
return "HIGHEST_BLOCK";
4546
}
4647

48+
@Override
49+
public boolean isSaveValue(@NotNull final OreSetting oreSetting, final double value, @NotNull final OreConfig oreConfig) {
50+
Validate.notNull(oreSetting, "OreSetting can not be null");
51+
Validate.notNull(oreConfig, "OreConfig can not be null");
52+
Validate.isTrue(neededOreSettings.contains(oreSetting), "The BlockSelector '" + getName() + "' does not need the OreSetting '" + oreSetting.getName() + "'");
53+
54+
return value >= 0;
55+
}
56+
4757
}

impl/ore-generators/src/main/java/de/derfrzocker/custom/ore/generator/impl/oregenerator/AbstractMinableGenerator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package de.derfrzocker.custom.ore.generator.impl.oregenerator;
22

33
import com.google.common.collect.Sets;
4+
import de.derfrzocker.custom.ore.generator.api.OreConfig;
45
import de.derfrzocker.custom.ore.generator.api.OreGenerator;
56
import de.derfrzocker.custom.ore.generator.api.OreSetting;
67
import de.derfrzocker.custom.ore.generator.api.OreSettings;
8+
import org.apache.commons.lang.Validate;
79
import org.jetbrains.annotations.NotNull;
810

911
import java.util.Collections;
@@ -25,4 +27,13 @@ public String getName() {
2527
return "VANILLA_MINABLE_GENERATOR";
2628
}
2729

30+
@Override
31+
public boolean isSaveValue(@NotNull final OreSetting oreSetting, final double value, @NotNull final OreConfig oreConfig) {
32+
Validate.notNull(oreSetting, "OreSetting can not be null");
33+
Validate.notNull(oreConfig, "OreConfig can not be null");
34+
Validate.isTrue(neededOreSettings.contains(oreSetting), "The OreGenerator '" + getName() + "' does not need the OreSetting '" + oreSetting.getName() + "'");
35+
36+
return value >= 0;
37+
}
38+
2839
}

impl/ore-generators/src/main/java/de/derfrzocker/custom/ore/generator/impl/oregenerator/AbstractSingleOreGenerator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package de.derfrzocker.custom.ore.generator.impl.oregenerator;
22

3+
import de.derfrzocker.custom.ore.generator.api.OreConfig;
34
import de.derfrzocker.custom.ore.generator.api.OreGenerator;
45
import de.derfrzocker.custom.ore.generator.api.OreSetting;
6+
import org.apache.commons.lang.Validate;
57
import org.jetbrains.annotations.NotNull;
68

79
import java.util.Collections;
@@ -24,4 +26,13 @@ public String getName() {
2426
return "SINGLE_ORE_GENERATOR";
2527
}
2628

29+
@Override
30+
public boolean isSaveValue(@NotNull final OreSetting oreSetting, final double value, @NotNull final OreConfig oreConfig) {
31+
Validate.notNull(oreSetting, "OreSetting can not be null");
32+
Validate.notNull(oreConfig, "OreConfig can not be null");
33+
Validate.isTrue(neededOreSettings.contains(oreSetting), "The OreGenerator '" + getName() + "' does not need the OreSetting '" + oreSetting.getName() + "'");
34+
35+
throw new IllegalArgumentException("The OreGenerator '" + getName() + "' does not need any OreSetting");
36+
}
37+
2738
}

impl/v1_10_R1/src/main/java/de/derfrzocker/custom/ore/generator/impl/v1_10_R1/oregenerator/MinableGenerator_v1_10_R1.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public class MinableGenerator_v1_10_R1 extends AbstractMinableGenerator {
2222

2323
@Override
2424
public void generate(@NotNull final OreConfig config, @NotNull final ChunkAccess chunkAccess, final int x, final int z, @NotNull final Random random, @NotNull final Biome biome, @NotNull final Set<Location> locations) {
25-
final int veinSize = NumberUtil.getInt(config.getValue(OreSettings.VEIN_SIZE).orElse(OreSettings.VEIN_SIZE.getSaveValue()), random);
25+
final int veinSize = NumberUtil.getInt(config.getValue(OreSettings.VEIN_SIZE).orElse(0d), random);
26+
27+
if(veinSize == 0)
28+
return;
2629

2730
final WorldServer worldServer = ((ChunkAccessImpl) chunkAccess).getWorldServer();
2831

impl/v1_11_R1/src/main/java/de/derfrzocker/custom/ore/generator/impl/v1_11_R1/oregenerator/MinableGenerator_v1_11_R1.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public class MinableGenerator_v1_11_R1 extends AbstractMinableGenerator {
2222

2323
@Override
2424
public void generate(@NotNull final OreConfig config, @NotNull final ChunkAccess chunkAccess, final int x, final int z, @NotNull final Random random, @NotNull final Biome biome, @NotNull final Set<Location> locations) {
25-
final int veinSize = NumberUtil.getInt(config.getValue(OreSettings.VEIN_SIZE).orElse(OreSettings.VEIN_SIZE.getSaveValue()), random);
25+
final int veinSize = NumberUtil.getInt(config.getValue(OreSettings.VEIN_SIZE).orElse(0d), random);
26+
27+
if(veinSize == 0)
28+
return;
2629

2730
final WorldServer worldServer = ((ChunkAccessImpl) chunkAccess).getWorldServer();
2831

0 commit comments

Comments
 (0)