1
mirror of https://github.com/CarmJos/UltraDepository.git synced 2024-09-19 19:55:45 +00:00

更换成插件入口名

This commit is contained in:
Carm Jos 2022-03-04 16:40:58 +08:00
parent 375f4d178a
commit 717e313f25
7 changed files with 340 additions and 171 deletions

View File

@ -0,0 +1,173 @@
package cc.carm.plugin.ultradepository;
import cc.carm.lib.easyplugin.EasyPlugin;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
import cc.carm.lib.easyplugin.utils.MessageUtils;
import cc.carm.plugin.ultradepository.command.DepositoryCommand;
import cc.carm.plugin.ultradepository.configuration.PluginConfig;
import cc.carm.plugin.ultradepository.hooker.GHUpdateChecker;
import cc.carm.plugin.ultradepository.hooker.PAPIExpansion;
import cc.carm.plugin.ultradepository.listener.CollectListener;
import cc.carm.plugin.ultradepository.listener.UserListener;
import cc.carm.plugin.ultradepository.manager.ConfigManager;
import cc.carm.plugin.ultradepository.manager.DepositoryManager;
import cc.carm.plugin.ultradepository.manager.EconomyManager;
import cc.carm.plugin.ultradepository.manager.UserManager;
import cc.carm.plugin.ultradepository.storage.DataStorage;
import cc.carm.plugin.ultradepository.storage.StorageMethod;
import cc.carm.plugin.ultradepository.util.JarResourceUtils;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bstats.charts.SingleLineChart;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.util.Optional;
public class Main extends EasyPlugin {
private static Main instance;
private DataStorage storage;
private UserManager userManager;
private EconomyManager economyManager;
private DepositoryManager depositoryManager;
public Main() {
super(new EasyPluginMessageProvider.zh_CN());
}
@Override
protected void load() {
instance = this;
}
@Override
protected boolean initialize() {
log("加载配置文件...");
if (!ConfigManager.initialize()) {
log("初始化配置文件失败,放弃加载。");
return false;
}
log("初始化存储方式...");
StorageMethod storageMethod = PluginConfig.STORAGE_METHOD.getOptional().orElse(StorageMethod.YAML);
log(" 正在使用 " + storageMethod.name() + " 进行数据存储");
this.storage = storageMethod.createStorage();
if (!storage.initialize()) {
error("初始化存储失败,请检查配置文件。");
storage.shutdown();
return false;
}
log("加载经济系统...");
if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
this.economyManager = new EconomyManager();
if (!economyManager.initialize()) {
error("经济系统初始化失败,关闭出售功能。");
}
} else {
log(" &7[-] 检测到未安装Vault关闭出售功能。");
}
log("加载仓库管理器...");
this.depositoryManager = new DepositoryManager();
getDepositoryManager().loadDepositories();
log("加载用户系统...");
this.userManager = new UserManager();
log("注册监听器...");
regListener(new UserListener());
regListener(new CollectListener());
GUI.initialize(this);
log("注册指令...");
registerCommand("UltraDepository", new DepositoryCommand());
if (MessageUtils.hasPlaceholderAPI()) {
log("注册变量...");
new PAPIExpansion(this).register();
} else {
log("检测到未安装PlaceholderAPI跳过变量注册。");
}
if (PluginConfig.METRICS.get()) {
log("启用统计数据...");
Metrics metrics = new Metrics(this, 13777);
metrics.addCustomChart(new SingleLineChart(
"active_depositories",
() -> getDepositoryManager().getDepositories().size())
);
metrics.addCustomChart(new SimplePie("storage_method", storageMethod::name));
metrics.addCustomChart(new SimplePie("economy_enabled", () -> economyManager.isInitialized() ? "YES" : "NO"));
metrics.addCustomChart(new SimplePie("papi_version", () -> {
Plugin plugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
if (plugin == null) return "Not installed";
else return plugin.getDescription().getVersion();
}));
}
if (PluginConfig.CHECK_UPDATE.get()) {
log("开始检查更新...");
GHUpdateChecker checker = new GHUpdateChecker(getLogger(), "CarmJos", "UltraDepository");
getScheduler().runAsync(() -> checker.checkUpdate(getDescription().getVersion()));
} else {
log("已禁用检查更新,跳过。");
}
getUserManager().loadPlayersData();
return true;
}
@Override
protected void shutdown() {
if (!isInitialized()) return;
log("保存现有用户数据...");
getUserManager().saveAll();
getUserManager().getDataCache().clear();
log("释放存储源...");
getStorage().shutdown();
log("卸载监听器...");
Bukkit.getServicesManager().unregisterAll(this);
}
protected DataStorage getStorage() {
return storage;
}
public static Main getInstance() {
return instance;
}
protected UserManager getUserManager() {
return userManager;
}
protected EconomyManager getEconomyManager() {
return economyManager;
}
protected DepositoryManager getDepositoryManager() {
return depositoryManager;
}
@Override
public boolean isDebugging() {
return PluginConfig.DEBUG.get();
}
@Override
public void outputInfo() {
Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log);
}
}

