Added storage of levels in the BSkyBlock database

This commit is contained in:
tastybento 2017-10-21 23:09:21 -07:00
parent 58e93923d9
commit f1842e2141
4 changed files with 114 additions and 27 deletions

View File

@ -1,5 +1,8 @@
package bskyblock.addin.level; package bskyblock.addin.level;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
@ -13,11 +16,15 @@ import org.bukkit.scheduler.BukkitTask;
import bskyblock.addin.level.config.LocaleManager; import bskyblock.addin.level.config.LocaleManager;
import bskyblock.addin.level.config.PluginConfig; import bskyblock.addin.level.config.PluginConfig;
import bskyblock.addin.level.database.object.Levels;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.ArgumentHandler; import us.tastybento.bskyblock.api.commands.ArgumentHandler;
import us.tastybento.bskyblock.api.commands.CanUseResp; import us.tastybento.bskyblock.api.commands.CanUseResp;
import us.tastybento.bskyblock.config.BSBLocale; import us.tastybento.bskyblock.config.BSBLocale;
import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.Util;
import us.tastybento.bskyblock.util.VaultHelper; import us.tastybento.bskyblock.util.VaultHelper;
@ -37,12 +44,30 @@ public class Level extends JavaPlugin {
private HashMap<UUID, Long> islandLevel; private HashMap<UUID, Long> islandLevel;
private AbstractDatabaseHandler<Levels> handler;
private BSBDatabase database;
private Levels levelsDatabase;
@Override @Override
public void onEnable() { public void onEnable() {
new PluginConfig(this); new PluginConfig(this);
bSkyBlock = BSkyBlock.getPlugin(); bSkyBlock = BSkyBlock.getPlugin();
islandLevel = new HashMap<>(); islandLevel = new HashMap<>();
// Set up database
database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve Island classes
handler = (AbstractDatabaseHandler<Levels>) database.getHandler(bSkyBlock, Levels.class);
try {
levelsDatabase = handler.loadObject("addon-levels");
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | SecurityException | ClassNotFoundException | IntrospectionException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new TopTen(this); new TopTen(this);
// Local locales // Local locales
localeManager = new LocaleManager(this); localeManager = new LocaleManager(this);
@ -90,7 +115,30 @@ public class Level extends JavaPlugin {
@Override @Override
public void onDisable(){ public void onDisable(){
if (levelsDatabase != null) {
save(false);
}
}
/**
* Save the levels to the database
* @param async - if true, saving will be done async
*/
public void save(boolean async){
Runnable save = () -> {
try {
handler.saveObject(levelsDatabase);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
if(async){
getServer().getScheduler().runTaskAsynchronously(this, save);
} else {
save.run();
}
} }
/** /**
@ -172,17 +220,15 @@ public class Level extends JavaPlugin {
return 0L; return 0L;
} }
public long getIslandLevel(UUID targetPlayer) { public Long getIslandLevel(UUID targetPlayer) {
//getLogger().info("DEBUG: getting island level for " + bSkyBlock.getPlayers().getName(targetPlayer)); //getLogger().info("DEBUG: getting island level for " + bSkyBlock.getPlayers().getName(targetPlayer));
if (islandLevel.containsKey(targetPlayer)) return levelsDatabase.getLevel(targetPlayer);
return islandLevel.get(targetPlayer);
return 0;
} }
public void setIslandLevel(UUID targetPlayer, long level) { public void setIslandLevel(UUID targetPlayer, long level) {
//getLogger().info("DEBUG: set island level to " + level + " for " + bSkyBlock.getPlayers().getName(targetPlayer)); //getLogger().info("DEBUG: set island level to " + level + " for " + bSkyBlock.getPlayers().getName(targetPlayer));
islandLevel.put(targetPlayer, level); levelsDatabase.addLevel(targetPlayer, level);
save(true);
} }
/** /**

View File

@ -43,6 +43,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
import bskyblock.addin.level.config.Settings; import bskyblock.addin.level.config.Settings;
import bskyblock.addin.level.util.MapUtil;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
/** /**

View File

@ -0,0 +1,40 @@
package bskyblock.addin.level.database.object;
import java.util.HashMap;
import java.util.UUID;
import us.tastybento.bskyblock.database.objects.DataObject;
public class Levels extends DataObject {
private String uniqueId = "addon-levels";
private HashMap<UUID, Long> islandLevel = new HashMap<>();
@Override
public String getUniqueId() {
return "addon-levels";
}
@Override
public void setUniqueId(String uniqueId) {
// do nothing
}
public HashMap<UUID, Long> getIslandLevel() {
return islandLevel;
}
public void setIslandLevel(HashMap<UUID, Long> islandLevel) {
this.islandLevel = islandLevel;
}
public void addLevel(UUID uuid, Long level) {
this.islandLevel.put(uuid, level);
}
public Long getLevel(UUID uuid) {
if (islandLevel.containsKey(uuid))
return (long)islandLevel.get(uuid);
return 0L;
}
}

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with ASkyBlock. If not, see <http://www.gnu.org/licenses/>. * along with ASkyBlock. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/ *******************************************************************************/
package bskyblock.addin.level; package bskyblock.addin.level.util;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;