Skip to content

Commit

Permalink
Rework and reimplement template file and its importing.
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Jan 25, 2022
1 parent c10fc63 commit 306e334
Show file tree
Hide file tree
Showing 2 changed files with 481 additions and 102 deletions.
150 changes: 118 additions & 32 deletions src/main/java/world/bentobox/biomes/managers/BiomesImportManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package world.bentobox.biomes.managers;


import com.google.common.base.Enums;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
Expand All @@ -18,6 +19,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
Expand Down Expand Up @@ -175,8 +177,8 @@ private void createBiomes(YamlConfiguration config, @Nullable User user, GameMod

if (config.isConfigurationSection("biomes"))
{
biomeCount = this.importBiomes(config.getConfigurationSection("biomes"),
gameMode.getOverWorld());
biomeCount = this.importBiomes(
Objects.requireNonNull(config.getConfigurationSection("biomes")), gameMode);
}
else
{
Expand All @@ -185,7 +187,8 @@ private void createBiomes(YamlConfiguration config, @Nullable User user, GameMod

if (config.isConfigurationSection("bundles"))
{
bundleCount = this.importBundles(config.getConfigurationSection("bundles"),
bundleCount = this.importBundles(
Objects.requireNonNull(config.getConfigurationSection("bundles")),
gameMode.getDescription().getName() + "_");
}
else
Expand All @@ -206,60 +209,143 @@ private void createBiomes(YamlConfiguration config, @Nullable User user, GameMod
* Import biomes from the given config.
*
* @param reader the reader
* @param world the world
* @param gameModeAddon the world
* @return the int
*/
private int importBiomes(ConfigurationSection reader, World world)
private int importBiomes(@NotNull ConfigurationSection reader, GameModeAddon gameModeAddon)
{
int size = 0;

Map<String, Biome> biomeNameMap = Utils.getBiomeNameMap();
final String prefix = gameModeAddon.getDescription().getName() + "_";

for (String biome : reader.getKeys(false))
for (String biomeId : reader.getKeys(false))
{
if (biomeNameMap.containsKey(biome.toUpperCase()))
{
BiomesObject newBiomeObject = new BiomesObject(Biome.valueOf(biome.toUpperCase()), world);
newBiomeObject.setDeployed(true);
ConfigurationSection details = reader.getConfigurationSection(biomeId);

ConfigurationSection details = reader.getConfigurationSection(biome);
if (details == null)
{
this.addon.logError("Cannot read template section: " + biomeId);
continue;
}

newBiomeObject.setFriendlyName(details.getString("friendlyName", biome));
BiomesObject biomesObject = new BiomesObject();
biomesObject.setUniqueId(Utils.sanitizeInput(prefix + biomeId.toLowerCase()));

newBiomeObject.setDescription(Arrays.stream(details.getString("description", "").split("\n")).toList());
newBiomeObject.setIcon(ItemParser.parse(details.getString("icon")));
// Read biome or replace it with VOID
biomesObject.setBiome(
Enums.getIfPresent(Biome.class, details.getString("biome", "").toUpperCase()).
or(Biome.THE_VOID));
// Read environment or replace with Normal
biomesObject.setEnvironment(
Enums.getIfPresent(World.Environment.class, details.getString("environment", "").toUpperCase()).
or(World.Environment.NORMAL));

newBiomeObject.setUnlockLevel(details.getLong("islandLevel", 0));
newBiomeObject.setUnlockCost(details.getDouble("cost", 0.0));
// Read GUI stuff
// Read Name
biomesObject.setFriendlyName(details.getString("name", biomeId.replace("_", " ")));

String environmentValue = details.getString("environment", "normal").toUpperCase();
// Read description
if (details.isList("description"))
{
biomesObject.setDescription(details.getStringList("description"));
}
else if (details.isString("description"))
{
String description = details.getString("description");

switch (environmentValue)
if (description != null)
{
case "NETHER" -> newBiomeObject.setEnvironment(World.Environment.NETHER);
case "THE_END" -> newBiomeObject.setEnvironment(World.Environment.THE_END);
default -> newBiomeObject.setEnvironment(World.Environment.NORMAL);
// Define as list.
biomesObject.setDescription(Arrays.asList(
description.replaceAll("\\|", "\n").
split("\n").
clone()));
}
}

// Read Icon
biomesObject.setIcon(ItemParser.parse(details.getString("icon"), new ItemStack(Material.PAPER)));
// Read Order
biomesObject.setOrder(details.getInt("order", -1));
// Set deployment.
biomesObject.setDeployed(true);

newBiomeObject.setOrder(details.getInt("order", 0));
// Read Unlock conditions
if (details.isConfigurationSection("unlock"))
{
ConfigurationSection unlockSection = Objects.requireNonNull(details.getConfigurationSection("unlock"));

// Set unlock level
biomesObject.setUnlockLevel(unlockSection.getLong("level"));

List<String> permissions = details.getStringList("permission");
// Set unlock permissions
Set<String> permissions = new HashSet<>(unlockSection.getStringList("permission"));

if (permissions.isEmpty())
if (!permissions.isEmpty())
{
newBiomeObject.setUnlockPermissions(Collections.emptySet());
biomesObject.setUnlockPermissions(permissions);
}
else

// Set unlock cost
biomesObject.setUnlockCost(unlockSection.getDouble("cost"));

// Set unlock items
final List<ItemStack> items = new ArrayList<>();

unlockSection.getStringList("items").forEach(text -> {
ItemStack item = ItemParser.parse(text);

if (item != null)
{
items.add(item);
}
});

if (!items.isEmpty())
{
newBiomeObject.setUnlockPermissions(new HashSet<>(permissions));
biomesObject.setUnlockItems(items);
}
}

// Read Change conditions
if (details.isConfigurationSection("change"))
{
ConfigurationSection changeSection = Objects.requireNonNull(details.getConfigurationSection("change"));

// Set change mode
biomesObject.setCostMode(
Enums.getIfPresent(BiomesObject.CostMode.class, changeSection.getString("mode", "").toUpperCase()).
or(BiomesObject.CostMode.STATIC));

// Set change increment
biomesObject.setCostIncrement(changeSection.getDouble("increment"));

// Set change cost
biomesObject.setCost(changeSection.getDouble("cost"));

// Set change items
final List<ItemStack> items = new ArrayList<>();

if (this.addon.getAddonManager().loadBiomes(newBiomeObject, false, null, true))
changeSection.getStringList("items").forEach(text -> {
ItemStack item = ItemParser.parse(text);

if (item != null)
{
items.add(item);
}
});

if (!items.isEmpty())
{
this.addon.getAddonManager().saveBiome(newBiomeObject);
size++;
biomesObject.setItemCost(items);
}
}

if (this.addon.getAddonManager().loadBiomes(biomesObject, false, null, true))
{
this.addon.getAddonManager().saveBiome(biomesObject);
size++;
}
}

return size;
Expand All @@ -280,7 +366,7 @@ private int importBundles(ConfigurationSection reader, String prefix)
for (String bundleId : reader.getKeys(false))
{
BiomesBundleObject generatorBundle = new BiomesBundleObject();
generatorBundle.setUniqueId(prefix + bundleId.toLowerCase());
generatorBundle.setUniqueId(Utils.sanitizeInput(prefix + bundleId.toLowerCase()));

ConfigurationSection details = reader.getConfigurationSection(bundleId);

Expand Down

0 comments on commit 306e334

Please sign in to comment.