View File

@ -1,182 +1,30 @@
package cc.carm.plugin.ultradepository;
import cc.carm.lib.easyplugin.EasyPlugin;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
import cc.carm.lib.easyplugin.utils.MessageUtils;
import cc.carm.plugin.ultradepository.command.DepositoryCommand;
import cc.carm.plugin.ultradepository.configuration.PluginConfig;
import cc.carm.plugin.ultradepository.hooker.PAPIExpansion;
import cc.carm.plugin.ultradepository.hooker.UpdateChecker;
import cc.carm.plugin.ultradepository.listener.CollectListener;
import cc.carm.plugin.ultradepository.listener.UserListener;
import cc.carm.plugin.ultradepository.manager.ConfigManager;
import cc.carm.plugin.ultradepository.manager.DepositoryManager;
import cc.carm.plugin.ultradepository.manager.EconomyManager;
import cc.carm.plugin.ultradepository.manager.UserManager;
import cc.carm.plugin.ultradepository.storage.DataStorage;
import cc.carm.plugin.ultradepository.storage.StorageMethod;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bstats.charts.SingleLineChart;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
public class UltraDepository extends EasyPlugin {
public class UltraDepository {
private static UltraDepository instance;
public static DataStorage getStorage() {
return Main.getInstance().getStorage();
}
private static DataStorage storage;
public static Main getInstance() {
return Main.getInstance();
}
private static UserManager userManager;
private static EconomyManager economyManager;
private static DepositoryManager depositoryManager;
public static UserManager getUserManager() {
return Main.getInstance().getUserManager();
}
public UltraDepository() {
super(new EasyPluginMessageProvider.zh_CN());
}
public static EconomyManager getEconomyManager() {
return Main.getInstance().getEconomyManager();
}
@Override
protected void load() {
instance = this;
}
@Override
protected boolean initialize() {
log("加载配置文件...");
if (!ConfigManager.initialize()) {
log("初始化配置文件失败,放弃加载。");
return false;
}
log("初始化存储方式...");
StorageMethod storageMethod = PluginConfig.STORAGE_METHOD.getOptional().orElse(StorageMethod.YAML);
log(" 正在使用 " + storageMethod.name() + " 进行数据存储");
storage = storageMethod.createStorage();
if (!storage.initialize()) {
error("初始化存储失败,请检查配置文件。");
storage.shutdown();
return false;
}
log("加载经济系统...");
if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
economyManager = new EconomyManager();
if (!economyManager.initialize()) {
error("经济系统初始化失败,关闭出售功能。");
}
} else {
log(" &7[-] 检测到未安装Vault关闭出售功能。");
}
log("加载仓库管理器...");
depositoryManager = new DepositoryManager();
getDepositoryManager().loadDepositories();
log("加载用户系统...");
userManager = new UserManager();
log("注册监听器...");
regListener(new UserListener());
regListener(new CollectListener());
GUI.initialize(this);
log("注册指令...");
registerCommand("UltraDepository", new DepositoryCommand());
if (MessageUtils.hasPlaceholderAPI()) {
log("注册变量...");
new PAPIExpansion(this).register();
} else {
log("检测到未安装PlaceholderAPI跳过变量注册。");
}
if (PluginConfig.METRICS.get()) {
log("启用统计数据...");
Metrics metrics = new Metrics(this, 13777);
metrics.addCustomChart(new SingleLineChart(
"active_depositories",
() -> getDepositoryManager().getDepositories().size())
);
metrics.addCustomChart(new SimplePie("storage_method", storageMethod::name));
metrics.addCustomChart(new SimplePie("economy_enabled", () -> economyManager.isInitialized() ? "YES" : "NO"));
metrics.addCustomChart(new SimplePie("papi_version", () -> {
Plugin plugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
if (plugin == null) return "Not installed";
else return plugin.getDescription().getVersion();
}));
}
if (PluginConfig.CHECK_UPDATE.get()) {
log("开始检查更新...");
UpdateChecker.checkUpdate(this);
} else {
log("已禁用检查更新,跳过。");
}
getUserManager().loadPlayersData();
return true;
}
@Override
protected void shutdown() {
if (!isInitialized()) return;
log("保存现有用户数据...");
getUserManager().saveAll();
getUserManager().getDataCache().clear();
log("释放存储源...");
getStorage().shutdown();
log("卸载监听器...");
Bukkit.getServicesManager().unregisterAll(this);
}
public static DataStorage getStorage() {
return storage;
}
public static UltraDepository getInstance() {
return instance;
}
public static UserManager getUserManager() {
return userManager;
}
public static EconomyManager getEconomyManager() {
return economyManager;
}
public static DepositoryManager getDepositoryManager() {
return depositoryManager;
}
@Override
public boolean isDebugging() {
return PluginConfig.DEBUG.get();
}
@Override
public void outputInfo() {
log(" ",
"&6 _ _ _ _ &e _____ _ _ ",
"&6| | | | | | &e| __ \\ (_) | ",
"&6| | | | | |_ _ __ __ _ &e| | | | ___ _ __ ___ ___ _| |_ ___ _ __ _ _ ",
"&6| | | | | __| '__/ _` |&e| | | |/ _ \\ '_ \\ / _ \\/ __| | __/ _ \\| '__| | | |",
"&6| |__| | | |_| | | (_| |&e| |__| | __/ |_) | (_) \\__ \\ | || (_) | | | |_| |",
"&6 \\____/|_|\\__|_| \\__,_|&e|_____/ \\___| .__/ \\___/|___/_|\\__\\___/|_| \\__, |",
"&6 &e| | __/ |",
"&6 &e|_| |___/ ",
"&f请访问项目主页查看详细插件介绍 &8/ &fView GitHub to get more information",
"&8-> &6https://github.com/CarmJos/UltraDepository",
" "
);
}
public static DepositoryManager getDepositoryManager() {
return Main.getInstance().getDepositoryManager();
}
}

