diff --git a/src/main/java/cc/carm/plugin/moeteleport/Main.java b/src/main/java/cc/carm/plugin/moeteleport/Main.java index 1a47146..837afdd 100644 --- a/src/main/java/cc/carm/plugin/moeteleport/Main.java +++ b/src/main/java/cc/carm/plugin/moeteleport/Main.java @@ -19,6 +19,7 @@ import cc.carm.plugin.moeteleport.manager.UserManager; import cc.carm.plugin.moeteleport.storage.DataStorage; import cc.carm.plugin.moeteleport.storage.StorageMethod; import cc.carm.plugin.moeteleport.util.ColorParser; +import cc.carm.plugin.moeteleport.util.JarResourceUtils; import cc.carm.plugin.moeteleport.util.SchedulerUtils; import org.bstats.bukkit.Metrics; import org.bstats.charts.SimplePie; @@ -31,6 +32,8 @@ import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; + public class Main extends JavaPlugin { private static Main instance; private static SchedulerUtils scheduler; @@ -44,6 +47,8 @@ public class Main extends JavaPlugin { public void onEnable() { instance = this; scheduler = new SchedulerUtils(this); + outputInfo(); + log(getName() + " " + getDescription().getVersion() + " &7开始加载..."); long startTime = System.currentTimeMillis(); @@ -102,6 +107,7 @@ public class Main extends JavaPlugin { @Override public void onDisable() { + outputInfo(); log(getName() + " " + getDescription().getVersion() + " 开始卸载..."); long startTime = System.currentTimeMillis(); @@ -126,6 +132,13 @@ public class Main extends JavaPlugin { Bukkit.getPluginManager().registerEvents(listener, getInstance()); } + public void outputInfo() { + String[] pluginInfo = JarResourceUtils.readResource(this.getResource("PLUGIN_INFO")); + if (pluginInfo != null) { + Arrays.stream(pluginInfo).forEach(Main::log); + } + } + public static void log(String message) { Bukkit.getConsoleSender().sendMessage(ColorParser.parse("[" + getInstance().getName() + "] " + message)); } diff --git a/src/main/java/cc/carm/plugin/moeteleport/util/JarResourceUtils.java b/src/main/java/cc/carm/plugin/moeteleport/util/JarResourceUtils.java new file mode 100644 index 0000000..bfc118e --- /dev/null +++ b/src/main/java/cc/carm/plugin/moeteleport/util/JarResourceUtils.java @@ -0,0 +1,105 @@ +package cc.carm.plugin.moeteleport.util; + +import org.jetbrains.annotations.Nullable; + +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; +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, "UTF-8")) { + List 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); + } +} \ No newline at end of file diff --git a/src/main/resources/PLUGIN_INFO b/src/main/resources/PLUGIN_INFO new file mode 100644 index 0000000..aeb5a52 --- /dev/null +++ b/src/main/resources/PLUGIN_INFO @@ -0,0 +1,6 @@ +&d _____ &5 _____ _ _ +&d| |___ ___&5|_ _|___| |___ ___ ___ ___| |_ +&d| | | | . | -_|&5 | | | -_| | -_| . | . | _| _| +&d|_|_|_|___|___|&5 |_| |___|_|___| _|___|_| |_| + |_| +&f 查看更多信息请访问项目主页&f https://github.com/CarmJos/MoeTeleport \ No newline at end of file