Added conversion to use blockconfig.yml

This commit is contained in:
tastybento 2020-04-19 15:17:34 -07:00
parent c463170fdd
commit fe6ad81141
8 changed files with 129 additions and 153 deletions

View File

@ -2,7 +2,6 @@ package world.bentobox.level;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -10,6 +9,7 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
@ -146,16 +146,47 @@ public class Level extends Addon {
// Save the default config from config.yml // Save the default config from config.yml
saveDefaultConfig(); saveDefaultConfig();
// Load settings from config.yml. This will check if there are any issues with it too. // 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 // Load settings again to get worlds
settings = configObject.loadConfigObject(); settings = configObject.loadConfigObject();
if (settings == null) { if (settings == null) {
// Disable // Disable
logError("Level settings could not load! Addon disabled."); logError("Level settings could not load! Addon disabled.");
setState(State.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(); YamlConfiguration blockValues = new YamlConfiguration();
try { 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) { } catch (IOException | InvalidConfigurationException e) {
// Disable // Disable
logError("Level blockconfig.yml settings could not load! Addon disabled."); logError("Level blockconfig.yml settings could not load! Addon disabled.");
setState(State.DISABLED); setState(State.DISABLED);
return; return;
} }
// Load the block config class
blockConfig = new BlockConfig(this, blockValues);
} }
@Override @Override
@ -238,27 +271,19 @@ public class Level extends Addon {
final int rank = i; final int rank = i;
// Value // Value
getPlugin().getPlaceholdersManager().registerPlaceholder(this, getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_top_value_" + rank, gm.getDescription().getName().toLowerCase() + "_top_value_" + rank, u -> String.valueOf(getTopTen().getTopTenLevel(gm.getOverWorld(), rank)));
user -> {
Collection<Long> values = getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().values();
return values.size() < rank ? "" : values.stream().skip(rank - 1).findFirst().map(String::valueOf).orElse("");
});
// Name // Name
getPlugin().getPlaceholdersManager().registerPlaceholder(this, getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gm.getDescription().getName().toLowerCase() + "_top_name_" + rank, gm.getDescription().getName().toLowerCase() + "_top_name_" + rank,
user -> { u -> getPlayers().getName(getTopTen().getTopTenUUID(gm.getOverWorld(), rank)));
Collection<UUID> values = getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().keySet();
return values.size() < rank ? "" : getPlayers().getName(values.stream().skip(rank - 1).findFirst().orElse(null));
});
} }
} }
private String getVisitedIslandLevel(GameModeAddon gm, User user) { private String getVisitedIslandLevel(GameModeAddon gm, User user) {
return getIslands().getIslandAt(user.getLocation()) return getIslands().getIslandAt(user.getLocation())
.map(island -> getIslandLevel(gm.getOverWorld(), island.getOwner())) .map(island -> getLevelPresenter().getLevelString(getIslandLevel(gm.getOverWorld(), island.getOwner())))
.map(level -> getLevelPresenter().getLevelString(level))
.orElse("0"); .orElse("0");
} }

View File

