This commit is contained in:
坏黑 2019-04-10 22:10:16 +08:00
parent 629bc77803
commit 26f1a4b5e2
18 changed files with 167 additions and 81 deletions

View File

@ -69,11 +69,11 @@ public final class TranslatableComponent extends BaseComponent {
* Creates a translatable component with the passed substitutions * Creates a translatable component with the passed substitutions
* *
* @param translate the translation key * @param translate the translation key
* @param with the {@link java.lang.String}s and * @param with the {@link String}s and
* {@link BaseComponent}s to use into the * {@link BaseComponent}s to use into the
* translation * translation
* @see #translate * @see #translate
* @see #setWith(java.util.List) * @see #setWith(List)
*/ */
public TranslatableComponent(String translate, Object... with) { public TranslatableComponent(String translate, Object... with) {
setTranslate(translate); setTranslate(translate);

View File

@ -7,6 +7,7 @@ import com.ilummc.tlib.util.IO;
import com.ilummc.tlib.util.Strings; import com.ilummc.tlib.util.Strings;
import me.skymc.taboolib.database.GlobalDataManager; import me.skymc.taboolib.database.GlobalDataManager;
import me.skymc.taboolib.database.PlayerDataManager; import me.skymc.taboolib.database.PlayerDataManager;
import me.skymc.taboolib.economy.EcoUtils;
import me.skymc.taboolib.fileutils.ConfigUtils; import me.skymc.taboolib.fileutils.ConfigUtils;
import me.skymc.taboolib.fileutils.FileUtils; import me.skymc.taboolib.fileutils.FileUtils;
import me.skymc.taboolib.listener.TListenerHandler; import me.skymc.taboolib.listener.TListenerHandler;
@ -16,8 +17,6 @@ import me.skymc.taboolib.other.NumberUtils;
import me.skymc.taboolib.playerdata.DataUtils; import me.skymc.taboolib.playerdata.DataUtils;
import me.skymc.taboolib.socket.TabooLibClient; import me.skymc.taboolib.socket.TabooLibClient;
import me.skymc.taboolib.socket.TabooLibServer; import me.skymc.taboolib.socket.TabooLibServer;
import me.skymc.taboolib.string.language2.Language2;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -45,11 +44,9 @@ public class Main extends JavaPlugin {
} }
private static Plugin inst; private static Plugin inst;
private static Economy economy;
private static File playerDataFolder; private static File playerDataFolder;
private static File serverDataFolder; private static File serverDataFolder;
private static StorageType storageType = StorageType.LOCAL; private static StorageType storageType = StorageType.LOCAL;
private static Language2 exampleLanguage2;
private static boolean disable = false; private static boolean disable = false;
private static boolean started = false; private static boolean started = false;
private static boolean isInternetOnline = false; private static boolean isInternetOnline = false;
@ -138,8 +135,6 @@ public class Main extends JavaPlugin {
}.runTask(this); }.runTask(this);
// 启动 // 启动
started = true; started = true;
// 载入语言文件
exampleLanguage2 = new Language2("Language2", this);
} }
@Override @Override
@ -198,14 +193,6 @@ public class Main extends JavaPlugin {
return "§8[§3§lTabooLib§8] §7"; return "§8[§3§lTabooLib§8] §7";
} }
public static net.milkbowl.vault.economy.Economy getEconomy() {
return economy;
}
public static void setEconomy(Economy economy) {
Main.economy = economy;
}
public static File getPlayerDataFolder() { public static File getPlayerDataFolder() {
return playerDataFolder; return playerDataFolder;
} }
@ -222,26 +209,10 @@ public class Main extends JavaPlugin {
return disable; return disable;
} }
public static MySQLConnection getConnection() {
return null;
}
public static Language2 getExampleLanguage2() {
return exampleLanguage2;
}
public static boolean isStarted() { public static boolean isStarted() {
return started; return started;
} }
public static Random getRandom() {
return NumberUtils.getRandom();
}
public static String getTablePrefix() {
return inst.getConfig().getString("MYSQL.PREFIX");
}
public static boolean isInternetOnline() { public static boolean isInternetOnline() {
return isInternetOnline; return isInternetOnline;
} }
@ -254,6 +225,26 @@ public class Main extends JavaPlugin {
return TLib.getTLib().getLibsFolder().listFiles().length > 0; return TLib.getTLib().getLibsFolder().listFiles().length > 0;
} }
@Deprecated
public static Random getRandom() {
return NumberUtils.getRandom();
}
@Deprecated
public static String getTablePrefix() {
return inst.getConfig().getString("MYSQL.PREFIX");
}
@Deprecated
public static MySQLConnection getConnection() {
return null;
}
@Deprecated
public static net.milkbowl.vault.economy.Economy getEconomy() {
return EcoUtils.getEconomy();
}
// ********************************* // *********************************
// //
// Private Setter // Private Setter

