mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-04 16:48:16 +08:00
build(all): 项目结构优化
优化整体项目结构,优化Javadoc生成方式与部署方式。
This commit is contained in:
@@ -0,0 +1,151 @@
|
|||||||
|
package cc.carm.lib.easyplugin;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
|
||||||
|
import cc.carm.lib.easyplugin.utils.JarResourceUtils;
|
||||||
|
import cc.carm.lib.easyplugin.utils.SchedulerUtils;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
|
import org.bukkit.command.TabCompleter;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public abstract class EasyPlugin extends JavaPlugin {
|
||||||
|
|
||||||
|
protected EasyPluginMessageProvider messageProvider;
|
||||||
|
|
||||||
|
public EasyPlugin() {
|
||||||
|
this(EasyPluginMessageProvider.ZH_CN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EasyPlugin(EasyPluginMessageProvider messageProvider) {
|
||||||
|
this.messageProvider = messageProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SchedulerUtils scheduler;
|
||||||
|
private boolean initialized = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onLoad() {
|
||||||
|
scheduler = new SchedulerUtils(this);
|
||||||
|
if (!hasOverride("load")) return;
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
log(messageProvider.loading(this));
|
||||||
|
load();
|
||||||
|
log(messageProvider.loaded(this, startTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onEnable() {
|
||||||
|
outputInfo();
|
||||||
|
|
||||||
|
log(messageProvider.enabling(this));
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
if (!(this.initialized = initialize())) {
|
||||||
|
setEnabled(false);
|
||||||
|
log(messageProvider.enableFailure(this, startTime));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log(messageProvider.enableSuccess(this, startTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onDisable() {
|
||||||
|
if (!hasOverride("shutdown") || !isInitialized()) return;
|
||||||
|
outputInfo();
|
||||||
|
|
||||||
|
log(messageProvider.disabling(this));
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
shutdown();
|
||||||
|
log(messageProvider.disabled(this, startTime));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void load() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract boolean initialize();
|
||||||
|
|
||||||
|
protected void shutdown() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重写以展示插件的相关信息,如插件横幅、下载地址等。
|
||||||
|
*/
|
||||||
|
public void outputInfo() {
|
||||||
|
Optional.ofNullable(JarResourceUtils.readResource(this.getResource("PLUGIN_INFO"))).ifPresent(this::log);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isInitialized() {
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDebugging() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SchedulerUtils getScheduler() {
|
||||||
|
return scheduler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerListener(@NotNull Listener... listeners) {
|
||||||
|
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerCommand(String commandName,
|
||||||
|
@NotNull CommandExecutor executor) {
|
||||||
|
registerCommand(commandName, executor, executor instanceof TabCompleter ? (TabCompleter) executor : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerCommand(String commandName,
|
||||||
|
@NotNull CommandExecutor executor,
|
||||||
|
@Nullable TabCompleter tabCompleter) {
|
||||||
|
PluginCommand command = Bukkit.getPluginCommand(commandName);
|
||||||
|
if (command == null) return;
|
||||||
|
command.setExecutor(executor);
|
||||||
|
if (tabCompleter != null) command.setTabCompleter(tabCompleter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print(@Nullable String prefix, @Nullable String... messages) {
|
||||||
|
messageProvider.print(this, prefix, messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void log(@Nullable String... messages) {
|
||||||
|
print(null, messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void error(String... messages) {
|
||||||
|
print("&c[ERROR] &r", messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void debug(@Nullable String... messages) {
|
||||||
|
if (isDebugging()) print("&8[DEBUG] &r", messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||||
|
private boolean hasOverride(String methodName) {
|
||||||
|
Map<Method, Method> methodMap = new HashMap<>();
|
||||||
|
Arrays.stream(EasyPlugin.class.getDeclaredMethods())
|
||||||
|
.filter(method -> method.getName().equals(methodName))
|
||||||
|
.forEach(method -> Arrays.stream(getClass().getDeclaredMethods())
|
||||||
|
.filter(extend -> extend.getName().equals(methodName))
|
||||||
|
.filter(extend -> extend.getReturnType().equals(method.getReturnType()))
|
||||||
|
.filter(extend -> extend.getParameterTypes().length == method.getParameterTypes().length)
|
||||||
|
.findFirst().ifPresent(extendMethod -> methodMap.put(method, extendMethod))
|
||||||
|
);
|
||||||
|
return !methodMap.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package cc.carm.lib.easyplugin.i18n;
|
||||||
|
|
||||||
|
import cc.carm.lib.easyplugin.utils.ColorParser;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public interface EasyPluginMessageProvider {
|
||||||
|
|
||||||
|
EasyPluginMessageProvider ZH_CN = new zh_CN();
|
||||||
|
EasyPluginMessageProvider EN_US = new en_US();
|
||||||
|
|
||||||
|
String loading(Plugin plugin);
|
||||||
|
|
||||||
|
String loaded(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
String enabling(Plugin plugin);
|
||||||
|
|
||||||
|
String enableSuccess(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
String enableFailure(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
String disabling(Plugin plugin);
|
||||||
|
|
||||||
|
String disabled(Plugin plugin, long startMillis);
|
||||||
|
|
||||||
|
default void print(@NotNull Plugin plugin, @Nullable String prefix, @Nullable String... messages) {
|
||||||
|
Arrays.stream(messages)
|
||||||
|
.map(message -> "[" + plugin.getName() + "] " + (prefix == null ? "" : prefix) + message)
|
||||||
|
.map(ColorParser::parse)
|
||||||
|
.forEach(message -> Bukkit.getConsoleSender().sendMessage(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
class zh_CN implements EasyPluginMessageProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loading(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始加载...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loaded(Plugin plugin, long startMillis) {
|
||||||
|
return "&f加载完成 ,共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始启动...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||||
|
return "&a启用完成! &f共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableFailure(Plugin plugin, long startMillis) {
|
||||||
|
return "&c启用失败! &f已耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始卸载...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabled(Plugin plugin, long startMillis) {
|
||||||
|
return "&f卸载完成! 共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class en_US implements EasyPluginMessageProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loading(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " loading...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String loaded(Plugin plugin, long startMillis) {
|
||||||
|
return "&fLoaded after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " enabling...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableSuccess(Plugin plugin, long startMillis) {
|
||||||
|
return "&aEnabled successfully!&f Cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String enableFailure(Plugin plugin, long startMillis) {
|
||||||
|
return "&cEnabled failed after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabling(Plugin plugin) {
|
||||||
|
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " begin to shutdown...";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String disabled(Plugin plugin, long startMillis) {
|
||||||
|
return "&fShutdown successfully, cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
package cc.carm.lib.easyplugin.utils;
|
||||||
|
|
||||||
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class MessageUtils {
|
||||||
|
|
||||||
|
public static boolean hasPlaceholderAPI() {
|
||||||
|
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void send(@Nullable CommandSender sender, String... messages) {
|
||||||
|
send(sender, Arrays.asList(messages));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void send(@Nullable CommandSender sender, List<String> messages) {
|
||||||
|
if (messages == null || messages.isEmpty() || sender == null) return;
|
||||||
|
for (String s : messages) {
|
||||||
|
sender.sendMessage(ColorParser.parse(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
|
||||||
|
sendWithPlaceholders(sender, Arrays.asList(messages));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
||||||
|
if (messages == null || messages.isEmpty() || sender == null) return;
|
||||||
|
send(sender, setPlaceholders(sender, messages));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) {
|
||||||
|
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
||||||
|
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String setPlaceholders(@Nullable CommandSender sender, @Nullable String message) {
|
||||||
|
if (message == null || sender == null) return message;
|
||||||
|
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||||
|
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
||||||
|
} else {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Contract("_, !null -> !null")
|
||||||
|
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
||||||
|
@Nullable List<String> messages) {
|
||||||
|
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
||||||
|
if (hasPlaceholderAPI() && sender instanceof Player) {
|
||||||
|
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
|
||||||
|
} else {
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String setPlaceholders(@Nullable CommandSender sender,
|
||||||
|
@NotNull String message,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
|
return setPlaceholders(sender, setCustomParams(message, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
||||||
|
@NotNull List<String> messages,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
|
return setPlaceholders(sender, setCustomParams(messages, params, values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String setCustomParams(@NotNull String message,
|
||||||
|
@NotNull String param,
|
||||||
|
@NotNull Object value) {
|
||||||
|
return setCustomParams(message, new String[]{param}, new Object[]{value});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Contract("!null, _, _-> !null ; null, _, _->null ")
|
||||||
|
public static String setCustomParams(@Nullable String message,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
|
if (message == null) return null;
|
||||||
|
if (params == null || values == null) return message;
|
||||||
|
if (params.length != values.length) return message;
|
||||||
|
|
||||||
|
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
paramsMap.put(params[i], values[i]);
|
||||||
|
}
|
||||||
|
return setCustomParams(message, paramsMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static String setCustomParams(@NotNull String message, @NotNull HashMap<String, Object> params) {
|
||||||
|
String afterMessage = message;
|
||||||
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||||
|
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
||||||
|
}
|
||||||
|
return afterMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static List<String> setCustomParams(@NotNull List<String> messages,
|
||||||
|
@NotNull String param,
|
||||||
|
@NotNull Object value) {
|
||||||
|
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static List<String> setCustomParams(@NotNull List<String> messages,
|
||||||
|
@Nullable String[] params,
|
||||||
|
@Nullable Object[] values) {
|
||||||
|
if (params == null || values == null) return messages;
|
||||||
|
if (params.length != values.length) return messages;
|
||||||
|
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
paramsMap.put(params[i], values[i]);
|
||||||
|
}
|
||||||
|
return setCustomParams(messages, paramsMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
||||||
|
return messages.stream()
|
||||||
|
.map(message -> setCustomParams(message, params))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.i18n.EasyPluginMessageProvider;
|
|
||||||
import cc.carm.lib.easyplugin.utils.SchedulerUtils;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandExecutor;
|
|
||||||
import org.bukkit.command.PluginCommand;
|
|
||||||
import org.bukkit.command.TabCompleter;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public abstract class EasyPlugin extends JavaPlugin {
|
|
||||||
|
|
||||||
protected EasyPluginMessageProvider messageProvider;
|
|
||||||
|
|
||||||
public EasyPlugin() {
|
|
||||||
this(new EasyPluginMessageProvider.en_US());
|
|
||||||
}
|
|
||||||
|
|
||||||
public EasyPlugin(EasyPluginMessageProvider messageProvider) {
|
|
||||||
this.messageProvider = messageProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
private SchedulerUtils scheduler;
|
|
||||||
private boolean initialized = false;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void onLoad() {
|
|
||||||
scheduler = new SchedulerUtils(this);
|
|
||||||
if (!hasOverride("load")) return;
|
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
log(messageProvider.loading(this));
|
|
||||||
load();
|
|
||||||
log(messageProvider.loaded(this, startTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void onEnable() {
|
|
||||||
outputInfo();
|
|
||||||
|
|
||||||
log(messageProvider.enabling(this));
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
if (!(this.initialized = initialize())) {
|
|
||||||
setEnabled(false);
|
|
||||||
log(messageProvider.enableFailure(this, startTime));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
log(messageProvider.enableSuccess(this, startTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final void onDisable() {
|
|
||||||
if (!hasOverride("shutdown") || !isInitialized()) return;
|
|
||||||
outputInfo();
|
|
||||||
|
|
||||||
log(messageProvider.disabling(this));
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
shutdown();
|
|
||||||
log(messageProvider.disabled(this, startTime));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void load() {
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract boolean initialize();
|
|
||||||
|
|
||||||
protected void shutdown() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重写以展示插件的相关信息,如插件横幅、下载地址等。
|
|
||||||
*/
|
|
||||||
public void outputInfo() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isInitialized() {
|
|
||||||
return initialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDebugging() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SchedulerUtils getScheduler() {
|
|
||||||
return scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void regListener(@NotNull Listener... listeners) {
|
|
||||||
Arrays.stream(listeners).forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerCommand(String commandName,
|
|
||||||
@NotNull CommandExecutor executor) {
|
|
||||||
registerCommand(commandName, executor, executor instanceof TabCompleter ? (TabCompleter) executor : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerCommand(String commandName,
|
|
||||||
@NotNull CommandExecutor executor,
|
|
||||||
@Nullable TabCompleter tabCompleter) {
|
|
||||||
PluginCommand command = Bukkit.getPluginCommand(commandName);
|
|
||||||
if (command == null) return;
|
|
||||||
command.setExecutor(executor);
|
|
||||||
if (tabCompleter != null) command.setTabCompleter(tabCompleter);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void print(@Nullable String prefix, @Nullable String... messages) {
|
|
||||||
messageProvider.print(this, prefix, messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void log(@Nullable String... messages) {
|
|
||||||
print(null, messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void error(String... messages) {
|
|
||||||
print("&c[ERROR] &r", messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void debug(@Nullable String... messages) {
|
|
||||||
if (isDebugging()) print("&8[DEBUG] &r", messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
|
||||||
private boolean hasOverride(String methodName) {
|
|
||||||
Map<Method, Method> methodMap = new HashMap<>();
|
|
||||||
Arrays.stream(EasyPlugin.class.getDeclaredMethods())
|
|
||||||
.filter(method -> method.getName().equals(methodName))
|
|
||||||
.forEach(method -> Arrays.stream(getClass().getDeclaredMethods())
|
|
||||||
.filter(extend -> extend.getName().equals(methodName))
|
|
||||||
.filter(extend -> extend.getReturnType().equals(method.getReturnType()))
|
|
||||||
.filter(extend -> extend.getParameterTypes().length == method.getParameterTypes().length)
|
|
||||||
.findFirst().ifPresent(extendMethod -> methodMap.put(method, extendMethod))
|
|
||||||
);
|
|
||||||
return !methodMap.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-110
@@ -1,110 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.i18n;
|
|
||||||
|
|
||||||
import cc.carm.lib.easyplugin.utils.ColorParser;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public interface EasyPluginMessageProvider {
|
|
||||||
|
|
||||||
String loading(Plugin plugin);
|
|
||||||
|
|
||||||
String loaded(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
String enabling(Plugin plugin);
|
|
||||||
|
|
||||||
String enableSuccess(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
String enableFailure(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
String disabling(Plugin plugin);
|
|
||||||
|
|
||||||
String disabled(Plugin plugin, long startMillis);
|
|
||||||
|
|
||||||
default void print(@NotNull Plugin plugin, @Nullable String prefix, @Nullable String... messages) {
|
|
||||||
Arrays.stream(messages)
|
|
||||||
.map(message -> "[" + plugin.getName() + "] " + (prefix == null ? "" : prefix) + message)
|
|
||||||
.map(ColorParser::parse)
|
|
||||||
.forEach(message -> Bukkit.getConsoleSender().sendMessage(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
class zh_CN implements EasyPluginMessageProvider {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loading(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始加载...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loaded(Plugin plugin, long startMillis) {
|
|
||||||
return "&f加载完成 ,共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始启动...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
|
||||||
return "&a启用完成! &f共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableFailure(Plugin plugin, long startMillis) {
|
|
||||||
return "&c启用失败! &f已耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " 开始卸载...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabled(Plugin plugin, long startMillis) {
|
|
||||||
return "&f卸载完成! 共耗时 " + (System.currentTimeMillis() - startMillis) + " ms 。";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class en_US implements EasyPluginMessageProvider {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loading(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " loading...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String loaded(Plugin plugin, long startMillis) {
|
|
||||||
return "&fLoaded after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " enabling...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableSuccess(Plugin plugin, long startMillis) {
|
|
||||||
return "&aEnabled successfully!&f Cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String enableFailure(Plugin plugin, long startMillis) {
|
|
||||||
return "&cEnabled failed after " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabling(Plugin plugin) {
|
|
||||||
return "&f" + plugin.getName() + " " + plugin.getDescription().getVersion() + " begin to shutdown...";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String disabled(Plugin plugin, long startMillis) {
|
|
||||||
return "&fShutdown successfully, cost " + (System.currentTimeMillis() - startMillis) + " ms.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
package cc.carm.lib.easyplugin.utils;
|
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderAPI;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class MessageUtils {
|
|
||||||
|
|
||||||
public static boolean hasPlaceholderAPI() {
|
|
||||||
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void send(@Nullable CommandSender sender, String... messages) {
|
|
||||||
send(sender, Arrays.asList(messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void send(@Nullable CommandSender sender, List<String> messages) {
|
|
||||||
if (messages == null || messages.isEmpty() || sender == null) return;
|
|
||||||
for (String s : messages) {
|
|
||||||
sender.sendMessage(ColorParser.parse(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(CommandSender sender, String... messages) {
|
|
||||||
sendWithPlaceholders(sender, Arrays.asList(messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages) {
|
|
||||||
if (messages == null || messages.isEmpty() || sender == null) return;
|
|
||||||
send(sender, setPlaceholders(sender, messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String param, Object value) {
|
|
||||||
sendWithPlaceholders(sender, messages, new String[]{param}, new Object[]{value});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendWithPlaceholders(@Nullable CommandSender sender, List<String> messages, String[] params, Object[] values) {
|
|
||||||
sendWithPlaceholders(sender, setCustomParams(messages, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String setPlaceholders(@Nullable CommandSender sender, @Nullable String message) {
|
|
||||||
if (message == null || sender == null) return message;
|
|
||||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
|
||||||
return PlaceholderAPI.setPlaceholders((Player) sender, message);
|
|
||||||
} else {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Contract("_, !null -> !null")
|
|
||||||
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
|
||||||
@Nullable List<String> messages) {
|
|
||||||
if (messages == null || messages.isEmpty() || sender == null) return messages;
|
|
||||||
if (hasPlaceholderAPI() && sender instanceof Player) {
|
|
||||||
return PlaceholderAPI.setPlaceholders((Player) sender, messages);
|
|
||||||
} else {
|
|
||||||
return messages;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String setPlaceholders(@Nullable CommandSender sender,
|
|
||||||
@NotNull String message,
|
|
||||||
@Nullable String[] params,
|
|
||||||
@Nullable Object[] values) {
|
|
||||||
return setPlaceholders(sender, setCustomParams(message, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> setPlaceholders(@Nullable CommandSender sender,
|
|
||||||
@NotNull List<String> messages,
|
|
||||||
@Nullable String[] params,
|
|
||||||
@Nullable Object[] values) {
|
|
||||||
return setPlaceholders(sender, setCustomParams(messages, params, values));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String setCustomParams(@NotNull String message,
|
|
||||||
@NotNull String param,
|
|
||||||
@NotNull Object value) {
|
|
||||||
return setCustomParams(message, new String[]{param}, new Object[]{value});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Contract("!null, _, _-> !null ; null, _, _->null ")
|
|
||||||
public static String setCustomParams(@Nullable String message,
|
|
||||||
@Nullable String[] params,
|
|
||||||
@Nullable Object[] values) {
|
|
||||||
if (message == null) return null;
|
|
||||||
if (params == null || values == null) return message;
|
|
||||||
if (params.length != values.length) return message;
|
|
||||||
|
|
||||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
|
||||||
for (int i = 0; i < params.length; i++) {
|
|
||||||
paramsMap.put(params[i], values[i]);
|
|
||||||
}
|
|
||||||
return setCustomParams(message, paramsMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static String setCustomParams(@NotNull String message, @NotNull HashMap<String, Object> params) {
|
|
||||||
String afterMessage = message;
|
|
||||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
||||||
afterMessage = afterMessage.replace(entry.getKey(), entry.getValue().toString());
|
|
||||||
}
|
|
||||||
return afterMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static List<String> setCustomParams(@NotNull List<String> messages,
|
|
||||||
@NotNull String param,
|
|
||||||
@NotNull Object value) {
|
|
||||||
return setCustomParams(messages, new String[]{param}, new Object[]{value});
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static List<String> setCustomParams(@NotNull List<String> messages,
|
|
||||||
@Nullable String[] params,
|
|
||||||
@Nullable Object[] values) {
|
|
||||||
if (params == null || values == null) return messages;
|
|
||||||
if (params.length != values.length) return messages;
|
|
||||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
|
||||||
for (int i = 0; i < params.length; i++) {
|
|
||||||
paramsMap.put(params[i], values[i]);
|
|
||||||
}
|
|
||||||
return setCustomParams(messages, paramsMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static List<String> setCustomParams(List<String> messages, HashMap<String, Object> params) {
|
|
||||||
return messages.stream()
|
|
||||||
.map(message -> setCustomParams(message, params))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user