/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.drtshock.playervaults;
import com.drtshock.playervaults.commands.ConvertCommand;
import com.drtshock.playervaults.commands.DeleteCommand;
import com.drtshock.playervaults.commands.SignCommand;
import com.drtshock.playervaults.commands.SignSetInfo;
import com.drtshock.playervaults.commands.VaultCommand;
import com.drtshock.playervaults.config.Loader;
import com.drtshock.playervaults.config.file.Config;
import com.drtshock.playervaults.listeners.Listeners;
import com.drtshock.playervaults.listeners.SignListener;
import com.drtshock.playervaults.listeners.VaultPreloadListener;
import com.drtshock.playervaults.tasks.Base64Conversion;
import com.drtshock.playervaults.tasks.Cleanup;
import com.drtshock.playervaults.tasks.UUIDConversion;
import com.drtshock.playervaults.translations.Lang;
import com.drtshock.playervaults.translations.Language;
import com.drtshock.playervaults.vaultmanagement.UUIDVaultManager;
import com.drtshock.playervaults.vaultmanagement.VaultManager;
import com.drtshock.playervaults.vaultmanagement.VaultViewInfo;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.logging.Level;
public class PlayerVaults extends JavaPlugin {
public static boolean DEBUG;
private static PlayerVaults instance;
private final HashMap setSign = new HashMap<>();
// Player name - VaultViewInfo
private final HashMap inVault = new HashMap<>();
// VaultViewInfo - Inventory
private final HashMap openInventories = new HashMap<>();
private final Set blockedMats = new HashSet<>();
private Economy economy;
private boolean useVault;
private YamlConfiguration signs;
private File signsFile;
private boolean saveQueued;
private boolean backupsEnabled;
private File backupsFolder;
private File uuidData;
private File vaultData;
private String _versionString;
private int maxVaultAmountPermTest;
private Metrics metrics;
private Config config = new Config();
public static PlayerVaults getInstance() {
return instance;
}
public static void debug(String s, long start) {
if (DEBUG) {
instance.getLogger().log(Level.INFO, "{0} took {1}ms", new Object[]{s, (System.currentTimeMillis() - start)});
}
}
public static void debug(String s) {
if (DEBUG) {
instance.getLogger().log(Level.INFO, s);
}
}
@Override
public void onEnable() {
instance = this;
long start = System.currentTimeMillis();
long time = System.currentTimeMillis();
loadConfig();
DEBUG = getConf().isDebug();
debug("config", time);
time = System.currentTimeMillis();
uuidData = new File(this.getDataFolder(), "uuidvaults");
vaultData = new File(this.getDataFolder(), "base64vaults");
debug("vaultdata", time);
time = System.currentTimeMillis();
getServer().getScheduler().runTask(this, new UUIDConversion()); // Convert to UUIDs first. Class checks if necessary.
debug("uuid conversion", time);
time = System.currentTimeMillis();
new VaultManager();
getServer().getScheduler().runTask(this, new Base64Conversion());
debug("base64 conversion", time);
time = System.currentTimeMillis();
loadLang();
debug("lang", time);
time = System.currentTimeMillis();
new UUIDVaultManager();
debug("uuidvaultmanager", time);
time = System.currentTimeMillis();
getServer().getPluginManager().registerEvents(new Listeners(this), this);
getServer().getPluginManager().registerEvents(new VaultPreloadListener(), this);
getServer().getPluginManager().registerEvents(new SignListener(this), this);
debug("registering listeners", time);
time = System.currentTimeMillis();
this.backupsEnabled = this.getConf().getStorage().getFlatFile().isBackups();
this.maxVaultAmountPermTest = this.getConf().getMaxVaultAmountPermTest();
loadSigns();
debug("loaded signs", time);
time = System.currentTimeMillis();
getCommand("pv").setExecutor(new VaultCommand());
getCommand("pvdel").setExecutor(new DeleteCommand());
getCommand("pvconvert").setExecutor(new ConvertCommand());
getCommand("pvsign").setExecutor(new SignCommand());
debug("registered commands", time);
time = System.currentTimeMillis();
useVault = setupEconomy();
debug("setup economy", time);
if (getConf().getPurge().isEnabled()) {
getServer().getScheduler().runTaskAsynchronously(this, new Cleanup(getConf().getPurge().getDaysSinceLastEdit()));
}
new BukkitRunnable() {
@Override
public void run() {
if (saveQueued) {
saveSignsFile();
}
}
}.runTaskTimer(this, 20, 20);
this.metrics = new Metrics(this, 6905);
Plugin vault = getServer().getPluginManager().getPlugin("Vault");
this.metricsDrillPie("vault", () -> this.metricsPluginInfo(vault));
if (vault != null) {
this.metricsDrillPie("vault_econ", () -> {
Map> map = new HashMap<>();
Map entry = new HashMap<>();
entry.put(economy == null ? "none" : economy.getName(), 1);
map.put(isEconomyEnabled() ? "enabled" : "disabled", entry);
return map;
});
if (isEconomyEnabled()) {
String name = economy.getName();
if (name.equals("Essentials Economy")) {
name = "Essentials";
}
Plugin plugin = getServer().getPluginManager().getPlugin(name);
if (plugin != null) {
this.metricsDrillPie("vault_econ_plugins", () -> {
Map> map = new HashMap<>();
Map entry = new HashMap<>();
entry.put(plugin.getDescription().getVersion(), 1);
map.put(plugin.getName(), entry);
return map;
});
}
}
}
if (vault != null) {
RegisteredServiceProvider provider = getServer().getServicesManager().getRegistration(Permission.class);
if (provider != null) {
Permission perm = provider.getProvider();
String name = perm.getName();
Plugin plugin = getServer().getPluginManager().getPlugin(name);
final String version;
if (plugin == null) {
version = "unknown";
} else {
version = plugin.getDescription().getVersion();
}
this.metricsDrillPie("vault_perms", () -> {
Map> map = new HashMap<>();
Map entry = new HashMap<>();
entry.put(version, 1);
map.put(name, entry);
return map;
});
}
}
this.metricsSimplePie("signs", () -> getConf().isSigns() ? "enabled" : "disabled");
this.metricsSimplePie("cleanup", () -> getConf().getPurge().isEnabled() ? "enabled" : "disabled");
this.metricsSimplePie("language", () -> getConf().getLanguage());
this.metricsDrillPie("block_items", () -> {
Map> map = new HashMap<>();
Map entry = new HashMap<>();
if (getConf().getItemBlocking().isEnabled()) {
for (Material material : blockedMats) {
entry.put(material.toString(), 1);
}
}
if (entry.isEmpty()) {
entry.put("none", 1);
}
map.put(getConf().getItemBlocking().isEnabled() ? "enabled" : "disabled", entry);
return map;
});
this.getLogger().info("Loaded! Took " + (System.currentTimeMillis() - start) + "ms");
}
private void metricsLine(String name, Callable callable) {
this.metrics.addCustomChart(new Metrics.SingleLineChart(name, callable));
}
private void metricsDrillPie(String name, Callable