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

344 lines
12 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.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Locale;
import java.util.UUID;
import org.bukkit.Material;
2017-05-20 23:09:53 +02:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
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;
2017-05-21 08:03:17 +02:00
import us.tastybento.bskyblock.config.BSBLocale;
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;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.NetherPortals;
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;
import us.tastybento.bskyblock.schematics.SchematicsMgr;
import us.tastybento.bskyblock.util.FileLister;
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{
final static String LOCALE_FOLDER = "locales";
2017-05-20 23:52:52 +02:00
private static BSkyBlock plugin;
private HashMap<String, BSBLocale> locales = new HashMap<>();
2017-05-20 23:09:53 +02:00
// Databases
private PlayersManager playersManager;
private IslandsManager islandsManager;
private OfflineHistoryMessages offlineHistoryMessages;
// Schematics
private SchematicsMgr schematicsManager;
2017-05-20 23:09:53 +02:00
// Metrics
private Metrics metrics;
private IslandCommand islandCommand;
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);
// If metrics are loaded, register the custom data charts
if(metrics != null){
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
islandCommand = new IslandCommand(BSkyBlock.this);
// 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);
2017-07-08 02:12:14 +02:00
// Load islands from database
islandsManager.load();
// Load schematics
// 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)
};
schematicsManager = new SchematicsMgr(plugin);
Settings.defaultLanguage = "en-US";
loadLocales();
2017-07-30 19:57:49 +02:00
new AdminCommand(BSkyBlock.this);
// Register Listeners
2017-07-07 07:00:21 +02:00
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);
}
// TODO Auto-generated method stub
});
}
2017-05-20 23:09:53 +02:00
}
public IslandCommand getIslandCommand() {
return islandCommand;
}
2017-07-07 07:00:21 +02:00
protected void registerListeners() {
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);
}
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
plugin = null;
}
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 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;
}
/**
* 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-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-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){
//getLogger().info("DEBUG: " + player);
//getLogger().info("DEBUG: " + getPlayers() == null ? "Players is null":"Players in not null");
//getLogger().info("DEBUG: " + getPlayers().getPlayer(player));
//getLogger().info("DEBUG: " + getPlayers().getPlayer(player).getLocale());
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-20 23:09:53 +02:00
return locales.get(locale);
}
/**
* Loads all the locales available. If the locale folder does not exist, one will be created and
* filled with locale files from the jar.
*/
public void loadLocales() {
// Describe the filter - we only want files that are correctly named
FilenameFilter ymlFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
//plugin.getLogger().info("DEBUG: filename = " + name);
if (name.toLowerCase().startsWith("bsb_") && name.toLowerCase().endsWith(".yml")) {
// See if this is a valid locale
//Locale localeObject = new Locale(name.substring(0, 2), name.substring(3, 5));
Locale localeObject = Locale.forLanguageTag(name.substring(4, name.length() - 4));
if (localeObject == null) {
plugin.getLogger().severe("Filename '" + name + "' is an unknown locale, skipping...");
return false;
}
return true;
} else {
if (name.toLowerCase().endsWith(".yml")) {
plugin.getLogger().severe("Filename '" + name + "' is not in the correct format for a locale file - skipping...");
}
return false;
}
}
};
// Run through the files and store the locales
File localeDir = new File(this.getDataFolder(), LOCALE_FOLDER);
// If the folder does not exist, then make it and fill with the locale files from the jar
if (!localeDir.exists()) {
localeDir.mkdir();
FileLister lister = new FileLister(this);
try {
for (String name : lister.listJar(LOCALE_FOLDER)) {
this.saveResource(name,true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Store all the locales available
for (String language : localeDir.list(ymlFilter)) {
try {
BSBLocale locale = new BSBLocale(this, language);
locales.put(locale.getLocaleId(), locale);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
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;
}
/**
* @return the schematics
*/
public SchematicsMgr getSchematics() {
return schematicsManager;
}
2017-05-20 23:09:53 +02:00
}