Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ on:
push:
branches:
- develop
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 21
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: '21'
- name: Cache SonarCloud packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
Expand All @@ -34,11 +35,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BentoBoxWorld_MagicCobblestoneGenerator
- run: mvn --batch-mode clean org.jacoco:jacoco-maven-plugin:prepare-agent install
- run: mkdir staging && cp target/*.jar staging
- name: Save artifacts
uses: actions/upload-artifact@v3
with:
name: Package
path: staging
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ Magic Cobblestone Generator have 5 placeholders:
- `[gamemode]_magiccobblestonegenerator_unlocked_generator_names` - Returns text that contains all unlocked generator names separated with `,`.
- `[gamemode]_magiccobblestonegenerator_purchased_generator_names` - Returns text that contains all purchased generator names separated with `,`.

## Compatibility

- [x] BentoBox - 1.15 version

## Config.yml

The config.yml has the following sections:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
<build.version>2.6.0</build.version>
<build.version>2.7.0</build.version>
<build.number>-LOCAL</build.number>
<!-- Sonar Cloud -->
<sonar.projectKey>BentoBoxWorld_MagicCobblestoneGenerator</sonar.projectKey>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

Expand Down Expand Up @@ -511,6 +513,9 @@ public GeneratorTierObject clone()

clone.setTreasureChance(this.treasureChance);
clone.setMaxTreasureAmount(this.maxTreasureAmount);
clone.setMinHeight(this.minHeight);
clone.setMaxHeight(this.maxHeight);
clone.setMaterialHeightMap(new TreeMap<>(this.materialHeightMap));

return clone;
}
Expand Down Expand Up @@ -682,5 +687,136 @@ public boolean includes(GeneratorType type)
*/
@Expose
private int maxTreasureAmount = 1;
}

/**
* Minimum height at which this generator can operate.
*/
@Expose
private int minHeight = -64;

/**
* Maximum height at which this generator can operate.
*/
@Expose
private int maxHeight = 320;

/**
* Map that stores min and max heights for each material in the generator.
*/
@Expose
private TreeMap<Material, int[]> materialHeightMap = new TreeMap<>();

/**
* Field to store block height ranges
*/
@Expose
private Map<Material, int[]> blockHeightRanges = new HashMap<>();

/**
* Gets the height ranges for each block type
* @return Map of Material to height range array [min, max]
*/
public Map<Material, int[]> getBlockHeightRanges()
{
return blockHeightRanges;
}

/**
* Sets the height ranges for each block type
* @param blockHeightRanges Map of Material to height range array [min, max]
*/
public void setBlockHeightRanges(Map<Material, int[]> blockHeightRanges)
{
this.blockHeightRanges = blockHeightRanges;
}

/**
* Gets the minimum height at which this generator can operate.
*
* @return the minimum height
*/
public int getMinHeight()
{
return this.minHeight;
}


/**
* Sets the minimum height at which this generator can operate.
*
* @param minHeight the minimum height
*/
public void setMinHeight(int minHeight)
{
this.minHeight = minHeight;
}


/**
* Gets the maximum height at which this generator can operate.
*
* @return the maximum height
*/
public int getMaxHeight()
{
return this.maxHeight;
}


/**
* Sets the maximum height at which this generator can operate.
*
* @param maxHeight the maximum height
*/
public void setMaxHeight(int maxHeight)
{
this.maxHeight = maxHeight;
}


/**
* Gets the map of material-specific height ranges.
*
* @return the material height map
*/
public TreeMap<Material, int[]> getMaterialHeightMap()
{
return this.materialHeightMap;
}


/**
* Sets the map of material-specific height ranges.
*
* @param materialHeightMap the material height map
*/
public void setMaterialHeightMap(TreeMap<Material, int[]> materialHeightMap)
{
this.materialHeightMap = materialHeightMap;
}


/**
* Gets the height range for a specific material.
*
* @param material the material
* @return an array where index 0 is minHeight and index 1 is maxHeight, or null if not set
*/
public int[] getMaterialHeightRange(Material material)
{
return this.materialHeightMap.get(material);
}


