2017-10-21 20:32:32 +02:00
|
|
|
package bskyblock.addin.level;
|
|
|
|
|
2017-10-22 08:09:21 +02:00
|
|
|
import java.beans.IntrospectionException;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.sql.SQLException;
|
2017-10-25 01:24:43 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
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
|
|
|
|
2017-10-22 04:52:11 +02:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2017-12-26 17:41:37 +01:00
|
|
|
import bskyblock.addin.level.commands.AdminLevel;
|
|
|
|
import bskyblock.addin.level.commands.AdminTop;
|
|
|
|
import bskyblock.addin.level.commands.IslandLevel;
|
|
|
|
import bskyblock.addin.level.commands.IslandTop;
|
2017-10-22 04:52:11 +02:00
|
|
|
import bskyblock.addin.level.config.PluginConfig;
|
2017-10-22 08:09:21 +02:00
|
|
|
import bskyblock.addin.level.database.object.Levels;
|
2017-10-21 20:32:32 +02:00
|
|
|
import us.tastybento.bskyblock.BSkyBlock;
|
2017-12-28 04:17:44 +01:00
|
|
|
import us.tastybento.bskyblock.api.addons.AddOn;
|
2017-12-26 17:41:37 +01:00
|
|
|
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
|
|
|
import us.tastybento.bskyblock.api.commands.User;
|
|
|
|
import us.tastybento.bskyblock.config.Settings;
|
2017-10-22 08:09:21 +02:00
|
|
|
import us.tastybento.bskyblock.database.BSBDatabase;
|
|
|
|
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
/**
|
|
|
|
* Addin to BSkyBlock that enables island level scoring and top ten functionality
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2017-12-28 04:17:44 +01:00
|
|
|
public class Level extends AddOn {
|
2017-10-22 08:09:21 +02:00
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
|
|
|
|
// The BSkyBlock plugin instance.
|
2017-10-22 04:52:11 +02:00
|
|
|
private BSkyBlock bSkyBlock;
|
2017-10-22 08:09:21 +02:00
|
|
|
|
2017-10-22 04:52:11 +02:00
|
|
|
// Level calc checker
|
|
|
|
BukkitTask checker = null;
|
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
// Database handler for level data
|
2017-10-22 08:09:21 +02:00
|
|
|
private AbstractDatabaseHandler<Levels> handler;
|
|
|
|
|
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.
|
|
|
|
private HashMap<UUID, Long> levelsCache;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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-23 04:09:59 +02:00
|
|
|
// Load the plugin's config
|
2017-10-22 04:52:11 +02:00
|
|
|
new PluginConfig(this);
|
2017-10-25 01:24:43 +02:00
|
|
|
// Get the BSkyBlock plugin. This will be available because this plugin depends on it in plugin.yml.
|
2017-12-28 04:17:44 +01:00
|
|
|
bSkyBlock = BSkyBlock.getInstance();
|
2017-10-25 01:24:43 +02:00
|
|
|
// Check if it is enabled - it might be loaded, but not enabled.
|
2017-10-23 04:09:59 +02:00
|
|
|
if (!bSkyBlock.isEnabled()) {
|
|
|
|
this.setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
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
|
2017-10-22 08:09:21 +02:00
|
|
|
handler = (AbstractDatabaseHandler<Levels>) database.getHandler(bSkyBlock, Levels.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-22 04:52:11 +02:00
|
|
|
// Local locales
|
2017-12-26 17:41:37 +01:00
|
|
|
//localeManager = new LocaleManager(this);
|
2017-10-23 04:09:59 +02:00
|
|
|
// Register commands
|
2017-12-28 04:17:44 +01:00
|
|
|
CompositeCommand bsbIslandCmd = (CompositeCommand) BSkyBlock.getInstance().getCommandsManager().getCommand(Settings.ISLANDCOMMAND);
|
2017-12-26 17:41:37 +01:00
|
|
|
new IslandLevel(this, bsbIslandCmd);
|
|
|
|
new IslandTop(this, bsbIslandCmd);
|
2017-12-28 04:17:44 +01:00
|
|
|
CompositeCommand bsbAdminCmd = (CompositeCommand) BSkyBlock.getInstance().getCommandsManager().getCommand(Settings.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
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
@Override
|
|
|
|
public void onDisable(){
|
2017-10-23 04:09:59 +02:00
|
|
|
// Save the cache
|
|
|
|
if (levelsCache != null) {
|
2017-10-22 08:09:21 +02:00
|
|
|
save(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 04:17:44 +01:00
|
|
|
public void load() {
|
|
|
|
try {
|
|
|
|
for (Levels level : handler.loadObjects()) {
|
|
|
|
levelsCache.put(UUID.fromString(level.getUniqueId()), level.getLevel());
|
|
|
|
}
|
|
|
|
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
|
|
|
| SecurityException | ClassNotFoundException | IntrospectionException | SQLException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
Levels lv = new Levels();
|
|
|
|
lv.setLevel(en.getValue());
|
|
|
|
lv.setUniqueId(en.getKey().toString());
|
|
|
|
handler.saveObject(lv);
|
|
|
|
}
|
2017-10-22 08:09:21 +02:00
|
|
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
|
|
|
|
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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
|
|
|
/**
|
2017-10-23 04:09:59 +02:00
|
|
|
* Get level from cache for a player
|
2017-10-22 04:52:11 +02:00
|
|
|
* @param targetPlayer
|
2017-10-23 04:09:59 +02:00
|
|
|
* @return Level of player
|
2017-10-22 04:52:11 +02:00
|
|
|
*/
|
2017-10-25 01:24:43 +02:00
|
|
|
public long getIslandLevel(UUID targetPlayer) {
|
2017-10-23 04:09:59 +02:00
|
|
|
//getLogger().info("DEBUG: getting island level for " + bSkyBlock.getPlayers().getName(targetPlayer));
|
2017-10-25 01:24:43 +02:00
|
|
|
if (levelsCache.containsKey(targetPlayer)) {
|
|
|
|
return levelsCache.get(targetPlayer);
|
|
|
|
}
|
|
|
|
// Get from database
|
|
|
|
Levels 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 | IllegalArgumentException | InvocationTargetException
|
|
|
|
| SecurityException | ClassNotFoundException | IntrospectionException | SQLException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return 0;
|
2017-10-22 04:52:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-25 01:24:43 +02:00
|
|
|
* Save the player's level
|
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
|
|
|
*/
|
|
|
|
public void setIslandLevel(UUID targetPlayer, long level) {
|
|
|
|
//getLogger().info("DEBUG: set island level to " + level + " for " + bSkyBlock.getPlayers().getName(targetPlayer));
|
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-25 01:24:43 +02:00
|
|
|
public AbstractDatabaseHandler<Levels> getHandler() {
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TopTen getTopTen() {
|
|
|
|
return topTen;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:41:37 +01:00
|
|
|
public void calculateIslandLevel(User user, UUID playerUUID, boolean b) {
|
|
|
|
levelCalc.calculateIslandLevel(user, playerUUID, b);
|
|
|
|
}
|
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|