View File

@ -0,0 +1,38 @@
package cc.carm.plugin.ultradepository.hooker;
import cc.carm.lib.githubreleases4j.GithubReleases4J;
import java.util.logging.Logger;
public class GHUpdateChecker {
private final Logger logger;
private final String owner;
private final String repo;
public GHUpdateChecker(Logger logger, String owner, String repo) {
this.logger = logger;
this.owner = owner;
this.repo = repo;
}
public void checkUpdate(String currentVersion) {
Integer behindVersions = GithubReleases4J.getVersionBehind(owner, repo, currentVersion);
String downloadURL = GithubReleases4J.getReleasesURL(owner, repo);
if (behindVersions == null) {
logger.severe("检查更新失败,请您定期查看插件是否更新,避免安全问题。");
logger.severe("下载地址 " + downloadURL);
} else if (behindVersions == 0) {
logger.info("检查完成,当前已是最新版本。");
} else if (behindVersions > 0) {
logger.info("发现新版本! 目前已落后 " + behindVersions + " 个版本。");
logger.info("最新版下载地址 " + downloadURL);
} else {
logger.severe("检查更新失败! 当前版本未知,请您使用原生版本以避免安全问题。");
logger.severe("最新版下载地址 " + downloadURL);
}
}
}

View File

@ -1,11 +1,12 @@
package cc.carm.plugin.ultradepository.hooker;
import cc.carm.lib.githubreleases4j.GithubReleases4J;
import cc.carm.plugin.ultradepository.Main;
import cc.carm.plugin.ultradepository.UltraDepository;
public class UpdateChecker {
public static void checkUpdate(UltraDepository plugin) {
public static void checkUpdate(Main plugin) {
plugin.getScheduler().runAsync(() -> {
Integer behindVersions = GithubReleases4J.getVersionBehind(

View File

@ -0,0 +1,106 @@
package cc.carm.plugin.ultradepository.util;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@SuppressWarnings("ResultOfMethodCallIgnored")
public class JarResourceUtils {
public static final char JAR_SEPARATOR = '/';
public static @Nullable String[] readResource(@Nullable InputStream resourceStream) {
if (resourceStream == null) return null;
try (Scanner scanner = new Scanner(resourceStream, StandardCharsets.UTF_8.name())) {
List<String> contents = new ArrayList<>();
while (scanner.hasNextLine()) {
contents.add(scanner.nextLine());
}
return contents.toArray(new String[0]);
} catch (Exception e) {
return null;
}
}
public static void copyFolderFromJar(String folderName, File destFolder, CopyOption option)
throws IOException {
copyFolderFromJar(folderName, destFolder, option, null);
}
public static void copyFolderFromJar(String folderName, File destFolder,
CopyOption option, PathTrimmer trimmer) throws IOException {
if (!destFolder.exists())
destFolder.mkdirs();
byte[] buffer = new byte[1024];
File fullPath;
String path = JarResourceUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
if (trimmer != null)
path = trimmer.trim(path);
try {
if (!path.startsWith("file"))
path = "file://" + path;
fullPath = new File(new URI(path));
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(fullPath));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.getName().startsWith(folderName + JAR_SEPARATOR))
continue;
String fileName = entry.getName();
if (fileName.charAt(fileName.length() - 1) == JAR_SEPARATOR) {
File file = new File(destFolder + File.separator + fileName);
if (file.isFile()) {
file.delete();
}
file.mkdirs();
continue;
}
File file = new File(destFolder + File.separator + fileName);
if (option == CopyOption.COPY_IF_NOT_EXIST && file.exists())
continue;
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
if (!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zis.closeEntry();
zis.close();
}
public enum CopyOption {
COPY_IF_NOT_EXIST, REPLACE_IF_EXIST
}
@FunctionalInterface
public interface PathTrimmer {
String trim(String original);
}
}

View File

@ -0,0 +1,3 @@
&f请访问项目主页查看详细插件介绍 &8/ &fView GitHub to get more information
&8-> &6${project.url}

View File

@ -1,4 +1,4 @@
main: cc.carm.plugin.ultradepository.UltraDepository
main: cc.carm.plugin.ultradepository.Main
name: UltraDepository
version: ${project.version}