2017-05-20 23:52:52 +02:00
|
|
|
package us.tastybento.bskyblock;
|
2017-05-20 23:09:53 +02:00
|
|
|
|
2017-08-13 03:51:48 +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;
|
|
|
|
|
2017-06-11 01:08:21 +02:00
|
|
|
import org.bukkit.Material;
|
2017-05-20 23:09:53 +02:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
2017-06-11 01:08:21 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2017-07-06 19:21:38 +02:00
|
|
|
import org.bukkit.plugin.PluginManager;
|
2017-05-20 23:09:53 +02:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
2017-08-13 03:51:48 +02:00
|
|
|
|
2017-07-30 19:57:49 +02:00
|
|
|
import us.tastybento.bskyblock.commands.AdminCommand;
|
2017-06-11 01:08:21 +02:00
|
|
|
import us.tastybento.bskyblock.commands.IslandCommand;
|
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-07-06 19:21:38 +02:00
|
|
|
import us.tastybento.bskyblock.listeners.JoinLeaveListener;
|
2017-07-07 01:51:40 +02:00
|
|
|
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;
|
2017-06-11 01:08:21 +02:00
|
|
|
import us.tastybento.bskyblock.schematics.SchematicsMgr;
|
2017-06-12 01:30:34 +02:00
|
|
|
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{
|
2017-06-12 01:30:34 +02:00
|
|
|
|
|
|
|
final static String LOCALE_FOLDER = "locales";
|
|
|
|
|
2017-05-20 23:52:52 +02:00
|
|
|
private static BSkyBlock plugin;
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-08-18 16:05:35 +02:00
|
|
|
private HashMap<String, BSBLocale> locales = new HashMap<>();
|
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-06-12 01:30:34 +02:00
|
|
|
|
2017-06-11 01:08:21 +02:00
|
|
|
// Schematics
|
|
|
|
private SchematicsMgr schematicsManager;
|
2017-06-12 01:30:34 +02:00
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
// Metrics
|
|
|
|
private Metrics metrics;
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-10-10 07:52:57 +02:00
|
|
|
private IslandCommand islandCommand;
|
|
|
|
|
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)){
|
|
|
|
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-06-12 01:30:34 +02:00
|
|
|
|
2017-06-11 01:08:21 +02:00
|
|
|
VaultHelper.setupPermissions();
|
2017-05-24 17:10:49 +02:00
|
|
|
|
2017-10-10 07:52:57 +02:00
|
|
|
// Set up commands
|
|
|
|
islandCommand = new IslandCommand(BSkyBlock.this);
|
|
|
|
|
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
|
|
|
|
new IslandWorld(plugin);
|
2017-07-08 02:12:14 +02:00
|
|
|
// Load islands from database
|
2017-05-30 04:02:43 +02:00
|
|
|
islandsManager.load();
|
2017-06-12 01:30:34 +02:00
|
|
|
|
2017-06-11 01:08:21 +02:00
|
|
|
// Load schematics
|
2017-06-11 01:40:43 +02:00
|
|
|
// 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)
|
2017-06-12 01:30:34 +02:00
|
|
|
};
|
2017-06-11 01:08:21 +02:00
|
|
|
schematicsManager = new SchematicsMgr(plugin);
|
2017-06-12 01:30:34 +02:00
|
|
|
|
|
|
|
Settings.defaultLanguage = "en-US";
|
|
|
|
loadLocales();
|
|
|
|
|
2017-10-10 07:52:57 +02:00
|
|
|
|
2017-07-30 19:57:49 +02:00
|
|
|
new AdminCommand(BSkyBlock.this);
|
|
|
|
|
2017-07-06 19:21:38 +02:00
|
|
|
// Register Listeners
|
2017-07-07 07:00:21 +02:00
|
|
|
registerListeners();
|
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-06-12 01:30:34 +02:00
|
|
|
*/
|
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-10-10 07:52:57 +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();
|
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-08-18 16:05:35 +02:00
|
|
|
private void registerCustomCharts(){
|
2017-05-20 23:09:53 +02:00
|
|
|
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
|
|
|
/**
|
|
|
|
* 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-07-07 01:51:40 +02:00
|
|
|
//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-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-06-12 01:30:34 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2017-08-18 16:05:35 +02:00
|
|
|
for (String language : localeDir.list(ymlFilter)) {
|
2017-06-12 01:30:34 +02:00
|
|
|
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-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;
|
|
|
|
}
|
2017-06-11 01:08:21 +02:00
|
|
|
|
|
|
|
public static BSkyBlock getPlugin() {
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the schematics
|
|
|
|
*/
|
|
|
|
public SchematicsMgr getSchematics() {
|
|
|
|
return schematicsManager;
|
|
|
|
}
|
|
|
|
|
2017-05-20 23:09:53 +02:00
|
|
|
}
|