addon-level/src/main/java/bskyblock/addon/level/Level.java

156 lines
4.6 KiB
Java
Raw Normal View History

package bskyblock.addon.level;
2017-10-21 20:32:32 +02:00
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
2017-10-21 20:32:32 +02:00
2018-05-26 04:59:44 +02:00
import org.bukkit.World;
import bskyblock.addon.level.commands.AdminLevel;
import bskyblock.addon.level.commands.AdminTop;
import bskyblock.addon.level.commands.IslandLevel;
import bskyblock.addon.level.commands.IslandTop;
import bskyblock.addon.level.config.Settings;
2018-01-07 20:25:24 +01:00
import bskyblock.addon.level.database.object.LevelsData;
import us.tastybento.bskyblock.Constants;
2017-12-28 17:43:07 +01:00
import us.tastybento.bskyblock.api.addons.Addon;
2017-12-26 17:41:37 +01:00
import us.tastybento.bskyblock.api.commands.CompositeCommand;
2018-03-12 01:36:33 +01:00
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.BSBDatabase;
2017-10-21 20:32:32 +02:00
2018-04-03 03:20:26 +02:00
/**
* Addon to BSkyBlock that enables island level scoring and top ten functionality
* @author tastybento
*
*/
2017-12-28 17:43:07 +01:00
public class Level extends Addon {
// Settings
private Settings settings;
// Database handler for level data
private BSBDatabase<LevelsData> handler;
2018-05-26 04:59:44 +02:00
// A cache of island levels.
private Map<UUID, LevelsData> levelsCache;
// The Top Ten object
private TopTen topTen;
2017-12-26 17:41:37 +01:00
// Level calculator
private LevelPresenter levelCalc;
/**
* Calculates a user's island
2018-05-26 04:59:44 +02:00
* @param world
* @param user
* @param playerUUID - the player's UUID
* @param b
*/
2018-05-26 04:59:44 +02:00
public void calculateIslandLevel(World world, User user, UUID playerUUID, boolean b) {
levelCalc.calculateIslandLevel(world, user, playerUUID, b);
}
/**
* Get level from cache for a player
* @param targetPlayer
* @return Level of player
*/
2018-05-26 04:59:44 +02:00
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()));
}
/**
* @return the settings
*/
public final Settings getSettings() {
return settings;
}
public TopTen getTopTen() {
return topTen;
}
@Override
public void onDisable(){
// Save the cache
if (levelsCache != null) {
save(false);
}
}
2017-10-21 20:32:32 +02:00
@Override
public void onEnable() {
// Check if it is enabled - it might be loaded, but not enabled.
if (getBSkyBlock() == null || !getBSkyBlock().isEnabled()) {
getLogger().severe("BSkyBlock does not exist or is not enabled. Stopping.");
this.setEnabled(false);
return;
}
// Load the plugin's config
settings = new Settings(this);
// Get the BSkyBlock database
// Set up the database handler to store and retrieve Island classes
// Note that these are saved by the BSkyBlock database
handler = new BSBDatabase<>(this, LevelsData.class);
// Initialize the cache
levelsCache = new HashMap<>();
2017-12-26 17:41:37 +01:00
// Load the calculator
levelCalc = new LevelPresenter(this);
// Start the top ten and register it for clicks
topTen = new TopTen(this);
2017-12-28 04:17:44 +01:00
registerListener(topTen);
// Register commands
CompositeCommand bsbIslandCmd = getBSkyBlock().getCommandsManager().getCommand(Constants.ISLANDCOMMAND);
2017-12-26 17:41:37 +01:00
new IslandLevel(this, bsbIslandCmd);
new IslandTop(this, bsbIslandCmd);
CompositeCommand bsbAdminCmd = getBSkyBlock().getCommandsManager().getCommand(Constants.ADMINCOMMAND);
2017-12-26 17:41:37 +01:00
new AdminLevel(this, bsbAdminCmd);
new AdminTop(this, bsbAdminCmd);
// Done
2017-10-21 20:32:32 +02:00
}
/**
* Save the levels to the database
* @param async - if true, saving will be done async
*/
public void save(boolean async){
2018-05-26 04:59:44 +02:00
Runnable save = () -> levelsCache.values().forEach(handler::saveObject);
if(async){
2017-12-28 04:17:44 +01:00
getServer().getScheduler().runTaskAsynchronously(getBSkyBlock(), save);
} else {
save.run();
}
2017-10-21 20:32:32 +02:00
}
/**
* Sets the player's level to a value
2018-05-26 04:59:44 +02:00
* @param world
* @param targetPlayer
* @param level
*/
2018-05-26 04:59:44 +02:00
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
2018-05-26 04:59:44 +02:00
levelsCache.put(targetPlayer, ld);
topTen.addEntry(world, targetPlayer, level);
}
public BSBDatabase<LevelsData> getHandler() {
return handler;
}
2017-10-21 20:32:32 +02:00
}