bentobox/src/main/java/us/tastybento/bskyblock/BSkyBlock.java

252 lines
8.9 KiB
Java
Raw Normal View History

2017-05-20 23:52:52 +02:00
package us.tastybento.bskyblock;
2017-05-20 23:09:53 +02:00
import java.util.UUID;
import org.bukkit.Material;
2017-05-20 23:09:53 +02:00
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
2017-05-20 23:09:53 +02:00
import org.bukkit.plugin.java.JavaPlugin;
2017-07-30 19:57:49 +02:00
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.commands.IslandCommand;
import us.tastybento.bskyblock.config.BSBLocale;
import us.tastybento.bskyblock.config.LocaleManager;
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.OfflineHistoryMessages;
import us.tastybento.bskyblock.database.managers.PlayersManager;
import us.tastybento.bskyblock.database.managers.island.IslandsManager;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.NetherPortals;
import us.tastybento.bskyblock.listeners.PanelListener;
2017-07-07 07:00:21 +02:00
import us.tastybento.bskyblock.listeners.protection.IslandGuard;
import us.tastybento.bskyblock.listeners.protection.IslandGuard1_8;
import us.tastybento.bskyblock.listeners.protection.IslandGuard1_9;
import us.tastybento.bskyblock.listeners.protection.NetherEvents;
2017-12-10 09:19:20 +01:00
import us.tastybento.bskyblock.util.Util;
2017-05-20 23:52:52 +02:00
import us.tastybento.bskyblock.util.VaultHelper;
2017-12-10 09:19:20 +01:00
import us.tastybento.bskyblock.util.nms.NMSAbstraction;
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
*/
public class BSkyBlock extends JavaPlugin {
2017-05-20 23:52:52 +02:00
private static BSkyBlock plugin;
2017-05-20 23:09:53 +02:00
// Databases
private PlayersManager playersManager;
private IslandsManager islandsManager;
private OfflineHistoryMessages offlineHistoryMessages;
2017-05-20 23:09:53 +02:00
// Metrics
private Metrics metrics;
// Commands
private IslandCommand islandCommand;
private AdminCommand adminCommand;
protected LocaleManager localeManager;
2017-05-20 23:09:53 +02:00
@Override
public void onEnable(){
plugin = this;
2017-05-24 22:23:16 +02:00
// Load configuration and locales. If there are no errors, load the plugin.
if(PluginConfig.loadPluginConfig(this)){
playersManager = new PlayersManager(this);
islandsManager = new IslandsManager(this);
// Only load metrics if set to true in config
if(Settings.metrics) {
metrics = new Metrics(plugin);
registerCustomCharts();
}
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;
}
VaultHelper.setupPermissions();
// Set up commands
2017-12-10 11:46:59 +01:00
islandCommand = new IslandCommand();
adminCommand = new AdminCommand();
// 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() {
@Override
public void run() {
// Create the world if it does not exist
new IslandWorld(plugin);
getServer().getScheduler().runTask(plugin, new Runnable() {
@Override
public void run() {
// Load islands from database
islandsManager.load();
// TODO: load these from config.yml
Settings.chestItems = new ItemStack[] {
new ItemStack(Material.LAVA_BUCKET,1),
new ItemStack(Material.ICE,2),
new ItemStack(Material.MELON_SEEDS,1),
new ItemStack(Material.BONE,2),
new ItemStack(Material.COBBLESTONE,5),
new ItemStack(Material.SAPLING,2)
};
Settings.defaultLanguage = "en-US";
localeManager = new LocaleManager(plugin);
// Register Listeners
registerListeners();
/*
*DEBUG CODE
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());
for (Entry<SettingsFlag, Boolean> flag: loadedIsland.getFlags().entrySet()) {
getLogger().info("Flag " + flag.getKey().name() + " = " + flag.getValue());
}
*/
// 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-20 23:09:53 +02:00
}
public IslandCommand getIslandCommand() {
return islandCommand;
}
private void registerListeners() {
2017-07-07 07:00:21 +02:00
PluginManager manager = getServer().getPluginManager();
// Player join events
manager.registerEvents(new JoinLeaveListener(this), this);
manager.registerEvents(new NetherEvents(this), this);
manager.registerEvents(new NetherPortals(this), this);
manager.registerEvents(new IslandGuard(this), this);
manager.registerEvents(new IslandGuard1_8(this), this);
manager.registerEvents(new IslandGuard1_9(this), this);
manager.registerEvents(new PanelListener(this), this);
2017-07-07 07:00:21 +02:00
}
2017-05-20 23:09:53 +02:00
@Override
public void onDisable(){
// Save data
playersManager.shutdown();
islandsManager.shutdown();
//offlineHistoryMessages.shutdown();
2017-05-20 23:09:53 +02:00
}
private void registerCustomCharts(){
2017-05-20 23:09:53 +02:00
metrics.addCustomChart(new Metrics.SingleLineChart("islands_count") {
2017-05-20 23:09:53 +02:00
@Override
public int getValue() {
return islandsManager.getCount();
}
});
2017-05-20 23:09:53 +02:00
metrics.addCustomChart(new Metrics.SingleLineChart("created_islands") {
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-20 23:09:53 +02:00
metrics.addCustomChart(new Metrics.SimplePie("default_locale") {
2017-05-20 23:09:53 +02:00
@Override
public String getValue() {
return Settings.defaultLanguage;
}
});
2017-05-20 23:09:53 +02:00
metrics.addCustomChart(new Metrics.SimplePie("database") {
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-20 23:09:53 +02:00
/**
* Returns the player database
* @return the player database
*/
public PlayersManager getPlayers(){
return playersManager;
}
2017-05-20 23:09:53 +02:00
/**
* Returns the island database
* @return the island database
*/
public IslandsManager getIslands(){
return islandsManager;
}
public static BSkyBlock getPlugin() {
return plugin;
}
/**
* @param sender
* @return Locale object for sender
*/
public BSBLocale getLocale(CommandSender sender) {
return localeManager.getLocale(sender);
}
/**
* @param uuid
* @return Locale object for UUID
*/
public BSBLocale getLocale(UUID uuid) {
return localeManager.getLocale(uuid);
}
2017-12-10 09:19:20 +01:00
public NMSAbstraction getNMSHandler() {
NMSAbstraction nmsHandler = null;
try {
nmsHandler = Util.getNMSHandler();
} catch(Exception e) {
e.printStackTrace();
}
return nmsHandler;
}
2017-05-20 23:09:53 +02:00
}