From 4f16196170d3a6f6cb470f77b0b4cb6df2d8ffa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Wed, 25 Apr 2018 22:58:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=20PlugMan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/ilummc/tlib/TLib.java | 2 +- .../com/ilummc/tlib/annotations/Logger.java | 2 +- .../tlib/inject/TDependencyInjector.java | 2 +- .../ilummc/tlib/inject/TPluginManager.java | 52 +++++++++++++- .../java/com/ilummc/tlib/logger/TLogger.java | 67 +++++++++++++++++++ .../tlib/resources/type/TLocaleTitle.java | 22 ++++-- 6 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/ilummc/tlib/logger/TLogger.java diff --git a/src/main/java/com/ilummc/tlib/TLib.java b/src/main/java/com/ilummc/tlib/TLib.java index 4b5514b..ac89b55 100644 --- a/src/main/java/com/ilummc/tlib/TLib.java +++ b/src/main/java/com/ilummc/tlib/TLib.java @@ -12,8 +12,8 @@ import com.ilummc.tlib.filter.TLoggerFilter; import com.ilummc.tlib.inject.TConfigWatcher; import com.ilummc.tlib.inject.TDependencyInjector; import com.ilummc.tlib.inject.TPluginManager; +import com.ilummc.tlib.logger.TLogger; import com.ilummc.tlib.resources.TLocaleLoader; -import com.ilummc.tlib.util.TLogger; import lombok.Getter; import me.skymc.taboolib.Main; diff --git a/src/main/java/com/ilummc/tlib/annotations/Logger.java b/src/main/java/com/ilummc/tlib/annotations/Logger.java index af54460..2fc75a9 100644 --- a/src/main/java/com/ilummc/tlib/annotations/Logger.java +++ b/src/main/java/com/ilummc/tlib/annotations/Logger.java @@ -5,7 +5,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import com.ilummc.tlib.util.TLogger; +import com.ilummc.tlib.logger.TLogger; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java b/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java index da78271..d60e313 100644 --- a/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java +++ b/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java @@ -14,9 +14,9 @@ import com.ilummc.tlib.annotations.Dependency; import com.ilummc.tlib.annotations.Logger; import com.ilummc.tlib.annotations.PluginInstance; import com.ilummc.tlib.dependency.TDependency; +import com.ilummc.tlib.logger.TLogger; import com.ilummc.tlib.resources.TLocaleLoader; import com.ilummc.tlib.util.Ref; -import com.ilummc.tlib.util.TLogger; public class TDependencyInjector { diff --git a/src/main/java/com/ilummc/tlib/inject/TPluginManager.java b/src/main/java/com/ilummc/tlib/inject/TPluginManager.java index 54b2318..59b80df 100644 --- a/src/main/java/com/ilummc/tlib/inject/TPluginManager.java +++ b/src/main/java/com/ilummc/tlib/inject/TPluginManager.java @@ -1,9 +1,18 @@ package com.ilummc.tlib.inject; import java.io.File; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.command.SimpleCommandMap; import org.bukkit.event.Event; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -17,16 +26,57 @@ import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.UnknownDependencyException; +import com.ilummc.tlib.TLib; + import me.skymc.taboolib.Main; +@SuppressWarnings("unused") public class TPluginManager implements PluginManager { private final PluginManager instance; - private final Main main = (Main) Main.getInst(); + private static File updateDirectory = null; + private Server server; + private Map fileAssociations = new HashMap<>(); + private List plugins = new ArrayList<>(); + private Map lookupNames = new HashMap<>(); + private SimpleCommandMap commandMap; + private Map permissions = new HashMap<>(); + private Map> defaultPerms = new LinkedHashMap<>(); + private Map> permSubs = new HashMap<>(); + private Map> defSubs = new HashMap<>(); + private boolean useTimings = false; public TPluginManager() { instance = Bukkit.getPluginManager(); + // clone all Field in SimplePluginManager + cloneField("updateDirectory"); + cloneField("server"); + cloneField("fileAssociations"); + cloneField("plugins"); + cloneField("lookupNames"); + cloneField("commandMap"); + cloneField("permissions"); + cloneField("defaultPerms"); + cloneField("permSubs"); + cloneField("defSubs"); + cloneField("useTimings"); + } + + private void cloneField(String bukkitName) { + try { + Field bukkitField = instance.getClass().getDeclaredField(bukkitName); + Field thisFiled = this.getClass().getDeclaredField(bukkitName); + if (bukkitField == null || thisFiled == null) { + TLib.getTLib().getLogger().warn("拷贝 " + bukkitName + " 对象失败"); + return; + } + bukkitField.setAccessible(true); + thisFiled.setAccessible(true); + thisFiled.set(this, bukkitField.get(instance)); + } catch (Exception ignored) { + TLib.getTLib().getLogger().error("拷贝 " + bukkitName + " 对象出错"); + } } @Override diff --git a/src/main/java/com/ilummc/tlib/logger/TLogger.java b/src/main/java/com/ilummc/tlib/logger/TLogger.java new file mode 100644 index 0000000..2f6b3e0 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/logger/TLogger.java @@ -0,0 +1,67 @@ +package com.ilummc.tlib.logger; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.plugin.Plugin; + +import com.ilummc.tlib.util.Strings; + +import lombok.Getter; +import lombok.Setter; +import me.skymc.taboolib.Main; + +public class TLogger { + + public static final int VERBOSE = 0, FINEST = 1, FINE = 2, INFO = 3, WARN = 4, ERROR = 5, FATAL = 6; + + @Getter + private static TLogger globalLogger = new TLogger("§8[§3§lTabooLib§8][§r{1}§8] §f{2}", Main.getInst(), TLogger.FINE); + @Getter + private final String pattern; + @Getter + private Plugin plugin; + @Getter + @Setter + private int level; + + public TLogger(String pattern, Plugin plugin, int level) { + this.pattern = pattern; + this.plugin = plugin; + this.level = level; + } + + public void verbose(String msg) { + if (level <= VERBOSE) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§f全部", ChatColor.translateAlternateColorCodes('&', msg))); + } + + public void finest(String msg) { + if (level <= FINEST) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§e良好", ChatColor.translateAlternateColorCodes('&', msg))); + } + + public void fine(String msg) { + if (level <= FINE) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§a正常", ChatColor.translateAlternateColorCodes('&', msg))); + } + + public void info(String msg) { + if (level <= INFO) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§b信息", ChatColor.translateAlternateColorCodes('&', msg))); + } + + public void warn(String msg) { + if (level <= WARN) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§6警告", "§6" + ChatColor.translateAlternateColorCodes('&', msg))); + } + + public void error(String msg) { + if (level <= ERROR) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§c错误", "§c" + ChatColor.translateAlternateColorCodes('&', msg))); + } + + public void fatal(String msg) { + if (level <= FATAL) + Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(pattern, plugin.getName(), "§4致命错误", "§4" + ChatColor.translateAlternateColorCodes('&', msg))); + } +} diff --git a/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java b/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java index 4f19da4..37c9386 100644 --- a/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java +++ b/src/main/java/com/ilummc/tlib/resources/type/TLocaleTitle.java @@ -5,24 +5,26 @@ import java.util.Map; import javax.annotation.concurrent.Immutable; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; +import org.bukkit.entity.Player; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.ilummc.tlib.TLib; +import com.ilummc.tlib.compat.PlaceholderHook; import com.ilummc.tlib.resources.TLocaleSendable; +import com.ilummc.tlib.util.Strings; -import lombok.Data; -import lombok.Getter; -import net.minecraft.server.v1_11_R1.EntityEvoker.e; +import me.skymc.taboolib.display.TitleUtils; /** * @author Bkm016 * @since 2018-04-22 */ +@Immutable @SerializableAs("TITLE") public class TLocaleTitle implements TLocaleSendable, ConfigurationSerializable { @@ -45,11 +47,16 @@ public class TLocaleTitle implements TLocaleSendable, ConfigurationSerializable @Override public void sendTo(CommandSender sender, String... args) { + if (sender instanceof Player) { + TitleUtils.sendTitle((Player) sender, replaceText(sender, title), replaceText(sender, subtitle), fadein, stay, fadeout); + } else { + TLib.getTLib().getLogger().error("该语言类型只能发送给玩家"); + } } @Override public String asString(String... args) { - return null; + return Strings.replaceWithOrder(title, args); } @Override @@ -79,5 +86,8 @@ public class TLocaleTitle implements TLocaleSendable, ConfigurationSerializable } return title; } - + + private String replaceText(CommandSender sender, String s) { + return ChatColor.translateAlternateColorCodes('&', usePlaceholder ? PlaceholderHook.replace(sender, s) : s); + } }