版本更新至 3.55
新增:ItemUtils 工具新增 isNull() 方法 新增:SpecialItem 工具类 新增:SpecialItemResult 枚举类 新增:AbstractSpecialItem 接口类 新增:Language2Value 对象新增 asString() 方法获取语言文本 新增:ITabooLibraryModule 接口类 新增:TabooLibraryModule 工具类 新增:TLM 框架大致完成。 新增:TLM 框架首个模块 TimeCycle 完成。 修复:Language2 的文本集合类型不再会移除第一条内容了。
This commit is contained in:
parent
757e44e87a
commit
4f10fb502f
@ -36,6 +36,7 @@ import me.skymc.taboolib.economy.EcoUtils;
|
||||
import me.skymc.taboolib.entity.EntityUtils;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.inventory.speciaitem.SpecialItem;
|
||||
import me.skymc.taboolib.javashell.JavaShell;
|
||||
import me.skymc.taboolib.listener.ListenerPlayerCommand;
|
||||
import me.skymc.taboolib.listener.ListenerPlayerQuit;
|
||||
@ -50,6 +51,9 @@ import me.skymc.taboolib.string.language2.Language2;
|
||||
import me.skymc.taboolib.support.SupportPlaceholder;
|
||||
import me.skymc.taboolib.team.TagUtils;
|
||||
import me.skymc.taboolib.timecycle.TimeCycleManager;
|
||||
import me.skymc.tlm.TLM;
|
||||
import me.skymc.tlm.command.TLMCommands;
|
||||
import me.skymc.tlm.module.TabooLibraryModule;
|
||||
import me.skymc.taboolib.nms.item.DabItemUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.permission.PermissionUtils;
|
||||
@ -168,6 +172,7 @@ public class Main extends JavaPlugin implements Listener {
|
||||
// 注册指令
|
||||
getCommand("taboolib").setExecutor(new MainCommands());
|
||||
getCommand("language2").setExecutor(new Language2Command());
|
||||
getCommand("taboolibrarymodule").setExecutor(new TLMCommands());
|
||||
|
||||
// 注册监听
|
||||
registerListener();
|
||||
@ -209,6 +214,10 @@ public class Main extends JavaPlugin implements Listener {
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
new SupportPlaceholder(getInst(), "taboolib").hook();
|
||||
}
|
||||
// 载入 SpecialItem 接口
|
||||
SpecialItem.getInst().loadItems();
|
||||
// 载入 TLM 接口
|
||||
TLM.getInst();
|
||||
}
|
||||
}.runTask(this);
|
||||
}
|
||||
@ -227,6 +236,10 @@ public class Main extends JavaPlugin implements Listener {
|
||||
PlayerDataManager.saveAllPlayers(false, true);
|
||||
// 结束脚本
|
||||
JavaShell.javaShellCancel();
|
||||
// 注销 SpecialItem 接口
|
||||
SpecialItem.getInst().unloadItems();
|
||||
// 注销 TLM 接口
|
||||
TabooLibraryModule.getInst().unloadModules();
|
||||
|
||||
// 结束数据库储存方法
|
||||
if (getStorageType() == StorageType.SQL) {
|
||||
|
@ -188,6 +188,10 @@ public class ItemUtils {
|
||||
return i;
|
||||
}
|
||||
|
||||
public static boolean isNull(ItemStack item) {
|
||||
return item == null || item.getType().equals(Material.AIR);
|
||||
}
|
||||
|
||||
public static boolean isName(ItemStack i, String a) {
|
||||
if (!isNamed(i) || i.getItemMeta() == null || i.getItemMeta().getDisplayName() == null || !i.getItemMeta().getDisplayName().equals(a)) {
|
||||
return false;
|
||||
|
@ -0,0 +1,46 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:35:42
|
||||
*/
|
||||
public abstract interface AbstractSpecialItem {
|
||||
|
||||
/**
|
||||
* 当接口被载入
|
||||
*/
|
||||
default void onEnable() {}
|
||||
|
||||
/**
|
||||
* 当接口被卸载
|
||||
*/
|
||||
default void onDisable() {}
|
||||
|
||||
/**
|
||||
* 获取识别名称
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
abstract String getName();
|
||||
|
||||
/**
|
||||
* 获取载入插件
|
||||
*
|
||||
* @return {@link Plugin}
|
||||
*/
|
||||
abstract Plugin getPlugin();
|
||||
|
||||
/**
|
||||
* 是否进行点击事件
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param currentItem 点击物品
|
||||
* @param cursorItem 持有物品
|
||||
* @return {@link SpecialItemResult[]}
|
||||
*/
|
||||
abstract SpecialItemResult[] isCorrectClick(Player player, ItemStack currentItem, ItemStack cursorItem);
|
||||
}
|
@ -0,0 +1,181 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:34:12
|
||||
*/
|
||||
public class SpecialItem implements Listener {
|
||||
|
||||
private static SpecialItem specialItem = null;
|
||||
|
||||
private final List<AbstractSpecialItem> ITEM_DATA = new CopyOnWriteArrayList<>();
|
||||
|
||||
@Getter
|
||||
private boolean isLoaded;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
private SpecialItem() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工具对象
|
||||
*
|
||||
* @return {@link SpecialItem}
|
||||
*/
|
||||
public static SpecialItem getInst() {
|
||||
if (specialItem == null) {
|
||||
synchronized (SpecialItem.class) {
|
||||
if (specialItem == null) {
|
||||
specialItem = new SpecialItem();
|
||||
// 注册监听器
|
||||
Bukkit.getPluginManager().registerEvents(specialItem, Main.getInst());
|
||||
}
|
||||
}
|
||||
}
|
||||
return specialItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册接口
|
||||
*
|
||||
* @param item 接口对象
|
||||
*/
|
||||
public void register(AbstractSpecialItem item) {
|
||||
if (contains(item.getName())) {
|
||||
MsgUtils.warn("特殊物品接口已存在, 检查名称 &4" + item.getName() + " &c是否重复");
|
||||
}
|
||||
else {
|
||||
ITEM_DATA.add(item);
|
||||
if (isLoaded) {
|
||||
item.onEnable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销接口
|
||||
*
|
||||
* @param name 注册名称
|
||||
*/
|
||||
public void cancel(String name) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getName() != null && specialitem.getName().equals(specialitem)) {
|
||||
specialitem.onDisable();
|
||||
ITEM_DATA.remove(specialitem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销接口
|
||||
*
|
||||
* @param plugin 注册插件
|
||||
*/
|
||||
public void cancel(Plugin plugin) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getPlugin() != null && specialitem.getPlugin().equals(plugin)) {
|
||||
specialitem.onDisable();
|
||||
ITEM_DATA.remove(specialitem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断名称是否存在
|
||||
*
|
||||
* @param name 注册名称
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(String name) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getName().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入所有已注册接口
|
||||
*/
|
||||
public void loadItems() {
|
||||
ITEM_DATA.forEach(x -> x.onEnable());
|
||||
isLoaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销所有已注册接口
|
||||
*/
|
||||
public void unloadItems() {
|
||||
ITEM_DATA.forEach(x -> x.onDisable());
|
||||
ITEM_DATA.clear();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDisable(PluginDisableEvent e) {
|
||||
cancel(e.getPlugin());
|
||||
}
|
||||
|
||||
@EventHandler (priority = EventPriority.MONITOR)
|
||||
public void click(InventoryClickEvent e) {
|
||||
if (e.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
if (ItemUtils.isNull(e.getCurrentItem()) || ItemUtils.isNull(e.getCursor())) {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) e.getWhoClicked();
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
for (SpecialItemResult result : specialitem.isCorrectClick(player, e.getCurrentItem(), e.getCursor())) {
|
||||
if (result == SpecialItemResult.CANCEL) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
else if (result == SpecialItemResult.BREAK) {
|
||||
return;
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURRENT) {
|
||||
e.setCurrentItem(null);
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURSOR) {
|
||||
e.getWhoClicked().setItemOnCursor(null);
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURRENT_AMOUNT_1) {
|
||||
if (e.getCurrentItem().getAmount() > 1) {
|
||||
e.getCurrentItem().setAmount(e.getCurrentItem().getAmount() - 1);
|
||||
}
|
||||
else {
|
||||
e.setCurrentItem(null);
|
||||
}
|
||||
}
|
||||
else if (result == SpecialItemResult.REMOVE_ITEM_CURSOR_AMOUNT_1) {
|
||||
if (e.getCursor().getAmount() > 1) {
|
||||
e.getCursor().setAmount(e.getCursor().getAmount() - 1);
|
||||
}
|
||||
else {
|
||||
e.getWhoClicked().setItemOnCursor(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:55:36
|
||||
*/
|
||||
public enum SpecialItemResult {
|
||||
|
||||
/**
|
||||
* 停止接口检测
|
||||
*/
|
||||
BREAK,
|
||||
|
||||
/**
|
||||
* 取消点击事件
|
||||
*/
|
||||
CANCEL,
|
||||
|
||||
/**
|
||||
* 移除点击物品
|
||||
*/
|
||||
REMOVE_ITEM_CURRENT,
|
||||
|
||||
/**
|
||||
* 移除鼠标物品
|
||||
*/
|
||||
REMOVE_ITEM_CURSOR,
|
||||
|
||||
/**
|
||||
* 移除一个点击物品
|
||||
*/
|
||||
REMOVE_ITEM_CURRENT_AMOUNT_1,
|
||||
|
||||
/**
|
||||
* 移除一个鼠标物品
|
||||
*/
|
||||
REMOVE_ITEM_CURSOR_AMOUNT_1;
|
||||
|
||||
}
|
@ -9,6 +9,8 @@ import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import lombok.Getter;
|
||||
@ -38,7 +40,7 @@ public class Language2Value {
|
||||
private LinkedHashMap<String, String> placeholder = new LinkedHashMap<>();
|
||||
|
||||
@Getter
|
||||
private boolean enablePlaceholderAPI;
|
||||
private boolean enablePlaceholderAPI = false;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
@ -63,6 +65,14 @@ public class Language2Value {
|
||||
// 获取类型
|
||||
String type = languageValue.get(0).toLowerCase();
|
||||
|
||||
// 是否有类型注释
|
||||
boolean isType = true;
|
||||
|
||||
// 是否启用PAPI
|
||||
if (type.contains("[papi]")) {
|
||||
enablePlaceholderAPI = true;
|
||||
}
|
||||
|
||||
// 判断类型
|
||||
if (type.contains("[json]")) {
|
||||
languageType = Language2Type.JSON;
|
||||
@ -75,18 +85,13 @@ public class Language2Value {
|
||||
}
|
||||
else {
|
||||
languageType = Language2Type.TEXT;
|
||||
isType = false;
|
||||
}
|
||||
|
||||
// 是否启用PAPI
|
||||
if (type.contains("[papi]")) {
|
||||
enablePlaceholderAPI = true;
|
||||
// 是否需要删除类型注释
|
||||
if (isType) {
|
||||
languageValue.remove(0);
|
||||
}
|
||||
else {
|
||||
enablePlaceholderAPI = false;
|
||||
}
|
||||
|
||||
// 删除类型
|
||||
languageValue.remove(0);
|
||||
}
|
||||
else {
|
||||
// 设置文本
|
||||
@ -108,24 +113,18 @@ public class Language2Value {
|
||||
public void send(Player player) {
|
||||
// 标题类型
|
||||
if (languageType == Language2Type.TITLE) {
|
||||
// 识别文本
|
||||
Language2Title title = new Language2Title(this);
|
||||
// 发送文本
|
||||
title.send(player);
|
||||
new Language2Title(this).send(player);
|
||||
}
|
||||
// 动作栏类型
|
||||
else if (languageType == Language2Type.ACTION) {
|
||||
// 识别文本
|
||||
Language2Action action = new Language2Action(this);
|
||||
// 发送文本
|
||||
action.send(player);
|
||||
new Language2Action(this).send(player);
|
||||
}
|
||||
// JSON类型
|
||||
else if (languageType == Language2Type.JSON) {
|
||||
// 识别文本
|
||||
Language2Json json = new Language2Json(this, player);
|
||||
// 发送文本
|
||||
json.send(player);
|
||||
new Language2Json(this, player).send(player);
|
||||
}
|
||||
else {
|
||||
// 遍历文本
|
||||
@ -186,6 +185,29 @@ public class Language2Value {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文本
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String asString() {
|
||||
// 标题类型
|
||||
if (languageType == Language2Type.TITLE) {
|
||||
return new Language2Title(this).getTitle();
|
||||
}
|
||||
// 动作栏类型
|
||||
else if (languageType == Language2Type.ACTION) {
|
||||
return new Language2Action(this).getText();
|
||||
}
|
||||
// JSON类型
|
||||
else if (languageType == Language2Type.JSON) {
|
||||
return new Language2Json(this, null).getText().toString();
|
||||
}
|
||||
else {
|
||||
return languageValue.size() == 0 ? ChatColor.DARK_RED + "[<ERROR-1>]" : languageValue.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 变量替换
|
||||
*
|
||||
|
72
src/main/src/me/skymc/tlm/TLM.java
Normal file
72
src/main/src/me/skymc/tlm/TLM.java
Normal file
@ -0,0 +1,72 @@
|
||||
package me.skymc.tlm;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.tlm.module.TabooLibraryModule;
|
||||
import me.skymc.tlm.module.sub.ModuleTimeCycle;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午10:28:05
|
||||
*/
|
||||
public class TLM {
|
||||
|
||||
private static TLM inst = null;
|
||||
|
||||
@Getter
|
||||
private FileConfiguration config;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
private TLM() {
|
||||
// 重载配置文件
|
||||
reloadConfig();
|
||||
// 载入模块
|
||||
if (isEnableModule("TimeCycle")) {
|
||||
TabooLibraryModule.getInst().register(new ModuleTimeCycle());
|
||||
}
|
||||
// 载入模块
|
||||
TabooLibraryModule.getInst().loadModules();
|
||||
// 提示
|
||||
MsgUtils.send("载入 &f" + TabooLibraryModule.getInst().getSize() + " &7个 &fTLM &7模块");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 TLM 对象
|
||||
*
|
||||
* @return TLM
|
||||
*/
|
||||
public static TLM getInst() {
|
||||
if (inst == null) {
|
||||
synchronized (TLM.class) {
|
||||
if (inst == null) {
|
||||
inst = new TLM();
|
||||
}
|
||||
}
|
||||
}
|
||||
return inst;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入配置文件
|
||||
*/
|
||||
public void reloadConfig() {
|
||||
config = ConfigUtils.saveDefaultConfig(Main.getInst(), "module.yml");
|
||||
}
|
||||
|
||||
/**
|
||||
* 模块是否启用
|
||||
*
|
||||
* @param name 名称
|
||||
* @return boolean
|
||||
*/
|
||||
private boolean isEnableModule(String name) {
|
||||
return config.getStringList("EnableModule").contains(name);
|
||||
}
|
||||
}
|
66
src/main/src/me/skymc/tlm/command/TLMCommands.java
Normal file
66
src/main/src/me/skymc/tlm/command/TLMCommands.java
Normal file
@ -0,0 +1,66 @@
|
||||
package me.skymc.tlm.command;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.tlm.TLM;
|
||||
import me.skymc.tlm.module.ITabooLibraryModule;
|
||||
import me.skymc.tlm.module.TabooLibraryModule;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月18日 上午12:02:08
|
||||
*/
|
||||
public class TLMCommands implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] args) {
|
||||
if (args.length == 0) {
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lTaooLibraryModule Commands §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§7 /tlm list §f- §8列出所有模块");
|
||||
sender.sendMessage("§7 /tlm reload [模块名/TLM/ALL] §f- §8重载配置文件");
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("reload")) {
|
||||
if (args.length != 2) {
|
||||
MsgUtils.send(sender, "&4参数错误。");
|
||||
}
|
||||
|
||||
else if (args[1].equalsIgnoreCase("tlm")) {
|
||||
TLM.getInst().reloadConfig();
|
||||
MsgUtils.send(sender, "&fTLM &7配置文件已重载。");
|
||||
}
|
||||
|
||||
else if (args[1].equalsIgnoreCase("all")) {
|
||||
TabooLibraryModule.getInst().reloadConfig();
|
||||
MsgUtils.send(sender, "所有模块配置文件已重载。");
|
||||
}
|
||||
|
||||
else {
|
||||
ITabooLibraryModule module = TabooLibraryModule.getInst().valueOf(args[1]);
|
||||
if (module == null) {
|
||||
MsgUtils.send(sender, "&4模块 &c" + args[1] + " &4不存在。");
|
||||
}
|
||||
else {
|
||||
TabooLibraryModule.getInst().reloadConfig(module, true);
|
||||
MsgUtils.send(sender, "模块 &f" + args[1] + " &7的配置文件已重载。");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("list")) {
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lTaooLibraryModule Modules §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
for (ITabooLibraryModule module : TabooLibraryModule.getInst().keySet()) {
|
||||
sender.sendMessage("§f - §8" + module.getName());
|
||||
}
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
22
src/main/src/me/skymc/tlm/module/ITabooLibraryModule.java
Normal file
22
src/main/src/me/skymc/tlm/module/ITabooLibraryModule.java
Normal file
@ -0,0 +1,22 @@
|
||||
package me.skymc.tlm.module;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018Äê2ÔÂ17ÈÕ ÏÂÎç11:22:42
|
||||
*/
|
||||
public abstract interface ITabooLibraryModule {
|
||||
|
||||
default void onEnable() {}
|
||||
|
||||
default void onDisable() {}
|
||||
|
||||
default void onReload() {};
|
||||
|
||||
abstract String getName();
|
||||
|
||||
default FileConfiguration getConfig() {
|
||||
return TabooLibraryModule.getInst().getConfig(this);
|
||||
}
|
||||
}
|
100
src/main/src/me/skymc/tlm/module/TabooLibraryModule.java
Normal file
100
src/main/src/me/skymc/tlm/module/TabooLibraryModule.java
Normal file
@ -0,0 +1,100 @@
|
||||
package me.skymc.tlm.module;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018Äê2ÔÂ17ÈÕ ÏÂÎç11:22:48
|
||||
*/
|
||||
public class TabooLibraryModule {
|
||||
|
||||
private final HashMap<ITabooLibraryModule, FileConfiguration> TLM_MODULE = new HashMap<>();
|
||||
private static TabooLibraryModule inst = null;
|
||||
|
||||
private TabooLibraryModule() {
|
||||
|
||||
}
|
||||
|
||||
public static TabooLibraryModule getInst() {
|
||||
if (inst == null) {
|
||||
synchronized (TabooLibraryModule.class) {
|
||||
if (inst == null) {
|
||||
inst = new TabooLibraryModule();
|
||||
}
|
||||
}
|
||||
}
|
||||
return inst;
|
||||
}
|
||||
|
||||
public void register(ITabooLibraryModule module) {
|
||||
if (!TLM_MODULE.containsKey(module)) {
|
||||
TLM_MODULE.put(module, new YamlConfiguration());
|
||||
reloadConfig(module, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void loadModules() {
|
||||
for (ITabooLibraryModule module : TLM_MODULE.keySet()) {
|
||||
module.onEnable();
|
||||
if (module instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener) module, Main.getInst());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void unloadModules() {
|
||||
TLM_MODULE.keySet().forEach(x -> x.onDisable());
|
||||
}
|
||||
|
||||
public void reloadConfig() {
|
||||
TLM_MODULE.keySet().forEach(x -> reloadConfig(x, true));
|
||||
}
|
||||
|
||||
public void reloadConfig(ITabooLibraryModule module, boolean isReload) {
|
||||
if (module.getName() == null) {
|
||||
return;
|
||||
}
|
||||
File file = new File(Main.getInst().getDataFolder(), "TLM/" + module.getName() + ".yml");
|
||||
if (!file.exists()) {
|
||||
Main.getInst().saveResource("TLM/" + module.getName() + ".yml", true);
|
||||
}
|
||||
try {
|
||||
TLM_MODULE.get(module).load(file);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
if (isReload) {
|
||||
module.onReload();
|
||||
}
|
||||
}
|
||||
|
||||
public FileConfiguration getConfig(ITabooLibraryModule module) {
|
||||
return TLM_MODULE.get(module);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return TLM_MODULE.size();
|
||||
}
|
||||
|
||||
public Set<ITabooLibraryModule> keySet() {
|
||||
return TLM_MODULE.keySet();
|
||||
}
|
||||
|
||||
public ITabooLibraryModule valueOf(String name) {
|
||||
for (ITabooLibraryModule module : TLM_MODULE.keySet()) {
|
||||
if (module.getName().equals(name)) {
|
||||
return module;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
114
src/main/src/me/skymc/tlm/module/sub/ModuleTimeCycle.java
Normal file
114
src/main/src/me/skymc/tlm/module/sub/ModuleTimeCycle.java
Normal file
@ -0,0 +1,114 @@
|
||||
package me.skymc.tlm.module.sub;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.other.DateUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.timecycle.TimeCycle;
|
||||
import me.skymc.taboolib.timecycle.TimeCycleEvent;
|
||||
import me.skymc.taboolib.timecycle.TimeCycleInitializeEvent;
|
||||
import me.skymc.taboolib.timecycle.TimeCycleManager;
|
||||
import me.skymc.tlm.module.ITabooLibraryModule;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午11:23:38
|
||||
*/
|
||||
public class ModuleTimeCycle implements ITabooLibraryModule, Listener {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "TimeCycle";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// 载入检查器
|
||||
loadCycles();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// 注销检查器
|
||||
unloadCycles();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReload() {
|
||||
// 注销检查器
|
||||
unloadCycles();
|
||||
// 载入检查器
|
||||
loadCycles();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onTimeCycleInitialize(TimeCycleInitializeEvent e) {
|
||||
if (e.getCycle().getName().contains("tlm|")) {
|
||||
// 获取名称
|
||||
String name = e.getCycle().getName().replace("tlm|", "");
|
||||
// 如果有初始化时间配置
|
||||
if (getConfig().contains("TimeCycle." + name + ".Initialise.InitialiseDate")) {
|
||||
// 获取时间
|
||||
Calendar date = Calendar.getInstance();
|
||||
// 遍历初始化规则
|
||||
for (String typeStr : getConfig().getStringList("TimeCycle." + name + ".Initialise.InitialiseDate")) {
|
||||
try {
|
||||
int type = (int) Calendar.class.getField(typeStr.split("=")[0]).get(Calendar.class);
|
||||
date.set(type, NumberUtils.getInteger(typeStr.split("=")[1]));
|
||||
} catch (Exception err) {
|
||||
MsgUtils.warn("模块配置载入异常");
|
||||
MsgUtils.warn("模块: &4TimeCycle");
|
||||
MsgUtils.warn("位于: &4" + typeStr);
|
||||
}
|
||||
}
|
||||
e.setTimeLine(date.getTimeInMillis());
|
||||
}
|
||||
// 如果有初始化命令
|
||||
if (getConfig().contains("TimeCycle." + name + ".Initialise.InitialiseCommand")) {
|
||||
// 遍历初始化命令
|
||||
for (String command : getConfig().getStringList("TimeCycle." + name + ".Initialise.InitialiseCommand")) {
|
||||
// 执行命令
|
||||
Bukkit.getScheduler().runTask(Main.getInst(), () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onTimeCycle(TimeCycleEvent e) {
|
||||
if (e.getCycle().getName().contains("tlm|")) {
|
||||
// 获取名称
|
||||
String name = e.getCycle().getName().replace("tlm|", "");
|
||||
// 如果有更新命令
|
||||
if (getConfig().contains("TimeCycle." + name + ".UpdateCommand")) {
|
||||
// 遍历更新命令
|
||||
for (String command : getConfig().getStringList("TimeCycle." + name + ".UpdateCommand")) {
|
||||
// 执行命令
|
||||
Bukkit.getScheduler().runTask(Main.getInst(), () -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCycles() {
|
||||
for (String name : getConfig().getConfigurationSection("TimeCycle").getKeys(false)) {
|
||||
TimeCycleManager.register(new TimeCycle("tlm|" + name, DateUtils.formatDate(getConfig().getString("TimeCycle." + name + ".Cycle")), Main.getInst()));
|
||||
}
|
||||
}
|
||||
|
||||
private void unloadCycles() {
|
||||
for (TimeCycle cycle : TimeCycleManager.getTimeCycles()) {
|
||||
if (cycle.getName().startsWith("tlm|")) {
|
||||
TimeCycleManager.cancel(cycle.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
35
src/resources/TLM/TimeCycle.yml
Normal file
35
src/resources/TLM/TimeCycle.yml
Normal file
@ -0,0 +1,35 @@
|
||||
# 时间检查器
|
||||
TimeCycle:
|
||||
# 检查器名称
|
||||
cycle_name:
|
||||
# 检查器周期
|
||||
# ---------- #
|
||||
# 1d = 1天
|
||||
# 1h = 1小时
|
||||
# 1m = 1分钟
|
||||
# 1s = 1秒钟
|
||||
# 时间之间用 ";" 分隔, 例如 1小时30分钟 = "1h;30m"
|
||||
# ---------- #
|
||||
Cycle: '1d'
|
||||
|
||||
# 更新配置
|
||||
UpdateCommand:
|
||||
- 'say 检查器 cycle_name 更新!'
|
||||
|
||||
# 初始化配置
|
||||
Initialise:
|
||||
# 初始化时间
|
||||
# 特殊时间
|
||||
# - DAY_OF_WEEK = 本周第几天 (最小:1)
|
||||
# - DAY_OF_MONTH = 本月第几天 (最小:1)
|
||||
InitialiseDate:
|
||||
# 初始化时将小时设置为 0
|
||||
- 'HOUR_OF_DAY=0'
|
||||
# 初始化时将分钟设置为 0
|
||||
- 'MINUTE=0'
|
||||
# 初始化时将秒钟设置为 0
|
||||
- 'SECOND=0'
|
||||
|
||||
# 初始化命令
|
||||
InitialiseCommand:
|
||||
- 'say 检查器 cycle_name 初始化完成!'
|
4
src/resources/module.yml
Normal file
4
src/resources/module.yml
Normal file
@ -0,0 +1,4 @@
|
||||
# 启用模块
|
||||
# 该配置需要重启服务器才会生效
|
||||
EnableModule:
|
||||
#- 'TimeCycle'
|
Loading…
Reference in New Issue
Block a user