1
1
mirror of https://github.com/geekfrog/PermissionsTime.git synced 2024-11-26 10:38:53 +00:00
PermissionsTime/src/main/gg/frog/mc/permissionstime/PluginMain.java

178 lines
6.4 KiB
Java
Raw Normal View History

2017-07-06 10:17:54 +00:00
package gg.frog.mc.permissionstime;
2017-07-13 22:33:19 +00:00
import java.text.MessageFormat;
2017-07-06 10:17:54 +00:00
import java.util.Locale;
2017-07-11 23:17:11 +00:00
import java.util.UUID;
2017-07-06 10:17:54 +00:00
import java.util.logging.Logger;
2017-07-10 05:06:41 +00:00
2017-07-10 12:20:24 +00:00
import org.bukkit.Bukkit;
2017-07-11 23:17:11 +00:00
import org.bukkit.OfflinePlayer;
2017-07-10 12:55:45 +00:00
import org.bukkit.plugin.RegisteredServiceProvider;
2017-07-06 10:17:54 +00:00
import org.bukkit.plugin.java.JavaPlugin;
2017-07-10 05:06:41 +00:00
import org.mcstats.Metrics;
2017-07-06 10:17:54 +00:00
2017-07-11 16:11:05 +00:00
import gg.frog.mc.permissionstime.command.MainCommand;
2017-07-06 10:17:54 +00:00
import gg.frog.mc.permissionstime.config.ConfigManager;
import gg.frog.mc.permissionstime.config.PluginCfg;
2017-07-11 12:50:53 +00:00
import gg.frog.mc.permissionstime.database.SqlManager;
2017-07-14 20:58:10 +00:00
import gg.frog.mc.permissionstime.listener.MainListener;
2017-07-13 22:33:19 +00:00
import gg.frog.mc.permissionstime.utils.FileUtil;
2017-07-06 10:17:54 +00:00
import gg.frog.mc.permissionstime.utils.StrUtil;
2017-07-10 12:55:45 +00:00
import net.milkbowl.vault.permission.Permission;
2017-07-06 10:17:54 +00:00
public class PluginMain extends JavaPlugin {
2017-07-11 23:17:11 +00:00
public String PLUGIN_NAME;
public String PLUGIN_VERSION;
public String PLUGIN_NAME_LOWER_CASE;
2017-07-10 12:55:45 +00:00
public static final String DEPEND_PLUGIN = "SQLibrary,Vault";
2017-07-10 12:20:24 +00:00
public static Logger LOG = Logger.getLogger("Minecraft");
2017-07-06 10:17:54 +00:00
2017-07-11 23:17:11 +00:00
private ConfigManager cm = null;
private PluginMain pm = null;
private SqlManager sm = null;
private Permission permission = null;
2017-07-10 02:45:18 +00:00
2017-07-10 12:20:24 +00:00
public PluginMain() {
PLUGIN_NAME = getDescription().getName();
PLUGIN_VERSION = getDescription().getVersion();
PLUGIN_NAME_LOWER_CASE = PLUGIN_NAME.toLowerCase(Locale.ENGLISH);
}
2017-07-06 10:17:54 +00:00
@Override
public void onEnable() {
2017-07-10 12:20:24 +00:00
super.onEnable();
pm = this;
2017-07-11 23:17:11 +00:00
cm = new ConfigManager(pm);
2017-07-10 12:20:24 +00:00
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "==============================="));
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX));
2017-07-11 23:17:11 +00:00
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " " + PLUGIN_NAME + " v" + PLUGIN_VERSION));
2017-07-10 12:20:24 +00:00
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " authorGeekFrog QQ324747460"));
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + " https://github.com/geekfrog/PermissionsTime/ "));
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX));
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "==============================="));
if (PluginCfg.IS_METRICS) {
try {
2017-07-11 23:17:11 +00:00
Metrics metrics = new Metrics(pm);
2017-07-10 12:20:24 +00:00
metrics.start();
} catch (Exception e) {
e.printStackTrace();
2017-07-10 05:06:41 +00:00
}
}
2017-07-11 23:17:11 +00:00
getServer().getScheduler().runTask(pm, new Runnable() {
2017-07-10 12:20:24 +00:00
public void run() {
if (!checkPluginDepends()) {
2017-07-16 02:38:19 +00:00
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "启动失败!"));
2017-07-10 12:20:24 +00:00
getServer().getPluginManager().disablePlugin(pm);
2017-07-10 12:55:45 +00:00
} else {
registerListeners();
registerCommands();
2017-07-16 02:38:19 +00:00
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "启动成功!"));
2017-07-10 12:20:24 +00:00
}
}
2017-07-11 12:50:53 +00:00
});
2017-07-06 10:17:54 +00:00
}
/**
* 注册监听器 <br/>
* 这里可以注册多个
*/
private void registerListeners() {
2017-07-14 20:58:10 +00:00
pm.getServer().getPluginManager().registerEvents(new MainListener(pm), pm);
2017-07-06 10:17:54 +00:00
}
/**
* 注册命令 <br/>
* 这里可以注册多个一般注册一个就够用
*/
private void registerCommands() {
2017-07-14 20:58:10 +00:00
MainCommand mcmd = new MainCommand(pm);
if (getDescription().getCommands().containsKey(PLUGIN_NAME_LOWER_CASE)) {
getCommand(PLUGIN_NAME_LOWER_CASE).setExecutor(mcmd);
}
if (getDescription().getCommands().containsKey("pt")) {
getCommand("pt").setExecutor(mcmd);
}
2017-07-06 10:17:54 +00:00
}
public ConfigManager getConfigManager() {
return cm;
}
2017-07-10 05:06:41 +00:00
2017-07-11 23:17:11 +00:00
public SqlManager getSqlManager() {
return sm;
}
public Permission getPermission() {
return permission;
}
2017-07-10 05:06:41 +00:00
private boolean checkPluginDepends() {
2017-07-10 12:20:24 +00:00
boolean needDepend = false;
2017-07-10 12:55:45 +00:00
for (String name : DEPEND_PLUGIN.split(",")) {
if (getServer().getPluginManager().getPlugin(name) == null) {
2017-07-10 12:20:24 +00:00
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Need depend plugins : " + name));
needDepend = true;
}
}
2017-07-11 23:17:11 +00:00
if (!needDepend && !setupPermissions()) {
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann''t hook vault permission"));
2017-07-10 12:55:45 +00:00
needDepend = true;
}
2017-07-11 23:17:11 +00:00
if (!needDepend && !setupDatabase()) {
getServer().getConsoleSender().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "Cann''t setup database"));
2017-07-10 12:55:45 +00:00
needDepend = true;
}
if (needDepend) {
2017-07-10 12:20:24 +00:00
return false;
}
2017-07-10 05:06:41 +00:00
return true;
}
2017-07-10 12:20:24 +00:00
@Override
public void onDisable() {
super.onDisable();
2017-07-11 23:17:11 +00:00
getServer().getServicesManager().unregisterAll(pm);
Bukkit.getScheduler().cancelTasks(pm);
try {
sm.getDb().close();
} catch (Exception e) {
}
2017-07-10 12:20:24 +00:00
}
2017-07-10 12:55:45 +00:00
private boolean setupPermissions() {
RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
if (permissionProvider != null) {
permission = permissionProvider.getProvider();
}
return (permission != null);
}
private boolean setupDatabase() {
2017-07-11 23:17:11 +00:00
sm = new SqlManager(pm);
2017-07-11 12:50:53 +00:00
return sm.updateDatabase();
2017-07-10 12:55:45 +00:00
}
2017-07-11 23:17:11 +00:00
public UUID getPlayerUUIDByName(String name) {
OfflinePlayer p = getOfflinePlayer(name);
if (p == null) {
return null;
} else {
return p.getUniqueId();
2017-07-11 23:17:11 +00:00
}
}
public OfflinePlayer getOfflinePlayer(String name) {
2017-07-11 23:17:11 +00:00
for (OfflinePlayer p : getServer().getOfflinePlayers()) {
if (p.getName().equalsIgnoreCase(name)) {
return p;
2017-07-11 23:17:11 +00:00
}
}
return null;
}
2017-07-13 22:33:19 +00:00
public void writeFailLog(String content, Object... args) {
FileUtil.writeOnFile(getDataFolder() + "/failure.log", "[" + StrUtil.nowTimeString() + "] " + MessageFormat.format(content, args));
}
2017-07-06 10:17:54 +00:00
}