史诗级大饼,请叫我黑饼王。
This commit is contained in:
parent
600ac49622
commit
24556b9f2a
19
pom.xml
19
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.skymc</groupId>
|
||||
<artifactId>TabooLib</artifactId>
|
||||
<version>4.15</version>
|
||||
<version>4.2</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@ -51,6 +51,7 @@
|
||||
<includes>
|
||||
<include>com.ilummc.eagletdl</include>
|
||||
<include>org.ow2.asm</include>
|
||||
<include>com.google.code.gson</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
@ -58,6 +59,17 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>me.skymc.taboolib.socket.TabooLibServer</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
@ -82,6 +94,11 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
|
@ -22,9 +22,11 @@ import me.skymc.taboolib.economy.EcoUtils;
|
||||
import me.skymc.taboolib.entity.EntityUtils;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.fileutils.TLogs;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.inventory.speciaitem.SpecialItem;
|
||||
import me.skymc.taboolib.itagapi.TagDataHandler;
|
||||
import me.skymc.taboolib.javascript.ScriptHandler;
|
||||
import me.skymc.taboolib.javashell.JavaShell;
|
||||
import me.skymc.taboolib.listener.*;
|
||||
import me.skymc.taboolib.message.ChatCatcher;
|
||||
@ -36,6 +38,7 @@ import me.skymc.taboolib.permission.PermissionUtils;
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
import me.skymc.taboolib.sign.SignUtils;
|
||||
import me.skymc.taboolib.skript.SkriptHandler;
|
||||
import me.skymc.taboolib.socket.TabooLibClient;
|
||||
import me.skymc.taboolib.string.StringUtils;
|
||||
import me.skymc.taboolib.string.language2.Language2;
|
||||
import me.skymc.taboolib.support.SupportPlaceholder;
|
||||
@ -61,11 +64,12 @@ import java.net.InetAddress;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class Main extends JavaPlugin implements Listener {
|
||||
public class Main extends JavaPlugin {
|
||||
|
||||
public Main() {
|
||||
inst = this;
|
||||
@ -80,7 +84,6 @@ public class Main extends JavaPlugin implements Listener {
|
||||
private static File playerDataFolder;
|
||||
private static File serverDataFolder;
|
||||
private static StorageType storageType = StorageType.LOCAL;
|
||||
private static MySQLConnection connection = null;
|
||||
private static Language2 exampleLanguage2;
|
||||
private static boolean disable = false;
|
||||
private static boolean started = false;
|
||||
@ -109,32 +112,23 @@ public class Main extends JavaPlugin implements Listener {
|
||||
@Override
|
||||
public void onLoad() {
|
||||
disable = false;
|
||||
// 载入配置
|
||||
// 载入配置文件
|
||||
saveDefaultConfig();
|
||||
// 载入牛逼玩意儿
|
||||
// 载入牛逼东西
|
||||
TLib.init();
|
||||
TLib.injectPluginManager();
|
||||
// 网络检测
|
||||
testInternet();
|
||||
// 创建文件夹
|
||||
setupDataFolder();
|
||||
// 创建数据库
|
||||
setupDatabase();
|
||||
// 载入离线库文件
|
||||
setupLibraries();
|
||||
// 载入牛逼玩意儿
|
||||
// 载入插件设置
|
||||
TabooLibSettings.setup();
|
||||
// 载入大饼
|
||||
TLib.initPost();
|
||||
// 注册连接池
|
||||
// 载入连接池
|
||||
HikariHandler.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// 注册命令
|
||||
registerCommands();
|
||||
// 注册监听
|
||||
registerListener();
|
||||
|
||||
// 注册插件配置
|
||||
TabooLibSettings.register();
|
||||
// 载入经济
|
||||
EcoUtils.setupEconomy();
|
||||
// 载入权限
|
||||
@ -146,32 +140,27 @@ public class Main extends JavaPlugin implements Listener {
|
||||
// 载入周期管理器
|
||||
TimeCycleManager.load();
|
||||
// 启动脚本
|
||||
JavaShell.javaShellSetup();
|
||||
ScriptHandler.inst();
|
||||
// 注册脚本
|
||||
SkriptHandler.register();
|
||||
// 注册头衔
|
||||
TagDataHandler.init(this);
|
||||
// 载入语言文件
|
||||
exampleLanguage2 = new Language2("Language2", this);
|
||||
|
||||
// 启动数据库储存方法
|
||||
if (getStorageType() == StorageType.SQL) {
|
||||
GlobalDataManager.SQLMethod.startSQLMethod();
|
||||
}
|
||||
|
||||
// 载入完成
|
||||
TLocale.Logger.info("NOTIFY.SUCCESS-LOADED", getDescription().getAuthors().toString(), getDescription().getVersion(), String.valueOf(TabooLib.getVersion()));
|
||||
|
||||
// 文件保存
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, DataUtils::saveAllCaches, 20, 20 * 120);
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> PlayerDataManager.saveAllCaches(true, false), 20, 20 * 60);
|
||||
|
||||
// 文件监控
|
||||
TLib.getTLib().getConfigWatcher().addSimpleListener(new File(getDataFolder(), "config.yml"), () -> {
|
||||
reloadConfig();
|
||||
TLocale.Logger.info("CONFIG.RELOAD-SUCCESS", inst.getName(), "config.yml");
|
||||
});
|
||||
|
||||
// 插件联动
|
||||
new BukkitRunnable() {
|
||||
|
||||
@ -181,14 +170,12 @@ public class Main extends JavaPlugin implements Listener {
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
new SupportPlaceholder(getInst(), "taboolib").hook();
|
||||
}
|
||||
|
||||
// 载入 TLM 接口
|
||||
TLM.getInst();
|
||||
// 载入 SpecialItem 接口
|
||||
SpecialItem.getInst().loadItems();
|
||||
// 载入 TranslateUUID 工具
|
||||
TranslateUUID.init();
|
||||
|
||||
// 面子工程
|
||||
InputStream inputStream = FileUtils.getResource("motd.txt");
|
||||
try {
|
||||
@ -198,14 +185,12 @@ public class Main extends JavaPlugin implements Listener {
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
// 本地通讯网络
|
||||
TabooLibClient.init();
|
||||
}
|
||||
}.runTask(this);
|
||||
|
||||
// 更新检测
|
||||
new UpdateTask();
|
||||
// 启动监控
|
||||
new Metrics(this);
|
||||
|
||||
// 启动
|
||||
started = true;
|
||||
}
|
||||
@ -213,13 +198,11 @@ public class Main extends JavaPlugin implements Listener {
|
||||
@Override
|
||||
public void onDisable() {
|
||||
disable = true;
|
||||
|
||||
// 如果插件尚未启动完成
|
||||
if (!started) {
|
||||
TLocale.Logger.error("NOTIFY.FAIL-DISABLE");
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存数据
|
||||
Bukkit.getOnlinePlayers().forEach(x -> DataUtils.saveOnline(x.getName()));
|
||||
// 结束线程
|
||||
@ -238,12 +221,12 @@ public class Main extends JavaPlugin implements Listener {
|
||||
TranslateUUID.cancel();
|
||||
// 注销连接池
|
||||
HikariHandler.closeDataSourceForce();
|
||||
|
||||
// 注销监听器
|
||||
TListenerHandler.cancelListeners();
|
||||
// 结束数据库储存方法
|
||||
if (getStorageType() == StorageType.SQL) {
|
||||
GlobalDataManager.SQLMethod.cancelSQLMethod();
|
||||
}
|
||||
|
||||
// 清理数据
|
||||
if (getStorageType() == StorageType.LOCAL && getConfig().getBoolean("DELETE-DATA")) {
|
||||
getPlayerDataFolder().delete();
|
||||
@ -252,124 +235,14 @@ public class Main extends JavaPlugin implements Listener {
|
||||
if (getStorageType() == StorageType.SQL && getConfig().getBoolean("DELETE-VARIABLE")) {
|
||||
GlobalDataManager.clearInvalidVariables();
|
||||
}
|
||||
|
||||
// 提示信息
|
||||
TLocale.Logger.info("NOTIFY.SUCCESS-DISABLE");
|
||||
|
||||
// 结束连接
|
||||
if (connection != null && connection.isConnection()) {
|
||||
connection.closeConnection();
|
||||
}
|
||||
|
||||
// 卸载牛逼玩意儿
|
||||
TLib.unload();
|
||||
|
||||
// 关闭服务器
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
|
||||
private void testInternet() {
|
||||
try {
|
||||
InetAddress inetAddress = InetAddress.getByName(getConfig().getString("TEST-URL", "aliyun.com"));
|
||||
isInternetOnline = inetAddress.isReachable(10000);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
if (!isInternetOnline() && !isOfflineVersion() && !isLibrariesExists()) {
|
||||
TLocale.Logger.error("TLIB.LOAD-FAIL-OFFLINE", getDescription().getVersion());
|
||||
// 死锁
|
||||
try {
|
||||
Thread.sleep(Long.MAX_VALUE);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setupDataFolder() {
|
||||
playerDataFolder = new File(getConfig().getString("DATAURL.PLAYER-DATA"));
|
||||
if (!playerDataFolder.exists()) {
|
||||
playerDataFolder.mkdirs();
|
||||
}
|
||||
serverDataFolder = new File(getConfig().getString("DATAURL.SERVER-DATA"));
|
||||
if (!serverDataFolder.exists()) {
|
||||
serverDataFolder.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
private void setupLibraries() {
|
||||
if (!isOfflineVersion()) {
|
||||
return;
|
||||
}
|
||||
for (Dependency dependency : TDependencyInjector.getDependencies(TLib.getTLib())) {
|
||||
if (dependency.type() == Dependency.Type.LIBRARY && dependency.maven().matches(".*:.*:.*")) {
|
||||
String fileName = String.join("-", dependency.maven().split(":")) + ".jar";
|
||||
File targetFile = FileUtils.file(TLib.getTLib().getLibsFolder(), fileName);
|
||||
InputStream inputStream = FileUtils.getResource("libs/" + fileName);
|
||||
if (!targetFile.exists() && inputStream != null) {
|
||||
FileUtils.inputStreamToFile(inputStream, FileUtils.file(TLib.getTLib().getLibsFolder(), fileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setupDatabase() {
|
||||
DataUtils.addPluginData("TabooLibrary", null);
|
||||
if (getConfig().getBoolean("MYSQL.ENABLE")) {
|
||||
connection = new MySQLConnection(getConfig().getString("MYSQL.HOST"), getConfig().getString("MYSQL.USER"), getConfig().getString("MYSQL.POST"), getConfig().getString("MYSQL.PASSWORD"), getConfig().getString("MYSQL.DATABASE"), 30, this);
|
||||
if (connection.isConnection()) {
|
||||
connection.createTable(getTablePrefix() + "_playerdata", "username", "configuration");
|
||||
connection.createTable(getTablePrefix() + "_plugindata", "name", "variable", "upgrade");
|
||||
connection.createTable(getTablePrefix() + "_serveruuid", "uuid", "hash");
|
||||
if (!connection.isExists(getTablePrefix() + "_serveruuid", "uuid", TabooLib.getServerUID())) {
|
||||
connection.intoValue(getTablePrefix() + "_serveruuid", TabooLib.getServerUID(), StringUtils.hashKeyForDisk(getDataFolder().getPath()));
|
||||
} else {
|
||||
String hash = connection.getValue(getTablePrefix() + "_serveruuid", "uuid", TabooLib.getServerUID(), "hash").toString();
|
||||
if (!hash.equals(StringUtils.hashKeyForDisk(getDataFolder().getPath()))) {
|
||||
TLocale.Logger.error("NOTIFY.ERROR-SERVER-KEY");
|
||||
TabooLib.resetServerUID();
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
TLocale.Logger.error("NOTIFY.ERROR-CONNECTION-FAIL");
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
storageType = StorageType.SQL;
|
||||
}
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
getCommand("language2").setExecutor(new Language2Command());
|
||||
getCommand("taboolibrarymodule").setExecutor(new TLMCommands());
|
||||
TBaseCommand.registerCommand("taboolib", new TabooLibMainCommand());
|
||||
TBaseCommand.registerCommand("tabooliblocale", new TabooLibLocaleCommand());
|
||||
TBaseCommand.registerCommand("taboolibplugin", new TabooLibPluginCommand());
|
||||
TBaseCommand.registerCommand("taboolibexecute", new TabooLibExecuteCommand());
|
||||
TBaseCommand.registerCommand("translateuuid", new TranslateUUIDCommand());
|
||||
}
|
||||
|
||||
private void registerListener() {
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPlayerCommand(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPlayerJump(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPlayerJoinAndQuit(), this);
|
||||
getServer().getPluginManager().registerEvents(new ChatCatcher(), this);
|
||||
getServer().getPluginManager().registerEvents(new DataUtils(), this);
|
||||
getServer().getPluginManager().registerEvents(new AnvilContainerAPI(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPluginDisable(), this);
|
||||
getServer().getPluginManager().registerEvents(new PlayerDataManager(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerItemListCommand(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerSoundsCommand(), this);
|
||||
|
||||
if (TabooLib.getVerint() > 10700) {
|
||||
getServer().getPluginManager().registerEvents(new EntityUtils(), this);
|
||||
getServer().getPluginManager().registerEvents(new SignUtils(), this);
|
||||
}
|
||||
|
||||
if (Bukkit.getPluginManager().getPlugin("YUM") != null) {
|
||||
getServer().getPluginManager().registerEvents(new ListenerNetWork(), this);
|
||||
}
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
@ -409,7 +282,7 @@ public class Main extends JavaPlugin implements Listener {
|
||||
}
|
||||
|
||||
public static MySQLConnection getConnection() {
|
||||
return connection;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Language2 getExampleLanguage2() {
|
||||
@ -439,4 +312,26 @@ public class Main extends JavaPlugin implements Listener {
|
||||
public static boolean isLibrariesExists() {
|
||||
return TLib.getTLib().getLibsFolder().listFiles().length > 0;
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Private Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
static void setIsInternetOnline(boolean isInternetOnline) {
|
||||
Main.isInternetOnline = isInternetOnline;
|
||||
}
|
||||
|
||||
static void setPlayerDataFolder(File playerDataFolder) {
|
||||
Main.playerDataFolder = playerDataFolder;
|
||||
}
|
||||
|
||||
static void setServerDataFolder(File serverDataFolder) {
|
||||
Main.serverDataFolder = serverDataFolder;
|
||||
}
|
||||
|
||||
static void setStorageType(StorageType storageType) {
|
||||
Main.storageType = storageType;
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ public class TabooLib {
|
||||
}
|
||||
}
|
||||
|
||||
public static Main instance() {
|
||||
return (Main) Main.getInst();
|
||||
}
|
||||
|
||||
public static boolean isSpigot() {
|
||||
return spigot;
|
||||
}
|
||||
|
128
src/main/java/me/skymc/taboolib/TabooLibDatabase.java
Normal file
128
src/main/java/me/skymc/taboolib/TabooLibDatabase.java
Normal file
@ -0,0 +1,128 @@
|
||||
package me.skymc.taboolib;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.mysql.builder.SQLColumn;
|
||||
import me.skymc.taboolib.mysql.builder.SQLColumnType;
|
||||
import me.skymc.taboolib.mysql.builder.SQLHost;
|
||||
import me.skymc.taboolib.mysql.builder.SQLTable;
|
||||
import me.skymc.taboolib.mysql.hikari.HikariHandler;
|
||||
import me.skymc.taboolib.string.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-23 17:15
|
||||
*/
|
||||
public class TabooLibDatabase {
|
||||
|
||||
private static SQLHost host;
|
||||
private static DataSource dataSource;
|
||||
private static HashMap<String, SQLTable> tables = new HashMap<>();
|
||||
|
||||
static void init() {
|
||||
if (Main.getStorageType() != Main.StorageType.SQL) {
|
||||
return;
|
||||
}
|
||||
// 数据库地址
|
||||
host = new SQLHost(
|
||||
// 地址
|
||||
Main.getInst().getConfig().getString("MYSQL.HOST"),
|
||||
// 用户
|
||||
Main.getInst().getConfig().getString("MYSQL.USER"),
|
||||
// 端口
|
||||
Main.getInst().getConfig().getString("MYSQL.POST"),
|
||||
// 密码
|
||||
Main.getInst().getConfig().getString("MYSQL.PASSWORD"),
|
||||
// 数据库
|
||||
Main.getInst().getConfig().getString("MYSQL.DATABASE"), TabooLib.instance());
|
||||
// 连接数据库
|
||||
try {
|
||||
dataSource = HikariHandler.createDataSource(host);
|
||||
} catch (Exception ignored) {
|
||||
TLocale.Logger.error("NOTIFY.ERROR-CONNECTION-FAIL");
|
||||
return;
|
||||
}
|
||||
// 创建各项数据表
|
||||
createTableWithPlayerData();
|
||||
createTableWithPluginData();
|
||||
createTableWithServerUUID();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建玩家数据表
|
||||
*/
|
||||
static void createTableWithPlayerData() {
|
||||
SQLTable table = new SQLTable(Main.getTablePrefix() + "_playerdata", SQLColumn.PRIMARY_KEY_ID, new SQLColumn(SQLColumnType.TEXT, "username"), new SQLColumn(SQLColumnType.TEXT, "configuration"));
|
||||
table.executeUpdate(table.createQuery()).dataSource(dataSource).run();
|
||||
tables.put("playerdata", table);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建插件数据表
|
||||
*/
|
||||
static void createTableWithPluginData() {
|
||||
SQLTable table = new SQLTable(Main.getTablePrefix() + "_plugindata", SQLColumn.PRIMARY_KEY_ID, new SQLColumn(SQLColumnType.TEXT, "name"), new SQLColumn(SQLColumnType.TEXT, "variable"), new SQLColumn(SQLColumnType.TEXT, "upgrade"));
|
||||
table.executeUpdate(table.createQuery()).dataSource(dataSource).run();
|
||||
tables.put("plugindata", table);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建服务器数据表
|
||||
*/
|
||||
static void createTableWithServerUUID() {
|
||||
SQLTable table = new SQLTable(Main.getTablePrefix() + "_serveruuid", SQLColumn.PRIMARY_KEY_ID, new SQLColumn(SQLColumnType.TEXT, "uuid"), new SQLColumn(SQLColumnType.TEXT, "hash"));
|
||||
table.executeUpdate(table.createQuery()).dataSource(dataSource).run();
|
||||
tables.put("serveruuid", table);
|
||||
// 获取当前服务器信息
|
||||
String hash = getServerHash(TabooLib.getServerUID());
|
||||
if (hash == null) {
|
||||
// 写入序列号
|
||||
table.executeUpdate("insert into " + table.getTableName() + " values(null, ?, ?)")
|
||||
.dataSource(dataSource)
|
||||
.statement(s -> {
|
||||
s.setString(1, TabooLib.getServerUID());
|
||||
s.setString(2, StringUtils.hashKeyForDisk(Main.getInst().getDataFolder().getPath()));
|
||||
}).run();
|
||||
} else if (!hash.equals(StringUtils.hashKeyForDisk(Main.getInst().getDataFolder().getPath()))) {
|
||||
TLocale.Logger.error("NOTIFY.ERROR-SERVER-KEY");
|
||||
TabooLib.resetServerUID();
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器序列号对应的目录哈希值
|
||||
*
|
||||
* @param uuid 服务器序列号
|
||||
* @return 目录哈希值
|
||||
*/
|
||||
public static String getServerHash(String uuid) {
|
||||
SQLTable table = tables.get("serveruuid");
|
||||
return table.executeQuery("select * from " + table.getTableName() + " where uuid = ?")
|
||||
.dataSource(dataSource)
|
||||
.statement(s -> s.setString(1, uuid))
|
||||
.resultNext(r -> r.getString("hash"))
|
||||
.run(null, "");
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static SQLHost getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public static HashMap<String, SQLTable> getTables() {
|
||||
return tables;
|
||||
}
|
||||
|
||||
public static DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
}
|
126
src/main/java/me/skymc/taboolib/TabooLibSettings.java
Normal file
126
src/main/java/me/skymc/taboolib/TabooLibSettings.java
Normal file
@ -0,0 +1,126 @@
|
||||
package me.skymc.taboolib;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.annotations.Dependency;
|
||||
import com.ilummc.tlib.inject.TDependencyInjector;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.bstats.Metrics;
|
||||
import me.skymc.taboolib.commands.language.Language2Command;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.listener.TListenerHandler;
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
import me.skymc.tlm.command.TLMCommands;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-23 17:04
|
||||
*/
|
||||
class TabooLibSettings {
|
||||
|
||||
static void setup() {
|
||||
testInternet();
|
||||
setupDataFolder();
|
||||
setupDatabase();
|
||||
setupLibraries();
|
||||
}
|
||||
|
||||
static void register() {
|
||||
registerCommands();
|
||||
registerListener();
|
||||
registerMetrics();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化插件文件夹
|
||||
*/
|
||||
static void setupDataFolder() {
|
||||
Main.setPlayerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.PLAYER-DATA")));
|
||||
Main.setServerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.SERVER-DATA")));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化插件依赖库
|
||||
*/
|
||||
static void setupLibraries() {
|
||||
if (!Main.isOfflineVersion()) {
|
||||
return;
|
||||
}
|
||||
for (Dependency dependency : TDependencyInjector.getDependencies(TLib.getTLib())) {
|
||||
if (dependency.type() == Dependency.Type.LIBRARY && dependency.maven().matches(".*:.*:.*")) {
|
||||
String fileName = String.join("-", dependency.maven().split(":")) + ".jar";
|
||||
File targetFile = FileUtils.file(TLib.getTLib().getLibsFolder(), fileName);
|
||||
InputStream inputStream = FileUtils.getResource("libs/" + fileName);
|
||||
if (!targetFile.exists() && inputStream != null) {
|
||||
FileUtils.inputStreamToFile(inputStream, FileUtils.file(TLib.getTLib().getLibsFolder(), fileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查网络连接状态
|
||||
*/
|
||||
static void testInternet() {
|
||||
try {
|
||||
InetAddress inetAddress = InetAddress.getByName(Main.getInst().getConfig().getString("TEST-URL", "aliyun.com"));
|
||||
Main.setIsInternetOnline(inetAddress.isReachable(10000));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
if (!Main.isInternetOnline() && !Main.isOfflineVersion() && !Main.isLibrariesExists()) {
|
||||
TLocale.Logger.error("TLIB.LOAD-FAIL-OFFLINE", Main.getInst().getDescription().getVersion());
|
||||
try {
|
||||
while (true) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件数据库
|
||||
*/
|
||||
static void setupDatabase() {
|
||||
DataUtils.addPluginData("TabooLibrary", null);
|
||||
// 检查是否启用数据库
|
||||
Main.setStorageType(Main.getInst().getConfig().getBoolean("MYSQL.ENABLE") ? Main.StorageType.SQL : Main.StorageType.LOCAL);
|
||||
// 初始化数据库
|
||||
TabooLibDatabase.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件命令
|
||||
*/
|
||||
static void registerCommands() {
|
||||
Bukkit.getPluginCommand("language2").setExecutor(new Language2Command());
|
||||
Bukkit.getPluginCommand("taboolibrarymodule").setExecutor(new TLMCommands());
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件监听
|
||||
*/
|
||||
static void registerListener() {
|
||||
// 载入所有 TListener 监听器
|
||||
TListenerHandler.setupListeners();
|
||||
// 注册所有 TListener 监听器
|
||||
Bukkit.getScheduler().runTask(TabooLib.instance(), TListenerHandler::registerListeners);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册插件统计
|
||||
*/
|
||||
static void registerMetrics() {
|
||||
Metrics metrics = new Metrics(TabooLib.instance());
|
||||
metrics.addCustomChart(new Metrics.SingleLineChart("plugins_using_taboolib", () -> Math.toIntExact(Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(plugin -> plugin.getDescription().getDepend().contains("TabooLib")).count())));
|
||||
metrics.addCustomChart(new Metrics.AdvancedPie("plugins_using_taboolib_name", () -> Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(plugin -> plugin.getDescription().getDepend().contains("TabooLib")).collect(Collectors.toMap(Plugin::getName, plugin -> 1, (a, b) -> b))));
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package me.skymc.taboolib.anvil;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
@ -19,9 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
@TListener
|
||||
public class AnvilContainerAPI implements Listener {
|
||||
|
||||
public static List<String> list = new ArrayList<>();
|
||||
|
@ -1,6 +1,5 @@
|
||||
package me.skymc.taboolib.bstats;
|
||||
|
||||
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -18,21 +17,28 @@ import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.logging.Level;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
/**
|
||||
* bStats collects some data for plugin authors.
|
||||
* <p>
|
||||
* Check out https://bStats.org/ to learn more about bStats!
|
||||
*
|
||||
* @author Bastian
|
||||
*/
|
||||
public class Metrics {
|
||||
|
||||
static {
|
||||
// You can use the property to disable the check in your test environment
|
||||
if (System.getProperty("bstats.relocatecheck") == null || !"false".equals(System.getProperty("bstats.relocatecheck"))) {
|
||||
if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false")) {
|
||||
// Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
|
||||
final String defaultPackage = new String(
|
||||
new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's', '.', 'b', 'u', 'k', 'k', 'i', 't'});
|
||||
@ -44,34 +50,22 @@ public class Metrics {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The version of this bStats class
|
||||
*/
|
||||
// The version of this bStats class
|
||||
public static final int B_STATS_VERSION = 1;
|
||||
|
||||
/**
|
||||
* The url to which the data is sent
|
||||
*/
|
||||
// The url to which the data is sent
|
||||
private static final String URL = "https://bStats.org/submitData/bukkit";
|
||||
|
||||
/**
|
||||
* Should failed requests be logged?
|
||||
*/
|
||||
// Should failed requests be logged?
|
||||
private static boolean logFailedRequests;
|
||||
|
||||
/**
|
||||
* The uuid of the server
|
||||
*/
|
||||
// The uuid of the server
|
||||
private static String serverUUID;
|
||||
|
||||
/**
|
||||
* The plugin
|
||||
*/
|
||||
// The plugin
|
||||
private final JavaPlugin plugin;
|
||||
|
||||
/**
|
||||
* A list with all custom charts
|
||||
*/
|
||||
// A list with all custom charts
|
||||
private final List<CustomChart> charts = new ArrayList<>();
|
||||
|
||||
/**
|
||||
@ -109,26 +103,21 @@ public class Metrics {
|
||||
).copyDefaults(true);
|
||||
try {
|
||||
config.save(configFile);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
} catch (IOException ignored) { }
|
||||
}
|
||||
|
||||
// Load the data
|
||||
serverUUID = config.getString("serverUuid");
|
||||
logFailedRequests = config.getBoolean("logFailedRequests", false);
|
||||
|
||||
// ignored config
|
||||
if (config.getBoolean("enabled", true)) {
|
||||
boolean found = false;
|
||||
// Search for all other bStats Metrics classes to see if we are the first one
|
||||
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
|
||||
try {
|
||||
// Our identifier :)
|
||||
service.getField("B_STATS_VERSION");
|
||||
// We aren't the first
|
||||
found = true;
|
||||
service.getField("B_STATS_VERSION"); // Our identifier :)
|
||||
found = true; // We aren't the first
|
||||
break;
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
}
|
||||
} catch (NoSuchFieldException ignored) { }
|
||||
}
|
||||
// Register our service
|
||||
Bukkit.getServicesManager().register(Metrics.class, this, plugin, ServicePriority.Normal);
|
||||
@ -137,6 +126,7 @@ public class Metrics {
|
||||
startSubmitting();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom chart.
|
||||
@ -154,23 +144,27 @@ public class Metrics {
|
||||
* Starts the Scheduler which submits our data every 30 minutes.
|
||||
*/
|
||||
private void startSubmitting() {
|
||||
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new BasicThreadFactory.Builder().namingPattern("metrics-schedule-pool-%d").daemon(true).build());
|
||||
executorService.scheduleAtFixedRate(() -> {
|
||||
if (!plugin.isEnabled()) {
|
||||
executorService.shutdown();
|
||||
final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!plugin.isEnabled()) { // Plugin was disabled
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
|
||||
* Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
|
||||
*/
|
||||
Bukkit.getScheduler().runTask(plugin, this::submitData);
|
||||
/*
|
||||
* Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
|
||||
* WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
|
||||
* WARNING: Just don't do it!
|
||||
*/
|
||||
}, 5, 30, TimeUnit.MINUTES);
|
||||
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
|
||||
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
|
||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
submitData();
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 1000*60*5, 1000*60*30);
|
||||
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
|
||||
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
|
||||
// WARNING: Just don't do it!
|
||||
}
|
||||
|
||||
/**
|
||||
@ -185,16 +179,13 @@ public class Metrics {
|
||||
String pluginName = plugin.getDescription().getName();
|
||||
String pluginVersion = plugin.getDescription().getVersion();
|
||||
|
||||
// Append the name of the plugin
|
||||
data.put("pluginName", pluginName);
|
||||
// Append the version of the plugin
|
||||
data.put("pluginVersion", pluginVersion);
|
||||
data.put("pluginName", pluginName); // Append the name of the plugin
|
||||
data.put("pluginVersion", pluginVersion); // Append the version of the plugin
|
||||
JSONArray customCharts = new JSONArray();
|
||||
for (CustomChart customChart : charts) {
|
||||
// Add the data of the custom charts
|
||||
JSONObject chart = customChart.getRequestJsonObject();
|
||||
// If the chart is null, we skip it
|
||||
if (chart == null) {
|
||||
if (chart == null) { // If the chart is null, we skip it
|
||||
continue;
|
||||
}
|
||||
customCharts.add(chart);
|
||||
@ -220,11 +211,10 @@ public class Metrics {
|
||||
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
|
||||
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
|
||||
} catch (Exception e) {
|
||||
// Just use the new method if the Reflection failed
|
||||
playerAmount = Bukkit.getOnlinePlayers().size();
|
||||
playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
|
||||
}
|
||||
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
|
||||
String bukkitVersion = Bukkit.getVersion();
|
||||
String bukkitVersion = org.bukkit.Bukkit.getVersion();
|
||||
bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1);
|
||||
|
||||
// OS/Java specific data
|
||||
@ -261,23 +251,22 @@ public class Metrics {
|
||||
// Search for all other bStats Metrics classes to get their plugin data
|
||||
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
|
||||
try {
|
||||
// Our identifier :)
|
||||
service.getField("B_STATS_VERSION");
|
||||
service.getField("B_STATS_VERSION"); // Our identifier :)
|
||||
|
||||
for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(service)) {
|
||||
try {
|
||||
pluginData.add(provider.getService().getMethod("getPluginData").invoke(provider.getProvider()));
|
||||
} catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
|
||||
}
|
||||
}
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
} catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { }
|
||||
}
|
||||
} catch (NoSuchFieldException ignored) { }
|
||||
}
|
||||
|
||||
data.put("plugins", pluginData);
|
||||
|
||||
// Create a new thread for the connection to the bStats server
|
||||
Executors.newSingleThreadExecutor().execute(() -> {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// Send the data
|
||||
sendData(data);
|
||||
@ -287,7 +276,8 @@ public class Metrics {
|
||||
plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -312,11 +302,9 @@ public class Metrics {
|
||||
connection.setRequestMethod("POST");
|
||||
connection.addRequestProperty("Accept", "application/json");
|
||||
connection.addRequestProperty("Connection", "close");
|
||||
// We gzip our request
|
||||
connection.addRequestProperty("Content-Encoding", "gzip");
|
||||
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
|
||||
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
|
||||
// We send our data in JSON format
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
|
||||
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
|
||||
|
||||
// Send data
|
||||
@ -326,8 +314,7 @@ public class Metrics {
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
|
||||
// We don't care about the response - Just send our data :)
|
||||
connection.getInputStream().close();
|
||||
connection.getInputStream().close(); // We don't care about the response - Just send our data :)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -353,9 +340,7 @@ public class Metrics {
|
||||
*/
|
||||
public static abstract class CustomChart {
|
||||
|
||||
/**
|
||||
* The id of the chart
|
||||
*/
|
||||
// The id of the chart
|
||||
final String chartId;
|
||||
|
||||
/**
|
||||
|
@ -8,8 +8,4 @@ import net.md_5.bungee.api.plugin.Plugin;
|
||||
*/
|
||||
public class TabooLibBungee extends Plugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
super.onEnable();
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.commands.internal.BaseMainCommand;
|
||||
import me.skymc.taboolib.commands.internal.BaseSubCommand;
|
||||
import me.skymc.taboolib.commands.internal.TCommand;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandArgument;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandRegister;
|
||||
import me.skymc.taboolib.string.ArrayUtils;
|
||||
@ -18,6 +19,7 @@ import org.bukkit.event.server.ServerCommandEvent;
|
||||
* @Author sky
|
||||
* @Since 2018-07-04 21:32
|
||||
*/
|
||||
@TCommand(name = "taboolibexecute")
|
||||
public class TabooLibExecuteCommand extends BaseMainCommand {
|
||||
|
||||
@Override
|
||||
|
@ -5,6 +5,7 @@ import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.commands.internal.BaseMainCommand;
|
||||
import me.skymc.taboolib.commands.internal.BaseSubCommand;
|
||||
import me.skymc.taboolib.commands.internal.TCommand;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandArgument;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandRegister;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandType;
|
||||
@ -33,6 +34,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* @Author sky
|
||||
* @Since 2018-05-09 21:38
|
||||
*/
|
||||
@TCommand(name = "taboolib")
|
||||
public class TabooLibMainCommand extends BaseMainCommand {
|
||||
|
||||
@Override
|
||||
|
@ -1,10 +1,23 @@
|
||||
package me.skymc.taboolib.commands.internal;
|
||||
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.PluginEnableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-23 2:43
|
||||
*/
|
||||
public class TBaseCommand {
|
||||
@TListener(register = "onLoad")
|
||||
public class TBaseCommand implements Listener {
|
||||
|
||||
void onLoad() {
|
||||
registerCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
* 向服务端注册 BaseMainCommand 类
|
||||
@ -16,4 +29,43 @@ public class TBaseCommand {
|
||||
public static BaseMainCommand registerCommand(String command, BaseMainCommand baseMainCommand) {
|
||||
return BaseMainCommand.createCommandExecutor(command, baseMainCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册所有插件的所有 TCommand 命令
|
||||
*/
|
||||
public static void registerCommands() {
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
try {
|
||||
registerCommand(plugin);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册插件的所有 TCommand 命令
|
||||
*
|
||||
* @param plugin 插件
|
||||
*/
|
||||
public static void registerCommand(Plugin plugin) {
|
||||
for (Class pluginClass : FileUtils.getClasses(plugin)) {
|
||||
if (BaseMainCommand.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TCommand.class)) {
|
||||
TCommand tCommand = (TCommand) pluginClass.getAnnotation(TCommand.class);
|
||||
try {
|
||||
registerCommand(tCommand.name(), (BaseMainCommand) pluginClass.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEnable(PluginEnableEvent e) {
|
||||
try {
|
||||
registerCommand(e.getPlugin());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package me.skymc.taboolib.commands.internal;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-23 20:34
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TCommand {
|
||||
|
||||
String name();
|
||||
|
||||
}
|
@ -4,12 +4,12 @@ import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleLoader;
|
||||
import me.skymc.taboolib.commands.internal.BaseMainCommand;
|
||||
import me.skymc.taboolib.commands.internal.BaseSubCommand;
|
||||
import me.skymc.taboolib.commands.internal.TCommand;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandArgument;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandRegister;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@ -22,6 +22,7 @@ import java.util.stream.IntStream;
|
||||
* @author sky
|
||||
* @since 2018-04-22 14:36:28
|
||||
*/
|
||||
@TCommand(name = "tabooliblocale")
|
||||
public class TabooLibLocaleCommand extends BaseMainCommand {
|
||||
|
||||
@Override
|
||||
|
@ -5,6 +5,7 @@ import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.commands.internal.BaseMainCommand;
|
||||
import me.skymc.taboolib.commands.internal.BaseSubCommand;
|
||||
import me.skymc.taboolib.commands.internal.ISubCommand;
|
||||
import me.skymc.taboolib.commands.internal.TCommand;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandArgument;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandRegister;
|
||||
import me.skymc.taboolib.plugin.PluginLoadState;
|
||||
@ -25,6 +26,7 @@ import java.util.stream.Collectors;
|
||||
* @Author sky
|
||||
* @Since 2018-05-07 20:14
|
||||
*/
|
||||
@TCommand(name = "taboolibplugin")
|
||||
public class TabooLibPluginCommand extends BaseMainCommand {
|
||||
|
||||
@Override
|
||||
|
@ -3,6 +3,7 @@ package me.skymc.taboolib.commands.taboolib.listener;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.inventory.InventoryUtil;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -23,6 +24,7 @@ import java.util.List;
|
||||
* @author sky
|
||||
* @since 2018年2月4日 下午4:35:00
|
||||
*/
|
||||
@TListener
|
||||
public class ListenerItemListCommand implements Listener {
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,7 @@ package me.skymc.taboolib.commands.taboolib.listener;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.inventory.InventoryUtil;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
@ -24,6 +25,7 @@ import java.util.stream.Collectors;
|
||||
* @author sky
|
||||
* @since 2018年2月4日 下午4:35:00
|
||||
*/
|
||||
@TListener
|
||||
public class ListenerSoundsCommand implements Listener {
|
||||
|
||||
public static void openInventory(Player player, int page, String search) {
|
||||
|
@ -28,8 +28,7 @@ public class GlobalDataManager {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
Object obj = Main.getConnection().getValueLast(Main.getTablePrefix() + "_plugindata", "name", name, "variable");
|
||||
return obj != null ? "null".equals(obj.toString()) ? defaultVariable : obj.toString() : defaultVariable;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return data.contains(name) ? data.getString(name) : defaultVariable;
|
||||
}
|
||||
}
|
||||
@ -45,8 +44,7 @@ public class GlobalDataManager {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
SQLVariable variable = SQLMethod.getSQLVariable(name);
|
||||
return variable == null ? defaultVariable : "null".equals(variable.getVariable()) ? defaultVariable : variable.getVariable();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return getVariable(name, defaultVariable);
|
||||
}
|
||||
}
|
||||
@ -60,8 +58,7 @@ public class GlobalDataManager {
|
||||
public static void setVariable(String name, String variable) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
Main.getConnection().intoValue(Main.getTablePrefix() + "_plugindata", name, variable == null ? "null" : variable, TabooLib.getServerUID());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
data.set(name, variable);
|
||||
}
|
||||
}
|
||||
@ -77,8 +74,7 @@ public class GlobalDataManager {
|
||||
SQLVariable _variable = SQLMethod.contains(name) ? SQLMethod.getSQLVariable(name).setVariable(variable == null ? "null" : variable) : SQLMethod.addSQLVariable(name, variable == null ? "null" : variable);
|
||||
// 更新数据
|
||||
SQLMethod.uploadVariable(_variable, true);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
setVariable(name, variable);
|
||||
}
|
||||
}
|
||||
@ -91,8 +87,7 @@ public class GlobalDataManager {
|
||||
public static boolean contains(String name) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
return getVariable(name, null) != null;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return data.contains(name);
|
||||
}
|
||||
}
|
||||
@ -106,8 +101,7 @@ public class GlobalDataManager {
|
||||
public static boolean containsAsynchronous(String name) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
return getVariableAsynchronous(name, null) != null;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return contains(name);
|
||||
}
|
||||
}
|
||||
@ -140,8 +134,7 @@ public class GlobalDataManager {
|
||||
map.put(_map.get("name").toString(), _map.get("variable").toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (String name : data.getConfigurationSection("").getKeys(false)) {
|
||||
map.put(name, data.getString(name));
|
||||
}
|
||||
@ -163,8 +156,7 @@ public class GlobalDataManager {
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return getVariables();
|
||||
}
|
||||
}
|
||||
@ -173,7 +165,6 @@ public class GlobalDataManager {
|
||||
* 数据库变量
|
||||
*
|
||||
* @author sky
|
||||
*
|
||||
*/
|
||||
public static class SQLVariable {
|
||||
|
||||
@ -209,7 +200,6 @@ public class GlobalDataManager {
|
||||
* 数据库方法
|
||||
*
|
||||
* @author sky
|
||||
*
|
||||
*/
|
||||
public static class SQLMethod {
|
||||
|
||||
@ -294,8 +284,7 @@ public class GlobalDataManager {
|
||||
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
@ -327,8 +316,7 @@ public class GlobalDataManager {
|
||||
if ("null".equals(value.get("variable"))) {
|
||||
// 删除变量
|
||||
variables.remove(name);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// 更新变量
|
||||
variables.get(name).setVariable(value.get("variable").toString());
|
||||
}
|
||||
@ -338,8 +326,7 @@ public class GlobalDataManager {
|
||||
else if (!"null".equals(value.get("variable"))) {
|
||||
variables.put(value.get("name").toString(), new SQLVariable(value.get("name").toString(), value.get("variable").toString(), value.get("upgrade").toString()));
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
// 移除
|
||||
variables.remove(name);
|
||||
// 提示
|
||||
@ -351,8 +338,7 @@ public class GlobalDataManager {
|
||||
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
@ -393,15 +379,13 @@ public class GlobalDataManager {
|
||||
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动数据库储存方法
|
||||
*
|
||||
*/
|
||||
public static void startSQLMethod() {
|
||||
long time = System.currentTimeMillis();
|
||||
@ -422,7 +406,6 @@ public class GlobalDataManager {
|
||||
|
||||
/**
|
||||
* 结束数据库储存方法
|
||||
*
|
||||
*/
|
||||
public static void cancelSQLMethod() {
|
||||
// 上传数据
|
||||
|
@ -7,6 +7,7 @@ import me.skymc.taboolib.events.PlayerLoadedEvent;
|
||||
import me.skymc.taboolib.exception.PlayerOfflineException;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@ -22,6 +23,7 @@ import java.io.File;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@TListener
|
||||
public class PlayerDataManager implements Listener {
|
||||
|
||||
private static final ConcurrentHashMap<String, FileConfiguration> PLAYER_DATA = new ConcurrentHashMap<>();
|
||||
|
@ -5,7 +5,8 @@ import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.exception.PluginNotFoundException;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
@ -17,6 +18,10 @@ import org.bukkit.event.entity.EntitySpawnEvent;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
@TListener(condition = "check")
|
||||
public class EntityUtils implements Listener {
|
||||
|
||||
private static Entity lastSpawnedEntity = null;
|
||||
@ -25,6 +30,10 @@ public class EntityUtils implements Listener {
|
||||
return lastSpawnedEntity;
|
||||
}
|
||||
|
||||
public static boolean check() {
|
||||
return TabooLib.getVerint() > 10700;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 UUID 获取生物
|
||||
*
|
||||
@ -42,7 +51,7 @@ public class EntityUtils implements Listener {
|
||||
* @param world
|
||||
* @return
|
||||
*/
|
||||
public static Entity getEntityWithUUID_World(UUID u, World world) {
|
||||
public static Entity getEntityWithUUID(UUID u, World world) {
|
||||
return world.getLivingEntities().stream().filter(e -> e.getUniqueId().equals(u)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
|
@ -1,97 +0,0 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
@Deprecated
|
||||
public class CopyUtils {
|
||||
|
||||
public static long Copy(File file1, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file1.exists()) {
|
||||
file1.createNewFile();
|
||||
}
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileInputStream in = new FileInputStream(file1);
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static long Copy(FileInputStream in, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,9 @@ import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
/**
|
||||
* @author Unknown
|
||||
*/
|
||||
public class EncodeUtils {
|
||||
|
||||
/**
|
||||
@ -53,7 +56,7 @@ public class EncodeUtils {
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
File[] fileList = null;
|
||||
File[] fileList;
|
||||
if (filter == null) {
|
||||
fileList = file.listFiles();
|
||||
} else {
|
||||
@ -63,10 +66,8 @@ public class EncodeUtils {
|
||||
convert(f, fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
} else {
|
||||
if (filter == null
|
||||
|| filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file,
|
||||
fromCharsetName);
|
||||
if (filter == null || filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file, fromCharsetName);
|
||||
saveFile2Charset(file, toCharsetName, fileContent);
|
||||
}
|
||||
}
|
||||
@ -85,8 +86,7 @@ public class EncodeUtils {
|
||||
throw new UnsupportedCharsetException(fromCharsetName);
|
||||
}
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream,
|
||||
fromCharsetName);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream, fromCharsetName);
|
||||
char[] chs = new char[(int) file.length()];
|
||||
reader.read(chs);
|
||||
String str = new String(chs).trim();
|
||||
@ -110,7 +110,5 @@ public class EncodeUtils {
|
||||
OutputStreamWriter outWrite = new OutputStreamWriter(outputStream, toCharsetName);
|
||||
outWrite.write(content);
|
||||
outWrite.close();
|
||||
|
||||
System.out.println("[Encodeing...] 更改文件: " + file.getPath());
|
||||
}
|
||||
}
|
||||
|
@ -2,22 +2,32 @@ package me.skymc.taboolib.fileutils;
|
||||
|
||||
import ch.njol.util.Closeable;
|
||||
import com.ilummc.tlib.util.IO;
|
||||
import javafx.print.PageLayout;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* 获取本地 IP 地址
|
||||
*
|
||||
* @return {@link String}
|
||||
*/
|
||||
public static String ip() {
|
||||
URL url;
|
||||
URLConnection con;
|
||||
@ -49,10 +59,77 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件所有类
|
||||
*
|
||||
* @return {@link List<Class>}
|
||||
*/
|
||||
public static List<Class> getClasses(Class<?> obj) {
|
||||
List<Class> classes = new ArrayList<>();
|
||||
URL url = getCaller(obj).getProtectionDomain().getCodeSource().getLocation();
|
||||
try {
|
||||
File src;
|
||||
try {
|
||||
src = new File(url.toURI());
|
||||
} catch (URISyntaxException e) {
|
||||
src = new File(url.getPath());
|
||||
}
|
||||
new JarFile(src).stream().filter(entry -> entry.getName().endsWith(".class")).forEach(entry -> {
|
||||
String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6);
|
||||
try {
|
||||
classes.add(Class.forName(className, false, obj.getClassLoader()));
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
});
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件所有类
|
||||
*
|
||||
* @return {@link List<Class>}
|
||||
*/
|
||||
public static List<Class> getClasses(Plugin plugin) {
|
||||
List<Class> classes = new ArrayList<>();
|
||||
URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
try {
|
||||
File src;
|
||||
try {
|
||||
src = new File(url.toURI());
|
||||
} catch (URISyntaxException e) {
|
||||
src = new File(url.getPath());
|
||||
}
|
||||
new JarFile(src).stream().filter(entry -> entry.getName().endsWith(".class")).forEach(entry -> {
|
||||
String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6);
|
||||
try {
|
||||
classes.add(Class.forName(className, false, plugin.getClass().getClassLoader()));
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
});
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取资源文件
|
||||
*
|
||||
* @param filename 文件名
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
public static InputStream getResource(String filename) {
|
||||
return getResource(Main.getInst(), filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件资源文件
|
||||
*
|
||||
* @param plugin 插件
|
||||
* @param filename 文件名
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
public static InputStream getResource(Plugin plugin, String filename) {
|
||||
try {
|
||||
URL url = plugin.getClass().getClassLoader().getResource(filename);
|
||||
@ -68,6 +145,12 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入文件
|
||||
*
|
||||
* @param inputStream 输入流
|
||||
* @param file 文件
|
||||
*/
|
||||
public static void inputStreamToFile(InputStream inputStream, File file) {
|
||||
try {
|
||||
String text = new String(IO.readFully(inputStream), Charset.forName("utf-8"));
|
||||
@ -93,15 +176,59 @@ public class FileUtils {
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测文件并创建(目录)
|
||||
*
|
||||
* @param file 文件
|
||||
*/
|
||||
public static void createNewFileAndPath(File file) {
|
||||
if (!file.exists()) {
|
||||
String filePath = file.getPath();
|
||||
int index = filePath.lastIndexOf(File.separator);
|
||||
String folderPath;
|
||||
File folder;
|
||||
if ((index >= 0) && (!(folder = new File(filePath.substring(0, index))).exists())) {
|
||||
folder.mkdirs();
|
||||
}
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取目录
|
||||
*
|
||||
* @param path 目录文件
|
||||
* @return
|
||||
*/
|
||||
public static File folder(File path) {
|
||||
if (!path.exists()) {
|
||||
path.mkdirs();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取目录
|
||||
*
|
||||
* @param path 目录地址
|
||||
* @return
|
||||
*/
|
||||
public static File folder(String path) {
|
||||
return folder(new File(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @param Path 目录
|
||||
* @param path 目录
|
||||
* @param filePath 地址
|
||||
* @return
|
||||
*/
|
||||
public static File file(File Path, String filePath) {
|
||||
return createNewFile(new File(Path, filePath));
|
||||
public static File file(File path, String filePath) {
|
||||
return createNewFile(new File(path, filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -136,29 +263,29 @@ public class FileUtils {
|
||||
/**
|
||||
* 复制文件夹
|
||||
*
|
||||
* @param file1 文件1
|
||||
* @param file2 文件2
|
||||
* @param originFileName 文件1
|
||||
* @param targetFileName 文件2
|
||||
*/
|
||||
public static void copyAllFile(String file1, String file2) {
|
||||
File _file1 = new File(file1);
|
||||
File _file2 = new File(file2);
|
||||
if (!_file2.exists()) {
|
||||
if (!_file1.isDirectory()) {
|
||||
createNewFile(_file2);
|
||||
public static void copyAllFile(String originFileName, String targetFileName) {
|
||||
File originFile = new File(originFileName);
|
||||
File targetFile = new File(targetFileName);
|
||||
if (!targetFile.exists()) {
|
||||
if (!originFile.isDirectory()) {
|
||||
createNewFile(targetFile);
|
||||
} else {
|
||||
_file2.mkdirs();
|
||||
targetFile.mkdirs();
|
||||
}
|
||||
}
|
||||
if (_file1.isDirectory()) {
|
||||
for (File file : Objects.requireNonNull(_file1.listFiles())) {
|
||||
if (originFile.isDirectory()) {
|
||||
for (File file : Objects.requireNonNull(originFile.listFiles())) {
|
||||
if (file.isDirectory()) {
|
||||
copyAllFile(file.getAbsolutePath(), file2 + "/" + file.getName());
|
||||
copyAllFile(file.getAbsolutePath(), targetFileName + "/" + file.getName());
|
||||
} else {
|
||||
fileChannelCopy(file, new File(file2 + "/" + file.getName()));
|
||||
fileChannelCopy(file, new File(targetFileName + "/" + file.getName()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fileChannelCopy(_file1, _file2);
|
||||
fileChannelCopy(originFile, targetFile);
|
||||
}
|
||||
}
|
||||
|
||||
@ -313,6 +440,17 @@ public class FileUtils {
|
||||
download(downloadURL, new File(saveDir, filename));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void close(Closeable closeable) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static byte[] read(InputStream in) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
@ -326,12 +464,17 @@ public class FileUtils {
|
||||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
public static void close(Closeable closeable) {
|
||||
// *********************************
|
||||
//
|
||||
// Private Methods
|
||||
//
|
||||
// *********************************
|
||||
|
||||
private static Class getCaller(Class<?> obj) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
return Class.forName(Thread.currentThread().getStackTrace()[3].getClassName(), false, obj.getClassLoader());
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.other.DateUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
||||
@Deprecated
|
||||
public class LogUtils {
|
||||
|
||||
public static void Log(String s, String s2) {
|
||||
try {
|
||||
File file = new File(Main.getInst().getDataFolder(), s2 + ".txt");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write(s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
} catch (Exception e) {
|
||||
Main.getInst().getLogger().warning(s2 + ":" + s);
|
||||
}
|
||||
}
|
||||
|
||||
public static void newLog(Plugin main, String s, String s2) {
|
||||
try {
|
||||
File file = new File(main.getDataFolder(), s2 + ".txt");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write("[" + DateUtils.CH_ALL.format(System.currentTimeMillis()) + "]" + s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
} catch (Exception e) {
|
||||
Main.getInst().getLogger().warning(s2 + ":" + s);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
153
src/main/java/me/skymc/taboolib/fileutils/TLogs.java
Normal file
153
src/main/java/me/skymc/taboolib/fileutils/TLogs.java
Normal file
@ -0,0 +1,153 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.commands.internal.BaseMainCommand;
|
||||
import me.skymc.taboolib.commands.internal.BaseSubCommand;
|
||||
import me.skymc.taboolib.commands.internal.TCommand;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandArgument;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandRegister;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
@TCommand(name = "tabooliblogs")
|
||||
public class TLogs extends BaseMainCommand {
|
||||
|
||||
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
|
||||
|
||||
@Override
|
||||
public String getCommandTitle() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.COMMAND-TITLE");
|
||||
}
|
||||
|
||||
@CommandRegister(priority = 0)
|
||||
BaseSubCommand info = new BaseSubCommand() {
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "info";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.INFO.DESCRIPTION");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandArgument[] getArguments() {
|
||||
return new CommandArgument[] {
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.INFO.ARGUMENTS.0")),
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.INFO.ARGUMENTS.1"))
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
info(args[0], args[1]);
|
||||
if (sender instanceof Player) {
|
||||
TLocale.sendTo(sender, "COMMANDS.TLOGS.INFO.SUCCESS");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@CommandRegister(priority = 1)
|
||||
BaseSubCommand error = new BaseSubCommand() {
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "error";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.ERROR.DESCRIPTION");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandArgument[] getArguments() {
|
||||
return new CommandArgument[] {
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.ERROR.ARGUMENTS.0")),
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.ERROR.ARGUMENTS.1"))
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
info(args[0], args[1]);
|
||||
if (sender instanceof Player) {
|
||||
TLocale.sendTo(sender, "COMMANDS.TLOGS.ERROR.SUCCESS");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@CommandRegister(priority = 2)
|
||||
BaseSubCommand warning = new BaseSubCommand() {
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "warning";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.WARNING.DESCRIPTION");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandArgument[] getArguments() {
|
||||
return new CommandArgument[] {
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.WARNING.ARGUMENTS.0")),
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.WARNING.ARGUMENTS.1"))
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
info(args[0], args[1]);
|
||||
if (sender instanceof Player) {
|
||||
TLocale.sendTo(sender, "COMMANDS.TLOGS.WARNING.SUCCESS");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static void info(String filePath, String text) {
|
||||
info(new File(!filePath.contains(".") ? filePath + ".txt" : filePath), text);
|
||||
}
|
||||
|
||||
public static void info(File file, String text) {
|
||||
write(file, "[{0} INFO]: {1}\n", text);
|
||||
}
|
||||
|
||||
public static void error(String filePath, String text) {
|
||||
info(new File(!filePath.contains(".") ? filePath + ".txt" : filePath), text);
|
||||
}
|
||||
|
||||
public static void error(File file, String text) {
|
||||
write(file, "[{0} ERROR]: {1}\n", text);
|
||||
}
|
||||
|
||||
public static void warning(String filePath, String text) {
|
||||
info(new File(!filePath.contains(".") ? filePath + ".txt" : filePath), text);
|
||||
}
|
||||
|
||||
public static void warning(File file, String text) {
|
||||
write(file, "[{0} WARNING]: {1}\n", text);
|
||||
}
|
||||
|
||||
public static void write(File file, String format, String text) {
|
||||
Bukkit.getScheduler().runTask(TabooLib.instance(), () -> {
|
||||
FileUtils.createNewFileAndPath(file);
|
||||
try (FileWriter writer = new FileWriter(file, true)) {
|
||||
writer.write(Strings.replaceWithOrder(format, dateFormat.format(System.currentTimeMillis()), text));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -7,6 +7,9 @@ import org.bukkit.inventory.ItemStack;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class InventoryUtil {
|
||||
|
||||
public final static LinkedList<Integer> SLOT_OF_CENTENTS = new LinkedList<>(Arrays.asList(
|
||||
|
@ -34,6 +34,9 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class ItemUtils {
|
||||
|
||||
private static FileConfiguration itemdir = null;
|
||||
@ -418,11 +421,11 @@ public class ItemUtils {
|
||||
meta.setLore(asString(section.getStringList("lore"), papiPlayer));
|
||||
}
|
||||
// 附魔
|
||||
if (section.contains("enchants")) {
|
||||
for (String preEnchant : section.getConfigurationSection("enchants").getKeys(false)) {
|
||||
if (section.contains("enchant")) {
|
||||
for (String preEnchant : section.getConfigurationSection("enchant").getKeys(false)) {
|
||||
Enchantment enchant = asEnchantment(preEnchant);
|
||||
if (enchant != null) {
|
||||
meta.addEnchant(enchant, section.getInt("enchants." + preEnchant), true);
|
||||
meta.addEnchant(enchant, section.getInt("enchant." + preEnchant), true);
|
||||
} else {
|
||||
TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-ENCHANTS", preEnchant);
|
||||
}
|
||||
|
@ -0,0 +1,176 @@
|
||||
package me.skymc.taboolib.inventory.builder;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.string.ArrayUtils;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.banner.Pattern;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.*;
|
||||
import org.bukkit.potion.PotionData;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 11:37
|
||||
* @BuilderVersion 1.0
|
||||
*/
|
||||
public class ItemBuilder {
|
||||
|
||||
private ItemStack itemStack;
|
||||
private ItemMeta itemMeta;
|
||||
|
||||
public ItemBuilder(Material material) {
|
||||
this(material, 1, 0);
|
||||
}
|
||||
|
||||
public ItemBuilder(Material material, int amount) {
|
||||
this(material, amount, 0);
|
||||
}
|
||||
|
||||
public ItemBuilder(Material material, int amount, int damage) {
|
||||
itemStack = new ItemStack(material, amount, (short) damage);
|
||||
itemMeta = itemStack.getItemMeta();
|
||||
}
|
||||
|
||||
public ItemBuilder material(int id) {
|
||||
itemStack.setType(Material.getMaterial(id));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder material(String material) {
|
||||
itemStack.setType(Material.getMaterial(material));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder material(Material material) {
|
||||
itemStack.setType(material);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder amount(int amount) {
|
||||
itemStack.setAmount(amount);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder damage(int damage) {
|
||||
itemStack.setDurability((short) damage);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder name(String name) {
|
||||
itemMeta.setDisplayName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder lore(List<String> lore) {
|
||||
itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder lore(String... lore) {
|
||||
itemMeta.setLore(ArrayUtils.asList(lore));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder flags(ItemFlag... flags) {
|
||||
itemMeta.addItemFlags(flags);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder enchant(Enchantment enchantment, int level) {
|
||||
return enchant(enchantment, level, false);
|
||||
}
|
||||
|
||||
public ItemBuilder enchant(Enchantment enchantment, int level, boolean bypass) {
|
||||
itemMeta.addEnchant(enchantment, level, bypass);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder shiny() {
|
||||
return enchant(Enchantment.LURE, 1, true).flags(ItemFlag.values());
|
||||
}
|
||||
|
||||
public ItemBuilder color(Color color) {
|
||||
if (itemMeta instanceof LeatherArmorMeta) {
|
||||
((LeatherArmorMeta) itemMeta).setColor(color);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder banner(Pattern... patterns) {
|
||||
if (itemMeta instanceof BannerMeta) {
|
||||
Arrays.stream(patterns).forEach(pattern -> ((BannerMeta) itemMeta).addPattern(pattern));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder potionData(PotionData potionData) {
|
||||
if (itemMeta instanceof PotionMeta) {
|
||||
((PotionMeta) itemMeta).setBasePotionData(potionData);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder potionColor(Color color) {
|
||||
if (itemMeta instanceof PotionMeta) {
|
||||
((PotionMeta) itemMeta).setColor(color);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder potionEffect(PotionEffect potionEffect) {
|
||||
if (itemMeta instanceof PotionMeta) {
|
||||
((PotionMeta) itemMeta).addCustomEffect(potionEffect, false);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder potionEffect(PotionEffect potionEffect, boolean b) {
|
||||
if (itemMeta instanceof PotionMeta) {
|
||||
((PotionMeta) itemMeta).addCustomEffect(potionEffect, b);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder eggType(EntityType entityType) {
|
||||
if (itemMeta instanceof SpawnEggMeta) {
|
||||
((SpawnEggMeta) itemMeta).setSpawnedType(entityType);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder unbreakable(boolean value) {
|
||||
if (TabooLib.getVersionNumber() >= 12000) {
|
||||
itemMeta.setUnbreakable(value);
|
||||
} else {
|
||||
itemMeta.spigot().setUnbreakable(value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder colored() {
|
||||
if (itemMeta.hasDisplayName()) {
|
||||
itemMeta.setDisplayName(TLocale.Translate.setColored(itemMeta.getDisplayName()));
|
||||
}
|
||||
if (itemMeta.hasLore()) {
|
||||
itemMeta.setLore(TLocale.Translate.setColored(itemMeta.getLore()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemStack build() {
|
||||
ItemStack buildItem = itemStack.clone();
|
||||
if (itemMeta != null) {
|
||||
buildItem.setItemMeta(itemMeta.clone());
|
||||
}
|
||||
return buildItem;
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package me.skymc.taboolib.inventory.builder;
|
||||
|
||||
import me.skymc.taboolib.inventory.builder.menu.MenuBuilderCallable;
|
||||
import me.skymc.taboolib.inventory.builder.menu.MenuBuilderHolder;
|
||||
import me.skymc.taboolib.inventory.builder.menu.MenuBuilderItem;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 13:40
|
||||
* @BuilderVersion 1.0
|
||||
*/
|
||||
public class MenuBuilder {
|
||||
|
||||
private boolean lock;
|
||||
private String name;
|
||||
private int rows = 9;
|
||||
|
||||
private HashMap<Integer, MenuBuilderItem> items = new HashMap<>();
|
||||
|
||||
public MenuBuilder() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
public MenuBuilder(boolean lock) {
|
||||
this.lock = lock;
|
||||
}
|
||||
|
||||
public MenuBuilder lock(boolean lock) {
|
||||
this.lock = lock;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder rows(int rows) {
|
||||
this.rows = rows * 9;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder item(ItemStack itemStack, int... slots) {
|
||||
Arrays.stream(slots).forEach(slot -> items.put(slot, new MenuBuilderItem(itemStack, null)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder item(ItemStack itemStack, MenuBuilderCallable callable, int... slots) {
|
||||
Arrays.stream(slots).forEach(slot -> items.put(slot, new MenuBuilderItem(itemStack, callable)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Inventory build() {
|
||||
Inventory inventory = Bukkit.createInventory(new MenuBuilderHolder(lock, items), rows, name);
|
||||
items.forEach((key, value) -> inventory.setItem(key, value.getItemStack()));
|
||||
return inventory;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package me.skymc.taboolib.inventory.builder.menu;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 15:41
|
||||
*/
|
||||
public interface MenuBuilderCallable {
|
||||
|
||||
void call(MenuBuilderEvent event);
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package me.skymc.taboolib.inventory.builder.menu;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 15:44
|
||||
*/
|
||||
public class MenuBuilderEvent {
|
||||
|
||||
private final InventoryClickEvent parentEvent;
|
||||
private final Player player;
|
||||
private final ItemStack clickItem;
|
||||
private final int clickSlot;
|
||||
|
||||
public MenuBuilderEvent(InventoryClickEvent parentEvent, Player player, ItemStack clickItem, int clickSlot) {
|
||||
this.parentEvent = parentEvent;
|
||||
this.player = player;
|
||||
this.clickItem = clickItem;
|
||||
this.clickSlot = clickSlot;
|
||||
}
|
||||
|
||||
public InventoryClickEvent getParentEvent() {
|
||||
return parentEvent;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public ItemStack getClickItem() {
|
||||
return clickItem;
|
||||
}
|
||||
|
||||
public int getClickSlot() {
|
||||
return clickSlot;
|
||||
}
|
||||
|
||||
public void setCancelled(boolean canceled) {
|
||||
parentEvent.setCancelled(canceled);
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return parentEvent.isCancelled();
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package me.skymc.taboolib.inventory.builder.menu;
|
||||
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @Since 2018-08-22 13:40
|
||||
*/
|
||||
public class MenuBuilderHolder implements InventoryHolder {
|
||||
|
||||
private final boolean lock;
|
||||
private final HashMap<Integer, MenuBuilderItem> items;
|
||||
|
||||
public MenuBuilderHolder(boolean lock, HashMap<Integer, MenuBuilderItem> items) {
|
||||
this.lock = lock;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public boolean isLock() {
|
||||
return lock;
|
||||
}
|
||||
|
||||
public HashMap<Integer, MenuBuilderItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package me.skymc.taboolib.inventory.builder.menu;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 15:36
|
||||
*/
|
||||
public class MenuBuilderItem {
|
||||
|
||||
private final ItemStack itemStack;
|
||||
private final MenuBuilderCallable callable;
|
||||
|
||||
public MenuBuilderItem(ItemStack itemStack, MenuBuilderCallable callable) {
|
||||
this.itemStack = itemStack;
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
public MenuBuilderCallable getCallable() {
|
||||
return callable;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package me.skymc.taboolib.inventory.builder.menu;
|
||||
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
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.InventoryAction;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 13:40
|
||||
*/
|
||||
@TListener
|
||||
public class MenuBuilderListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onClick(InventoryClickEvent e) {
|
||||
if (!(e.getInventory().getHolder() instanceof MenuBuilderHolder)) {
|
||||
return;
|
||||
}
|
||||
MenuBuilderHolder holder = (MenuBuilderHolder) e.getInventory().getHolder();
|
||||
if (holder.isLock() || e.getAction() == InventoryAction.COLLECT_TO_CURSOR) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
Optional.ofNullable(holder.getItems().get(e.getRawSlot())).ifPresent(item -> {
|
||||
if (item.getCallable() != null) {
|
||||
item.getCallable().call(new MenuBuilderEvent(e, (Player) e.getWhoClicked(), e.getCurrentItem(), e.getRawSlot()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package me.skymc.taboolib.javascript;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
@Deprecated
|
||||
public class JavaScriptUtils {
|
||||
|
||||
private static ScriptEngineManager manager = new ScriptEngineManager();
|
||||
|
||||
public static ScriptEngineManager getScriptManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public static void invokeJavaScript(File jsFile, String method, Object... o) {
|
||||
ScriptEngine engine = manager.getEngineByName("javascript");
|
||||
try {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Thread.currentThread().setContextClassLoader(Main.class.getClassLoader());
|
||||
|
||||
FileReader reader = new FileReader(jsFile);
|
||||
engine.eval(reader);
|
||||
|
||||
// TODO run
|
||||
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object JavaScriptInterface(String jsFile, Object... o) {
|
||||
|
||||
ScriptEngine engine = manager.getEngineByName("javascript");
|
||||
try {
|
||||
FileReader reader = new FileReader(jsFile);
|
||||
engine.eval(reader);
|
||||
|
||||
if (engine instanceof Invocable) {
|
||||
return ((Invocable) engine).invokeFunction("main", o);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
} catch (NoSuchMethodException | IOException | ScriptException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void JavaScriptExecute(String jsFile, Object... o) {
|
||||
|
||||
ScriptEngine engine = manager.getEngineByName("javascript");
|
||||
try {
|
||||
FileReader reader = new FileReader(jsFile);
|
||||
engine.eval(reader);
|
||||
|
||||
if (engine instanceof Invocable) {
|
||||
((Invocable) engine).invokeFunction("main", o);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
} catch (NoSuchMethodException | IOException | ScriptException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package me.skymc.taboolib.javascript;
|
||||
|
||||
import com.ilummc.tlib.logger.TLogger;
|
||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-06-02 22:48
|
||||
*/
|
||||
public class ScriptHandler {
|
||||
|
||||
private static ScriptEngine scriptEngine;
|
||||
private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
|
||||
private static FileConfiguration scriptsFile;
|
||||
|
||||
public static void inst() {
|
||||
try {
|
||||
NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null);
|
||||
scriptEngine = Objects.requireNonNull(factory).getScriptEngine("-doe", "--global-per-engine");
|
||||
} catch (Exception ignored) {
|
||||
scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
|
||||
}
|
||||
}
|
||||
|
||||
public static CompiledScript compile(String script) {
|
||||
try {
|
||||
Compilable compilable = (Compilable) scriptEngine;
|
||||
return compilable.compile(script);
|
||||
} catch (Exception e) {
|
||||
TLogger.getGlobalLogger().info("§4JavaScript §c" + script + "§4 Compile Failed: §c" + e.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static ScriptEngine getScriptEngine() {
|
||||
return scriptEngine;
|
||||
}
|
||||
|
||||
public static ScriptEngineManager getScriptEngineManager() {
|
||||
return scriptEngineManager;
|
||||
}
|
||||
}
|
@ -18,18 +18,14 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
@Deprecated
|
||||
public class JavaShell {
|
||||
|
||||
private static String paths = "";
|
||||
|
||||
private static File javaShellFolder;
|
||||
|
||||
private static File scriptFolder;
|
||||
|
||||
private static File cacheFolder;
|
||||
|
||||
private static File libFolder;
|
||||
|
||||
private static HashMap<String, Class<?>> shells = new HashMap<>();
|
||||
|
||||
public static String getPaths() {
|
||||
|
@ -11,6 +11,7 @@ import pw.yumc.Yum.events.PluginNetworkEvent;
|
||||
* @author sky
|
||||
* @since 2018年2月23日 下午11:10:03
|
||||
*/
|
||||
@TListener(depend = "YUM")
|
||||
public class ListenerNetWork implements Listener {
|
||||
|
||||
public static final String GG = "本监听只是为了防止本插件的更新检测被 YUM 插件阻止,别无它用。";
|
||||
|
@ -13,6 +13,7 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.server.ServerCommandEvent;
|
||||
|
||||
@TListener
|
||||
public class ListenerPlayerCommand implements Listener {
|
||||
|
||||
@EventHandler
|
||||
|
@ -13,6 +13,7 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
@TListener
|
||||
public class ListenerPlayerJoinAndQuit implements Listener {
|
||||
|
||||
@EventHandler
|
||||
|
@ -14,6 +14,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@TListener
|
||||
public class ListenerPlayerJump implements Listener {
|
||||
|
||||
public HashMap<Player, Long> cooldown = new HashMap<>();
|
||||
|
@ -14,6 +14,7 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@TListener
|
||||
public class ListenerPluginDisable implements Listener {
|
||||
|
||||
@EventHandler
|
||||
|
44
src/main/java/me/skymc/taboolib/listener/TListener.java
Normal file
44
src/main/java/me/skymc/taboolib/listener/TListener.java
Normal file
@ -0,0 +1,44 @@
|
||||
package me.skymc.taboolib.listener;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 13:41
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TListener {
|
||||
|
||||
/**
|
||||
* 注册时执行方法
|
||||
*
|
||||
* @return 方法名
|
||||
*/
|
||||
String register() default "";
|
||||
|
||||
/**
|
||||
* 注销时执行方法
|
||||
*
|
||||
* @return 方法名
|
||||
*/
|
||||
String cancel() default "";
|
||||
|
||||
/**
|
||||
* 注册时判断条件
|
||||
*
|
||||
* @return 方法名
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
/**
|
||||
* 注册前判断依赖插件
|
||||
*
|
||||
* @return 依赖插件
|
||||
*/
|
||||
String[] depend() default "";
|
||||
|
||||
}
|
180
src/main/java/me/skymc/taboolib/listener/TListenerHandler.java
Normal file
180
src/main/java/me/skymc/taboolib/listener/TListenerHandler.java
Normal file
@ -0,0 +1,180 @@
|
||||
package me.skymc.taboolib.listener;
|
||||
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.event.server.PluginEnableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 13:48
|
||||
*/
|
||||
@TListener
|
||||
public class TListenerHandler implements Listener {
|
||||
|
||||
private static HashMap<String, List<Listener>> listeners = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 初始化所有插件的所有监听器
|
||||
*/
|
||||
public static void setupListeners() {
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
try {
|
||||
setupListener(plugin);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化插件的所有监听器
|
||||
* 该操作会执行无参构造方法
|
||||
*
|
||||
* @param plugin 插件
|
||||
*/
|
||||
public static void setupListener(Plugin plugin) {
|
||||
List<Class> classes = FileUtils.getClasses(plugin);
|
||||
for (Class<?> pluginClass : classes) {
|
||||
if (Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) {
|
||||
try {
|
||||
TListener tListener = pluginClass.getAnnotation(TListener.class);
|
||||
// 检查注册条件
|
||||
if (tListener.depend().length > 0 && !Strings.isBlank(tListener.depend()[0])) {
|
||||
if (Arrays.stream(tListener.depend()).anyMatch(depend -> Bukkit.getPluginManager().getPlugin(depend) == null)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 实例化监听器
|
||||
Listener listener = plugin.getClass().equals(pluginClass) ? (Listener) plugin : (Listener) pluginClass.newInstance();
|
||||
listeners.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(listener);
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册所有插件的所有监听器
|
||||
*/
|
||||
public static void registerListeners() {
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
try {
|
||||
registerListener(plugin);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册插件的所有监听器
|
||||
* 该操作会执行 TListener 注解中的 register() 对应方法
|
||||
*
|
||||
* @param plugin 插件
|
||||
*/
|
||||
public static void registerListener(Plugin plugin) {
|
||||
Optional.ofNullable(listeners.get(plugin.getName())).ifPresent(listeners -> {
|
||||
for (Listener listener : listeners) {
|
||||
TListener tListener = listener.getClass().getAnnotation(TListener.class);
|
||||
// 检查注册条件
|
||||
if (!Strings.isBlank(tListener.condition())) {
|
||||
try {
|
||||
Method method = listener.getClass().getDeclaredMethod(tListener.condition());
|
||||
method.setAccessible(true);
|
||||
if (!(boolean) method.invoke(listener)) {
|
||||
continue;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// 执行注册方法
|
||||
if (!Strings.isBlank(tListener.register())) {
|
||||
try {
|
||||
Method method = listener.getClass().getDeclaredMethod(tListener.register());
|
||||
method.setAccessible(true);
|
||||
method.invoke(listener);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// 注册监听
|
||||
Bukkit.getPluginManager().registerEvents(listener, plugin);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销所有插件的所有监听器
|
||||
*/
|
||||
public static void cancelListeners() {
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
try {
|
||||
cancelListener(plugin);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销插件的所有监听器
|
||||
* 该操作会执行 TListener 注解中的 cancel() 对应方法
|
||||
*
|
||||
* @param plugin 插件
|
||||
*/
|
||||
public static void cancelListener(Plugin plugin) {
|
||||
Optional.ofNullable(listeners.remove(plugin.getName())).ifPresent(listeners -> {
|
||||
for (Listener listener : listeners) {
|
||||
HandlerList.unregisterAll(listener);
|
||||
TListener tListener = listener.getClass().getAnnotation(TListener.class);
|
||||
if (!Strings.isBlank(tListener.cancel())) {
|
||||
try {
|
||||
Method method = listener.getClass().getDeclaredMethod(tListener.cancel());
|
||||
method.setAccessible(true);
|
||||
method.invoke(listener);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPluginEnable(PluginEnableEvent e) {
|
||||
try {
|
||||
setupListener(e.getPlugin());
|
||||
registerListener(e.getPlugin());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPluginDisable(PluginDisableEvent e) {
|
||||
try {
|
||||
cancelListener(e.getPlugin());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static HashMap<String, List<Listener>> getListeners() {
|
||||
return listeners;
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package me.skymc.taboolib.message;
|
||||
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@ -9,6 +10,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
@TListener
|
||||
public class ChatCatcher implements Listener {
|
||||
|
||||
private static HashMap<String, LinkedList<Catcher>> playerdata = new HashMap<>();
|
||||
|
@ -11,6 +11,7 @@ import java.util.*;
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
@Deprecated
|
||||
public class MySQLConnection {
|
||||
|
||||
private String url;
|
||||
|
@ -5,6 +5,7 @@ import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.database.PlayerDataManager;
|
||||
import me.skymc.taboolib.exception.PlayerOfflineException;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@ -19,6 +20,7 @@ import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@TListener
|
||||
public class DataUtils implements Listener {
|
||||
|
||||
public static final ConcurrentHashMap<String, HashMap<String, FileConfiguration>> CACHE_DATA_PLUGIN = new ConcurrentHashMap<>();
|
||||
|
@ -1,6 +1,8 @@
|
||||
package me.skymc.taboolib.sign;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import me.skymc.taboolib.location.LocationUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
@ -22,10 +24,15 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Deprecated
|
||||
@TListener(condition = "check")
|
||||
public class SignUtils implements Listener {
|
||||
|
||||
public static HashMap<String, Block> signs = new HashMap<>();
|
||||
|
||||
public static boolean check() {
|
||||
return TabooLib.getVerint() > 10700;
|
||||
}
|
||||
|
||||
public static void openSign(Player p, Block b) {
|
||||
|
||||
if (!(b.getType().equals(Material.WALL_SIGN) || b.getType().equals(Material.SIGN_POST))) {
|
||||
|
121
src/main/java/me/skymc/taboolib/socket/TabooLibClient.java
Normal file
121
src/main/java/me/skymc/taboolib/socket/TabooLibClient.java
Normal file
@ -0,0 +1,121 @@
|
||||
package me.skymc.taboolib.socket;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 22:27
|
||||
*/
|
||||
public class TabooLibClient {
|
||||
|
||||
private static Socket socket;
|
||||
private static BufferedReader reader;
|
||||
private static PrintWriter writer;
|
||||
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
private static Packet packet;
|
||||
private static boolean notify = false;
|
||||
private static long latestResponse = System.currentTimeMillis();
|
||||
|
||||
public static void init() {
|
||||
if (TabooLibSettings.load()) {
|
||||
connect();
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(TabooLib.instance(), TabooLibClient::reconnect, 0, 20);
|
||||
} else {
|
||||
TLocale.sendToConsole("COMMUNICATION.FAILED-LOAD-SETTINGS", TabooLibSettings.getThrowable().toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendPacket(Packet packet) {
|
||||
writer.println(PacketSerializer.serialize(packet));
|
||||
}
|
||||
|
||||
public static void reconnect() {
|
||||
if (System.currentTimeMillis() - latestResponse > NumberUtils.getInteger(TabooLibSettings.getSettings().getProperty("channel.timeout"))) {
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
public static void connect() {
|
||||
try {
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
try {
|
||||
socket = new Socket("localhost", NumberUtils.getInteger(TabooLibSettings.getSettings().getProperty("channel.port")));
|
||||
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), TabooLibSettings.getCharset()));
|
||||
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), TabooLibSettings.getCharset()), true);
|
||||
notify = false;
|
||||
TLocale.sendToConsole("COMMUNICATION.SUCCESS-CONNECTED");
|
||||
} catch (SocketException e) {
|
||||
/*
|
||||
防止未启用终端服务器导致重复提示连接失败信息
|
||||
*/
|
||||
if (!notify) {
|
||||
notify = true;
|
||||
TLocale.sendToConsole("COMMUNICATION.FAILED-CONNECT-SERVER");
|
||||
}
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
TLocale.sendToConsole("COMMUNICATION.FAILED-CONNECT-CLIENT", e.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
executorService.execute(() -> {
|
||||
try {
|
||||
while (!socket.isClosed() && (packet = PacketSerializer.unSerialize(reader.readLine())) != null) {
|
||||
packet.readOnClient();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
TLocale.sendToConsole("COMMUNICATION.FAILED-READING-PACKET", e.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public static BufferedReader getReader() {
|
||||
return reader;
|
||||
}
|
||||
|
||||
public static PrintWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
public static ExecutorService getExecutorService() {
|
||||
return executorService;
|
||||
}
|
||||
|
||||
public static Packet getLatestPacket() {
|
||||
return packet;
|
||||
}
|
||||
|
||||
public static long getLatestResponse() {
|
||||
return latestResponse;
|
||||
}
|
||||
|
||||
public static void setLatestResponse(long latestResponse) {
|
||||
TabooLibClient.latestResponse = latestResponse;
|
||||
}
|
||||
}
|
118
src/main/java/me/skymc/taboolib/socket/TabooLibServer.java
Normal file
118
src/main/java/me/skymc/taboolib/socket/TabooLibServer.java
Normal file
@ -0,0 +1,118 @@
|
||||
package me.skymc.taboolib.socket;
|
||||
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketSerializer;
|
||||
import me.skymc.taboolib.socket.packet.impl.PacketHeartbeat;
|
||||
import me.skymc.taboolib.socket.packet.impl.PacketQuit;
|
||||
import me.skymc.taboolib.socket.server.ClientConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 研究了一个小时 log4j 愣是没整明白,不搞那些高端日志工具了
|
||||
*
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 20:45
|
||||
*/
|
||||
public class TabooLibServer {
|
||||
|
||||
private static SimpleDateFormat infoFormat = new SimpleDateFormat("HH:mm:ss");
|
||||
private static ServerSocket server = null;
|
||||
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
private static ConcurrentHashMap<Integer, ClientConnection> client = new ConcurrentHashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
println("TabooLib Communication Area Starting...");
|
||||
|
||||
if (!TabooLibSettings.load()) {
|
||||
println("Settings loading failed: " + TabooLibSettings.getThrowable().toString());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
server = new ServerSocket(NumberUtils.getInteger(TabooLibSettings.getSettings().getProperty("channel.port")));
|
||||
println("Starting TabooLib server on " + server.getInetAddress().getHostName() + ":" + server.getLocalPort());
|
||||
} catch (IOException e) {
|
||||
println("Server starting failed: " + e.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
|
||||
/*
|
||||
向所有已连接的客户端发送心跳包
|
||||
*/
|
||||
sendPacket(new PacketHeartbeat(0));
|
||||
/*
|
||||
检测无效的客户端连接,如果超过 5000 毫秒没有收到客户端的回应(上一次心跳包的回应)则注销链接
|
||||
*/
|
||||
client.entrySet().stream().filter(connection -> connection.getValue().isAlive()).map(connection -> new PacketQuit(connection.getKey(), "Lost connection")).forEach(TabooLibServer::sendPacket);
|
||||
}, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
Socket socket = server.accept();
|
||||
ClientConnection connection = new ClientConnection(socket);
|
||||
client.put(socket.getPort(), connection);
|
||||
executorService.execute(connection);
|
||||
println("Client accepted: " + socket.getPort() + " online: " + client.size());
|
||||
} catch (Exception e) {
|
||||
println("Client accept failed: " + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendPacket(Packet packet) {
|
||||
sendPacket(PacketSerializer.serialize(packet));
|
||||
}
|
||||
|
||||
public static void sendPacket(String origin) {
|
||||
println("Packet sending: " + origin + ", online: " + client.size());
|
||||
// 在服务端尝试解析动作并运行
|
||||
Optional.ofNullable(PacketSerializer.unSerialize(origin)).ifPresent(Packet::readOnServer);
|
||||
// 将动作发送至所有客户端
|
||||
for (ClientConnection connection : TabooLibServer.getClient().values()) {
|
||||
try {
|
||||
connection.getWriter().println(origin);
|
||||
} catch (Exception e) {
|
||||
TabooLibServer.println("Packet sending failed: " + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void println(Object obj) {
|
||||
System.out.println("[" + infoFormat.format(System.currentTimeMillis()) + " INFO]: " + obj);
|
||||
}
|
||||
|
||||
|
||||
public static Optional<Map.Entry<Integer, ClientConnection>> getConnection(int port) {
|
||||
return client.entrySet().stream().filter(entry -> entry.getKey().equals(port)).findFirst();
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static ServerSocket getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public static ConcurrentHashMap<Integer, ClientConnection> getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public static ExecutorService getExecutorService() {
|
||||
return executorService;
|
||||
}
|
||||
}
|
62
src/main/java/me/skymc/taboolib/socket/TabooLibSettings.java
Normal file
62
src/main/java/me/skymc/taboolib/socket/TabooLibSettings.java
Normal file
@ -0,0 +1,62 @@
|
||||
package me.skymc.taboolib.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:55
|
||||
*/
|
||||
public class TabooLibSettings {
|
||||
|
||||
private static Charset charset = Charset.forName("UTF-8");
|
||||
private static Properties settings = new Properties();
|
||||
private static Throwable throwable;
|
||||
|
||||
public static boolean load() {
|
||||
try {
|
||||
settings.load(getSettingsInputStream());
|
||||
return true;
|
||||
} catch (Throwable e) {
|
||||
throwable = e;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static InputStream getSettingsInputStream() {
|
||||
try {
|
||||
URL url = TabooLibServer.class.getClassLoader().getResource("settings.properties");
|
||||
if (url == null) {
|
||||
return null;
|
||||
} else {
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setUseCaches(false);
|
||||
return connection.getInputStream();
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static Properties getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
public static Throwable getThrowable() {
|
||||
return throwable;
|
||||
}
|
||||
|
||||
public static Charset getCharset() {
|
||||
return charset;
|
||||
}
|
||||
}
|
45
src/main/java/me/skymc/taboolib/socket/packet/Packet.java
Normal file
45
src/main/java/me/skymc/taboolib/socket/packet/Packet.java
Normal file
@ -0,0 +1,45 @@
|
||||
package me.skymc.taboolib.socket.packet;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:01
|
||||
*/
|
||||
public abstract class Packet {
|
||||
|
||||
private final int port;
|
||||
private final String uid;
|
||||
|
||||
public Packet(int port) {
|
||||
this.port = port;
|
||||
this.uid = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getUid() {
|
||||
return uid;
|
||||
}
|
||||
|
||||
public void readOnServer() {
|
||||
|
||||
}
|
||||
|
||||
public void readOnClient() {
|
||||
|
||||
}
|
||||
|
||||
public void serialize(JsonObject json) {
|
||||
|
||||
}
|
||||
|
||||
public void unSerialize(JsonObject json) {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package me.skymc.taboolib.socket.packet;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import me.skymc.taboolib.socket.packet.impl.PacketEmpty;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:32
|
||||
*/
|
||||
public class PacketSerializer {
|
||||
|
||||
private static PacketParser parser = new PacketParser();
|
||||
|
||||
public static String serialize(Packet packet) {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("uid", packet.getUid());
|
||||
json.addProperty("port", packet.getPort());
|
||||
json.addProperty("packet", packet.getClass().getAnnotation(PacketType.class).name());
|
||||
packet.serialize(json);
|
||||
return json.toString();
|
||||
}
|
||||
|
||||
public static Packet unSerialize(String origin) {
|
||||
Packet packet = null;
|
||||
try {
|
||||
packet = parser.parser((JsonObject) new JsonParser().parse(origin));
|
||||
} catch (JsonSyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return packet == null ? new PacketEmpty(0) : packet;
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static PacketParser getParser() {
|
||||
return parser;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package me.skymc.taboolib.socket.packet;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:09
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface PacketType {
|
||||
|
||||
String name();
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package me.skymc.taboolib.socket.packet.impl;
|
||||
|
||||
import me.skymc.taboolib.socket.TabooLibServer;
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketType;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:01
|
||||
*/
|
||||
@PacketType(name = "alive")
|
||||
public class PacketAlive extends Packet {
|
||||
|
||||
public PacketAlive(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnServer() {
|
||||
TabooLibServer.getConnection(getPort()).ifPresent(connect -> connect.getValue().setLatestResponse(System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnClient() {
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package me.skymc.taboolib.socket.packet.impl;
|
||||
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketType;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:01
|
||||
*/
|
||||
@PacketType(name = "empty")
|
||||
public class PacketEmpty extends Packet {
|
||||
|
||||
public PacketEmpty(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnServer() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnClient() {
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package me.skymc.taboolib.socket.packet.impl;
|
||||
|
||||
import me.skymc.taboolib.socket.TabooLibClient;
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketType;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:01
|
||||
*/
|
||||
@PacketType(name = "heartbeat")
|
||||
public class PacketHeartbeat extends Packet {
|
||||
|
||||
public PacketHeartbeat(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnServer() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnClient() {
|
||||
// 更新响应时间
|
||||
TabooLibClient.setLatestResponse(System.currentTimeMillis());
|
||||
// 回应服务端
|
||||
TabooLibClient.sendPacket(new PacketAlive(TabooLibClient.getSocket().getLocalPort()));
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package me.skymc.taboolib.socket.packet.impl;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.socket.TabooLibServer;
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketType;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:38
|
||||
*/
|
||||
@PacketType(name = "join")
|
||||
public class PacketJoin extends Packet {
|
||||
|
||||
public PacketJoin(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnServer() {
|
||||
TabooLibServer.println("Client " + getPort() + " joined Communication Area.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnClient() {
|
||||
TLocale.sendToConsole("COMMUNICATION.CLIENT-JOINED", String.valueOf(getPort()));
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package me.skymc.taboolib.socket.packet.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.socket.TabooLibServer;
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketType;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:01
|
||||
*/
|
||||
@PacketType(name = "message")
|
||||
public class PacketMessage extends Packet {
|
||||
|
||||
private String message;
|
||||
|
||||
public PacketMessage(int port) {
|
||||
this(port, "none");
|
||||
}
|
||||
|
||||
public PacketMessage(int port, String message) {
|
||||
super(port);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnServer() {
|
||||
TabooLibServer.println(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnClient() {
|
||||
TLocale.sendToConsole("COMMUNICATION.PACKET-MESSAGE", String.valueOf(getPort()), message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(JsonObject json) {
|
||||
json.addProperty("message", message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unSerialize(JsonObject json) {
|
||||
message = json.get("message").getAsString();
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package me.skymc.taboolib.socket.packet.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.socket.TabooLibServer;
|
||||
import me.skymc.taboolib.socket.packet.Packet;
|
||||
import me.skymc.taboolib.socket.packet.PacketType;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 23:38
|
||||
*/
|
||||
@PacketType(name = "quit")
|
||||
public class PacketQuit extends Packet {
|
||||
|
||||
private String message;
|
||||
|
||||
public PacketQuit(int port) {
|
||||
this(port, "connect closed.");
|
||||
}
|
||||
|
||||
public PacketQuit(int port, String message) {
|
||||
super(port);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnServer() {
|
||||
TabooLibServer.getConnection(getPort()).ifPresent(connection -> {
|
||||
// 注销连接
|
||||
TabooLibServer.getClient().remove(connection.getKey());
|
||||
// 关闭连接
|
||||
try {
|
||||
connection.getValue().getSocket().close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
// 提示信息
|
||||
TabooLibServer.println("Client " + getPort() + " leaved Communication Area: " + message);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readOnClient() {
|
||||
TLocale.sendToConsole("COMMUNICATION.CLIENT-QUITED", String.valueOf(getPort()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(JsonObject json) {
|
||||
json.addProperty("message", message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unSerialize(JsonObject json) {
|
||||
message = json.get("message").getAsString();
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package me.skymc.taboolib.socket.server;
|
||||
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.socket.TabooLibServer;
|
||||
import me.skymc.taboolib.socket.TabooLibSettings;
|
||||
import me.skymc.taboolib.socket.packet.impl.PacketJoin;
|
||||
import me.skymc.taboolib.socket.packet.impl.PacketQuit;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 22:30
|
||||
*/
|
||||
public class ClientConnection implements Runnable {
|
||||
|
||||
private final Socket socket;
|
||||
private BufferedReader reader;
|
||||
private PrintWriter writer;
|
||||
private String latestPacket;
|
||||
private long latestResponse = System.currentTimeMillis();
|
||||
|
||||
public ClientConnection(Socket socket) {
|
||||
this.socket = socket;
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), TabooLibSettings.getCharset()));
|
||||
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), TabooLibSettings.getCharset()), true);
|
||||
TabooLibServer.sendPacket(new PacketJoin(socket.getPort()));
|
||||
} catch (Exception e) {
|
||||
TabooLibServer.println("Client joined failed: " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while ((latestPacket = reader.readLine()) != null) {
|
||||
TabooLibServer.sendPacket(latestPacket);
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
/*
|
||||
连接丢失,客户端退出
|
||||
*/
|
||||
TabooLibServer.sendPacket(new PacketQuit(socket.getPort(), e.getMessage()));
|
||||
} catch (Exception e) {
|
||||
TabooLibServer.println("Client running failed: " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return System.currentTimeMillis() - latestResponse < NumberUtils.getInteger(TabooLibSettings.getSettings().getProperty("channel.timeout"));
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public BufferedReader getReader() {
|
||||
return reader;
|
||||
}
|
||||
|
||||
public PrintWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
public String getLatestPacket() {
|
||||
return latestPacket;
|
||||
}
|
||||
|
||||
public long getLatestResponse() {
|
||||
return latestResponse;
|
||||
}
|
||||
|
||||
public void setLatestResponse(long latestResponse) {
|
||||
this.latestResponse = latestResponse;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package me.skymc.taboolib.translateuuid;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.commands.internal.BaseMainCommand;
|
||||
import me.skymc.taboolib.commands.internal.BaseSubCommand;
|
||||
import me.skymc.taboolib.commands.internal.TCommand;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandArgument;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandRegister;
|
||||
import org.bukkit.command.Command;
|
||||
@ -12,6 +13,7 @@ import org.bukkit.command.CommandSender;
|
||||
* @Author sky
|
||||
* @Since 2018-06-22 17:09
|
||||
*/
|
||||
@TCommand(name = "translateuuid")
|
||||
public class TranslateUUIDCommand extends BaseMainCommand {
|
||||
|
||||
@Override
|
||||
|
@ -367,6 +367,7 @@ COMMANDS:
|
||||
- ' &f- &7注册插件: &f{1}'
|
||||
- ' &f- &7上次刷新时间: &f{2}'
|
||||
- ' &f- &7下次刷新时间: &f{3}'
|
||||
- ''
|
||||
LIST:
|
||||
HEAD:
|
||||
- ''
|
||||
@ -548,7 +549,37 @@ COMMANDS:
|
||||
0: '目标'
|
||||
1: '内容'
|
||||
INVALID-TARGET-NOT-FOUND: '&8[&3&lTabooLib&8] &4目标 &c{0} &4不存在.'
|
||||
TLOGS:
|
||||
COMMAND-TITLE: '&e&l----- &6&lTabooLibLogs Commands &e&l-----'
|
||||
INFO:
|
||||
DESCRIPTION: '写入一般记录'
|
||||
ARGUMENTS:
|
||||
0: '目录'
|
||||
1: '内容'
|
||||
SUCCESS: '&8[&3&lTabooLib&8] &7写入完成.'
|
||||
ERROR:
|
||||
DESCRIPTION: '写入错误记录'
|
||||
ARGUMENTS:
|
||||
0: '目录'
|
||||
1: '内容'
|
||||
SUCCESS: '&8[&3&lTabooLib&8] &7写入完成.'
|
||||
WARNING:
|
||||
DESCRIPTION: '写入警告记录'
|
||||
ARGUMENTS:
|
||||
0: '目录'
|
||||
1: '内容'
|
||||
SUCCESS: '&8[&3&lTabooLib&8] &7写入完成.'
|
||||
|
||||
DATABASE:
|
||||
CONNECTION-ESTABLISHED: '成功连接到 {0} 数据库,连接池大小 {1}'
|
||||
CONNECTION-ERROR: '连接到数据库错误:{0}'
|
||||
|
||||
COMMUNICATION:
|
||||
FAILED-LOAD-SETTINGS: '§8[§3§lTabooLibClient§8] &4配置载入失败: {0}'
|
||||
FAILED-CONNECT-SERVER: '§8[§3§lTabooLibClient§8] &4本地通讯网络连接失败.'
|
||||
FAILED-CONNECT-CLIENT: '§8[§3§lTabooLibClient§8] &4本地通讯网络连接出错: {0}'
|
||||
FAILED-READING-PACKET: '§8[§3§lTabooLibClient§8] &4本地通讯网络数据包读取失败: {0}'
|
||||
SUCCESS-CONNECTED: '§8[§3§lTabooLibClient§8] &7本地通讯网络连接成功.'
|
||||
CLIENT-JOINED: '§8[§3§lTabooLibClient§8] &7服务器 &flocalhost:{0} &7加入本地通讯网络.'
|
||||
CLIENT-QUITED: '§8[§3§lTabooLibClient§8] &7服务器 &flocalhost:{0} &7退出本地通讯网络.'
|
||||
CLIENT-MESSAGE: '§8[§3§lTabooLibClient§8] &7服务器 &flocalhost:{0} &7发送信息: &f{1}'
|
@ -25,5 +25,8 @@ commands:
|
||||
taboolibrarymodule:
|
||||
aliases: [tlm]
|
||||
translateuuid:
|
||||
aliases: 'tuuid'
|
||||
aliases: [tuuid]
|
||||
permission: taboolib.admin
|
||||
tabooliblogs:
|
||||
aliases: [tlog, tlogs]
|
||||
permission: taboolib.admin
|
2
src/main/resources/settings.properties
Normal file
2
src/main/resources/settings.properties
Normal file
@ -0,0 +1,2 @@
|
||||
channel.port=5260
|
||||
channel.timeout=5000
|
Loading…
Reference in New Issue
Block a user