1
mirror of https://github.com/CarmJos/MoeTeleport.git synced 2024-09-19 21:35:56 +00:00

启动与卸载时输出插件信息

This commit is contained in:
Carm Jos 2022-02-25 05:22:20 +08:00
parent 67a8b60670
commit c350bb2037
3 changed files with 124 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import cc.carm.plugin.moeteleport.manager.UserManager;
import cc.carm.plugin.moeteleport.storage.DataStorage; import cc.carm.plugin.moeteleport.storage.DataStorage;
import cc.carm.plugin.moeteleport.storage.StorageMethod; import cc.carm.plugin.moeteleport.storage.StorageMethod;
import cc.carm.plugin.moeteleport.util.ColorParser; import cc.carm.plugin.moeteleport.util.ColorParser;
import cc.carm.plugin.moeteleport.util.JarResourceUtils;
import cc.carm.plugin.moeteleport.util.SchedulerUtils; import cc.carm.plugin.moeteleport.util.SchedulerUtils;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie; import org.bstats.charts.SimplePie;
@ -31,6 +32,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
public class Main extends JavaPlugin { public class Main extends JavaPlugin {
private static Main instance; private static Main instance;
private static SchedulerUtils scheduler; private static SchedulerUtils scheduler;
@ -44,6 +47,8 @@ public class Main extends JavaPlugin {
public void onEnable() { public void onEnable() {
instance = this; instance = this;
scheduler = new SchedulerUtils(this); scheduler = new SchedulerUtils(this);
outputInfo();
log(getName() + " " + getDescription().getVersion() + " &7开始加载..."); log(getName() + " " + getDescription().getVersion() + " &7开始加载...");
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
@ -102,6 +107,7 @@ public class Main extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
outputInfo();
log(getName() + " " + getDescription().getVersion() + " 开始卸载..."); log(getName() + " " + getDescription().getVersion() + " 开始卸载...");
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
@ -126,6 +132,13 @@ public class Main extends JavaPlugin {
Bukkit.getPluginManager().registerEvents(listener, getInstance()); 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) { public static void log(String message) {
Bukkit.getConsoleSender().sendMessage(ColorParser.parse("[" + getInstance().getName() + "] " + message)); Bukkit.getConsoleSender().sendMessage(ColorParser.parse("[" + getInstance().getName() + "] " + message));
} }

View File

@ -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<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,6 @@
&d _____ &5 _____ _ _
&d| |___ ___&5|_ _|___| |___ ___ ___ ___| |_
&d| | | | . | -_|&5 | | | -_| | -_| . | . | _| _|
&d|_|_|_|___|___|&5 |_| |___|_|___| _|___|_| |_|
|_|
&f 查看更多信息请访问项目主页&f https://github.com/CarmJos/MoeTeleport