Now supports multiworld.

This commit is contained in:
tastybento 2018-05-25 19:59:44 -07:00
parent e5afb84e46
commit f49d6e9e42
11 changed files with 292 additions and 176 deletions

View File

@ -302,4 +302,14 @@ blocks:
YELLOW_FLOWER: 1
YELLOW_GLAZED_TERRACOTTA: 1
YELLOW_SHULKER_BOX: 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
worlds:
AcidIsland-world:
SAND: 0
SANDSTONE: 0
ICE: 0

View File

@ -2,9 +2,10 @@ package bskyblock.addon.level;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.World;
import bskyblock.addon.level.commands.AdminLevel;
import bskyblock.addon.level.commands.AdminTop;
import bskyblock.addon.level.commands.IslandLevel;
@ -31,10 +32,8 @@ public class Level extends Addon {
// Database handler for level data
private BSBDatabase<LevelsData> handler;
// A cache of island levels. Island levels are not kept in memory unless required.
// The cache is saved when the server shuts down and the plugin is disabled.
// TODO: Save regularly to avoid crash issues.
private Map<UUID, Long> levelsCache;
// A cache of island levels.
private Map<UUID, LevelsData> levelsCache;
// The Top Ten object
private TopTen topTen;
@ -44,12 +43,13 @@ public class Level extends Addon {
/**
* Calculates a user's island
* @param world
* @param user
* @param playerUUID - the player's UUID
* @param b
*/
public void calculateIslandLevel(User user, UUID playerUUID, boolean b) {
levelCalc.calculateIslandLevel(user, playerUUID, b);
public void calculateIslandLevel(World world, User user, UUID playerUUID, boolean b) {
levelCalc.calculateIslandLevel(world, user, playerUUID, b);
}
/**
@ -57,19 +57,14 @@ public class Level extends Addon {
* @param targetPlayer
* @return Level of player
*/
public long getIslandLevel(UUID targetPlayer) {
if (levelsCache.containsKey(targetPlayer)) {
return levelsCache.get(targetPlayer);
}
// Get from database
LevelsData level;
level = handler.loadObject(targetPlayer.toString());
if (level == null) {
// We do not know this player, set to zero
return 0;
}
levelsCache.put(targetPlayer, level.getLevel());
return level.getLevel();
public long getIslandLevel(World world, UUID targetPlayer) {
LevelsData ld = getLevelsData(targetPlayer);
return ld == null ? 0L : ld.getLevel(world);
}
private LevelsData getLevelsData(UUID targetPlayer) {
// Load player
return levelsCache.getOrDefault(targetPlayer, handler.loadObject(targetPlayer.toString()));
}
/**
@ -83,12 +78,6 @@ public class Level extends Addon {
return topTen;
}
private void load() {
for (LevelsData level : handler.loadObjects()) {
levelsCache.put(UUID.fromString(level.getUniqueId()), level.getLevel());
}
}
@Override
public void onDisable(){
// Save the cache
@ -113,8 +102,6 @@ public class Level extends Addon {
handler = new BSBDatabase<>(this, LevelsData.class);
// Initialize the cache
levelsCache = new HashMap<>();
// Load all the levels
load();
// Load the calculator
levelCalc = new LevelPresenter(this);
// Start the top ten and register it for clicks
@ -135,14 +122,7 @@ public class Level extends Addon {
* @param async - if true, saving will be done async
*/
public void save(boolean async){
Runnable save = () -> {
for (Entry<UUID, Long> en : levelsCache.entrySet()) {
LevelsData lv = new LevelsData();
lv.setLevel(en.getValue());
lv.setUniqueId(en.getKey().toString());
handler.saveObject(lv);
}
};
Runnable save = () -> levelsCache.values().forEach(handler::saveObject);
if(async){
getServer().getScheduler().runTaskAsynchronously(getBSkyBlock(), save);
} else {
@ -152,13 +132,20 @@ public class Level extends Addon {
/**
* Sets the player's level to a value
* @param world
* @param targetPlayer
* @param level
*/
protected void setIslandLevel(UUID targetPlayer, long level) {
protected void setIslandLevel(World world, UUID targetPlayer, long level) {
LevelsData ld = getLevelsData(targetPlayer);
if (ld == null) {
ld = new LevelsData(targetPlayer, level, world);
} else {
ld.setLevel(world, level);
}
// Add to cache
levelsCache.put(targetPlayer, level);
topTen.addEntry(targetPlayer, level);
levelsCache.put(targetPlayer, ld);
topTen.addEntry(world, targetPlayer, level);
}
public BSBDatabase<LevelsData> getHandler() {

View File

@ -61,7 +61,7 @@ public class LevelCalcByChunk {
this.targetPlayer = targetPlayer;
this.limitCount = new HashMap<>(addon.getSettings().getBlockLimits());
this.report = report;
this.oldLevel = addon.getIslandLevel(targetPlayer);
this.oldLevel = addon.getIslandLevel(world, targetPlayer);
// Results go here
result = new Results();
@ -143,14 +143,12 @@ public class LevelCalcByChunk {
@SuppressWarnings("deprecation")
MaterialData md = new MaterialData(type, (byte) blockData);
int count = limitCount(md);
if (count > 0) {
if (belowSeaLevel) {
result.underWaterBlockCount += count;
result.uwCount.add(md);
} else {
result.rawBlockCount += count;
result.mdCount.add(md);
}
if (belowSeaLevel) {
result.underWaterBlockCount += count;
result.uwCount.add(md);
} else {
result.rawBlockCount += count;
result.mdCount.add(md);
}
}
@ -161,33 +159,45 @@ public class LevelCalcByChunk {
*/
private int limitCount(MaterialData md) {
MaterialData generic = new MaterialData(md.getItemType());
if (limitCount.containsKey(md) && addon.getSettings().getBlockValues().containsKey(md)) {
if (limitCount.containsKey(md)) {
int count = limitCount.get(md);
if (count > 0) {
limitCount.put(md, --count);
return addon.getSettings().getBlockValues().get(md);
return getValue(md);
} else {
result.ofCount.add(md);
return 0;
}
} else if (limitCount.containsKey(generic) && addon.getSettings().getBlockValues().containsKey(generic)) {
} else if (limitCount.containsKey(generic)) {
int count = limitCount.get(generic);
if (count > 0) {
limitCount.put(generic, --count);
return addon.getSettings().getBlockValues().get(generic);
return getValue(generic);
} else {
result.ofCount.add(md);
return 0;
}
} else if (addon.getSettings().getBlockValues().containsKey(md)) {
return addon.getSettings().getBlockValues().get(md);
return getValue(md);
} else if (addon.getSettings().getBlockValues().containsKey(generic)) {
return addon.getSettings().getBlockValues().get(generic);
return getValue(generic);
} else {
result.ncCount.add(md);
return 0;
}
}
/**
* World blocks trump regular block values
* @param md
* @return
*/
private int getValue(MaterialData md) {
if (addon.getSettings().getWorldBlockValues().containsKey(world) && addon.getSettings().getWorldBlockValues().get(world).containsKey(md)) {
return addon.getSettings().getWorldBlockValues().get(world).get(md);
}
return addon.getSettings().getBlockValues().getOrDefault(md, 0);
}
/**
* Get a set of all the chunks in island
@ -257,7 +267,7 @@ public class LevelCalcByChunk {
return;
}
// Tell the asker
asker.sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(targetPlayer)));
asker.sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer)));
// Console
if (report) {
sendConsoleReport(asker);
@ -275,10 +285,10 @@ public class LevelCalcByChunk {
asker.sendMessage("island.level.required-points-to-next-level", "[points]", String.valueOf(event.getPointsToNextLevel()));
}
// Tell other team members
if (addon.getIslandLevel(targetPlayer) != oldLevel) {
if (addon.getIslandLevel(world, targetPlayer) != oldLevel) {
for (UUID member : island.getMemberSet()) {
if (!member.equals(asker.getUniqueId())) {
User.getInstance(member).sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(targetPlayer)));
User.getInstance(member).sendMessage("island.level.island-level-is", "[level]", String.valueOf(addon.getIslandLevel(world, targetPlayer)));
}
}
}
@ -291,22 +301,22 @@ public class LevelCalcByChunk {
addon.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
// Save the value
addon.setIslandLevel(island.getOwner(), event.getLevel());
if (addon.getIslands().inTeam(targetPlayer)) {
addon.setIslandLevel(world, island.getOwner(), event.getLevel());
if (addon.getIslands().inTeam(island.getWorld(), targetPlayer)) {
//plugin.getLogger().info("DEBUG: player is in team");
for (UUID member : addon.getIslands().getMembers(targetPlayer)) {
for (UUID member : addon.getIslands().getMembers(island.getWorld(), targetPlayer)) {
//plugin.getLogger().info("DEBUG: updating team member level too");
if (addon.getIslandLevel(member) != event.getLevel()) {
addon.setIslandLevel(member, event.getLevel());
if (addon.getIslandLevel(world, member) != event.getLevel()) {
addon.setIslandLevel(world, member, event.getLevel());
}
}
if (addon.getIslands().inTeam(targetPlayer)) {
UUID leader = addon.getIslands().getTeamLeader(targetPlayer);
if (addon.getIslands().inTeam(island.getWorld(), targetPlayer)) {
UUID leader = addon.getIslands().getTeamLeader(island.getWorld(), targetPlayer);
if (leader != null) {
addon.getTopTen().addEntry(leader, event.getLevel());
addon.getTopTen().addEntry(world, leader, event.getLevel());
}
} else {
addon.getTopTen().addEntry(targetPlayer, event.getLevel());
addon.getTopTen().addEntry(world, targetPlayer, event.getLevel());
}
}
}
@ -383,11 +393,9 @@ public class LevelCalcByChunk {
// Generic
value = addon.getSettings().getBlockValues().get(new MaterialData(type.getItemType()));
}
if (value > 0) {
result.add(type.toString() + ":"
+ String.format("%,d",en.getCount()) + " blocks x " + value + " = " + (value * en.getCount()));
total += (value * en.getCount());
}
result.add(type.toString() + ":"
+ String.format("%,d",en.getCount()) + " blocks x " + value + " = " + (value * en.getCount()));
total += (value * en.getCount());
}
result.add("Subtotal = " + total);
result.add("==================================");

View File

@ -4,6 +4,8 @@ import java.util.Calendar;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.World;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.user.User;
@ -20,36 +22,45 @@ public class LevelPresenter extends LevelPlugin {
/**
* Calculates the island level
*
* @param world - world to check
* @param sender
* - Player object of player who is asking
* @param targetPlayer
* - UUID of the player's island that is being requested
* @return - true if successful.
*/
public boolean calculateIslandLevel(final User sender, final UUID targetPlayer) {
return calculateIslandLevel(sender, targetPlayer, false);
public boolean calculateIslandLevel(World world, final User sender, final UUID targetPlayer) {
return calculateIslandLevel(world, sender, targetPlayer, false);
}
/**
* Calculates the island level
* @param world - world to check
* @param sender - asker of the level info
* @param targetPlayer
* @param report - if true, a detailed report will be provided
* @return - false if this is cannot be done
*/
public boolean calculateIslandLevel(final User sender, final UUID targetPlayer, boolean report) {
public boolean calculateIslandLevel(World world, final User sender, UUID targetPlayer, boolean report) {
// Check if sender has island
if (!bSkyBlock.getIslands().hasIsland(targetPlayer)) {
sender.sendMessage("general.errors.player-has-no-island");
return false;
boolean inTeam = false;
if (!bSkyBlock.getIslands().hasIsland(world, targetPlayer)) {
// Player may be in a team
if (bSkyBlock.getIslands().inTeam(world, targetPlayer)) {
targetPlayer = bSkyBlock.getIslands().getTeamLeader(world, targetPlayer);
inTeam = true;
} else {
sender.sendMessage("general.errors.player-has-no-island");
return false;
}
}
// Player asking for their own island calc
if (!sender.isPlayer() || sender.getUniqueId().equals(targetPlayer) || sender.isOp() || sender.hasPermission(Constants.PERMPREFIX + "mod.info")) {
if (inTeam || !sender.isPlayer() || sender.getUniqueId().equals(targetPlayer) || sender.isOp() || sender.hasPermission(Constants.PERMPREFIX + "mod.info")) {
// Newer better system - uses chunks
if (!onLevelWaitTime(sender) || levelWait <= 0 || sender.isOp() || sender.hasPermission(Constants.PERMPREFIX + "mod.info")) {
sender.sendMessage("island.level.calculating");
setLevelWaitTime(sender);
new LevelCalcByChunk(plugin, bSkyBlock.getIslands().getIsland(targetPlayer), targetPlayer, sender, report);
new LevelCalcByChunk(plugin, bSkyBlock.getIslands().getIsland(world, targetPlayer), targetPlayer, sender, report);
} else {
// Cooldown
sender.sendMessage("island.level.cooldown", "[time]", String.valueOf(getLevelWaitTime(sender)));
@ -57,7 +68,7 @@ public class LevelPresenter extends LevelPlugin {
} else {
// Asking for the level of another player
sender.sendMessage("island.level.island-level-is","[level]", String.valueOf(plugin.getIslandLevel(targetPlayer)));
sender.sendMessage("island.level.island-level-is","[level]", String.valueOf(plugin.getIslandLevel(world, targetPlayer)));
}
return true;
}

View File

@ -1,13 +1,16 @@
package bskyblock.addon.level;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
@ -31,7 +34,7 @@ import us.tastybento.bskyblock.database.BSBDatabase;
public class TopTen implements Listener {
private Level addon;
// Top ten list of players
private TopTenData topTenList;
private Map<World,TopTenData> topTenList;
private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25};
private final boolean DEBUG = true;
private BSBDatabase<TopTenData> handler;
@ -50,18 +53,24 @@ public class TopTen implements Listener {
* @param ownerUUID
* @param l
*/
public void addEntry(UUID ownerUUID, long l) {
public void addEntry(World world, UUID ownerUUID, long l) {
// Check if player is an island owner or not
if (!addon.getIslands().isOwner(world, ownerUUID)) {
return;
}
// Set up world data
topTenList.putIfAbsent(world, new TopTenData());
// Try and see if the player is online
Player player = addon.getServer().getPlayer(ownerUUID);
if (player != null) {
// Online
if (!player.hasPermission(Constants.PERMPREFIX + "intopten")) {
topTenList.remove(ownerUUID);
topTenList.get(world).remove(ownerUUID);
return;
}
}
topTenList.addLevel(ownerUUID, l);
saveTopTen();
topTenList.get(world).addLevel(ownerUUID, l);
}
/**
@ -79,10 +88,8 @@ public class TopTen implements Listener {
}
// Convert to UUID
UUID playerUUID = UUID.fromString(lv.getUniqueId());
// Check if the player is an owner or team leader
if (addon.getIslands().isOwner(playerUUID)) {
topTenList.addLevel(playerUUID, lv.getLevel());
}
// Get the world
lv.getLevels().forEach((k,v) -> addEntry(Bukkit.getWorld(k), playerUUID, v));
}
saveTopTen();
}
@ -95,17 +102,18 @@ public class TopTen implements Listener {
* @return - true if successful, false if no Top Ten list exists
*/
public boolean getGUI(final User user) {
// Check world
topTenList.putIfAbsent(user.getWorld(), new TopTenData());
if (DEBUG)
addon.getLogger().info("DEBUG: GUI display");
// New GUI display (shown by default)
if (topTenList == null) create();
PanelBuilder panel = new PanelBuilder()
.name(user.getTranslation("island.top.gui-title"))
.user(user);
int i = 1;
Iterator<Entry<UUID, Long>> it = topTenList.getTopTen().entrySet().iterator();
Iterator<Entry<UUID, Long>> it = topTenList.get(user.getWorld()).getTopTen().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<UUID, Long> m = it.next();
UUID topTenUUID = m.getKey();
@ -147,11 +155,11 @@ public class TopTen implements Listener {
final String name = addon.getPlayers().getName(playerUUID);
List<String> description = new ArrayList<>();
if (name != null) {
description.add(asker.getTranslation("island.top.gui-heading", "[name]", addon.getIslands().getIslandName(playerUUID), "[rank]", String.valueOf(rank)));
description.add(asker.getTranslation("island.top.gui-heading", "[name]", name, "[rank]", String.valueOf(rank)));
description.add(asker.getTranslation("island.top.island-level","[level]", String.valueOf(level)));
if (addon.getIslands().inTeam(playerUUID)) {
if (addon.getIslands().inTeam(asker.getWorld(), playerUUID)) {
List<String> memberList = new ArrayList<>();
for (UUID members : addon.getIslands().getMembers(playerUUID)) {
for (UUID members : addon.getIslands().getMembers(asker.getWorld(), playerUUID)) {
memberList.add(ChatColor.AQUA + addon.getPlayers().getName(members));
}
description.addAll(memberList);
@ -178,18 +186,16 @@ public class TopTen implements Listener {
return builder.build();
}
public TopTenData getTopTenList() {
return topTenList;
public TopTenData getTopTenList(World world) {
return topTenList.get(world);
}
/**
* Loads the top ten from the database
* Loads all the top tens from the database
*/
public void loadTopTen() {
topTenList = handler.loadObject("topten");
if (topTenList == null) {
topTenList = new TopTenData();
}
topTenList = new HashMap<>();
handler.loadObjects().forEach(tt -> topTenList.put(Bukkit.getWorld(tt.getUniqueId()), tt));
}
/**
@ -197,17 +203,13 @@ public class TopTen implements Listener {
*
* @param ownerUUID
*/
public void removeEntry(UUID ownerUUID) {
topTenList.remove(ownerUUID);
public void removeEntry(World world, UUID ownerUUID) {
topTenList.putIfAbsent(world, new TopTenData());
topTenList.get(world).remove(ownerUUID);
}
public void saveTopTen() {
//plugin.getLogger().info("Saving top ten list");
if (topTenList == null) {
//plugin.getLogger().info("DEBUG: toptenlist = null!");
return;
}
handler.saveObject(topTenList);
topTenList.values().forEach(handler::saveObject);
}
}

View File

@ -3,15 +3,17 @@ package bskyblock.addon.level.commands;
import java.util.List;
import java.util.UUID;
import org.bukkit.World;
import bskyblock.addon.level.Level;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
public class AdminLevel extends CompositeCommand {
private final Level levelPlugin;
public AdminLevel(Level levelPlugin, CompositeCommand parent) {
super(parent, "level");
this.levelPlugin = levelPlugin;
@ -19,23 +21,34 @@ public class AdminLevel extends CompositeCommand {
@Override
public boolean execute(User user, List<String> args) {
if (!args.isEmpty()) {
if (args.size() == 2) {
// Get world
World world = null;
if (getPlugin().getIWM().isOverWorld(args.get(0))) {
world = getPlugin().getIWM().getIslandWorld(args.get(0));
} else {
user.sendMessage("commands.admin.top.unknown-world");
return false;
}
// Asking for another player's level?
// Convert name to a UUID
final UUID playerUUID = getPlugin().getPlayers().getUUID(args.get(0));
//getLogger().info("DEBUG: console player info UUID = " + playerUUID);
if (playerUUID == null) {
user.sendMessage("error.UnknownPlayer");
user.sendMessage("general.errors.unknown-player");
return true;
} else {
if (user.isPlayer()) {
levelPlugin.calculateIslandLevel(user, playerUUID, false);
} else {
levelPlugin.calculateIslandLevel(user, playerUUID, true);
}
}
}
return true;
} else {
if (user.isPlayer()) {
levelPlugin.calculateIslandLevel(world, user, playerUUID, false);
} else {
levelPlugin.calculateIslandLevel(world, user, playerUUID, true);
}
}
return true;
} else {
showHelp(this, user);
return false;
}
}
@Override
@ -44,7 +57,7 @@ public class AdminLevel extends CompositeCommand {
this.setOnlyPlayer(false);
this.setParameters("admin.level.parameters");
this.setDescription("admin.level.description");
}
}

View File

@ -4,16 +4,18 @@ import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.World;
import bskyblock.addon.level.Level;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
public class AdminTop extends CompositeCommand {
private final Level levelPlugin;
public AdminTop(Level levelPlugin, CompositeCommand parent) {
super(parent, "top", "topten");
this.levelPlugin = levelPlugin;
@ -21,13 +23,33 @@ public class AdminTop extends CompositeCommand {
@Override
public boolean execute(User user, List<String> args) {
// Get world
World world = null;
if (args.isEmpty()) {
if (getPlugin().getIWM().getOverWorlds().size() == 1) {
world = getPlugin().getIWM().getOverWorlds().get(0);
} else {
showHelp(this, user);
return false;
}
} else {
if (getPlugin().getIWM().isOverWorld(args.get(0))) {
world = getPlugin().getIWM().getIslandWorld(args.get(0));
} else {
user.sendMessage("commands.admin.top.unknown-world");
return false;
}
}
int rank = 0;
for (Entry<UUID, Long> topTen : levelPlugin.getTopTen().getTopTenList().getTopTen().entrySet()) {
UUID player = topTen.getKey();
rank++;
String item = String.valueOf(rank) + ":" + BSkyBlock.getInstance().getIslands().getIslandName(player) + " "
+ user.getTranslation("topten.islandLevel", "[level]", String.valueOf(topTen.getValue()));
user.sendRawMessage(item);
for (Entry<UUID, Long> topTen : levelPlugin.getTopTen().getTopTenList(world).getTopTen().entrySet()) {
Island island = getPlugin().getIslands().getIsland(world, topTen.getKey());
if (island != null) {
rank++;
String item = String.valueOf(rank) + ":" + island.getName() + " "
+ user.getTranslation("topten.islandLevel", "[level]", String.valueOf(topTen.getValue()));
user.sendRawMessage(item);
}
}
return true;

View File

@ -29,14 +29,14 @@ public class IslandLevel extends CompositeCommand {
return true;
} else if (user.getUniqueId().equals(playerUUID) ) {
// Self level request
levelPlugin.calculateIslandLevel(user, user.getUniqueId(), false);
levelPlugin.calculateIslandLevel(user.getWorld(), user, user.getUniqueId(), false);
} else {
user.sendMessage("island.level.island-level-is", "[level]", String.valueOf(levelPlugin.getIslandLevel(playerUUID)));
user.sendMessage("island.level.island-level-is", "[level]", String.valueOf(levelPlugin.getIslandLevel(user.getWorld(), playerUUID)));
return true;
}
} else {
// Self level request
levelPlugin.calculateIslandLevel(user, user.getUniqueId(), false);
levelPlugin.calculateIslandLevel(user.getWorld(), user, user.getUniqueId(), false);
}
return false;
}

View File

@ -1,9 +1,13 @@
package bskyblock.addon.level.config;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.material.MaterialData;
import bskyblock.addon.level.Level;
@ -13,8 +17,9 @@ public class Settings {
private static final boolean DEBUG = false;
private boolean sumTeamDeaths;
private int seaHeight;
private HashMap<MaterialData, Integer> blockLimits = new HashMap<>();
private HashMap<MaterialData, Integer> blockValues = new HashMap<>();
private Map<MaterialData, Integer> blockLimits = new HashMap<>();
private Map<MaterialData, Integer> blockValues = new HashMap<>();
private Map<World, Map<MaterialData, Integer>> worldBlockValues = new HashMap<>();
private double underWaterMultiplier;
private int deathpenalty;
private long levelCost;
@ -48,19 +53,7 @@ public class Settings {
HashMap<MaterialData, Integer> blockLimits = new HashMap<>();
for (String material : level.getConfig().getConfigurationSection("limits").getKeys(false)) {
try {
String[] split = material.split(":");
byte data = 0;
if (split.length>1) {
data = Byte.valueOf(split[1]);
}
Material mat;
if (StringUtils.isNumeric(split[0])) {
mat = Material.getMaterial(Integer.parseInt(split[0]));
} else {
mat = Material.valueOf(split[0].toUpperCase());
}
MaterialData materialData = new MaterialData(mat);
materialData.setData(data);
MaterialData materialData = getMaterialData(material);
blockLimits.put(materialData, level.getConfig().getInt("limits." + material, 0));
if (DEBUG) {
level.getLogger().info("Maximum number of " + materialData + " will be " + blockLimits.get(materialData));
@ -72,22 +65,11 @@ public class Settings {
setBlockLimits(blockLimits);
}
if (level.getConfig().isSet("blocks")) {
HashMap<MaterialData, Integer> blockValues = new HashMap<>();
Map<MaterialData, Integer> blockValues = new HashMap<>();
for (String material : level.getConfig().getConfigurationSection("blocks").getKeys(false)) {
try {
String[] split = material.split(":");
byte data = 0;
if (split.length>1) {
data = Byte.valueOf(split[1]);
}
MaterialData materialData = null;
if (StringUtils.isNumeric(split[0])) {
materialData = new MaterialData(Integer.parseInt(split[0]));
} else {
materialData = new MaterialData(Material.valueOf(split[0].toUpperCase()));
}
materialData.setData(data);
try {
MaterialData materialData = getMaterialData(material);
blockValues.put(materialData, level.getConfig().getInt("blocks." + material, 0));
if (DEBUG) {
level.getLogger().info(materialData.toString() + " value = " + blockValues.get(materialData));
@ -101,9 +83,44 @@ public class Settings {
} else {
level.getLogger().severe("No block values in blockvalues.yml! All island levels will be zero!");
}
// Worlds
if (level.getConfig().isSet("worlds")) {
ConfigurationSection worlds = level.getConfig().getConfigurationSection("worlds");
for (String world : worlds.getKeys(false)) {
World bWorld = Bukkit.getWorld(world);
if (bWorld != null) {
ConfigurationSection worldValues = worlds.getConfigurationSection(world);
for (String material : worldValues.getKeys(false)) {
MaterialData materialData = getMaterialData(material);
Map<MaterialData, Integer> values = worldBlockValues.getOrDefault(bWorld, new HashMap<>());
values.put(materialData, worldValues.getInt("blocks." + material, 0));
worldBlockValues.put(bWorld, values);
}
} else {
level.getLogger().severe("No such world : " + world);
}
}
}
// All done
}
private MaterialData getMaterialData(String material) {
String[] split = material.split(":");
byte data = 0;
if (split.length>1) {
data = Byte.valueOf(split[1]);
}
MaterialData materialData = null;
if (StringUtils.isNumeric(split[0])) {
materialData = new MaterialData(Integer.parseInt(split[0]));
} else {
materialData = new MaterialData(Material.valueOf(split[0].toUpperCase()));
}
materialData.setData(data);
return materialData;
}
/**
* @return the sumTeamDeaths
*/
@ -131,7 +148,7 @@ public class Settings {
/**
* @return the blockLimits
*/
public final HashMap<MaterialData, Integer> getBlockLimits() {
public final Map<MaterialData, Integer> getBlockLimits() {
return blockLimits;
}
/**
@ -143,14 +160,14 @@ public class Settings {
/**
* @return the blockValues
*/
public final HashMap<MaterialData, Integer> getBlockValues() {
public final Map<MaterialData, Integer> getBlockValues() {
return blockValues;
}
/**
* @param blockValues the blockValues to set
* @param blockValues2 the blockValues to set
*/
public final void setBlockValues(HashMap<MaterialData, Integer> blockValues) {
this.blockValues = blockValues;
public final void setBlockValues(Map<MaterialData, Integer> blockValues2) {
this.blockValues = blockValues2;
}
/**
* @return the underWaterMultiplier
@ -249,4 +266,11 @@ public class Settings {
this.teamJoinDeathReset = teamJoinDeathReset;
}
/**
* @return the worldBlockValues
*/
public Map<World, Map<MaterialData, Integer>> getWorldBlockValues() {
return worldBlockValues;
}
}

View File

@ -1,31 +1,69 @@
package bskyblock.addon.level.database.object;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.World;
import com.google.gson.annotations.Expose;
import us.tastybento.bskyblock.database.objects.DataObject;
public class LevelsData implements DataObject {
// uniqueId is the player's UUID
@Expose
private String uniqueId = "";
// Map - world name, level
@Expose
private long level = 0;
private Map<String, Long> levels = new HashMap<>();
/**
* Create a level entry for target player
* @param targetPlayer
* @param level
* @param world
*/
public LevelsData(UUID targetPlayer, long level, World world) {
uniqueId = targetPlayer.toString();
levels.put(world.getName(), level);
}
/* (non-Javadoc)
* @see us.tastybento.bskyblock.database.objects.DataObject#getUniqueId()
*/
public String getUniqueId() {
return uniqueId;
}
/* (non-Javadoc)
* @see us.tastybento.bskyblock.database.objects.DataObject#setUniqueId(java.lang.String)
*/
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
public long getLevel() {
return level;
public Long getLevel(World world) {
return levels.getOrDefault(world.getName(), 0L);
}
/**
* @return the levels
*/
public Map<String, Long> getLevels() {
return levels;
}
public void setLevel(long level) {
this.level = level;
/**
* @param levels the levels to set
*/
public void setLevels(Map<String, Long> levels) {
this.levels = levels;
}
public void setLevel(World world, Long lv) {
levels.put(world.getName(),lv);
}
}

View File

@ -17,8 +17,9 @@ import us.tastybento.bskyblock.database.objects.DataObject;
*/
public class TopTenData implements DataObject {
// UniqueId is the world name
@Expose
private String uniqueId = "topten";
private String uniqueId = "";
@Expose
private Map<UUID, Long> topTen = new LinkedHashMap<>();