Skip to content

Commit dd701b3

Browse files
authored
Add support for height ranges in generators and blocks; update legacy templates compatibility; and fix color formats lang es (#145)
1 parent f051600 commit dd701b3

11 files changed

Lines changed: 1375 additions & 525 deletions

File tree

src/main/java/world/bentobox/magiccobblestonegenerator/database/objects/GeneratorTierObject.java

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
import java.util.ArrayList;
1111
import java.util.Collections;
12+
import java.util.HashMap;
1213
import java.util.HashSet;
1314
import java.util.List;
15+
import java.util.Map;
1416
import java.util.Set;
1517
import java.util.TreeMap;
1618

@@ -511,6 +513,9 @@ public GeneratorTierObject clone()
511513

512514
clone.setTreasureChance(this.treasureChance);
513515
clone.setMaxTreasureAmount(this.maxTreasureAmount);
516+
clone.setMinHeight(this.minHeight);
517+
clone.setMaxHeight(this.maxHeight);
518+
clone.setMaterialHeightMap(new TreeMap<>(this.materialHeightMap));
514519

515520
return clone;
516521
}
@@ -682,5 +687,136 @@ public boolean includes(GeneratorType type)
682687
*/
683688
@Expose
684689
private int maxTreasureAmount = 1;
685-
}
686690

691+
/**
692+
* Minimum height at which this generator can operate.
693+
*/
694+
@Expose
695+
private int minHeight = -64;
696+
697+
/**
698+
* Maximum height at which this generator can operate.
699+
*/
700+
@Expose
701+
private int maxHeight = 320;
702+
703+
/**
704+
* Map that stores min and max heights for each material in the generator.
705+
*/
706+
@Expose
707+
private TreeMap<Material, int[]> materialHeightMap = new TreeMap<>();
708+
709+
/**
710+
* Field to store block height ranges
711+
*/
712+
@Expose
713+
private Map<Material, int[]> blockHeightRanges = new HashMap<>();
714+
715+
/**
716+
* Gets the height ranges for each block type
717+
* @return Map of Material to height range array [min, max]
718+
*/
719+
public Map<Material, int[]> getBlockHeightRanges()
720+
{
721+
return blockHeightRanges;
722+
}
723+
724+
/**
725+
* Sets the height ranges for each block type
726+
* @param blockHeightRanges Map of Material to height range array [min, max]
727+
*/
728+
public void setBlockHeightRanges(Map<Material, int[]> blockHeightRanges)
729+
{
730+
this.blockHeightRanges = blockHeightRanges;
731+
}
732+
733+
/**
734+
* Gets the minimum height at which this generator can operate.
735+
*
736+
* @return the minimum height
737+
*/
738+
public int getMinHeight()
739+
{
740+
return this.minHeight;
741+
}
742+
743+
744+
/**
745+
* Sets the minimum height at which this generator can operate.
746+
*
747+
* @param minHeight the minimum height
748+
*/
749+
public void setMinHeight(int minHeight)
750+
{
751+
this.minHeight = minHeight;
752+
}
753+
754+
755+
/**
756+
* Gets the maximum height at which this generator can operate.
757+
*
758+
* @return the maximum height
759+
*/
760+
public int getMaxHeight()
761+
{
762+
return this.maxHeight;
763+
}
764+
765+
766+
/**
767+
* Sets the maximum height at which this generator can operate.
768+
*
769+
* @param maxHeight the maximum height
770+
*/
771+
public void setMaxHeight(int maxHeight)
772+
{
773+
this.maxHeight = maxHeight;
774+
}
775+
776+
777+
/**
778+
* Gets the map of material-specific height ranges.
779+
*
780+
* @return the material height map
781+
*/
782+
public TreeMap<Material, int[]> getMaterialHeightMap()
783+
{
784+
return this.materialHeightMap;
785+
}
786+
787+
788+
/**
789+
* Sets the map of material-specific height ranges.
790+
*
791+
* @param materialHeightMap the material height map
792+
*/
793+
public void setMaterialHeightMap(TreeMap<Material, int[]> materialHeightMap)
794+
{
795+
this.materialHeightMap = materialHeightMap;
796+
}
797+
798+
799+
/**
800+
* Gets the height range for a specific material.
801+
*
802+
* @param material the material
803+
* @return an array where index 0 is minHeight and index 1 is maxHeight, or null if not set
804+
*/
805+
public int[] getMaterialHeightRange(Material material)
806+
{
807+
return this.materialHeightMap.get(material);
808+
}
809+
810+
811+
/**
812+
* Sets the height range for a specific material.
813+
*
814+
* @param material the material
815+
* @param minHeight the minimum height
816+
* @param maxHeight the maximum height
817+
*/
818+
public void setMaterialHeightRange(Material material, int minHeight, int maxHeight)
819+
{
820+
this.materialHeightMap.put(material, new int[]{minHeight, maxHeight});
821+
}
822+
}

