2018-10-31 14:09:56 +01:00
|
|
|
package world.bentobox.level;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
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-22 04:52:11 +02:00
|
|
|
import java.util.UUID;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2018-05-26 04:59:44 +02:00
|
|
|
import org.bukkit.World;
|
|
|
|
|
2018-12-23 21:03:08 +01:00
|
|
|
import world.bentobox.level.commands.admin.AdminLevelCommand;
|
|
|
|
import world.bentobox.level.commands.admin.AdminTopCommand;
|
|
|
|
import world.bentobox.level.commands.island.IslandLevelCommand;
|
|
|
|
import world.bentobox.level.commands.island.IslandTopCommand;
|
2018-10-31 14:09:56 +01:00
|
|
|
import world.bentobox.level.config.Settings;
|
|
|
|
import world.bentobox.level.objects.LevelsData;
|
|
|
|
import world.bentobox.level.listeners.JoinLeaveListener;
|
|
|
|
import world.bentobox.level.listeners.NewIslandListener;
|
2018-08-01 18:49:43 +02:00
|
|
|
import world.bentobox.bentobox.api.addons.Addon;
|
|
|
|
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
|
|
|
import world.bentobox.bentobox.api.user.User;
|
2018-08-06 16:43:17 +02:00
|
|
|
import world.bentobox.bentobox.database.Database;
|
2018-08-01 18:49:43 +02:00
|
|
|
import world.bentobox.bentobox.database.objects.Island;
|
2018-12-23 21:20:15 +01:00
|
|
|
import world.bentobox.level.requests.LevelRequestHandler;
|
2017-10-21 20:32:32 +02:00
|
|
|
|
2017-10-25 01:24:43 +02:00
|
|
|
/**
|
2018-12-23 21:03:08 +01:00
|
|
|
* Addon to BSkyBlock/AcidIsland 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-04-16 02:32:09 +02:00
|
|
|
|
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-08-06 16:43:17 +02:00
|
|
|
private Database<LevelsData> handler;
|
2017-10-22 08:09:21 +02:00
|
|
|
|
2018-07-28 22:38:04 +02:00
|
|
|
// A cache of island levels.
|
2018-05-26 04:59:44 +02:00
|
|
|
private Map<UUID, LevelsData> levelsCache;
|
2017-10-25 01:24:43 +02:00
|
|
|
|
|
|
|
// The Top Ten object
|
|
|
|
private TopTen topTen;
|
2018-04-16 02:32:09 +02:00
|
|
|
|
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
|
2018-07-28 22:38:04 +02:00
|
|
|
* @param world - the world where this island is
|
|
|
|
* @param user - the user who is asking, or null if none
|
|
|
|
* @param playerUUID - the target island member's UUID
|
2018-02-19 22:06:58 +01:00
|
|
|
*/
|
2018-07-28 22:38:04 +02:00
|
|
|
public void calculateIslandLevel(World world, User user, UUID playerUUID) {
|
|
|
|
levelCalc.calculateIslandLevel(world, user, playerUUID);
|
2018-02-19 22:06:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get level from cache for a player
|
2018-08-26 17:21:41 +02:00
|
|
|
* @param targetPlayer - target player
|
2018-02-19 22:06:58 +01:00
|
|
|
* @return Level of player
|
|
|
|
*/
|
2018-07-28 22:38:04 +02:00
|
|
|
public long getIslandLevel(World world, UUID targetPlayer) {
|
2018-05-26 04:59:44 +02:00
|
|
|
LevelsData ld = getLevelsData(targetPlayer);
|
|
|
|
return ld == null ? 0L : ld.getLevel(world);
|
|
|
|
}
|
2018-05-27 03:20:33 +02:00
|
|
|
|
2018-08-05 00:55:21 +02:00
|
|
|
/**
|
|
|
|
* Load a player from the cache or database
|
|
|
|
* @param targetPlayer - UUID of target player
|
|
|
|
* @return LevelsData object or null if not found
|
|
|
|
*/
|
|
|
|
public LevelsData getLevelsData(UUID targetPlayer) {
|
|
|
|
// Get from database if not in cache
|
|
|
|
if (!levelsCache.containsKey(targetPlayer) && handler.objectExists(targetPlayer.toString())) {
|
|
|
|
levelsCache.put(targetPlayer, handler.loadObject(targetPlayer.toString()));
|
|
|
|
}
|
|
|
|
// Return cached value or null
|
|
|
|
return levelsCache.get(targetPlayer);
|
2018-02-19 22:06:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the settings
|
|
|
|
*/
|
|
|
|
public final Settings getSettings() {
|
|
|
|
return settings;
|
|
|
|
}
|
2018-04-16 02:32:09 +02:00
|
|
|
|
2018-02-19 22:06:58 +01:00
|
|
|
public TopTen getTopTen() {
|
|
|
|
return topTen;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable(){
|
|
|
|
// Save the cache
|
|
|
|
if (levelsCache != null) {
|
2018-08-26 17:21:41 +02:00
|
|
|
save();
|
2018-02-19 22:06:58 +01:00
|
|
|
}
|
2018-05-27 03:20:33 +02:00
|
|
|
if (topTen != null) {
|
|
|
|
topTen.saveTopTen();
|
|
|
|
}
|
2018-02-19 22:06:58 +01:00
|
|
|
}
|
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
@Override
|
2017-10-22 04:52:11 +02:00
|
|
|
public void onEnable() {
|
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
|
|
|
// 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-08-06 16:43:17 +02:00
|
|
|
handler = new Database<>(this, LevelsData.class);
|
2017-10-25 01:24:43 +02:00
|
|
|
// Initialize the cache
|
|
|
|
levelsCache = new HashMap<>();
|
2017-12-26 17:41:37 +01:00
|
|
|
// Load the calculator
|
2018-07-30 02:22:23 +02:00
|
|
|
levelCalc = new LevelPresenter(this, this.getPlugin());
|
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);
|
2018-08-18 06:59:36 +02:00
|
|
|
// Register commands
|
2018-05-27 03:20:33 +02:00
|
|
|
// AcidIsland hook in
|
2018-08-18 06:59:36 +02:00
|
|
|
this.getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> {
|
2018-09-17 00:44:48 +02:00
|
|
|
CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand(getConfig().getString("acidisland.user-command","ai"));
|
2018-08-18 06:59:36 +02:00
|
|
|
if (acidIslandCmd != null) {
|
2018-12-23 21:03:08 +01:00
|
|
|
new IslandLevelCommand(this, acidIslandCmd);
|
|
|
|
new IslandTopCommand(this, acidIslandCmd);
|
2018-09-17 00:44:48 +02:00
|
|
|
CompositeCommand acidCmd = getPlugin().getCommandsManager().getCommand(getConfig().getString("acidisland.admin-command","acid"));
|
2018-12-23 21:03:08 +01:00
|
|
|
new AdminLevelCommand(this, acidCmd);
|
|
|
|
new AdminTopCommand(this, acidCmd);
|
2018-08-18 06:59:36 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// BSkyBlock hook in
|
|
|
|
this.getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> {
|
2018-09-17 00:44:48 +02:00
|
|
|
CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand(getConfig().getString("bskyblock.user-command","island"));
|
2018-08-18 06:59:36 +02:00
|
|
|
if (bsbIslandCmd != null) {
|
2018-12-23 21:03:08 +01:00
|
|
|
new IslandLevelCommand(this, bsbIslandCmd);
|
|
|
|
new IslandTopCommand(this, bsbIslandCmd);
|
2018-09-17 00:44:48 +02:00
|
|
|
CompositeCommand bsbAdminCmd = getPlugin().getCommandsManager().getCommand(getConfig().getString("bskyblock.admin-command","bsbadmin"));
|
2018-12-23 21:03:08 +01:00
|
|
|
new AdminLevelCommand(this, bsbAdminCmd);
|
|
|
|
new AdminTopCommand(this, bsbAdminCmd);
|
2018-08-18 06:59:36 +02:00
|
|
|
}
|
2018-05-27 03:20:33 +02:00
|
|
|
});
|
2018-07-29 06:42:29 +02:00
|
|
|
|
2018-07-28 22:38:04 +02:00
|
|
|
// Register new island listener
|
|
|
|
registerListener(new NewIslandListener(this));
|
2018-08-05 00:55:21 +02:00
|
|
|
registerListener(new JoinLeaveListener(this));
|
2018-12-23 21:20:15 +01:00
|
|
|
|
|
|
|
// Register request handlers
|
|
|
|
registerRequestHandler(new LevelRequestHandler(this));
|
|
|
|
|
2017-10-23 04:09:59 +02:00
|
|
|
// Done
|
2018-07-28 22:38:04 +02:00
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|
2017-10-22 08:09:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the levels to the database
|
|
|
|
*/
|
2018-08-26 17:21:41 +02:00
|
|
|
private void save(){
|
2018-08-05 00:55:21 +02:00
|
|
|
// No async for now
|
|
|
|
levelsCache.values().forEach(handler::saveObject);
|
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
|
2018-08-26 17:21:41 +02:00
|
|
|
* @param world - world
|
|
|
|
* @param targetPlayer - target player
|
|
|
|
* @param level - level
|
2017-10-22 04:52:11 +02:00
|
|
|
*/
|
2018-07-28 22:38:04 +02:00
|
|
|
public void setIslandLevel(World world, UUID targetPlayer, long level) {
|
2018-05-26 04:59:44 +02:00
|
|
|
LevelsData ld = getLevelsData(targetPlayer);
|
|
|
|
if (ld == null) {
|
|
|
|
ld = new LevelsData(targetPlayer, level, world);
|
|
|
|
} else {
|
|
|
|
ld.setLevel(world, level);
|
|
|
|
}
|
2017-10-25 01:24:43 +02:00
|
|
|
// Add to cache
|
2018-05-26 04:59:44 +02:00
|
|
|
levelsCache.put(targetPlayer, ld);
|
2018-07-28 22:38:04 +02:00
|
|
|
topTen.addEntry(world, targetPlayer, getIslandLevel(world, targetPlayer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the initial island level
|
|
|
|
* @param island - island
|
|
|
|
* @param level - initial calculated island level
|
|
|
|
*/
|
|
|
|
public void setInitialIslandLevel(Island island, long level) {
|
|
|
|
setIslandLevel(island.getWorld(), island.getOwner(), level);
|
|
|
|
levelsCache.get(island.getOwner()).setInitialIslandLevel(level);
|
2017-10-22 04:52:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 22:38:04 +02:00
|
|
|
|
2018-08-06 16:43:17 +02:00
|
|
|
public Database<LevelsData> getHandler() {
|
2018-04-16 02:32:09 +02:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2018-08-05 00:55:21 +02:00
|
|
|
public void uncachePlayer(UUID uniqueId) {
|
|
|
|
if (levelsCache.containsKey(uniqueId)) {
|
|
|
|
handler.saveObject(levelsCache.get(uniqueId));
|
|
|
|
}
|
|
|
|
levelsCache.remove(uniqueId);
|
|
|
|
}
|
|
|
|
|
2017-10-21 20:32:32 +02:00
|
|
|
}
|