/**
* Sets the height range for a specific material.
*
* @param material the material
* @param minHeight the minimum height
* @param maxHeight the maximum height
*/
public void setMaterialHeightRange(Material material, int minHeight, int maxHeight)
{
this.materialHeightMap.put(material, new int[]{minHeight, maxHeight});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ private void createGenerators(YamlConfiguration config, @Nullable User user, Gam
// Set activation cost
generatorTier.setActivationCost(details.getDouble("activation-cost", 0.0));

// Set global height range
ConfigurationSection heightRange = details.getConfigurationSection("height_range");
if (heightRange != null) {
generatorTier.setMinHeight(heightRange.getInt("min", 0));
generatorTier.setMaxHeight(heightRange.getInt("max", 256));
}

// Search and read requirements only if it is not default generator.
if (!generatorTier.isDefaultGenerator()) {
this.populateRequirements(generatorTier, details.getConfigurationSection("requirements"), biomeMap);
Expand Down Expand Up @@ -305,23 +312,47 @@ private void populateRequirements(GeneratorTierObject generatorTier, Configurati
* @param materials Config that contains data.
*/
private void populateMaterials(GeneratorTierObject generatorTier, ConfigurationSection materials) {
if (materials != null) {
TreeMap<Double, Material> blockChances = new TreeMap<>();

for (String materialKey : materials.getKeys(false)) {
try {
Material material = Material.valueOf(materialKey.toUpperCase());
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
blockChances.put(lastEntry + materials.getDouble(materialKey, 0), material);
} catch (Exception e) {
this.addon.logWarning(
"Unknown material (" + materialKey + ") in generatorTemplate.yml blocks section for tier "
+ generatorTier.getUniqueId() + ". Skipping...");
}
}

generatorTier.setBlockChanceMap(blockChances);
}
if (materials != null) {
TreeMap<Double, Material> blockChances = new TreeMap<>();
TreeMap<Material, int[]> materialHeightMap = new TreeMap<>();

for (String materialKey : materials.getKeys(false)) {
try {
Material material = Material.valueOf(materialKey.toUpperCase());

// Support for both formats
if (materials.isConfigurationSection(materialKey)) {
// New format with chance and height_range
ConfigurationSection materialSection = materials.getConfigurationSection(materialKey);
double chance = materialSection.getDouble("chance", 0);
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
blockChances.put(lastEntry + chance, material);

// Get height range if specified
ConfigurationSection heightRange = materialSection.getConfigurationSection("height_range");
if (heightRange != null) {
int minHeight = heightRange.getInt("min", 0);
int maxHeight = heightRange.getInt("max", 256);
materialHeightMap.put(material, new int[]{minHeight, maxHeight});
}
} else {
// Old format where the value is directly the probability
double chance = materials.getDouble(materialKey, 0);
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
blockChances.put(lastEntry + chance, material);
}
} catch (Exception e) {
this.addon.logWarning(
"Unknown material (" + materialKey + ") in generatorTemplate.yml blocks section for tier "
+ generatorTier.getUniqueId() + ". Skipping...");
}
}

generatorTier.setBlockChanceMap(blockChances);
if (!materialHeightMap.isEmpty()) {
generatorTier.setMaterialHeightMap(materialHeightMap);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package world.bentobox.magiccobblestonegenerator.managers;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -1142,6 +1144,7 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
@NotNull GeneratorDataObject generatorData, @NotNull GeneratorTierObject generatorTier) {

final User owner = island.isSpawn() || island.getOwner() == null ? null : User.getInstance(island.getOwner());
NumberFormat numberFormat = NumberFormat.getNumberInstance(owner.getLocale());

if (generatorData.getPurchasedTiers().contains(generatorTier.getUniqueId())) {
// Generator is not unlocked. Return false.
Expand All @@ -1157,7 +1160,7 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "island-level-not-reached", Constants.GENERATOR,
generatorTier.getFriendlyName(), TextVariables.NUMBER,
String.valueOf(generatorTier.getRequiredMinIslandLevel())));
numberFormat.format(generatorTier.getRequiredMinIslandLevel())));
return false;
} else if (!generatorTier.getRequiredPermissions().isEmpty() && (owner == null || !owner.isPlayer()
|| !generatorTier.getRequiredPermissions().stream().allMatch(owner::hasPermission))) {
Expand All @@ -1178,8 +1181,9 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
// Return true only if user has enough money.
return true;
} else {

Utils.sendMessage(user, user.getTranslation(Constants.MESSAGES + "no-credits-buy-bank",
TextVariables.NUMBER, String.valueOf(generatorTier.getGeneratorTierCost())));
TextVariables.NUMBER, numberFormat.format(generatorTier.getGeneratorTierCost())));
return false;
}
} else {
Expand All @@ -1188,7 +1192,7 @@ public boolean canPurchaseGenerator(@NotNull User user, @NotNull Island island,
return true;
} else {
Utils.sendMessage(user, user.getTranslation(Constants.MESSAGES + "no-credits-buy",
TextVariables.NUMBER, String.valueOf(generatorTier.getGeneratorTierCost())));
TextVariables.NUMBER, numberFormat.format(generatorTier.getGeneratorTierCost())));
return false;
}
}
Expand Down Expand Up @@ -1259,6 +1263,9 @@ public void purchaseGenerator(@NotNull User user, @NotNull Island island,
* @param money the money
*/
private void withdrawMoney(CompletableFuture<Boolean> withdrawStage, User user, Island island, double money) {
if(money <= 0.0)
return;

if (this.addon.getSettings().isUseBankAccount() && this.addon.isBankProvided()) {
BankManager bankManager = this.addon.getBankAddon().getBankManager();
bankManager.withdraw(user, island, new Money(money), TxType.WITHDRAW).thenAccept(response -> {
Expand Down
Loading
Loading