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
*
* @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
* translation
* @see #translate
* @see #setWith(java.util.List)
* @see #setWith(List)
*/
public TranslatableComponent(String translate, Object... with) {
setTranslate(translate);

View File

@ -7,6 +7,7 @@ import com.ilummc.tlib.util.IO;
import com.ilummc.tlib.util.Strings;
import me.skymc.taboolib.database.GlobalDataManager;
import me.skymc.taboolib.database.PlayerDataManager;
import me.skymc.taboolib.economy.EcoUtils;
import me.skymc.taboolib.fileutils.ConfigUtils;
import me.skymc.taboolib.fileutils.FileUtils;
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.socket.TabooLibClient;
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.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
@ -45,11 +44,9 @@ public class Main extends JavaPlugin {
}
private static Plugin inst;
private static Economy economy;
private static File playerDataFolder;
private static File serverDataFolder;
private static StorageType storageType = StorageType.LOCAL;
private static Language2 exampleLanguage2;
private static boolean disable = false;
private static boolean started = false;
private static boolean isInternetOnline = false;
@ -138,8 +135,6 @@ public class Main extends JavaPlugin {
}.runTask(this);
// 启动
started = true;
// 载入语言文件
exampleLanguage2 = new Language2("Language2", this);
}
@Override
@ -198,14 +193,6 @@ public class Main extends JavaPlugin {
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() {
return playerDataFolder;
}
@ -222,26 +209,10 @@ public class Main extends JavaPlugin {
return disable;
}
public static MySQLConnection getConnection() {
return null;
}
public static Language2 getExampleLanguage2() {
return exampleLanguage2;
}
public static boolean isStarted() {
return started;
}
public static Random getRandom() {
return NumberUtils.getRandom();
}
public static String getTablePrefix() {
return inst.getConfig().getString("MYSQL.PREFIX");
}
public static boolean isInternetOnline() {
return isInternetOnline;
}
@ -254,6 +225,26 @@ public class Main extends JavaPlugin {
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

View File

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

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.logging.Level;
@ -171,7 +172,7 @@ public class Metrics {
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(outputStream);
gzip.write(str.getBytes("UTF-8"));
gzip.write(str.getBytes(StandardCharsets.UTF_8));
gzip.close();
return outputStream.toByteArray();
}
@ -262,7 +263,7 @@ public class Metrics {
playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
}
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);
// 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;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* @Author 坏黑
* @Since 2019-03-08 17:28
*/
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) {
TSerializer.read(this, value);
default Object read(String value) {
return TSerializer.read(this, value);
}
default String write() {
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 {
Field declaredField = serializable.getClass().getDeclaredField(jsonElementEntry.getKey());
declaredField.setAccessible(true);
if (declaredField.isAnnotationPresent(DoNotSerialize.class)) {
continue;
}
Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst();
if (serializer.isPresent()) {
declaredField.set(serializable, serializer.get().getSerializer().read(jsonElementEntry.getValue().getAsString()));
@ -56,13 +59,13 @@ public class TSerializer {
JsonObject serializeObject = new JsonObject();
for (Field declaredField : serializable.getClass().getDeclaredFields()) {
try {
if (!Modifier.isStatic(declaredField.getModifiers())) {
if (!declaredField.isAnnotationPresent(DoNotSerialize.class) && !Modifier.isStatic(declaredField.getModifiers())) {
declaredField.setAccessible(true);
Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst();
Object o = declaredField.get(serializable);
if (o == null) {
continue;
}
Optional<TSerializerElementGeneral> serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst();
if (serializer.isPresent()) {
serializeObject.addProperty(declaredField.getName(), serializer.get().getSerializer().write(o));
} else {

View File

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

View File

@ -1,9 +1,11 @@
package me.skymc.taboolib.common.serialize;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* @Author 坏黑
@ -17,13 +19,13 @@ public class TSerializerExample {
// 修改参数
date.number = 100;
// 序列化
String value = date.write();
String value = date.writeBase64();
// 打印
System.out.println(value);
// 创建新的对象
SimpleData dataCopy = new SimpleData();
// 反序列化
dataCopy.read(value);
dataCopy.readBase64(value);
// 打印
System.out.println(dataCopy);
}
@ -38,13 +40,24 @@ public class TSerializerExample {
* 基本类型不需要手动进行序列化
* 包含: StringintshortlongdoublefloatbooleanItemStackLocation
*/
private String text;
private int number;
private String text = "123";
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,23 +65,16 @@ public class TSerializerExample {
*/
@Override
public void read(String fieldName, String value) {
switch (fieldName) {
case "list": {
// List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化
TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE);
break;
}
default:
if (fieldName.equals("list")) {
// List 类型可以直接通过 TSerializer 提供的预设方法进行反序列化
TSerializer.readCollection(list, value, TSerializerElementGeneral.DOUBLE);
}
}
@Override
public String write(String fieldName, Object value) {
switch (fieldName) {
case "list": {
// 序列化同理
return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE);
}
if (fieldName.equals("list")) {
return TSerializer.writeCollection((Collection) value, TSerializerElementGeneral.DOUBLE);
}
return null;
}
@ -79,6 +85,7 @@ public class TSerializerExample {
"text='" + text + '\'' +
", number=" + number +
", 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) {
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 {
TListener tListener = pluginClass.getAnnotation(TListener.class);
// 检查注册条件

View File

@ -32,9 +32,7 @@ public class SQLColumn extends IColumn {
/**
* CHAR 类型常用构造器
*
* @param columnType
* @param columnName
* new SQLColumn(SQLColumnType.CHAR, 1, "data");
*/
public SQLColumn(SQLColumnType columnType, int m, String columnName) {
this(columnType, m, 0, columnName, null);
@ -56,6 +54,10 @@ public class SQLColumn extends IColumn {
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() {
return Strings.replaceWithOrder("drop table if exists `{0}`" + tableName);
return Strings.replaceWithOrder("drop table if exists `{0}`", tableName);
}
public String cleanQuery() {
return Strings.replaceWithOrder("delete from `{0}`" + tableName);
return Strings.replaceWithOrder("delete from `{0}`", tableName);
}
public String truncateQuery() {

View File

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

View File

@ -30,16 +30,6 @@ public class SQLiteColumn extends IColumn {
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);
@ -56,6 +46,10 @@ public class SQLiteColumn extends IColumn {
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 {
private Sound sound;
private float a;
private float b;
private Float a;
private Float b;
private int delay;
/**
@ -70,11 +70,11 @@ public class SoundPack {
return sound;
}
public float getA() {
public Float getA() {
return a;
}
public float getB() {
public Float getB() {
return b;
}