2018-01-01 02:21:07 +01:00
|
|
|
package bskyblock.addon.level;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2017-10-22 08:09:21 +02:00
|
|
|
import java.beans.IntrospectionException;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2017-10-25 01:24:43 +02:00
|
|
|
import java.util.HashMap;
|
2018-02-19 22:06:58 +01:00
|
|
|
import java.util.Map;
|
2017-10-25 01:24:43 +02:00
|
|
|
import java.util.Map.Entry;
|
2017-10-22 04:52:11 +02:00
|
|
|
import java.util.UUID;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2018-01-01 02:21:07 +01:00
|
|
|
import bskyblock.addon.level.commands.AdminLevel;
|
|
|
|
import bskyblock.addon.level.commands.AdminTop;
|
|
|
|
import bskyblock.addon.level.commands.IslandLevel;
|
|
|
|
import bskyblock.addon.level.commands.IslandTop;
|
2018-02-14 05:48:27 +01:00
|
|
|
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.AbstractDatabaseHandler;
|
2017-10-22 08:09:21 +02:00
|
|
|
import us.tastybento.bskyblock.database.BSBDatabase;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2018-04-03 03:20:26 +02:00
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
/**
|
2018-02-19 22:06:58 +01:00
|
|
|
* Addon to BSkyBlock that enables island level scoring and top ten functionality
|
2017-10-25 01:24:43 +02:00
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2017-12-28 17:43:07 +01:00
|
|
|
public class Level extends Addon {
|
2018-02-14 05:48:27 +01:00
|
|
|
|
|
|
|
// Settings
|
|
|
|
private Settings settings;
|
2017-10-22 08:09:21 +02:00
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
// Database handler for level data
|
2018-01-07 20:25:24 +01:00
|
|
|
private AbstractDatabaseHandler<LevelsData> handler;
|
2017-10-22 08:09:21 +02:00
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
// The BSkyBlock database object
|
2017-10-22 08:09:21 +02:00
|
|
|
private BSBDatabase database;
|
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
// 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.
|
2018-02-19 22:06:58 +01:00
|
|
|
private Map<UUID, Long> levelsCache;
|
2017-10-25 01:24:43 +02:00
|
|
|
|
|
|
|
// The Top Ten object
|
|
|
|
private TopTen topTen;
|
2017-12-26 17:41:37 +01:00
|
|
|
|
|
|
|
// Level calculator
|
|
|
|
private LevelPresenter levelCalc;
|
2017-10-22 08:09:21 +02:00
|
|
|
|
2018-02-19 22:06:58 +01:00
|
|
|
/**
|
|
|
|
* Calculates a user's island
|
|
|
|
* @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 AbstractDatabaseHandler<LevelsData> getHandler() {
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get level from cache for a player
|
|
|
|
* @param targetPlayer
|
|
|
|
* @return Level of player
|
|
|
|
*/
|
|
|
|
public long getIslandLevel(UUID targetPlayer) {
|
|
|
|
if (levelsCache.containsKey(targetPlayer)) {
|
|
|
|
return levelsCache.get(targetPlayer);
|
|
|
|
}
|
|
|
|
// Get from database
|
|
|
|
LevelsData level;
|
|
|
|
try {
|
|
|
|
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();
|
|
|
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
|
2018-03-12 01:36:33 +01:00
|
|
|
| ClassNotFoundException | IntrospectionException e) {
|
2018-02-19 22:06:58 +01:00
|
|
|
getLogger().severe("Could not load player's level! " + e.getMessage());
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the settings
|
|
|
|
*/
|
|
|
|
public final Settings getSettings() {
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TopTen getTopTen() {
|
|
|
|
return topTen;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void load() {
|
|
|
|
try {
|
|
|
|
for (LevelsData level : handler.loadObjects()) {
|
|
|
|
levelsCache.put(UUID.fromString(level.getUniqueId()), level.getLevel());
|
|
|
|
}
|
|
|
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
|
2018-03-12 01:36:33 +01:00
|
|
|
| ClassNotFoundException | IntrospectionException e) {
|
2018-02-19 22:06:58 +01:00
|
|
|
getLogger().severe("Could not load levels cache data! " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable(){
|
|
|
|
// Save the cache
|
|
|
|
if (levelsCache != null) {
|
|
|
|
save(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-10-21 20:32:32 +02:00
|
|
|
@Override
|
2017-10-22 04:52:11 +02:00
|
|
|
public void onEnable() {
|
2017-10-25 01:24:43 +02:00
|
|
|
// Check if it is enabled - it might be loaded, but not enabled.
|
2018-02-19 22:06:58 +01:00
|
|
|
if (getBSkyBlock() == null || !getBSkyBlock().isEnabled()) {
|
|
|
|
getLogger().severe("BSkyBlock does not exist or is not enabled. Stopping.");
|
2017-10-23 04:09:59 +02:00
|
|
|
this.setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-19 22:06:58 +01:00
|
|
|
// Load the plugin's config
|
|
|
|
settings = new Settings(this);
|
2017-10-25 01:24:43 +02:00
|
|
|
// Get the BSkyBlock database
|
2017-10-22 08:09:21 +02:00
|
|
|
database = BSBDatabase.getDatabase();
|
|
|
|
// Set up the database handler to store and retrieve Island classes
|
2017-10-23 04:09:59 +02:00
|
|
|
// Note that these are saved by the BSkyBlock database
|
2018-01-07 20:25:24 +01:00
|
|
|
handler = (AbstractDatabaseHandler<LevelsData>) database.getHandler(LevelsData.class);
|
2017-10-25 01:24:43 +02:00
|
|
|
// Initialize the cache
|
|
|
|
levelsCache = new HashMap<>();
|
2017-12-28 04:17:44 +01:00
|
|
|
// Load all the levels
|
|
|
|
load();
|
2017-12-26 17:41:37 +01:00
|
|
|
// Load the calculator
|
|
|
|
levelCalc = new LevelPresenter(this);
|
2017-10-25 01:24:43 +02:00
|
|
|
// Start the top ten and register it for clicks
|
|
|
|
topTen = new TopTen(this);
|
2017-12-28 04:17:44 +01:00
|
|
|
registerListener(topTen);
|
2017-10-23 04:09:59 +02:00
|
|
|
// Register commands
|
2018-02-19 22:06:58 +01:00
|
|
|
CompositeCommand bsbIslandCmd = getBSkyBlock().getCommandsManager().getCommand(Constants.ISLANDCOMMAND);
|
2017-12-26 17:41:37 +01:00
|
|
|
new IslandLevel(this, bsbIslandCmd);
|
|
|
|
new IslandTop(this, bsbIslandCmd);
|
2018-02-19 22:06:58 +01:00
|
|
|
CompositeCommand bsbAdminCmd = getBSkyBlock().getCommandsManager().getCommand(Constants.ADMINCOMMAND);
|
2017-12-26 17:41:37 +01:00
|
|
|
new AdminLevel(this, bsbAdminCmd);
|
|
|
|
new AdminTop(this, bsbAdminCmd);
|
2017-10-23 04:09:59 +02:00
|
|
|
// Done
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|
2017-10-22 08:09:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the levels to the database
|
|
|
|
* @param async - if true, saving will be done async
|
|
|
|
*/
|
|
|
|
public void save(boolean async){
|
|
|
|
Runnable save = () -> {
|
|
|
|
try {
|
2017-10-25 01:24:43 +02:00
|
|
|
for (Entry<UUID, Long> en : levelsCache.entrySet()) {
|
2018-01-07 20:25:24 +01:00
|
|
|
LevelsData lv = new LevelsData();
|
2017-10-25 01:24:43 +02:00
|
|
|
lv.setLevel(en.getValue());
|
|
|
|
lv.setUniqueId(en.getKey().toString());
|
|
|
|
handler.saveObject(lv);
|
|
|
|
}
|
2018-03-12 01:36:33 +01:00
|
|
|
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
|
2018-02-19 22:06:58 +01:00
|
|
|
getLogger().severe("Could not save levels async! " + e.getMessage());
|
2017-10-22 08:09:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if(async){
|
2017-12-28 04:17:44 +01:00
|
|
|
getServer().getScheduler().runTaskAsynchronously(getBSkyBlock(), save);
|
2017-10-22 08:09:21 +02:00
|
|
|
} else {
|
|
|
|
save.run();
|
|
|
|
}
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 04:52:11 +02:00
|
|
|
/**
|
2018-02-19 22:06:58 +01:00
|
|
|
* Sets the player's level to a value
|
2017-10-22 04:52:11 +02:00
|
|
|
* @param targetPlayer
|
2017-10-23 04:09:59 +02:00
|
|
|
* @param level
|
2017-10-22 04:52:11 +02:00
|
|
|
*/
|
2018-02-19 22:06:58 +01:00
|
|
|
protected void setIslandLevel(UUID targetPlayer, long level) {
|
2017-10-25 01:24:43 +02:00
|
|
|
// Add to cache
|
|
|
|
levelsCache.put(targetPlayer, level);
|
2017-10-25 03:40:17 +02:00
|
|
|
topTen.addEntry(targetPlayer, level);
|
2017-10-22 04:52:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|