View File

@ -160,11 +160,13 @@ public class TabooLibLoader implements Listener {
} }
static void setupAddons() { static void setupAddons() {
TabooLib.instance().saveResource("Addons/TabooLibDeprecated", true); File origin = new File(TabooLib.instance().getDataFolder(), "Addons/TabooLibDeprecated.jar");
origin.delete();
// 傻逼 Gradle shadow 插件会将所有 jar 排除 // 傻逼 Gradle shadow 插件会将所有 jar 排除
// https://github.com/johnrengelman/shadow/issues/276 // https://github.com/johnrengelman/shadow/issues/276
TabooLib.instance().saveResource("Addons/TabooLibDeprecated", true);
File from = new File(TabooLib.instance().getDataFolder(), "Addons/TabooLibDeprecated"); File from = new File(TabooLib.instance().getDataFolder(), "Addons/TabooLibDeprecated");
from.renameTo(new File(TabooLib.instance().getDataFolder(), "Addons/TabooLibDeprecated.jar")); from.renameTo(origin);
from.delete(); from.delete();
File file = new File(TabooLib.instance().getDataFolder(), "Addons"); File file = new File(TabooLib.instance().getDataFolder(), "Addons");
if (file.exists()) { if (file.exists()) {

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.logging.Level; import java.util.logging.Level;
@ -171,7 +172,7 @@ public class Metrics {
} }
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(outputStream); GZIPOutputStream gzip = new GZIPOutputStream(outputStream);
gzip.write(str.getBytes("UTF-8")); gzip.write(str.getBytes(StandardCharsets.UTF_8));
gzip.close(); gzip.close();
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
@ -262,7 +263,7 @@ public class Metrics {
playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
} }
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0; int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
String bukkitVersion = org.bukkit.Bukkit.getVersion(); String bukkitVersion = Bukkit.getVersion();
bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1); bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1);
// OS/Java specific data // OS/Java specific data

View File

@ -0,0 +1,16 @@
package me.skymc.taboolib.common.serialize;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Author sky
* @Since 2018-10-05 12:11
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DoNotSerialize {
}

View File

@ -1,20 +1,34 @@
package me.skymc.taboolib.common.serialize; package me.skymc.taboolib.common.serialize;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/** /**
* @Author 坏黑 * @Author 坏黑
* @Since 2019-03-08 17:28 * @Since 2019-03-08 17:28
*/ */
public interface TSerializable { public interface TSerializable {
void read(String fieldName, String value); default void read(String fieldName, String value) {
}
String write(String fieldName, Object value); default String write(String fieldName, Object value) {
return null;
}
default void read(String value) { default Object read(String value) {
TSerializer.read(this, value); return TSerializer.read(this, value);
} }
default String write() { default String write() {
return TSerializer.write(this); return TSerializer.write(this);
} }
default Object readBase64(String value) {
return TSerializer.read(this, new String(Base64.getDecoder().decode(value), StandardCharsets.UTF_8));
}
default String writeBase64() {
return Base64.getEncoder().encodeToString(TSerializer.write(this).getBytes(StandardCharsets.UTF_8));
}
} }

View File

