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.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<Long> 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<UUID> 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");
}

View File

@ -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
*

View File

@ -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<Material, Integer> blockValues = 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<>();
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<Material, Integer> 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);
}
/**

View File

@ -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<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) {
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}
#
# 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