Skip to content

Commit

Permalink
Added conversion to use blockconfig.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Apr 19, 2020
1 parent c463170 commit fe6ad81
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 153 deletions.
59 changes: 42 additions & 17 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
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;
Expand Down Expand Up @@ -146,16 +146,47 @@ public void onLoad() {
// 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));
}
}

Expand All @@ -165,15 +196,17 @@ private void loadBlockSettings() {

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
Expand Down Expand Up @@ -238,27 +271,19 @@ private void registerPlaceholders(GameModeAddon gm) {
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");
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/world/bentobox/level/TopTen.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -159,6 +160,27 @@ public TopTenData getTopTenList(World 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
*
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/world/bentobox/level/config/BlockConfig.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -66,6 +71,7 @@ public BlockConfig(Level level, YamlConfiguration blockValues) {
}
}
// All done
blockValues.save(file);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/world/bentobox/level/objects/TopTenData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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;
Expand All @@ -32,6 +34,27 @@ public Map<UUID, Long> getTopTen() {
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;
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit fe6ad81

Please sign in to comment.