@ -14,6 +14,7 @@ import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.eclipse.jdt.annotation.NonNull; 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.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
@ -159,6 +160,27 @@ public class TopTen implements Listener {
return topTenList.get(world); 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 * Removes ownerUUID from the top ten list
* *

View File

@ -1,5 +1,8 @@
package world.bentobox.level.config; package world.bentobox.level.config;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -18,38 +21,40 @@ public class BlockConfig {
private Map<Material, Integer> blockValues = new HashMap<>(); private Map<Material, Integer> blockValues = new HashMap<>();
private final Map<World, Map<Material, Integer>> worldBlockValues = new HashMap<>(); private final Map<World, Map<Material, Integer>> 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<Material, Integer> bl = new HashMap<>(); HashMap<Material, Integer> 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 { try {
Material mat = Material.valueOf(material); Material mat = Material.valueOf(material);
bl.put(mat, blockValues.getInt("limits." + material, 0)); bl.put(mat, limits.getInt(material, 0));
} catch (Exception e) { } catch (Exception e) {
level.logWarning("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping..."); level.logWarning("Unknown material (" + material + ") in blockconfig.yml Limits section. Skipping...");
} }
} }
setBlockLimits(bl); setBlockLimits(bl);
} }
if (blockValues.isSet("blocks")) { if (blockValues.isConfigurationSection("blocks")) {
ConfigurationSection blocks = Objects.requireNonNull(blockValues.getConfigurationSection("blocks"));
Map<Material, Integer> bv = new HashMap<>(); Map<Material, Integer> bv = new HashMap<>();
for (String material : Objects.requireNonNull(blockValues.getConfigurationSection("blocks")).getKeys(false)) { // Update blockvalues to latest settings
Arrays.stream(Material.values()).filter(Material::isBlock)
try { .filter(m -> !m.name().startsWith("LEGACY_"))
Material mat = Material.valueOf(material); .forEach(m -> {
bv.put(mat, blockValues.getInt("blocks." + material, 0)); if (!blocks.contains(m.name(), true)) {
} catch (Exception e) { blocks.set(m.name(), 1);
level.logWarning("Unknown material (" + material + ") in blockconfig.yml blocks section. Skipping...");
} }
} bv.put(m, blocks.getInt(m.name(), 1));
});
setBlockValues(bv); setBlockValues(bv);
} else { } else {
level.logWarning("No block values in blockconfig.yml! All island levels will be zero!"); level.logWarning("No block values in blockconfig.yml! All island levels will be zero!");
} }
// Worlds // Worlds
if (blockValues.isSet("worlds")) { if (blockValues.isConfigurationSection("worlds")) {
ConfigurationSection worlds = blockValues.getConfigurationSection("worlds"); ConfigurationSection worlds = Objects.requireNonNull(blockValues.getConfigurationSection("worlds"));
for (String world : Objects.requireNonNull(worlds).getKeys(false)) { for (String world : Objects.requireNonNull(worlds).getKeys(false)) {
World bWorld = Bukkit.getWorld(world); World bWorld = Bukkit.getWorld(world);
if (bWorld != null) { if (bWorld != null) {
@ -66,6 +71,7 @@ public class BlockConfig {
} }
} }
// All done // All done
blockValues.save(file);
} }
/** /**

View File

@ -6,6 +6,8 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.Nullable;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.database.objects.DataObject; 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)); 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<UUID, Long> 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<UUID, Long> tt = getTopTen();
return tt.size() < rank ? (UUID)tt.keySet().toArray()[(rank-1)] : null;
}
public void setTopTen(Map<UUID, Long> topTen) { public void setTopTen(Map<UUID, Long> topTen) {
this.topTen = topTen; this.topTen = topTen;
} }

View File

@ -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()));
}
}

View File

@ -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<UUID> values = level.getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().keySet();
return values.size() < i ? "" : level.getPlayers().getName(values.stream().skip(i).findFirst().orElse(null));
}
}

View File

@ -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<Long> values = level.getTopTen().getTopTenList(gm.getOverWorld()).getTopTen().values();
return values.size() < i ? "" : values.stream().skip(i).findFirst().map(String::valueOf).orElse("");
}
}

View File

@ -1,8 +1,8 @@
# Block Config file for Level add-on Version ${version} # Block Config file for Level add-on Version ${version}
#
# This file lists the values for various blocks that are used to calculate the # This file lists the values for various blocks that are used to calculate the
# island level. # island level.
#
# This section lists the limits for any particular block. Blocks over this amount # This section lists the limits for any particular block. Blocks over this amount
# are not counted. # are not counted.
# Format: # Format:
@ -10,11 +10,6 @@
limits: limits:
COBBLESTONE: 10000 COBBLESTONE: 10000
NETHERRACK: 1000 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: blocks:
ACACIA_BUTTON: 1 ACACIA_BUTTON: 1
ACACIA_DOOR: 2 ACACIA_DOOR: 2
@ -678,14 +673,27 @@ blocks:
YELLOW_WOOL: 2 YELLOW_WOOL: 2
ZOMBIE_HEAD: 1 ZOMBIE_HEAD: 1
ZOMBIE_WALL_HEAD: 1 ZOMBIE_WALL_HEAD: 1
AIR: 1
# World differences BEEHIVE: 1
# List any blocks that have a different value in a specific world BEE_NEST: 1
# If a block is not listed, the default value will be used DEAD_BRAIN_CORAL: 1
# Prefix with world name 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: worlds:
acidisland_world: acidisland_world:
SAND: 0 SAND: 0
SANDSTONE: 0 SANDSTONE: 0
ICE: 0 ICE: 0