Updated to work with latest BSB Database

This commit is contained in:
Tastybento 2018-04-15 17:32:09 -07:00
parent 6837218ae4
commit dee5ba55c6
3 changed files with 42 additions and 120 deletions

View File

@ -2,7 +2,7 @@ name: BSkyBlock-Level
main: bskyblock.addon.level.Level main: bskyblock.addon.level.Level
version: 0.1 version: 0.1
authors: [tastybento] authors: tastybento
permissions: permissions:
bskyblock.intopten: bskyblock.intopten:
@ -11,7 +11,7 @@ permissions:
bskyblock.island.level: bskyblock.island.level:
description: Player can use level command description: Player can use level command
default: true default: true
bskyblock.island.topten: bskyblock.island.top:
description: Player can use top ten command description: Player can use top ten command
default: true default: true
bskyblock.admin.level: bskyblock.admin.level:

View File

@ -1,7 +1,5 @@
package bskyblock.addon.level; package bskyblock.addon.level;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -17,7 +15,6 @@ import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.addons.Addon; import us.tastybento.bskyblock.api.addons.Addon;
import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase; import us.tastybento.bskyblock.database.BSBDatabase;
@ -32,10 +29,7 @@ public class Level extends Addon {
private Settings settings; private Settings settings;
// Database handler for level data // Database handler for level data
private AbstractDatabaseHandler<LevelsData> handler; private BSBDatabase<LevelsData> handler;
// The BSkyBlock database object
private BSBDatabase database;
// A cache of island levels. Island levels are not kept in memory unless required. // 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. // The cache is saved when the server shuts down and the plugin is disabled.
@ -58,10 +52,6 @@ public class Level extends Addon {
levelCalc.calculateIslandLevel(user, playerUUID, b); levelCalc.calculateIslandLevel(user, playerUUID, b);
} }
public AbstractDatabaseHandler<LevelsData> getHandler() {
return handler;
}
/** /**
* Get level from cache for a player * Get level from cache for a player
* @param targetPlayer * @param targetPlayer
@ -73,7 +63,6 @@ public class Level extends Addon {
} }
// Get from database // Get from database
LevelsData level; LevelsData level;
try {
level = handler.loadObject(targetPlayer.toString()); level = handler.loadObject(targetPlayer.toString());
if (level == null) { if (level == null) {
// We do not know this player, set to zero // We do not know this player, set to zero
@ -81,11 +70,6 @@ public class Level extends Addon {
} }
levelsCache.put(targetPlayer, level.getLevel()); levelsCache.put(targetPlayer, level.getLevel());
return level.getLevel(); return level.getLevel();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
| ClassNotFoundException | IntrospectionException e) {
getLogger().severe("Could not load player's level! " + e.getMessage());
}
return 0;
} }
/** /**
@ -100,14 +84,9 @@ public class Level extends Addon {
} }
private void load() { private void load() {
try {
for (LevelsData level : handler.loadObjects()) { for (LevelsData level : handler.loadObjects()) {
levelsCache.put(UUID.fromString(level.getUniqueId()), level.getLevel()); levelsCache.put(UUID.fromString(level.getUniqueId()), level.getLevel());
} }
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
| ClassNotFoundException | IntrospectionException e) {
getLogger().severe("Could not load levels cache data! " + e.getMessage());
}
} }
@Override @Override
@ -118,7 +97,6 @@ public class Level extends Addon {
} }
} }
@SuppressWarnings("unchecked")
@Override @Override
public void onEnable() { public void onEnable() {
// Check if it is enabled - it might be loaded, but not enabled. // Check if it is enabled - it might be loaded, but not enabled.
@ -130,10 +108,9 @@ public class Level extends Addon {
// Load the plugin's config // Load the plugin's config
settings = new Settings(this); settings = new Settings(this);
// Get the BSkyBlock database // Get the BSkyBlock database
database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve Island classes // Set up the database handler to store and retrieve Island classes
// Note that these are saved by the BSkyBlock database // Note that these are saved by the BSkyBlock database
handler = (AbstractDatabaseHandler<LevelsData>) database.getHandler(LevelsData.class); handler = new BSBDatabase<>(this, LevelsData.class);
// Initialize the cache // Initialize the cache
levelsCache = new HashMap<>(); levelsCache = new HashMap<>();
// Load all the levels // Load all the levels
@ -159,16 +136,12 @@ public class Level extends Addon {
*/ */
public void save(boolean async){ public void save(boolean async){
Runnable save = () -> { Runnable save = () -> {
try {
for (Entry<UUID, Long> en : levelsCache.entrySet()) { for (Entry<UUID, Long> en : levelsCache.entrySet()) {
LevelsData lv = new LevelsData(); LevelsData lv = new LevelsData();
lv.setLevel(en.getValue()); lv.setLevel(en.getValue());
lv.setUniqueId(en.getKey().toString()); lv.setUniqueId(en.getKey().toString());
handler.saveObject(lv); handler.saveObject(lv);
} }
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
getLogger().severe("Could not save levels async! " + e.getMessage());
}
}; };
if(async){ if(async){
getServer().getScheduler().runTaskAsynchronously(getBSkyBlock(), save); getServer().getScheduler().runTaskAsynchronously(getBSkyBlock(), save);
@ -188,4 +161,8 @@ public class Level extends Addon {
topTen.addEntry(targetPlayer, level); topTen.addEntry(targetPlayer, level);
} }
public BSBDatabase<LevelsData> getHandler() {
return handler;
}
} }

View File

@ -1,7 +1,5 @@
package bskyblock.addon.level; package bskyblock.addon.level;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -23,7 +21,6 @@ import us.tastybento.bskyblock.api.panels.PanelItem.ClickHandler;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder; import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder; import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase; import us.tastybento.bskyblock.database.BSBDatabase;
/** /**
@ -38,17 +35,13 @@ public class TopTen implements Listener {
private TopTenData topTenList; private TopTenData topTenList;
private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25}; private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25};
private final boolean DEBUG = true; private final boolean DEBUG = true;
private BSBDatabase database; private BSBDatabase<TopTenData> handler;
private AbstractDatabaseHandler<TopTenData> handler;
@SuppressWarnings("unchecked")
public TopTen(Level addon) { public TopTen(Level addon) {
this.addon = addon; this.addon = addon;
// Set up database
database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve the TopTenList class // Set up the database handler to store and retrieve the TopTenList class
// Note that these are saved in the BSkyBlock database // Note that these are saved in the BSkyBlock database
handler = (AbstractDatabaseHandler<TopTenData>) database.getHandler(TopTenData.class); handler = new BSBDatabase<>(addon, TopTenData.class);
loadTopTen(); loadTopTen();
} }
@ -79,8 +72,7 @@ public class TopTen implements Listener {
*/ */
public void create() { public void create() {
// Obtain all the levels for each known player // Obtain all the levels for each known player
AbstractDatabaseHandler<LevelsData> levelHandler = addon.getHandler(); BSBDatabase<LevelsData> levelHandler = addon.getHandler();
try {
long index = 0; long index = 0;
for (LevelsData lv : levelHandler.loadObjects()) { for (LevelsData lv : levelHandler.loadObjects()) {
if (index++ % 1000 == 0) { if (index++ % 1000 == 0) {
@ -93,11 +85,6 @@ public class TopTen implements Listener {
topTenList.addLevel(playerUUID, lv.getLevel()); topTenList.addLevel(playerUUID, lv.getLevel());
} }
} }
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException | ClassNotFoundException | IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
saveTopTen(); saveTopTen();
} }
@ -208,48 +195,12 @@ public class TopTen implements Listener {
* Loads the top ten from the database * Loads the top ten from the database
*/ */
public void loadTopTen() { public void loadTopTen() {
try {
topTenList = handler.loadObject("topten"); topTenList = handler.loadObject("topten");
if (topTenList == null) { if (topTenList == null) {
topTenList = new TopTenData(); topTenList = new TopTenData();
} }
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException | ClassNotFoundException | IntrospectionException e) {
e.printStackTrace();
} }
}
/*
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled=true)
public void onInventoryClick(InventoryClickEvent event) {
Inventory inventory = event.getInventory(); // The inventory that was clicked in
if (inventory.getName() == null) {
return;
}
// The player that clicked the item
Player player = (Player) event.getWhoClicked();
if (!inventory.getTitle().equals("topten.guiTitle")) {
return;
}
event.setCancelled(true);
player.updateInventory();
if(event.getCurrentItem() != null && event.getCurrentItem().getType().equals(Material.SKULL_ITEM) && event.getCurrentItem().hasItemMeta()){
player.closeInventory();
// Fire click event
TopTenClick clickEvent = new TopTenClick(((SkullMeta)event.getCurrentItem().getItemMeta()).getOwningPlayer().getName());
addon.getServer().getPluginManager().callEvent(clickEvent);
return;
}
if (event.getSlotType().equals(SlotType.OUTSIDE)) {
player.closeInventory();
return;
}
if (event.getClick().equals(ClickType.SHIFT_RIGHT)) {
player.closeInventory();
return;
}
}
*/
/** /**
* Removes ownerUUID from the top ten list * Removes ownerUUID from the top ten list
* *
@ -265,13 +216,7 @@ public class TopTen implements Listener {
//plugin.getLogger().info("DEBUG: toptenlist = null!"); //plugin.getLogger().info("DEBUG: toptenlist = null!");
return; return;
} }
try {
handler.saveObject(topTenList); handler.saveObject(topTenList);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
| IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }