From fe6ad811413d54eef5ced40c338429a74537e4cb Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 19 Apr 2020 15:17:34 -0700 Subject: [PATCH] Added conversion to use blockconfig.yml --- src/main/java/world/bentobox/level/Level.java | 59 +++++++++++++------ .../java/world/bentobox/level/TopTen.java | 22 +++++++ .../bentobox/level/config/BlockConfig.java | 36 ++++++----- .../bentobox/level/objects/TopTenData.java | 23 ++++++++ .../level/placeholders/LevelPlaceholder.java | 34 ----------- .../placeholders/TopTenNamePlaceholder.java | 37 ------------ .../level/placeholders/TopTenPlaceholder.java | 37 ------------ src/main/resources/blockconfig.yml | 34 +++++++---- 8 files changed, 129 insertions(+), 153 deletions(-) delete mode 100644 src/main/java/world/bentobox/level/placeholders/LevelPlaceholder.java delete mode 100644 src/main/java/world/bentobox/level/placeholders/TopTenNamePlaceholder.java delete mode 100644 src/main/java/world/bentobox/level/placeholders/TopTenPlaceholder.java diff --git a/src/main/java/world/bentobox/level/Level.java b/src/main/java/world/bentobox/level/Level.java index 6ee3eab..a1929fb 100644 --- a/src/main/java/world/bentobox/level/Level.java +++ b/src/main/java/world/bentobox/level/Level.java @@ -2,7 +2,6 @@ package world.bentobox.level; import java.io.File; import java.io.IOException; -import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -10,6 +9,7 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.eclipse.jdt.annotation.NonNull; @@ -146,16 +146,47 @@ public class Level extends Addon { // Save the default config from config.yml saveDefaultConfig(); // Load settings from config.yml. This will check if there are any issues with it too. - loadSettings(); + if (loadSettings()) { + configObject.saveConfigObject(settings); + } } - private void loadSettings() { + private boolean loadSettings() { // Load settings again to get worlds settings = configObject.loadConfigObject(); if (settings == null) { // Disable logError("Level settings could not load! Addon disabled."); setState(State.DISABLED); + return false; + } + // Check for legacy blocks and limits etc. + if (getConfig().isConfigurationSection("blocks") + || getConfig().isConfigurationSection("limits") + || getConfig().isConfigurationSection("worlds")) { + logWarning("Converting old config.yml format - shifting blocks, limits and worlds to blockconfig.yml"); + File blockConfigFile = new File(this.getDataFolder(), "blockconfig.yml"); + if (blockConfigFile.exists()) { + logError("blockconfig.yml already exists! Saving config as blockconfig.yml.2"); + blockConfigFile = new File(this.getDataFolder(), "blockconfig.yml.2"); + } + YamlConfiguration blockConfig = new YamlConfiguration(); + copyConfigSection(blockConfig, "limits"); + copyConfigSection(blockConfig, "blocks"); + copyConfigSection(blockConfig, "worlds"); + try { + blockConfig.save(blockConfigFile); + } catch (IOException e) { + logError("Could not save! " + e.getMessage()); + } + } + return true; + } + + private void copyConfigSection(YamlConfiguration blockConfig, String sectionName) { + ConfigurationSection section = getConfig().getConfigurationSection(sectionName); + for (String k:section.getKeys(true)) { + blockConfig.set(sectionName + "." + k, section.get(k)); } } @@ -165,15 +196,17 @@ public class Level extends Addon { YamlConfiguration blockValues = new YamlConfiguration(); try { - blockValues.load(new File(this.getDataFolder(), "blockconfig.yml")); + File file = new File(this.getDataFolder(), "blockconfig.yml"); + blockValues.load(file); + // Load the block config class + blockConfig = new BlockConfig(this, blockValues, file); } catch (IOException | InvalidConfigurationException e) { // Disable logError("Level blockconfig.yml settings could not load! Addon disabled."); setState(State.DISABLED); return; } - // Load the block config class - blockConfig = new BlockConfig(this, blockValues); + } @Override @@ -238,27 +271,19 @@ public class Level extends Addon { final int rank = i; // Value getPlugin().getPlaceholdersManager().registerPlaceholder(this, - gm.getDescription().getName().toLowerCase() + "_top_value_" + rank, - user -> { - Collection values = getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().values(); - return values.size() < rank ? "" : values.stream().skip(rank - 1).findFirst().map(String::valueOf).orElse(""); - }); + gm.getDescription().getName().toLowerCase() + "_top_value_" + rank, u -> String.valueOf(getTopTen().getTopTenLevel(gm.getOverWorld(), rank))); // Name getPlugin().getPlaceholdersManager().registerPlaceholder(this, gm.getDescription().getName().toLowerCase() + "_top_name_" + rank, - user -> { - Collection values = getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().keySet(); - return values.size() < rank ? "" : getPlayers().getName(values.stream().skip(rank - 1).findFirst().orElse(null)); - }); + u -> getPlayers().getName(getTopTen().getTopTenUUID(gm.getOverWorld(), rank))); } } private String getVisitedIslandLevel(GameModeAddon gm, User user) { return getIslands().getIslandAt(user.getLocation()) - .map(island -> getIslandLevel(gm.getOverWorld(), island.getOwner())) - .map(level -> getLevelPresenter().getLevelString(level)) + .map(island -> getLevelPresenter().getLevelString(getIslandLevel(gm.getOverWorld(), island.getOwner()))) .orElse("0"); } diff --git a/src/main/java/world/bentobox/level/TopTen.java b/src/main/java/world/bentobox/level/TopTen.java index 6cc1375..805a6d0 100644 --- a/src/main/java/world/bentobox/level/TopTen.java +++ b/src/main/java/world/bentobox/level/TopTen.java @@ -14,6 +14,7 @@ import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; @@ -159,6 +160,27 @@ public class TopTen implements Listener { return topTenList.get(world); } + /** + * Get the UUID for this rank in this world + * @param world - world + * @param rank - rank between 1 and 10 + * @return UUID or null + */ + @Nullable + public UUID getTopTenUUID(World world, int rank) { + return getTopTenList(world).getTopTenUUID(rank); + } + + /** + * Get the island level for this rank in this world + * @param world - world + * @param rank - rank between 1 and 10 + * @return level or 0 + */ + public long getTopTenLevel(World world, int rank) { + return getTopTenList(world).getTopTenLevel(rank); + } + /** * Removes ownerUUID from the top ten list * diff --git a/src/main/java/world/bentobox/level/config/BlockConfig.java b/src/main/java/world/bentobox/level/config/BlockConfig.java index 0f84aa5..ba61117 100644 --- a/src/main/java/world/bentobox/level/config/BlockConfig.java +++ b/src/main/java/world/bentobox/level/config/BlockConfig.java @@ -1,5 +1,8 @@ package world.bentobox.level.config; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -18,38 +21,40 @@ public class BlockConfig { private Map blockValues = new HashMap<>(); private final Map> worldBlockValues = new HashMap<>(); - public BlockConfig(Level level, YamlConfiguration blockValues) { + public BlockConfig(Level level, YamlConfiguration blockValues, File file) throws IOException { - if (blockValues.isSet("limits")) { + if (blockValues.isConfigurationSection("limits")) { HashMap bl = new HashMap<>(); - for (String material : Objects.requireNonNull(blockValues.getConfigurationSection("limits")).getKeys(false)) { + ConfigurationSection limits = Objects.requireNonNull(blockValues.getConfigurationSection("limits")); + for (String material : limits.getKeys(false)) { try { Material mat = Material.valueOf(material); - bl.put(mat, blockValues.getInt("limits." + material, 0)); + bl.put(mat, limits.getInt(material, 0)); } catch (Exception e) { level.logWarning("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping..."); } } setBlockLimits(bl); } - if (blockValues.isSet("blocks")) { + if (blockValues.isConfigurationSection("blocks")) { + ConfigurationSection blocks = Objects.requireNonNull(blockValues.getConfigurationSection("blocks")); Map bv = new HashMap<>(); - for (String material : Objects.requireNonNull(blockValues.getConfigurationSection("blocks")).getKeys(false)) { - - try { - Material mat = Material.valueOf(material); - bv.put(mat, blockValues.getInt("blocks." + material, 0)); - } catch (Exception e) { - level.logWarning("Unknown material (" + material + ") in blockconfig.yml blocks section. Skipping..."); + // Update blockvalues to latest settings + Arrays.stream(Material.values()).filter(Material::isBlock) + .filter(m -> !m.name().startsWith("LEGACY_")) + .forEach(m -> { + if (!blocks.contains(m.name(), true)) { + blocks.set(m.name(), 1); } - } + bv.put(m, blocks.getInt(m.name(), 1)); + }); setBlockValues(bv); } else { level.logWarning("No block values in blockconfig.yml! All island levels will be zero!"); } // Worlds - if (blockValues.isSet("worlds")) { - ConfigurationSection worlds = blockValues.getConfigurationSection("worlds"); + if (blockValues.isConfigurationSection("worlds")) { + ConfigurationSection worlds = Objects.requireNonNull(blockValues.getConfigurationSection("worlds")); for (String world : Objects.requireNonNull(worlds).getKeys(false)) { World bWorld = Bukkit.getWorld(world); if (bWorld != null) { @@ -66,6 +71,7 @@ public class BlockConfig { } } // All done + blockValues.save(file); } /** diff --git a/src/main/java/world/bentobox/level/objects/TopTenData.java b/src/main/java/world/bentobox/level/objects/TopTenData.java index 832c90f..7ade3e3 100644 --- a/src/main/java/world/bentobox/level/objects/TopTenData.java +++ b/src/main/java/world/bentobox/level/objects/TopTenData.java @@ -6,6 +6,8 @@ import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; +import org.eclipse.jdt.annotation.Nullable; + import com.google.gson.annotations.Expose; import world.bentobox.bentobox.database.objects.DataObject; @@ -32,6 +34,27 @@ public class TopTenData implements DataObject { Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); } + /** + * Get the level for the rank + * @param rank - rank + * @return level value or 0 if none. + */ + public long getTopTenLevel(int rank) { + Map tt = getTopTen(); + return tt.size() < rank ? (long)tt.values().toArray()[(rank-1)] : 0; + } + + /** + * Get the UUID of the rank + * @param rank - rank + * @return UUID or null + */ + @Nullable + public UUID getTopTenUUID(int rank) { + Map tt = getTopTen(); + return tt.size() < rank ? (UUID)tt.keySet().toArray()[(rank-1)] : null; + } + public void setTopTen(Map topTen) { this.topTen = topTen; } diff --git a/src/main/java/world/bentobox/level/placeholders/LevelPlaceholder.java b/src/main/java/world/bentobox/level/placeholders/LevelPlaceholder.java deleted file mode 100644 index e9cef31..0000000 --- a/src/main/java/world/bentobox/level/placeholders/LevelPlaceholder.java +++ /dev/null @@ -1,34 +0,0 @@ -package world.bentobox.level.placeholders; - -import world.bentobox.bentobox.api.addons.GameModeAddon; -import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; -import world.bentobox.bentobox.api.user.User; -import world.bentobox.level.Level; - -/** - * @author tastybento - */ -public class LevelPlaceholder implements PlaceholderReplacer { - - private final Level addon; - private final GameModeAddon gm; - - /** - * Provides placeholder support - * @param addon - Level addon - * @param gm - Game mode - */ - public LevelPlaceholder(Level addon, GameModeAddon gm) { - this.addon = addon; - this.gm = gm; - } - - /* (non-Javadoc) - * @see world.bentobox.bentobox.api.placeholders.PlaceholderReplacer#onReplace(world.bentobox.bentobox.api.user.User) - */ - @Override - public String onReplace(User user) { - return addon.getLevelPresenter().getLevelString(addon.getIslandLevel(gm.getOverWorld(), user.getUniqueId())); - } - -} diff --git a/src/main/java/world/bentobox/level/placeholders/TopTenNamePlaceholder.java b/src/main/java/world/bentobox/level/placeholders/TopTenNamePlaceholder.java deleted file mode 100644 index fe52a5e..0000000 --- a/src/main/java/world/bentobox/level/placeholders/TopTenNamePlaceholder.java +++ /dev/null @@ -1,37 +0,0 @@ -package world.bentobox.level.placeholders; - -import java.util.Collection; -import java.util.UUID; - -import world.bentobox.bentobox.api.addons.GameModeAddon; -import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; -import world.bentobox.bentobox.api.user.User; -import world.bentobox.level.Level; - -/** - * @author tastybento - * @deprecated As of 1.9.0, for removal. - */ -@Deprecated -public class TopTenNamePlaceholder implements PlaceholderReplacer { - - private final Level level; - private final GameModeAddon gm; - private final int i; - - public TopTenNamePlaceholder(Level level, GameModeAddon gm, int i) { - this.level = level; - this.gm = gm; - this.i = i - 1; - } - - /* (non-Javadoc) - * @see world.bentobox.bentobox.api.placeholders.PlaceholderReplacer#onReplace(world.bentobox.bentobox.api.user.User) - */ - @Override - public String onReplace(User user) { - Collection values = level.getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().keySet(); - return values.size() < i ? "" : level.getPlayers().getName(values.stream().skip(i).findFirst().orElse(null)); - } - -} diff --git a/src/main/java/world/bentobox/level/placeholders/TopTenPlaceholder.java b/src/main/java/world/bentobox/level/placeholders/TopTenPlaceholder.java deleted file mode 100644 index f96c9be..0000000 --- a/src/main/java/world/bentobox/level/placeholders/TopTenPlaceholder.java +++ /dev/null @@ -1,37 +0,0 @@ -package world.bentobox.level.placeholders; - -import java.util.Collection; - -import world.bentobox.bentobox.api.addons.GameModeAddon; -import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; -import world.bentobox.bentobox.api.user.User; -import world.bentobox.level.Level; - -/** - * Provides the level values to placeholders - * @author tastybento - * @deprecated As of 1.9.0, for removal. - */ -@Deprecated -public class TopTenPlaceholder implements PlaceholderReplacer { - - private final Level level; - private final GameModeAddon gm; - private final int i; - - public TopTenPlaceholder(Level level, GameModeAddon gm, int i) { - this.level = level; - this.gm = gm; - this.i = i - 1; - } - - /* (non-Javadoc) - * @see world.bentobox.bentobox.api.placeholders.PlaceholderReplacer#onReplace(world.bentobox.bentobox.api.user.User) - */ - @Override - public String onReplace(User user) { - Collection values = level.getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().values(); - return values.size() < i ? "" : values.stream().skip(i).findFirst().map(String::valueOf).orElse(""); - } - -} diff --git a/src/main/resources/blockconfig.yml b/src/main/resources/blockconfig.yml index 58403b7..f988632 100644 --- a/src/main/resources/blockconfig.yml +++ b/src/main/resources/blockconfig.yml @@ -1,8 +1,8 @@ # Block Config file for Level add-on Version ${version} - +# # This file lists the values for various blocks that are used to calculate the # island level. - +# # This section lists the limits for any particular block. Blocks over this amount # are not counted. # Format: @@ -10,11 +10,6 @@ limits: COBBLESTONE: 10000 NETHERRACK: 1000 - -# This section lists the value of a block. Value must be an integer. -# Any blocks not listed will have a value of 0. AIR is always zero. -# Format is MATERIAL: value - blocks: ACACIA_BUTTON: 1 ACACIA_DOOR: 2 @@ -678,14 +673,27 @@ blocks: YELLOW_WOOL: 2 ZOMBIE_HEAD: 1 ZOMBIE_WALL_HEAD: 1 - -# World differences -# List any blocks that have a different value in a specific world -# If a block is not listed, the default value will be used -# Prefix with world name + AIR: 1 + BEEHIVE: 1 + BEE_NEST: 1 + DEAD_BRAIN_CORAL: 1 + DEAD_BUBBLE_CORAL: 1 + DEAD_FIRE_CORAL: 1 + DEAD_HORN_CORAL: 1 + DEAD_TUBE_CORAL: 1 + HONEYCOMB_BLOCK: 1 + HONEY_BLOCK: 1 + HORN_CORAL: 1 + JIGSAW: 1 + MOVING_PISTON: 1 + MUSHROOM_STEM: 1 + NETHER_WART: 1 + PISTON_HEAD: 1 + STRUCTURE_BLOCK: 1 + STRUCTURE_VOID: 1 + TUBE_CORAL: 1 worlds: acidisland_world: SAND: 0 SANDSTONE: 0 ICE: 0 - \ No newline at end of file