@ -30,6 +30,9 @@ public class TSerializer {
try { try {
Field declaredField = serializable.getClass().getDeclaredField(jsonElementEntry.getKey()); Field declaredField = serializable.getClass().getDeclaredField(jsonElementEntry.getKey());
declaredField.setAccessible(true); declaredField.setAccessible(true);
if (declaredField.isAnnotationPresent(DoNotSerialize.class)) {
continue;
}
Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst(); Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst();
if (serializer.isPresent()) { if (serializer.isPresent()) {
declaredField.set(serializable, serializer.get().getSerializer().read(jsonElementEntry.getValue().getAsString())); declaredField.set(serializable, serializer.get().getSerializer().read(jsonElementEntry.getValue().getAsString()));
@ -56,13 +59,13 @@ public class TSerializer {
JsonObject serializeObject = new JsonObject(); JsonObject serializeObject = new JsonObject();
for (Field declaredField : serializable.getClass().getDeclaredFields()) { for (Field declaredField : serializable.getClass().getDeclaredFields()) {
try { try {
if (!Modifier.isStatic(declaredField.getModifiers())) { if (!declaredField.isAnnotationPresent(DoNotSerialize.class) && !Modifier.isStatic(declaredField.getModifiers())) {
declaredField.setAccessible(true); declaredField.setAccessible(true);
Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst();
Object o = declaredField.get(serializable); Object o = declaredField.get(serializable);
if (o == null) { if (o == null) {
continue; continue;
} }
Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst();
if (serializer.isPresent()) { if (serializer.isPresent()) {
serializeObject.addProperty(declaredField.getName(), serializer.get().getSerializer().write(o)); serializeObject.addProperty(declaredField.getName(), serializer.get().getSerializer().write(o));
} else { } else {

View File

@ -8,7 +8,7 @@ public interface TSerializerElement<T> {
T read(String value); T read(String value);
String write(Object value); String write(T value);
boolean matches(Class objectClass); boolean matches(Class objectClass);

View File

@ -1,9 +1,11 @@
package me.skymc.taboolib.common.serialize; package me.skymc.taboolib.common.serialize;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Author 坏黑 * @Author 坏黑
@ -17,13 +19,13 @@ public class TSerializerExample {
// 修改参数 // 修改参数
date.number = 100; date.number = 100;
// 序列化 // 序列化
String value = date.write(); String value = date.writeBase64();
// 打印 // 打印
System.out.println(value); System.out.println(value);
// 创建新的对象 // 创建新的对象
SimpleData dataCopy = new SimpleData(); SimpleData dataCopy = new SimpleData();
// 反序列化 // 反序列化
dataCopy.read(value); dataCopy.readBase64(value);
// 打印 // 打印
System.out.println(dataCopy); System.out.println(dataCopy);
} }
@ -38,13 +40,24 @@ public class TSerializerExample {
* 基本类型不需要手动进行序列化 * 基本类型不需要手动进行序列化
* 包含: StringintshortlongdoublefloatbooleanItemStackLocation * 包含: StringintshortlongdoublefloatbooleanItemStackLocation
*/ */
private String text; private String text = "123";
private int number; private int number = 100;
/** /**
* 特殊类型需要进行手动序列化 * 特殊类型需要进行手动序列化
* 本工具提供了基本容器的序列化方法 * 标注 @TSerializeCollection @TSerializeMap 来进行自动序列化未完成
*/ */
private List<Double> list = Lists.newArrayList(); // @TSerializeCollection
private List<Double> list = Lists.newArrayList(1.0, 2.0, 3.0);
// @TSerializeMap
private Map<String, String> map = ImmutableMap.of("abc", "def");
/**
* 跳过序列化
*/
@DoNotSerialize
private String ignoreSerialize = "aaa";
/** /**
* 基本类型不会执行以下两个方法 * 基本类型不会执行以下两个方法
@ -52,24 +65,17 @@ public class TSerializerExample {
*/ */
@Override @Override
public void read(String fieldName, String value) { public void read(String fieldName, String value) {
switch (fieldName) { if (fieldName.equals("list")) {
case "list": {
// List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化 // List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化
TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE); TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE);
break;
}
default:
} }
} }
@Override @Override
public String write(String fieldName, Object value) { public String write(String fieldName, Object value) {
switch (fieldName) { if (fieldName.equals("list")) {
case "list": {
// 序列化同理
return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE); return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE);
} }
}
return null; return null;
} }
@ -79,6 +85,7 @@ public class TSerializerExample {
"text='" + text + '\'' + "text='" + text + '\'' +
", number=" + number + ", number=" + number +
", list=" + list + ", list=" + list +
", ignoreSerialize='" + ignoreSerialize + '\'' +
'}'; '}';
} }
} }

View File

@ -0,0 +1,24 @@
package me.skymc.taboolib.common.serialize.container;
import me.skymc.taboolib.common.serialize.TSerializerElement;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Collection;
/**
* @Author sky
* @Since 2018-10-05 12:11
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TSerializeCollection {
Class<? extends Collection> type() default ArrayList.class;
Class<? extends TSerializerElement> element() default TSerializerElement.class;
}

View File

@ -0,0 +1,20 @@
package me.skymc.taboolib.common.serialize.container;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.Map;
/**
* @Author sky
* @Since 2018-10-05 12:11
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TSerializeMap {
Class<? extends Map> type() default HashMap.class;
}

View File

@ -47,7 +47,7 @@ public class TListenerHandler implements Listener {
*/ */
public static void setupListener(Plugin plugin) { public static void setupListener(Plugin plugin) {
for (Class<?> pluginClass : TabooLibLoader.getPluginClassSafely(plugin)) { for (Class<?> pluginClass : TabooLibLoader.getPluginClassSafely(plugin)) {
if (org.bukkit.event.Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) { if (Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) {
try { try {
TListener tListener = pluginClass.getAnnotation(TListener.class); TListener tListener = pluginClass.getAnnotation(TListener.class);
// 检查注册条件 // 检查注册条件

View File

@ -32,9 +32,7 @@ public class SQLColumn extends IColumn {
/** /**
* CHAR 类型常用构造器 * CHAR 类型常用构造器
* * new SQLColumn(SQLColumnType.CHAR, 1, "data");
* @param columnType
* @param columnName
*/ */
public SQLColumn(SQLColumnType columnType, int m, String columnName) { public SQLColumn(SQLColumnType columnType, int m, String columnName) {
this(columnType, m, 0, columnName, null); this(columnType, m, 0, columnName, null);
@ -56,6 +54,10 @@ public class SQLColumn extends IColumn {
this(columnType, 0, 0, columnName, defaultValue); this(columnType, 0, 0, columnName, defaultValue);
} }
public SQLColumn(SQLColumnType columnType, String columnName, Object defaultValue, SQLColumnOption... columnOptions) {
this(columnType, 0, 0, columnName, defaultValue, columnOptions);
}
/** /**
* 完整构造器 * 完整构造器
* *

View File

@ -49,11 +49,11 @@ public class SQLTable {
} }
public String deleteQuery() { public String deleteQuery() {
return Strings.replaceWithOrder("drop table if exists `{0}`" + tableName); return Strings.replaceWithOrder("drop table if exists `{0}`", tableName);
} }
public String cleanQuery() { public String cleanQuery() {
return Strings.replaceWithOrder("delete from `{0}`" + tableName); return Strings.replaceWithOrder("delete from `{0}`", tableName);
} }
public String truncateQuery() { public String truncateQuery() {

View File

@ -19,6 +19,7 @@ public class RunnableQuery {
private TaskStatement statement; private TaskStatement statement;
private TaskResult result; private TaskResult result;
private TaskResult resultNext; private TaskResult resultNext;
private TaskResult resultAutoNext;
private Connection connection; private Connection connection;
private boolean autoClose; private boolean autoClose;
private String query; private String query;
@ -47,6 +48,11 @@ public class RunnableQuery {
return this; return this;
} }
public RunnableQuery resultAutoNext(TaskResult result) {
this.resultAutoNext = result;
return this;
}
public RunnableQuery connection(Connection connection) { public RunnableQuery connection(Connection connection) {
return connection(connection, false); return connection(connection, false);
} }
@ -113,6 +119,12 @@ public class RunnableQuery {
return resultNext.execute(resultSet); return resultNext.execute(resultSet);
} else if (result != null) { } else if (result != null) {
return result.execute(resultSet); return result.execute(resultSet);
} else if (resultAutoNext != null) {
Object result = null;
while (resultSet.next()) {
result = resultAutoNext.execute(resultSet);
}
return result;
} else { } else {
return null; return null;
} }

View File

@ -30,16 +30,6 @@ public class SQLiteColumn extends IColumn {
this(columnType, 0, 0, columnName, null); this(columnType, 0, 0, columnName, null);
} }
/**
* CHAR 类型常用构造器
*
* @param columnType
* @param columnName
*/
public SQLiteColumn(SQLiteColumnType columnType, int m, String columnName) {
this(columnType, m, 0, columnName, null);
}
/** /**
* 主键 类型常用构造器 * 主键 类型常用构造器
* new SQLColumn(SQLiteColumnType.TEXT, "username", SQLiteColumnOption.PRIMARY_KEY, SQLiteColumnOption.AUTO_INCREMENT); * new SQLColumn(SQLiteColumnType.TEXT, "username", SQLiteColumnOption.PRIMARY_KEY, SQLiteColumnOption.AUTO_INCREMENT);
@ -56,6 +46,10 @@ public class SQLiteColumn extends IColumn {
this(columnType, 0, 0, columnName, defaultValue); this(columnType, 0, 0, columnName, defaultValue);
} }
public SQLiteColumn(SQLiteColumnType columnType, String columnName, Object defaultValue, SQLiteColumnOption... columnOptions) {
this(columnType, 0, 0, columnName, defaultValue, columnOptions);
}
/** /**
* 完整构造器 * 完整构造器
* *

View File

@ -9,8 +9,8 @@ import org.bukkit.entity.Player;
public class SoundPack { public class SoundPack {
private Sound sound; private Sound sound;
private float a; private Float a;
private float b; private Float b;
private int delay; private int delay;
/** /**
@ -70,11 +70,11 @@ public class SoundPack {
return sound; return sound;
} }
public float getA() { public Float getA() {
return a; return a;
} }
public float getB() { public Float getB() {
return b; return b;
} }