src/main/java/world/bentobox/magiccobblestonegenerator/managers/StoneGeneratorImportManager.java

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ private void createGenerators(YamlConfiguration config, @Nullable User user, Gam
201201
// Set activation cost
202202
generatorTier.setActivationCost(details.getDouble("activation-cost", 0.0));
203203

204+
// Set global height range
205+
ConfigurationSection heightRange = details.getConfigurationSection("height_range");
206+
if (heightRange != null) {
207+
generatorTier.setMinHeight(heightRange.getInt("min", 0));
208+
generatorTier.setMaxHeight(heightRange.getInt("max", 256));
209+
}
210+
204211
// Search and read requirements only if it is not default generator.
205212
if (!generatorTier.isDefaultGenerator()) {
206213
this.populateRequirements(generatorTier, details.getConfigurationSection("requirements"), biomeMap);
@@ -305,23 +312,47 @@ private void populateRequirements(GeneratorTierObject generatorTier, Configurati
305312
* @param materials Config that contains data.
306313
*/
307314
private void populateMaterials(GeneratorTierObject generatorTier, ConfigurationSection materials) {
308-
if (materials != null) {
309-
TreeMap<Double, Material> blockChances = new TreeMap<>();
310-
311-
for (String materialKey : materials.getKeys(false)) {
312-
try {
313-
Material material = Material.valueOf(materialKey.toUpperCase());
314-
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
315-
blockChances.put(lastEntry + materials.getDouble(materialKey, 0), material);
316-
} catch (Exception e) {
317-
this.addon.logWarning(
318-
"Unknown material (" + materialKey + ") in generatorTemplate.yml blocks section for tier "
319-
+ generatorTier.getUniqueId() + ". Skipping...");
320-
}
321-
}
322-
323-
generatorTier.setBlockChanceMap(blockChances);
324-
}
315+
if (materials != null) {
316+
TreeMap<Double, Material> blockChances = new TreeMap<>();
317+
TreeMap<Material, int[]> materialHeightMap = new TreeMap<>();
318+
319+
for (String materialKey : materials.getKeys(false)) {
320+
try {
321+
Material material = Material.valueOf(materialKey.toUpperCase());
322+
323+
// Support for both formats
324+
if (materials.isConfigurationSection(materialKey)) {
325+
// New format with chance and height_range
326+
ConfigurationSection materialSection = materials.getConfigurationSection(materialKey);
327+
double chance = materialSection.getDouble("chance", 0);
328+
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
329+
blockChances.put(lastEntry + chance, material);
330+
331+
// Get height range if specified
332+
ConfigurationSection heightRange = materialSection.getConfigurationSection("height_range");
333+
if (heightRange != null) {
334+
int minHeight = heightRange.getInt("min", 0);
335+
int maxHeight = heightRange.getInt("max", 256);
336+
materialHeightMap.put(material, new int[]{minHeight, maxHeight});
337+
}
338+
} else {
339+
// Old format where the value is directly the probability
340+
double chance = materials.getDouble(materialKey, 0);
341+
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
342+
blockChances.put(lastEntry + chance, material);
343+
}
344+
} catch (Exception e) {
345+
this.addon.logWarning(
346+
"Unknown material (" + materialKey + ") in generatorTemplate.yml blocks section for tier "
347+
+ generatorTier.getUniqueId() + ". Skipping...");
348+
}
349+
}
350+
351+
generatorTier.setBlockChanceMap(blockChances);
352+
if (!materialHeightMap.isEmpty()) {
353+
generatorTier.setMaterialHeightMap(materialHeightMap);
354+
}
355+
}
325356
}
326357

327358
/**

src/main/java/world/bentobox/magiccobblestonegenerator/panels/CommonPanel.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ protected List<String> generateGeneratorDescription(GeneratorTierObject generato
181181
String type = this.generateTypeDescription(generator);
182182
// Get status in single string
183183
String status = this.generateStatusDescription(generator, isActive, isUnlocked, isPurchased);
184+
// Get height range in single string
185+
String heightRange = this.generateHeightRangeDescription(generator);
184186

185187
if (!description.replaceAll("(?m)^[ \\t]*\\r?\\n", "").isEmpty())
186188
{
@@ -189,7 +191,8 @@ protected List<String> generateGeneratorDescription(GeneratorTierObject generato
189191
"[treasures]", treasures,
190192
"[requirements]", requirements,
191193
"[type]", type,
192-
"[status]", status);
194+
"[status]", status,
195+
"[height-range]", heightRange);
193196

194197
// remove empty lines from the generated text.
195198
List<String> collect =
@@ -217,7 +220,8 @@ protected List<String> generateGeneratorDescription(GeneratorTierObject generato
217220
"[treasures]", treasures,
218221
"[requirements]", requirements,
219222
"[type]", type,
220-
"[status]", status);
223+
"[status]", status,
224+
"[height-range]", heightRange);
221225

222226
// Remove empty lines and returns as a list.
223227

@@ -254,6 +258,8 @@ protected List<String> generateGeneratorDescription(GeneratorTierObject generato
254258
String type = this.generateTypeDescription(generator);
255259
// Get status in single string
256260
String status = this.generateStatusDescription(generator, false, true, false);
261+
// Get height range in single string
262+
String heightRange = this.generateHeightRangeDescription(generator);
257263

258264
if (!description.replaceAll("(?m)^[ \\t]*\\r?\\n", "").isEmpty())
259265
{
@@ -262,7 +268,8 @@ protected List<String> generateGeneratorDescription(GeneratorTierObject generato
262268
"[treasures]", treasures,
263269
"[requirements]", requirements,
264270
"[type]", type,
265-
"[status]", status);
271+
"[status]", status,
272+
"[height-range]", heightRange);
266273

267274
// remove empty lines from the generated text.
268275
List<String> collect =
@@ -290,7 +297,8 @@ protected List<String> generateGeneratorDescription(GeneratorTierObject generato
290297
"[treasures]", treasures,
291298
"[requirements]", requirements,
292299
"[type]", type,
293-
"[status]", status);
300+
"[status]", status,
301+
"[height-range]", heightRange);
294302

295303
// Remove empty lines and returns as a list.
296304

@@ -301,6 +309,25 @@ protected List<String> generateGeneratorDescription(GeneratorTierObject generato
301309
}
302310

303311

312+
/**
313+
* This method generates height range description for a generator.
314+
*
315+
* @param generator GeneratorTier which height range must be generated.
316+
* @return String that describes generator height range.
317+
*/
318+
protected String generateHeightRangeDescription(GeneratorTierObject generator)
319+
{
320+
final String reference = Constants.DESCRIPTIONS + "generator.";
321+
322+
int minHeight = generator.getMinHeight();
323+
int maxHeight = generator.getMaxHeight();
324+
325+
return this.user.getTranslation(reference + "height-range",
326+
Constants.MIN_HEIGHT, String.valueOf(minHeight),
327+
Constants.MAX_HEIGHT, String.valueOf(maxHeight));
328+
}
329+
330+
304331
/**
305332
* This method generates list of required blocks in a single string with using user translations.
306333
*
@@ -339,14 +366,26 @@ private String generateBlockListDescription(GeneratorTierObject generator)
339366
{
340367
Double value = (entry.getKey() - previousValue) / maxValue * 100.0;
341368

369+
// Get height range for this material if it exists
370+
int[] heightRange = generator.getMaterialHeightRange(entry.getValue());
371+
String heightInfo = "";
372+
373+
if (heightRange != null)
374+
{
375+
heightInfo = this.user.getTranslation(reference + "height-range",
376+
Constants.MIN_HEIGHT, String.valueOf(heightRange[0]),
377+
Constants.MAX_HEIGHT, String.valueOf(heightRange[1]));
378+
}
379+
342380
blocks.append(this.user.getTranslation(reference + "value",
343381
Constants.BLOCK, Utils.prettifyObject(this.user, entry.getValue()),
344382
TextVariables.NUMBER, String.valueOf(value),
345383
Constants.TENS, this.tensFormat.format(value),
346384
Constants.HUNDREDS, this.hundredsFormat.format(value),
347385
Constants.THOUSANDS, this.thousandsFormat.format(value),
348386
Constants.TEN_THOUSANDS, this.tenThousandsFormat.format(value),
349-
Constants.HUNDRED_THOUSANDS, this.hundredThousandsFormat.format(value)));
387+
Constants.HUNDRED_THOUSANDS, this.hundredThousandsFormat.format(value),
388+
Constants.HEIGHT_INFO, heightInfo));
350389

351390
previousValue = entry.getKey();
352391

0 commit comments

Comments
 (0)