2017-05-20 23:52:52 +02:00
|
|
|
package us.tastybento.bskyblock;
|
2017-05-20 23:09:53 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
2017-05-21 08:03:17 +02:00
|
|
|
import us.tastybento.bskyblock.config.BSBLocale;
|
2017-05-24 17:10:49 +02:00
|
|
|
import us.tastybento.bskyblock.config.PluginConfig;
|
2017-05-20 23:52:52 +02:00
|
|
|
import us.tastybento.bskyblock.config.Settings;
|
2017-05-21 08:03:17 +02:00
|
|
|
import us.tastybento.bskyblock.database.BSBDatabase;
|
2017-05-31 16:32:36 +02:00
|
|
|
import us.tastybento.bskyblock.database.managers.IslandsManager;
|
|
|
|
import us.tastybento.bskyblock.database.managers.OfflineHistoryMessages;
|
|
|
|
import us.tastybento.bskyblock.database.managers.PlayersManager;
|
2017-06-10 23:03:41 +02:00
|
|
|
import us.tastybento.bskyblock.generators.IslandWorld;
|
2017-05-20 23:52:52 +02:00
|
|
|
import us.tastybento.bskyblock.util.VaultHelper;
|
2017-05-20 23:09:53 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-20 23:52:52 +02:00
|
|
|
* Main BSkyBlock class - provides an island minigame in the sky
|
2017-05-20 23:09:53 +02:00
|
|
|
* @author Tastybento
|
|
|
|
* @author Poslovitch
|
|
|
|
*/
|
2017-05-20 23:52:52 +02:00
|
|
|
public class BSkyBlock extends JavaPlugin{
|
|
|
|
private static BSkyBlock plugin;
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-21 08:03:17 +02:00
|
|
|
private HashMap<String, BSBLocale> locales = new HashMap<String, BSBLocale>();
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
// Databases
|
|
|
|
private PlayersManager playersManager;
|
|
|
|
private IslandsManager islandsManager;
|
|
|
|
private OfflineHistoryMessages offlineHistoryMessages;
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
// Metrics
|
|
|
|
private Metrics metrics;
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
@Override
|
|
|
|
public void onEnable(){
|
|
|
|
plugin = this;
|
2017-05-30 04:02:43 +02:00
|
|
|
|
2017-05-24 22:23:16 +02:00
|
|
|
// Load configuration and locales. If there are no errors, load the plugin.
|
2017-05-24 17:10:49 +02:00
|
|
|
if(PluginConfig.loadPluginConfig(this)){
|
2017-05-26 07:54:04 +02:00
|
|
|
// TEMP DEBUG DATABASE
|
2017-06-10 17:23:12 +02:00
|
|
|
/*
|
2017-05-26 07:54:04 +02:00
|
|
|
Settings.databaseType = DatabaseType.MYSQL;
|
|
|
|
Settings.dbHost = "localhost";
|
|
|
|
Settings.dbPort = 3306;
|
|
|
|
Settings.dbName = "ASkyBlock";
|
2017-05-26 17:15:53 +02:00
|
|
|
Settings.dbUsername = "username";
|
2017-05-26 07:54:04 +02:00
|
|
|
Settings.dbPassword = "password";
|
2017-06-10 17:23:12 +02:00
|
|
|
*/
|
2017-05-24 17:10:49 +02:00
|
|
|
playersManager = new PlayersManager(this);
|
|
|
|
islandsManager = new IslandsManager(this);
|
2017-05-30 04:02:43 +02:00
|
|
|
// Only load metrics if set to true in config
|
|
|
|
if(Settings.metrics) metrics = new Metrics(plugin);
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-30 04:02:43 +02:00
|
|
|
// If metrics are loaded, register the custom data charts
|
|
|
|
if(metrics != null){
|
|
|
|
registerCustomCharts();
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
|
|
|
offlineHistoryMessages = new OfflineHistoryMessages(this);
|
|
|
|
offlineHistoryMessages.load();
|
|
|
|
|
|
|
|
if (Settings.useEconomy && !VaultHelper.setupEconomy()) {
|
|
|
|
getLogger().warning("Could not set up economy! - Running without an economy.");
|
|
|
|
Settings.useEconomy = false;
|
|
|
|
}
|
|
|
|
|
2017-05-30 04:02:43 +02:00
|
|
|
// These items have to be loaded when the server has done 1 tick.
|
|
|
|
// Note Worlds are not loaded this early, so any Locations or World reference will be null
|
|
|
|
// at this point. Therefore, the 1 tick scheduler is required.
|
|
|
|
getServer().getScheduler().runTask(this, new Runnable() {
|
2017-05-24 17:10:49 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-06-10 23:03:41 +02:00
|
|
|
// Create the world if it does not exist
|
|
|
|
// TODO: get world name from config.yml
|
|
|
|
Settings.worldName = "BSkyBlock_world";
|
|
|
|
Settings.createNether = true;
|
|
|
|
Settings.createEnd = true;
|
|
|
|
Settings.islandNether = false;
|
|
|
|
Settings.islandEnd = false;
|
|
|
|
new IslandWorld(plugin);
|
2017-05-30 04:02:43 +02:00
|
|
|
|
|
|
|
// Test: Create a random island and save it
|
|
|
|
// TODO: ideally this should be in a test class!
|
2017-06-10 19:59:53 +02:00
|
|
|
/*
|
2017-06-10 17:23:12 +02:00
|
|
|
UUID owner = UUID.fromString("ddf561c5-72b6-4ec6-a7ea-8b50a893beb2");
|
|
|
|
|
|
|
|
Island island = islandsManager.createIsland(new Location(getServer().getWorld("world"),0,0,0,0,0), owner);
|
2017-05-30 04:02:43 +02:00
|
|
|
// Add members
|
|
|
|
Set<UUID> randomSet = new HashSet<UUID>();
|
2017-06-10 17:23:12 +02:00
|
|
|
island.addMember(owner);
|
2017-05-30 04:02:43 +02:00
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
randomSet.add(UUID.randomUUID());
|
|
|
|
island.addMember(UUID.randomUUID());
|
|
|
|
island.addToBanList(UUID.randomUUID());
|
|
|
|
}
|
|
|
|
island.setBanned(randomSet);
|
|
|
|
island.setCoops(randomSet);
|
|
|
|
island.setTrustees(randomSet);
|
|
|
|
island.setMembers(randomSet);
|
|
|
|
for (SettingsFlag flag: SettingsFlag.values()) {
|
|
|
|
island.setFlag(flag, true);
|
|
|
|
}
|
|
|
|
island.setLocked(true);
|
|
|
|
island.setName("new name");
|
|
|
|
island.setPurgeProtected(true);
|
|
|
|
islandsManager.save(false);
|
2017-06-05 03:35:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
getLogger().info("DEBUG: ************ Finished saving, now loading *************");
|
2017-06-10 17:23:12 +02:00
|
|
|
|
2017-06-10 19:59:53 +02:00
|
|
|
*/
|
2017-06-10 17:23:12 +02:00
|
|
|
|
2017-05-30 04:02:43 +02:00
|
|
|
playersManager.load();
|
|
|
|
islandsManager.load();
|
2017-06-10 19:59:53 +02:00
|
|
|
/*
|
|
|
|
*DEBUG CODE
|
2017-06-10 17:23:12 +02:00
|
|
|
Island loadedIsland = islandsManager.getIsland(owner);
|
|
|
|
getLogger().info("Island name = " + loadedIsland.getName());
|
|
|
|
getLogger().info("Island locked = " + loadedIsland.getLocked());
|
|
|
|
//getLogger().info("Random set = " + randomSet);
|
|
|
|
getLogger().info("Island coops = " + loadedIsland.getCoops());
|
2017-06-10 19:59:53 +02:00
|
|
|
for (Entry<SettingsFlag, Boolean> flag: loadedIsland.getFlags().entrySet()) {
|
|
|
|
getLogger().info("Flag " + flag.getKey().name() + " = " + flag.getValue());
|
|
|
|
}
|
|
|
|
*/
|
2017-05-30 04:02:43 +02:00
|
|
|
// Save islands & players data asynchronously every X minutes
|
|
|
|
Settings.databaseBackupPeriod = 10 * 60 * 20;
|
|
|
|
plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
playersManager.save(true);
|
|
|
|
islandsManager.save(true);
|
|
|
|
offlineHistoryMessages.save(true);
|
|
|
|
}
|
|
|
|
}, Settings.databaseBackupPeriod, Settings.databaseBackupPeriod);
|
2017-05-24 17:10:49 +02:00
|
|
|
}
|
2017-05-30 04:02:43 +02:00
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
});
|
2017-05-24 17:10:49 +02:00
|
|
|
}
|
2017-05-20 23:09:53 +02:00
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
@Override
|
|
|
|
public void onDisable(){
|
|
|
|
// Save data
|
|
|
|
playersManager.shutdown();
|
|
|
|
islandsManager.shutdown();
|
2017-05-22 06:37:10 +02:00
|
|
|
//offlineHistoryMessages.shutdown();
|
2017-05-20 23:09:53 +02:00
|
|
|
plugin = null;
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
private void registerCustomCharts(){
|
|
|
|
metrics.addCustomChart(new Metrics.SingleLineChart("islands_count") {
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
@Override
|
|
|
|
public int getValue() {
|
|
|
|
return islandsManager.getCount();
|
|
|
|
}
|
|
|
|
});
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
metrics.addCustomChart(new Metrics.SingleLineChart("created_islands") {
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
@Override
|
|
|
|
public int getValue() {
|
|
|
|
int created = islandsManager.metrics_getCreatedCount();
|
|
|
|
islandsManager.metrics_setCreatedCount(0);
|
|
|
|
return created;
|
|
|
|
}
|
|
|
|
});
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
metrics.addCustomChart(new Metrics.SimplePie("default_locale") {
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
@Override
|
|
|
|
public String getValue() {
|
|
|
|
return Settings.defaultLanguage;
|
|
|
|
}
|
|
|
|
});
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
metrics.addCustomChart(new Metrics.SimplePie("database") {
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
@Override
|
|
|
|
public String getValue() {
|
2017-05-21 08:03:17 +02:00
|
|
|
return BSBDatabase.getDatabase().toString();
|
2017-05-20 23:09:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
/**
|
2017-05-20 23:52:52 +02:00
|
|
|
* Returns BSkyBlock object instance
|
|
|
|
* @return BSkyBlock instance
|
2017-05-20 23:09:53 +02:00
|
|
|
*/
|
2017-05-20 23:52:52 +02:00
|
|
|
public static BSkyBlock getInstance(){
|
2017-05-20 23:09:53 +02:00
|
|
|
return plugin;
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
/**
|
|
|
|
* Returns an HashMap of locale identifier and the related object
|
|
|
|
* @return the locales
|
|
|
|
*/
|
2017-05-21 08:03:17 +02:00
|
|
|
public HashMap<String, BSBLocale> getLocales(){
|
2017-05-20 23:09:53 +02:00
|
|
|
return locales;
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the available locales
|
|
|
|
* @param locales - the locales to set
|
|
|
|
*/
|
|
|
|
public void setLocales(HashMap<String, BSBLocale> locales){
|
|
|
|
this.locales = locales;
|
|
|
|
}
|
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
/**
|
|
|
|
* Returns the default locale
|
|
|
|
* @return the default locale
|
|
|
|
*/
|
2017-05-21 08:03:17 +02:00
|
|
|
public BSBLocale getLocale(){
|
2017-05-20 23:09:53 +02:00
|
|
|
return locales.get(Settings.defaultLanguage);
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
/**
|
|
|
|
* Returns the locale for the specified CommandSender
|
|
|
|
* @param sender - CommandSender to get the locale
|
|
|
|
* @return if sender is a player, the player's locale, otherwise the default locale
|
|
|
|
*/
|
2017-05-21 08:03:17 +02:00
|
|
|
public BSBLocale getLocale(CommandSender sender){
|
2017-05-20 23:09:53 +02:00
|
|
|
if(sender instanceof Player) return getLocale(((Player) sender).getUniqueId());
|
|
|
|
else return getLocale();
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
/**
|
|
|
|
* Returns the locale for the specified player
|
|
|
|
* @param player - Player to get the locale
|
|
|
|
* @return the locale for this player
|
|
|
|
*/
|
2017-05-21 08:03:17 +02:00
|
|
|
public BSBLocale getLocale(UUID player){
|
2017-05-20 23:09:53 +02:00
|
|
|
String locale = getPlayers().getPlayer(player).getLocale();
|
|
|
|
if(locale.isEmpty() || !locales.containsKey(locale)) return locales.get(Settings.defaultLanguage);
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
return locales.get(locale);
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
/**
|
|
|
|
* Returns the player database
|
|
|
|
* @return the player database
|
|
|
|
*/
|
|
|
|
public PlayersManager getPlayers(){
|
|
|
|
return playersManager;
|
|
|
|
}
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
/**
|
|
|
|
* Returns the island database
|
|
|
|
* @return the island database
|
|
|
|
*/
|
|
|
|
public IslandsManager getIslands(){
|
|
|
|
return islandsManager;
|
|
|
|
}
|
|
|
|
}
|