Framework adjustment
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package io.izzel.taboolib.module.command;
|
||||
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.module.command.base.BaseCommand;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.command.base.BaseMainCommand;
|
||||
import io.izzel.taboolib.module.inject.TFunction;
|
||||
@@ -107,7 +108,7 @@ public class TCommandHandler {
|
||||
* @param baseMainCommand 命令对象
|
||||
* @return {@link BaseMainCommand}
|
||||
*/
|
||||
public static BaseMainCommand registerCommand(TCommand tCommand, String command, BaseMainCommand baseMainCommand, Plugin plugin) {
|
||||
public static BaseMainCommand registerCommand(BaseCommand tCommand, String command, BaseMainCommand baseMainCommand, Plugin plugin) {
|
||||
if (Bukkit.getPluginCommand(command) == null) {
|
||||
registerPluginCommand(
|
||||
plugin,
|
||||
@@ -129,8 +130,8 @@ public class TCommandHandler {
|
||||
*/
|
||||
public static void registerCommand(Plugin plugin) {
|
||||
for (Class pluginClass : Files.getClasses(plugin)) {
|
||||
if (BaseMainCommand.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TCommand.class)) {
|
||||
TCommand tCommand = (TCommand) pluginClass.getAnnotation(TCommand.class);
|
||||
if (BaseMainCommand.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(BaseCommand.class)) {
|
||||
BaseCommand tCommand = (BaseCommand) pluginClass.getAnnotation(BaseCommand.class);
|
||||
try {
|
||||
registerCommand(tCommand, tCommand.name(), (BaseMainCommand) pluginClass.newInstance(), plugin);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.command;
|
||||
package io.izzel.taboolib.module.command.base;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -11,7 +11,7 @@ import java.lang.annotation.Target;
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TCommand {
|
||||
public @interface BaseCommand {
|
||||
|
||||
String name();
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.List;
|
||||
* @Since 2018-08-27 8:42
|
||||
* @BuilderLevel 1.0
|
||||
*/
|
||||
public class SimpleCommandBuilder {
|
||||
public class CommandBuilder {
|
||||
|
||||
public static final CompleterTab EMPTY_COMPLETER_TAB = ((sender, args) -> new ArrayList<>());
|
||||
public static final CompleterCommand EMPTY_COMPLETER_COMMAND = ((sender, args) -> {});
|
||||
@@ -30,7 +30,7 @@ public class SimpleCommandBuilder {
|
||||
private boolean forceRegister;
|
||||
private boolean build;
|
||||
|
||||
SimpleCommandBuilder(String command, Plugin plugin) {
|
||||
CommandBuilder(String command, Plugin plugin) {
|
||||
this.command = command;
|
||||
this.plugin = plugin;
|
||||
this.description = "";
|
||||
@@ -39,61 +39,61 @@ public class SimpleCommandBuilder {
|
||||
this.build = false;
|
||||
}
|
||||
|
||||
public static SimpleCommandBuilder create(String command, Plugin plugin) {
|
||||
return new SimpleCommandBuilder(command.toLowerCase(), plugin);
|
||||
public static CommandBuilder create(String command, Plugin plugin) {
|
||||
return new CommandBuilder(command.toLowerCase(), plugin);
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder command(String command) {
|
||||
public CommandBuilder command(String command) {
|
||||
this.command = command;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder plugin(Plugin plugin) {
|
||||
public CommandBuilder plugin(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder description(String description) {
|
||||
public CommandBuilder description(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder usage(String usage) {
|
||||
public CommandBuilder usage(String usage) {
|
||||
this.usage = usage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder aliases(String... aliases) {
|
||||
public CommandBuilder aliases(String... aliases) {
|
||||
this.aliases = ArrayUtil.asList(aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder permission(String permission) {
|
||||
public CommandBuilder permission(String permission) {
|
||||
this.permission = permission;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder permissionMessage(String permissionMessage) {
|
||||
public CommandBuilder permissionMessage(String permissionMessage) {
|
||||
this.permissionMessage = permissionMessage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder execute(CompleterCommand completerCommand) {
|
||||
public CommandBuilder execute(CompleterCommand completerCommand) {
|
||||
this.completerCommand = completerCommand;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder tab(CompleterTab completerTab) {
|
||||
public CommandBuilder tab(CompleterTab completerTab) {
|
||||
this.completerTab = completerTab;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder forceRegister() {
|
||||
public CommandBuilder forceRegister() {
|
||||
this.forceRegister = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder build() {
|
||||
public CommandBuilder build() {
|
||||
Preconditions.checkNotNull(plugin, "缺少 \"plugin\" 部分");
|
||||
Preconditions.checkNotNull(command, "缺少 \"command\" 部分");
|
||||
Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分");
|
||||
@@ -4,7 +4,7 @@ import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import io.izzel.taboolib.util.Ref;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql;
|
||||
package io.izzel.taboolib.module.db;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql;
|
||||
package io.izzel.taboolib.module.db;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package io.izzel.taboolib.module.mysql.hikari;
|
||||
package io.izzel.taboolib.module.db.source;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.config.TConfig;
|
||||
import io.izzel.taboolib.module.inject.TInject;
|
||||
import io.izzel.taboolib.module.mysql.IHost;
|
||||
import io.izzel.taboolib.module.mysql.builder.SQLHost;
|
||||
import io.izzel.taboolib.module.mysql.lite.SQLiteHost;
|
||||
import io.izzel.taboolib.module.db.IHost;
|
||||
import io.izzel.taboolib.module.db.sql.SQLHost;
|
||||
import io.izzel.taboolib.module.db.sqlite.SQLiteHost;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.mysql.hikari;
|
||||
package io.izzel.taboolib.module.db.source;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import io.izzel.taboolib.module.mysql.IHost;
|
||||
import io.izzel.taboolib.module.db.IHost;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.mysql.builder;
|
||||
package io.izzel.taboolib.module.db.sql;
|
||||
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.module.mysql.IColumn;
|
||||
import io.izzel.taboolib.module.db.IColumn;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql.builder;
|
||||
package io.izzel.taboolib.module.db.sql;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql.builder;
|
||||
package io.izzel.taboolib.module.db.sql;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql.builder;
|
||||
package io.izzel.taboolib.module.db.sql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.izzel.taboolib.module.mysql.builder;
|
||||
package io.izzel.taboolib.module.db.sql;
|
||||
|
||||
import io.izzel.taboolib.module.mysql.IHost;
|
||||
import io.izzel.taboolib.module.db.IHost;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.izzel.taboolib.module.mysql.builder;
|
||||
package io.izzel.taboolib.module.db.sql;
|
||||
|
||||
import io.izzel.taboolib.module.mysql.IColumn;
|
||||
import io.izzel.taboolib.module.mysql.builder.query.RunnableQuery;
|
||||
import io.izzel.taboolib.module.mysql.builder.query.RunnableUpdate;
|
||||
import io.izzel.taboolib.module.db.IColumn;
|
||||
import io.izzel.taboolib.module.db.sql.query.RunnableQuery;
|
||||
import io.izzel.taboolib.module.db.sql.query.RunnableUpdate;
|
||||
import io.izzel.taboolib.util.ArrayUtil;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.mysql.builder.query;
|
||||
package io.izzel.taboolib.module.db.sql.query;
|
||||
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.mysql.builder.SQLExecutor;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import io.izzel.taboolib.module.db.sql.SQLExecutor;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.mysql.builder.query;
|
||||
package io.izzel.taboolib.module.db.sql.query;
|
||||
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.mysql.builder.SQLExecutor;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import io.izzel.taboolib.module.db.sql.SQLExecutor;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql.builder.query;
|
||||
package io.izzel.taboolib.module.db.sql.query;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql.builder.query;
|
||||
package io.izzel.taboolib.module.db.sql.query;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.mysql.lite;
|
||||
package io.izzel.taboolib.module.db.sqlite;
|
||||
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.module.mysql.IColumn;
|
||||
import io.izzel.taboolib.module.db.IColumn;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql.lite;
|
||||
package io.izzel.taboolib.module.db.sqlite;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.mysql.lite;
|
||||
package io.izzel.taboolib.module.db.sqlite;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.izzel.taboolib.module.mysql.lite;
|
||||
package io.izzel.taboolib.module.db.sqlite;
|
||||
|
||||
import io.izzel.taboolib.module.mysql.IHost;
|
||||
import io.izzel.taboolib.module.db.IHost;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
@@ -0,0 +1,95 @@
|
||||
package io.izzel.taboolib.module.db.yaml;
|
||||
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.inject.TListener;
|
||||
import io.izzel.taboolib.common.event.PlayerLoadedEvent;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
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<>();
|
||||
|
||||
public static UsernameType getUsernameType() {
|
||||
return TabooLib.getConfig().getBoolean("ENABLE-UUID") ? UsernameType.UUID : UsernameType.USERNAME;
|
||||
}
|
||||
|
||||
public static FileConfiguration getPlayerData(Player player) {
|
||||
return getUsernameType() == UsernameType.UUID ? loadPlayerData(player.getUniqueId().toString()) : loadPlayerData(player.getName());
|
||||
}
|
||||
|
||||
public static FileConfiguration loadPlayerData(String username) {
|
||||
return PLAYER_DATA.computeIfAbsent(username, n -> YamlConfiguration.loadConfiguration(Files.file(TabooLib.getInst().getPlayerDataFolder(), username + ".yml")));
|
||||
}
|
||||
|
||||
public static void savePlayerData(String username, boolean remove) {
|
||||
// 没有数据
|
||||
if (!PLAYER_DATA.containsKey(username)) {
|
||||
return;
|
||||
}
|
||||
// 读取文件
|
||||
File file = Files.file(TabooLib.getInst().getPlayerDataFolder(), username + ".yml");
|
||||
// 保存配置
|
||||
try {
|
||||
PLAYER_DATA.get(username).save(file);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
// 获取玩家
|
||||
Player player = getUsernameType() == UsernameType.UUID ? Bukkit.getPlayer(UUID.fromString(username)) : Bukkit.getPlayerExact(username);
|
||||
// 移除数据
|
||||
if (remove || player == null) {
|
||||
PLAYER_DATA.remove(username);
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveAllCaches(boolean sync, boolean remove) {
|
||||
if (sync) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(TabooLib.getPlugin(), () -> PLAYER_DATA.keySet().forEach(name -> savePlayerData(name, false)));
|
||||
} else {
|
||||
PLAYER_DATA.keySet().forEach(name -> savePlayerData(name, false));
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveAllPlayers(boolean sync, boolean remove) {
|
||||
if (sync) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(TabooLib.getPlugin(), () -> Bukkit.getOnlinePlayers().forEach(player -> savePlayerData(TabooLib.getConfig().getBoolean("ENABLE-UUID") ? player.getUniqueId().toString() : player.getName(), remove)));
|
||||
} else {
|
||||
Bukkit.getOnlinePlayers().forEach(player -> savePlayerData(TabooLib.getConfig().getBoolean("ENABLE-UUID") ? player.getUniqueId().toString() : player.getName(), remove));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void join(PlayerJoinEvent e) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(TabooLib.getPlugin(), () -> {
|
||||
// 载入数据
|
||||
loadPlayerData(TabooLib.getConfig().getBoolean("ENABLE-UUID") ? e.getPlayer().getUniqueId().toString() : e.getPlayer().getName());
|
||||
// 载入完成
|
||||
Bukkit.getPluginManager().callEvent(new PlayerLoadedEvent(e.getPlayer()));
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(TabooLib.getPlugin(), () -> {
|
||||
// 保存数据
|
||||
savePlayerData(TabooLib.getConfig().getBoolean("ENABLE-UUID") ? e.getPlayer().getUniqueId().toString() : e.getPlayer().getName(), true);
|
||||
});
|
||||
}
|
||||
|
||||
public enum UsernameType {
|
||||
UUID, USERNAME
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package io.izzel.taboolib.module.db.yaml;
|
||||
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class PluginDataManager {
|
||||
|
||||
public static final ConcurrentHashMap<String, HashMap<String, FileConfiguration>> CACHE_DATA_PLUGIN = new ConcurrentHashMap<>();
|
||||
|
||||
public static void saveAllCaches(Plugin plugin) {
|
||||
saveAllCaches(plugin, false);
|
||||
}
|
||||
|
||||
public static void saveAllCaches(Plugin plugin, boolean remove) {
|
||||
if (plugin == null || !CACHE_DATA_PLUGIN.containsKey(plugin.getName())) {
|
||||
return;
|
||||
}
|
||||
for (String fileName : CACHE_DATA_PLUGIN.get(plugin.getName()).keySet()) {
|
||||
saveConfiguration(CACHE_DATA_PLUGIN.get(plugin.getName()).get(fileName), Files.file(getDataSaveFolder(plugin), fileName));
|
||||
}
|
||||
if (remove) {
|
||||
CACHE_DATA_PLUGIN.remove(plugin.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveAllCaches() {
|
||||
saveAllCaches(false);
|
||||
}
|
||||
|
||||
public static void saveAllCaches(boolean remove) {
|
||||
CACHE_DATA_PLUGIN.keySet().forEach(plugin -> saveAllCaches(getFixedPlugin(plugin), remove));
|
||||
}
|
||||
|
||||
public static void saveConfiguration(FileConfiguration conf, File file) {
|
||||
try {
|
||||
conf.save(file);
|
||||
} catch (IOException e) {
|
||||
TLocale.Logger.error("DATA-UTILS.FAIL-SAVE-FILE", file.getName(), e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFixedFileName(String name) {
|
||||
return name.contains(".") ? name : name + ".yml";
|
||||
}
|
||||
|
||||
public static Plugin getFixedPlugin(String pluginName) {
|
||||
return Bukkit.getPluginManager().getPlugin(pluginName) == null ? TabooLib.getPlugin() : Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
}
|
||||
|
||||
public static File getDataSaveFolder(Plugin plugin) {
|
||||
return plugin == null || plugin.getName().equals("TabooLib") ? TabooLib.getInst().getServerDataFolder() : plugin.getDataFolder();
|
||||
}
|
||||
|
||||
public static String getDataSaveKey(Plugin plugin) {
|
||||
return plugin == null ? "TabooLib" : plugin.getName();
|
||||
}
|
||||
|
||||
public static FileConfiguration addPluginData(String name, Plugin plugin) {
|
||||
return setPluginData(getFixedFileName(name), plugin, YamlConfiguration.loadConfiguration(Files.file(getDataSaveFolder(plugin), getFixedFileName(name))));
|
||||
}
|
||||
|
||||
public static FileConfiguration getPluginData(String name, Plugin plugin) {
|
||||
return !CACHE_DATA_PLUGIN.containsKey(getDataSaveKey(plugin)) ? new YamlConfiguration() : CACHE_DATA_PLUGIN.get(getDataSaveKey(plugin)).get(getFixedFileName(name));
|
||||
}
|
||||
|
||||
public static FileConfiguration setPluginData(String name, Plugin plugin, FileConfiguration conf) {
|
||||
if (!CACHE_DATA_PLUGIN.containsKey(getDataSaveKey(plugin))) {
|
||||
CACHE_DATA_PLUGIN.put(getDataSaveKey(plugin), new HashMap<>());
|
||||
}
|
||||
CACHE_DATA_PLUGIN.get(getDataSaveKey(plugin)).put(getFixedFileName(name), conf);
|
||||
return conf;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.inject;
|
||||
|
||||
import io.izzel.taboolib.TabooLibLoader;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.izzel.taboolib.module.inject;
|
||||
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.TabooLibLoader;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -3,13 +3,13 @@ package io.izzel.taboolib.module.inject;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.TabooLibLoader;
|
||||
import io.izzel.taboolib.module.command.lite.SimpleCommandBuilder;
|
||||
import io.izzel.taboolib.module.command.lite.CommandBuilder;
|
||||
import io.izzel.taboolib.module.config.TConfig;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import io.izzel.taboolib.module.packet.TPacketHandler;
|
||||
import io.izzel.taboolib.module.packet.TPacketListener;
|
||||
import io.izzel.taboolib.origin.lite.cooldown.Cooldown;
|
||||
import io.izzel.taboolib.origin.lite.cooldown.Cooldowns;
|
||||
import io.izzel.taboolib.util.lite.cooldown.Cooldown;
|
||||
import io.izzel.taboolib.util.lite.cooldown.Cooldowns;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@@ -58,9 +58,9 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
||||
}
|
||||
});
|
||||
// SimpleCommandBuilder Inject
|
||||
injectTypes.put(SimpleCommandBuilder.class, (plugin, field, args, instance) -> {
|
||||
injectTypes.put(CommandBuilder.class, (plugin, field, args, instance) -> {
|
||||
try {
|
||||
SimpleCommandBuilder builder = (SimpleCommandBuilder) field.get(instance);
|
||||
CommandBuilder builder = (CommandBuilder) field.get(instance);
|
||||
if (builder.isBuild()) {
|
||||
TLogger.getGlobalLogger().error("Command was registered. (" + field.getType().getName() + ")");
|
||||
} else {
|
||||
|
||||
@@ -3,8 +3,8 @@ package io.izzel.taboolib.module.inject;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.TabooLibLoader;
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.origin.cronus.util.StringExpression;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import io.izzel.taboolib.cronus.util.StringExpression;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.util.Reflection;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.TabooLibLoader;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
|
||||
@@ -1,193 +0,0 @@
|
||||
package io.izzel.taboolib.module.item;
|
||||
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.util.ArrayUtil;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
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.List;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-22 11:37
|
||||
* @BuilderVersion 1.1
|
||||
*/
|
||||
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(ItemStack itemStack) {
|
||||
this.itemStack = itemStack;
|
||||
this.itemMeta = itemStack.getItemMeta();
|
||||
}
|
||||
|
||||
public ItemBuilder(OfflinePlayer player) {
|
||||
this(Material.SKULL_ITEM, 1, 3);
|
||||
this.skullOwner(player.getName());
|
||||
}
|
||||
|
||||
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(ArrayUtil.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) {
|
||||
java.util.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 skullOwner(String name) {
|
||||
if (itemMeta instanceof SkullMeta) {
|
||||
((SkullMeta) itemMeta).setOwner(name);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder unbreakable(boolean value) {
|
||||
if (Version.isAfter(Version.v1_12)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,286 +0,0 @@
|
||||
package io.izzel.taboolib.module.item;
|
||||
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.lite.SimpleI18n;
|
||||
import io.izzel.taboolib.module.nms.NMSHandler;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTBase;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTCompound;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTList;
|
||||
import io.izzel.taboolib.origin.lite.Numbers;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.LeatherArmorMeta;
|
||||
import org.bukkit.inventory.meta.PotionMeta;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.util.NumberConversions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-07-05 16:44
|
||||
*/
|
||||
public class Items {
|
||||
|
||||
public final static Integer[] INVENTORY_CENTER = {
|
||||
10, 11, 12, 13, 14, 15, 16,
|
||||
19, 20, 21, 22, 23, 24, 25,
|
||||
28, 29, 30, 31, 32, 33, 34,
|
||||
37, 38, 39, 40, 41, 42, 43
|
||||
};
|
||||
|
||||
public static String getName(ItemStack item) {
|
||||
return SimpleI18n.getCustomName(item);
|
||||
}
|
||||
|
||||
public static boolean isNull(ItemStack item) {
|
||||
return item == null || item.getType().equals(Material.AIR);
|
||||
}
|
||||
|
||||
public static boolean hasLore(ItemStack i, String a) {
|
||||
return hasLore(i) && i.getItemMeta().getLore().toString().contains(a);
|
||||
}
|
||||
|
||||
public static boolean hasLore(ItemStack i) {
|
||||
return !isNull(i) && i.getItemMeta().hasLore();
|
||||
}
|
||||
|
||||
public static boolean hasName(ItemStack i) {
|
||||
return !isNull(i) && i.getItemMeta().hasDisplayName();
|
||||
}
|
||||
|
||||
public static Material asMaterial(String args) {
|
||||
try {
|
||||
Material material = Material.getMaterial(args.toUpperCase());
|
||||
return material != null ? material : Material.getMaterial(Integer.valueOf(args));
|
||||
} catch (Exception e) {
|
||||
return Material.STONE;
|
||||
}
|
||||
}
|
||||
|
||||
public static Enchantment asEnchantment(String enchant) {
|
||||
try {
|
||||
Enchantment enchantment = Enchantment.getByName(enchant);
|
||||
return enchantment != null ? enchantment : Enchantment.getById(Integer.valueOf(enchant));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static PotionEffectType asPotionEffectType(String potion) {
|
||||
try {
|
||||
PotionEffectType type = PotionEffectType.getByName(potion);
|
||||
return type != null ? type : PotionEffectType.getById(Integer.valueOf(potion));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemFlag asItemFlag(String flag) {
|
||||
try {
|
||||
return ItemFlag.valueOf(flag);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Color asColor(String color) {
|
||||
try {
|
||||
return Color.fromBGR(Integer.valueOf(color.split("-")[0]), Integer.valueOf(color.split("-")[1]), Integer.valueOf(color.split("-")[2]));
|
||||
} catch (Exception e) {
|
||||
return Color.fromBGR(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public static String asAttribute(String name) {
|
||||
switch (name.toLowerCase()) {
|
||||
case "damage":
|
||||
return "generic.attackDamage";
|
||||
case "attackspeed":
|
||||
return "generic.attackSpeed";
|
||||
case "health":
|
||||
return "generic.maxHealth";
|
||||
case "speed":
|
||||
return "generic.movementSpeed";
|
||||
case "knockback":
|
||||
return "generic.knockbackResistance";
|
||||
case "armor":
|
||||
return "generic.armor";
|
||||
case "luck":
|
||||
return "generic.luck";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemStack replaceLore(ItemStack item, String loreOld, String loreNew) {
|
||||
if (hasLore(item)) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
List<String> lore = meta.getLore();
|
||||
for (int i = 0; i < lore.size(); i++) {
|
||||
lore.set(i, lore.get(i).replace(loreOld, loreNew));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public static boolean checkItem(Player player, ItemStack item, int amount, boolean remove) {
|
||||
return checkItem(player.getInventory(), item, amount, remove);
|
||||
}
|
||||
|
||||
public static boolean checkItem(Inventory inventory, ItemStack item, int amount, boolean remove) {
|
||||
int hasAmount = 0;
|
||||
for (ItemStack _item : inventory) {
|
||||
if (item.isSimilar(_item)) {
|
||||
hasAmount += _item.getAmount();
|
||||
}
|
||||
}
|
||||
if (hasAmount < amount) {
|
||||
return false;
|
||||
}
|
||||
int requireAmount = amount;
|
||||
for (int i = 0; i < inventory.getSize() && remove; i++) {
|
||||
ItemStack _item = inventory.getItem(i);
|
||||
if (_item != null && _item.isSimilar(item)) {
|
||||
if (_item.getAmount() < requireAmount) {
|
||||
inventory.setItem(i, null);
|
||||
requireAmount -= _item.getAmount();
|
||||
} else if (_item.getAmount() == requireAmount) {
|
||||
inventory.setItem(i, null);
|
||||
return true;
|
||||
} else {
|
||||
_item.setAmount(_item.getAmount() - requireAmount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static ItemStack loadItem(ConfigurationSection section) {
|
||||
if (section.get("bukkit") instanceof ItemStack) {
|
||||
return section.getItemStack("bukkit");
|
||||
}
|
||||
// 材质
|
||||
ItemStack item = new ItemStack(asMaterial(section.getString("material")));
|
||||
// 数量
|
||||
item.setAmount(section.contains("amount") ? section.getInt("amount") : 1);
|
||||
// 耐久
|
||||
item.setDurability((short) section.getInt("data"));
|
||||
// 元数据
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
// 展示名
|
||||
if (section.contains("name")) {
|
||||
meta.setDisplayName(section.getString("name"));
|
||||
}
|
||||
// 描述
|
||||
if (section.contains("lore")) {
|
||||
meta.setLore(section.getStringList("lore"));
|
||||
}
|
||||
// 附魔
|
||||
if (section.contains("enchant")) {
|
||||
for (String preEnchant : section.getConfigurationSection("enchant").getKeys(false)) {
|
||||
Enchantment enchant = asEnchantment(preEnchant);
|
||||
if (enchant != null) {
|
||||
meta.addEnchant(enchant, section.getInt("enchant." + preEnchant), true);
|
||||
} else {
|
||||
TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-ENCHANTS", preEnchant);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 标签
|
||||
if (section.contains("flags") && Version.isAfter(Version.v1_8)) {
|
||||
for (String preFlag : section.getStringList("flags")) {
|
||||
ItemFlag flag = asItemFlag(preFlag);
|
||||
if (flag != null) {
|
||||
meta.addItemFlags(flag);
|
||||
} else {
|
||||
TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-FLAG", preFlag);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 皮革
|
||||
if (meta instanceof LeatherArmorMeta && section.contains("color")) {
|
||||
((LeatherArmorMeta) meta).setColor(asColor(section.getString("color")));
|
||||
}
|
||||
// 药水
|
||||
if (meta instanceof PotionMeta && section.contains("potions")) {
|
||||
PotionMeta potionMeta = (PotionMeta) meta;
|
||||
for (String prePotionName : section.getConfigurationSection("potions").getKeys(false)) {
|
||||
PotionEffectType potionEffectType = asPotionEffectType(prePotionName);
|
||||
if (potionEffectType != null) {
|
||||
potionMeta.addCustomEffect(new PotionEffect(
|
||||
potionEffectType,
|
||||
NumberConversions.toInt(section.getString("potions." + prePotionName).split("-")[0]),
|
||||
NumberConversions.toInt(section.getString("potions." + prePotionName).split("-")[1]) - 1), true);
|
||||
} else {
|
||||
TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-POTION", prePotionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 元数据
|
||||
item.setItemMeta(meta);
|
||||
// 数据
|
||||
NBTCompound nbt = NMSHandler.getHandler().loadNBT(item);
|
||||
// 物品标签
|
||||
if (section.contains("nbt")) {
|
||||
for (String name : section.getConfigurationSection("nbt").getKeys(false)) {
|
||||
Object obj = section.get("nbt." + name);
|
||||
if (obj instanceof String) {
|
||||
nbt.put(name, new NBTBase(obj.toString()));
|
||||
} else if (obj instanceof Double) {
|
||||
nbt.put(name, new NBTBase(NumberConversions.toDouble(obj)));
|
||||
} else if (obj instanceof Integer) {
|
||||
nbt.put(name, new NBTBase(NumberConversions.toInt(obj)));
|
||||
} else if (obj instanceof Long) {
|
||||
nbt.put(name, new NBTBase(NumberConversions.toLong(obj)));
|
||||
}
|
||||
}
|
||||
}
|
||||
// 物品属性
|
||||
if (section.contains("attributes")) {
|
||||
NBTList attr = new NBTList();
|
||||
for (String hand : section.getConfigurationSection("attributes").getKeys(false)) {
|
||||
for (String name : section.getConfigurationSection("attributes." + hand).getKeys(false)) {
|
||||
if (asAttribute(name) != null) {
|
||||
try {
|
||||
NBTCompound a = new NBTCompound();
|
||||
String num = section.getString("attributes." + hand + "." + name);
|
||||
if (num.endsWith("%")) {
|
||||
a.put("Amount", new NBTBase(NumberConversions.toDouble(num.substring(0, num.length() - 1)) / 100D));
|
||||
a.put("Operation", new NBTBase(1));
|
||||
} else {
|
||||
a.put("Amount", new NBTBase(NumberConversions.toDouble(num)));
|
||||
a.put("Operation", new NBTBase(0));
|
||||
}
|
||||
a.put("AttributeName", new NBTBase(asAttribute(name)));
|
||||
a.put("UUIDMost", new NBTBase(Numbers.getRandom().nextInt(Integer.MAX_VALUE)));
|
||||
a.put("UUIDLeast", new NBTBase(Numbers.getRandom().nextInt(Integer.MAX_VALUE)));
|
||||
a.put("Name", new NBTBase(asAttribute(name)));
|
||||
if (!hand.equals("all")) {
|
||||
a.put("Slot", new NBTBase(hand));
|
||||
}
|
||||
attr.add(a);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
} else {
|
||||
TLocale.Logger.error("ITEM-UTILS.FAIL-LOAD-POTION", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
nbt.put("AttributeModifiers", attr);
|
||||
}
|
||||
return NMSHandler.getHandler().saveNBT(item, nbt);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
import org.bukkit.event.inventory.InventoryInteractEvent;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 18:09
|
||||
*/
|
||||
public class ClickEvent {
|
||||
|
||||
private ClickType clickType;
|
||||
private Event event;
|
||||
private char slot;
|
||||
|
||||
public ClickEvent(ClickType clickType, Event event, char slot) {
|
||||
this.clickType = clickType;
|
||||
this.event = event;
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
public InventoryClickEvent castClick() {
|
||||
return (InventoryClickEvent) event;
|
||||
}
|
||||
|
||||
public InventoryDragEvent castDrag() {
|
||||
return (InventoryDragEvent) event;
|
||||
}
|
||||
|
||||
public char getSlot() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
public ClickType getClickType() {
|
||||
return clickType;
|
||||
}
|
||||
|
||||
public Player getClicker() {
|
||||
return (Player) ((InventoryInteractEvent) event).getWhoClicked();
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
import io.izzel.taboolib.module.inject.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||
import org.bukkit.event.player.PlayerItemHeldEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 18:16
|
||||
*/
|
||||
@TListener
|
||||
class ClickListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onDisable(PluginDisableEvent e) {
|
||||
Bukkit.getOnlinePlayers().stream().filter(player -> player.getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && e.getPlugin().equals(((MenuHolder) player.getOpenInventory().getTopInventory().getHolder()).getBuilder().getPlugin())).forEach(HumanEntity::closeInventory);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onClick(InventoryClickEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof MenuHolder) {
|
||||
if (((MenuHolder) e.getInventory().getHolder()).getBuilder().isLockHand() && (e.getRawSlot() - e.getInventory().getSize() - 27 == e.getWhoClicked().getInventory().getHeldItemSlot() || (e.getClick() == org.bukkit.event.inventory.ClickType.NUMBER_KEY && e.getHotbarButton() == e.getWhoClicked().getInventory().getHeldItemSlot()))) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.CLICK, e, ((MenuHolder) e.getInventory().getHolder()).getBuilder().getSlot(e.getRawSlot()))));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDrag(InventoryDragEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof MenuHolder) {
|
||||
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.DRAG, e, ' ')));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDrag(InventoryCloseEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof MenuHolder) {
|
||||
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getCloseTask()).ifPresent(t -> t.run(e));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDrop(PlayerDropItemEvent e) {
|
||||
if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onHeld(PlayerItemHeldEvent e) {
|
||||
if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
import io.izzel.taboolib.module.inject.TListener;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 22:04
|
||||
*/
|
||||
@TListener(version = ">=10900")
|
||||
class ClickListenerOffhand implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onSwap(PlayerSwapHandItemsEvent e) {
|
||||
if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 18:14
|
||||
*/
|
||||
public interface ClickTask {
|
||||
|
||||
void run(ClickEvent event);
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 18:10
|
||||
*/
|
||||
public enum ClickType {
|
||||
|
||||
CLICK, DRAG
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 18:14
|
||||
*/
|
||||
public interface CloseTask {
|
||||
|
||||
void run(InventoryCloseEvent event);
|
||||
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.util.Ref;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 15:54
|
||||
*/
|
||||
public class MenuBuilder {
|
||||
|
||||
private Map<Character, ItemStack> keys = Maps.newHashMap();
|
||||
private Plugin plugin;
|
||||
private String title;
|
||||
private int rows;
|
||||
private char[][] items ;
|
||||
private ClickTask clickTask;
|
||||
private CloseTask closeTask;
|
||||
private boolean lockHand;
|
||||
|
||||
public MenuBuilder(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public static MenuBuilder builder(Plugin plugin) {
|
||||
return new MenuBuilder(plugin);
|
||||
}
|
||||
|
||||
public static MenuBuilder builder() {
|
||||
return new MenuBuilder(Ref.getCallerPlugin(Ref.getCallerClass(3).orElse(TabooLib.class)));
|
||||
}
|
||||
|
||||
public MenuBuilder lockHand() {
|
||||
this.lockHand = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder event(ClickTask clickTask) {
|
||||
this.clickTask = clickTask;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder close(CloseTask closeTask) {
|
||||
this.closeTask = closeTask;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder title(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder rows(int rows) {
|
||||
this.rows = rows * 9;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder put(char key, ItemStack item) {
|
||||
keys.put(key, item);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MenuBuilder items(String... placeholder) {
|
||||
items = new char[placeholder.length][];
|
||||
for (int i = 0; i < placeholder.length; i++) {
|
||||
items[i] = placeholder[i].toCharArray();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Inventory build() {
|
||||
Inventory inventory = Bukkit.createInventory(new MenuHolder(this), rows, String.valueOf(title));
|
||||
for (int i = 0; i < items.length && i < rows; i++) {
|
||||
char[] line = items[i];
|
||||
for (int j = 0; j < line.length && j < 9; j++) {
|
||||
inventory.setItem(i * 9 + j, keys.getOrDefault(line[j], new ItemStack(Material.AIR)));
|
||||
}
|
||||
}
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public char getSlot(int slot) {
|
||||
for (int i = 0; i < items.length && i < rows; i++) {
|
||||
char[] line = items[i];
|
||||
for (int j = 0; j < line.length && j < 9; j++) {
|
||||
if (i * 9 + j == slot) {
|
||||
return line[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return ' ';
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public Map<Character, ItemStack> getKeys() {
|
||||
return keys;
|
||||
}
|
||||
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public int getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public char[][] getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public ClickTask getClickTask() {
|
||||
return clickTask;
|
||||
}
|
||||
|
||||
public CloseTask getCloseTask() {
|
||||
return closeTask;
|
||||
}
|
||||
|
||||
public boolean isLockHand() {
|
||||
return lockHand;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package io.izzel.taboolib.module.item.inventory;
|
||||
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-21 20:28
|
||||
*/
|
||||
class MenuHolder implements InventoryHolder {
|
||||
|
||||
private MenuBuilder builder;
|
||||
|
||||
public MenuHolder(MenuBuilder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public MenuBuilder getBuilder() {
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.locale;
|
||||
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.logger.TLoggerManager;
|
||||
import io.izzel.taboolib.module.locale.logger.TLoggerManager;
|
||||
import io.izzel.taboolib.module.nms.NMSHandler;
|
||||
import io.izzel.taboolib.module.tellraw.TellrawCreator;
|
||||
import io.izzel.taboolib.util.Ref;
|
||||
|
||||
@@ -4,7 +4,7 @@ import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.module.locale.type.*;
|
||||
import io.izzel.taboolib.module.config.TConfigWatcher;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import io.izzel.taboolib.util.IO;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.logger;
|
||||
package io.izzel.taboolib.module.locale.logger;
|
||||
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.izzel.taboolib.module.logger;
|
||||
package io.izzel.taboolib.module.locale.logger;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@@ -5,8 +5,8 @@ import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.module.tellraw.TellrawJson;
|
||||
import io.izzel.taboolib.origin.book.BookFormatter;
|
||||
import io.izzel.taboolib.origin.book.builder.BookBuilder;
|
||||
import io.izzel.taboolib.util.book.BookFormatter;
|
||||
import io.izzel.taboolib.util.book.builder.BookBuilder;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.util.chat.ComponentSerializer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.origin.lite.Sounds;
|
||||
import io.izzel.taboolib.util.lite.SoundPack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -21,19 +21,19 @@ import java.util.stream.Collectors;
|
||||
@SerializableAs("ACTION")
|
||||
public class TLocaleSound extends TLocaleSerialize {
|
||||
|
||||
private final List<Sounds> soundPacks;
|
||||
private final List<SoundPack> soundPacks;
|
||||
|
||||
public TLocaleSound(List<Sounds> soundPacks) {
|
||||
public TLocaleSound(List<SoundPack> soundPacks) {
|
||||
this.soundPacks = soundPacks;
|
||||
}
|
||||
|
||||
public static TLocaleSound valueOf(Map<String, Object> map) {
|
||||
List<Sounds> soundPacks = new ArrayList<>();
|
||||
List<SoundPack> soundPacks = new ArrayList<>();
|
||||
Object sounds = map.containsKey("sounds") ? map.get("sounds") : map.getOrDefault("sound", "");
|
||||
if (sounds instanceof List) {
|
||||
soundPacks = ((List<String>) sounds).stream().map(Sounds::new).collect(Collectors.toList());
|
||||
soundPacks = ((List<String>) sounds).stream().map(SoundPack::new).collect(Collectors.toList());
|
||||
} else {
|
||||
soundPacks.add(new Sounds(sounds.toString()));
|
||||
soundPacks.add(new SoundPack(sounds.toString()));
|
||||
}
|
||||
return new TLocaleSound(soundPacks);
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class TLocaleSound extends TLocaleSerialize {
|
||||
if (soundPacks.size() == 1) {
|
||||
map.put("sounds", soundPacks.get(0).toString());
|
||||
} else if (soundPacks.size() > 1) {
|
||||
map.put("sounds", soundPacks.stream().map(Sounds::toString).collect(Collectors.toList()));
|
||||
map.put("sounds", soundPacks.stream().map(SoundPack::toString).collect(Collectors.toList()));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.izzel.taboolib.module.packet.channel;
|
||||
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import io.netty.channel.Channel;
|
||||
import net.minecraft.server.v1_8_R3.Packet;
|
||||
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package io.izzel.taboolib.module.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 {
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package io.izzel.taboolib.module.serialize;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-03-08 17:28
|
||||
*/
|
||||
public interface TSerializable {
|
||||
|
||||
default void read(String fieldName, String value) {
|
||||
}
|
||||
|
||||
default String write(String fieldName, Object value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package io.izzel.taboolib.module.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
|
||||
*
|
||||
* 用于标识 Collection 类型的字段
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TSerializeCollection {
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package io.izzel.taboolib.module.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
|
||||
*
|
||||
* 用于标识 TSerializable 类型的字段
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TSerializeCustom {
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package io.izzel.taboolib.module.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
|
||||
*
|
||||
* 用于标识 Map 类型的字段
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TSerializeMap {
|
||||
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
package io.izzel.taboolib.module.serialize;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.gson.*;
|
||||
import io.izzel.taboolib.module.lite.SimpleReflection;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-03-08 17:31
|
||||
*/
|
||||
public class TSerializer {
|
||||
|
||||
private static Map<String, TSerializerElement> generated = Maps.newHashMap();
|
||||
|
||||
public static TSerializable read(TSerializable serializable, String serializedString) {
|
||||
SimpleReflection.checkAndSave(serializable.getClass());
|
||||
try {
|
||||
JsonObject jsonObject = (JsonObject) new JsonParser().parse(serializedString);
|
||||
if (jsonObject.has("serializeObject")) {
|
||||
JsonObject serializeObject = jsonObject.getAsJsonObject("serializeObject");
|
||||
for (Map.Entry<String, JsonElement> jsonElementEntry : serializeObject.entrySet()) {
|
||||
try {
|
||||
Field declaredField = SimpleReflection.getField(serializable.getClass(), jsonElementEntry.getKey());
|
||||
if (declaredField.isAnnotationPresent(DoNotSerialize.class)) {
|
||||
continue;
|
||||
}
|
||||
// Serializable
|
||||
if (declaredField.isAnnotationPresent(TSerializeCustom.class) && TSerializable.class.isAssignableFrom(declaredField.getType())) {
|
||||
declaredField.set(serializable, generateElement((Class<? extends TSerializable>) declaredField.getType()).read(jsonElementEntry.getValue().getAsString()));
|
||||
}
|
||||
// List
|
||||
else if (declaredField.isAnnotationPresent(TSerializeCollection.class) && Collection.class.isAssignableFrom(declaredField.getType())) {
|
||||
Class listType = SimpleReflection.getListType(declaredField);
|
||||
if (listType == null) {
|
||||
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
|
||||
continue;
|
||||
}
|
||||
TSerializerElementGeneral serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(listType)).findFirst().orElse(null);
|
||||
if (serializer == null) {
|
||||
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
|
||||
} else {
|
||||
readCollection((Collection) declaredField.get(serializable), jsonElementEntry.getValue().getAsString(), checkCustom(listType, serializer));
|
||||
}
|
||||
}
|
||||
// Map
|
||||
else if (declaredField.isAnnotationPresent(TSerializeMap.class) && Map.class.isAssignableFrom(declaredField.getType())) {
|
||||
Class[] mapType = SimpleReflection.getMapType(declaredField);
|
||||
if (mapType == null) {
|
||||
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
|
||||
continue;
|
||||
}
|
||||
TSerializerElementGeneral serializerK = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(mapType[0])).findFirst().orElse(null);
|
||||
TSerializerElementGeneral serializerV = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(mapType[1])).findFirst().orElse(null);
|
||||
if (serializerK == null || serializerV == null) {
|
||||
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
|
||||
} else {
|
||||
readMap((Map) declaredField.get(serializable), jsonElementEntry.getValue().getAsString(), checkCustom(mapType[0], serializerK), checkCustom(mapType[1], serializerV));
|
||||
}
|
||||
}
|
||||
// 未声明类型
|
||||
else {
|
||||
TSerializerElementGeneral serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst().orElse(null);
|
||||
if (serializer == null) {
|
||||
serializable.read(jsonElementEntry.getKey(), jsonElementEntry.getValue().getAsString());
|
||||
} else {
|
||||
declaredField.set(serializable, serializer.getSerializer().read(jsonElementEntry.getValue().getAsString()));
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return serializable;
|
||||
}
|
||||
|
||||
public static String write(TSerializable serializable) {
|
||||
SimpleReflection.checkAndSave(serializable.getClass());
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
JsonObject serializeObject = new JsonObject();
|
||||
for (Field declaredField : SimpleReflection.getFields(serializable.getClass()).values()) {
|
||||
try {
|
||||
if (!declaredField.isAnnotationPresent(DoNotSerialize.class) && !Modifier.isStatic(declaredField.getModifiers())) {
|
||||
Object fieldObject = declaredField.get(serializable);
|
||||
if (fieldObject == null) {
|
||||
continue;
|
||||
}
|
||||
// Serializable
|
||||
if (declaredField.isAnnotationPresent(TSerializeCustom.class) && TSerializable.class.isAssignableFrom(declaredField.getType())) {
|
||||
serializeObject.addProperty(declaredField.getName(), generateElement((Class<? extends TSerializable>) declaredField.getType()).write(fieldObject));
|
||||
}
|
||||
// List
|
||||
else if (declaredField.isAnnotationPresent(TSerializeCollection.class) && Collection.class.isAssignableFrom(declaredField.getType())) {
|
||||
Class listType = SimpleReflection.getListType(declaredField);
|
||||
if (listType == null) {
|
||||
Optional.ofNullable(serializable.write(declaredField.getName(), fieldObject)).ifPresent(value -> serializeObject.addProperty(declaredField.getName(), value));
|
||||
continue;
|
||||
}
|
||||
TSerializerElementGeneral serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(listType)).findFirst().orElse(null);
|
||||
if (serializer == null) {
|
||||
Optional.ofNullable(serializable.write(declaredField.getName(), fieldObject)).ifPresent(value -> serializeObject.addProperty(declaredField.getName(), value));
|
||||
} else {
|
||||
serializeObject.addProperty(declaredField.getName(), writeCollection((Collection) fieldObject, checkCustom(listType, serializer)));
|
||||
}
|
||||
}
|
||||
// Map
|
||||
else if (declaredField.isAnnotationPresent(TSerializeMap.class) && Map.class.isAssignableFrom(declaredField.getType())) {
|
||||
Class[] mapType = SimpleReflection.getMapType(declaredField);
|
||||
if (mapType == null) {
|
||||
Optional.ofNullable(serializable.write(declaredField.getName(), fieldObject)).ifPresent(value -> serializeObject.addProperty(declaredField.getName(), value));
|
||||
continue;
|
||||
}
|
||||
TSerializerElementGeneral serializerK = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(mapType[0])).findFirst().orElse(null);
|
||||
TSerializerElementGeneral serializerV = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(mapType[1])).findFirst().orElse(null);
|
||||
if (serializerK == null || serializerV == null) {
|
||||
Optional.ofNullable(serializable.write(declaredField.getName(), fieldObject)).ifPresent(value -> serializeObject.addProperty(declaredField.getName(), value));
|
||||
} else {
|
||||
serializeObject.addProperty(declaredField.getName(), writeMap((Map) fieldObject, checkCustom(mapType[0], serializerK), checkCustom(mapType[1], serializerV)));
|
||||
}
|
||||
}
|
||||
// 未声明类型
|
||||
else {
|
||||
TSerializerElementGeneral serializer = Arrays.stream(TSerializerElementGeneral.values()).filter(serializerElements -> serializerElements.getSerializer().matches(declaredField.getType())).findFirst().orElse(null);
|
||||
if (serializer == null) {
|
||||
Optional.ofNullable(serializable.write(declaredField.getName(), fieldObject)).ifPresent(value -> serializeObject.addProperty(declaredField.getName(), value));
|
||||
} else {
|
||||
serializeObject.addProperty(declaredField.getName(), serializer.getSerializer().write(fieldObject));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
jsonObject.add("serializeObject", serializeObject);
|
||||
return jsonObject.toString();
|
||||
}
|
||||
|
||||
public static void readMap(Map map, String serializedString, TSerializerElementGeneral elementKey, TSerializerElementGeneral elementValue) {
|
||||
readMap(map, serializedString, elementKey.getSerializer(), elementValue.getSerializer());
|
||||
}
|
||||
|
||||
public static void readMap(Map map, String serializedString, TSerializerElement elementKey, TSerializerElement elementValue) {
|
||||
try {
|
||||
JsonObject jsonObject = (JsonObject) new JsonParser().parse(serializedString);
|
||||
for (Map.Entry<String, JsonElement> jsonElementEntry : jsonObject.entrySet()) {
|
||||
try {
|
||||
map.put(elementKey.read(jsonElementEntry.getKey()), elementValue.read(jsonElementEntry.getValue().getAsString()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String writeMap(Map<?, ?> map, TSerializerElementGeneral elementKey, TSerializerElementGeneral elementValue) {
|
||||
return writeMap(map, elementKey.getSerializer(), elementValue.getSerializer());
|
||||
}
|
||||
|
||||
public static String writeMap(Map<?, ?> map, TSerializerElement elementKey, TSerializerElement elementValue) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
||||
try {
|
||||
jsonObject.addProperty(elementKey.write(entry.getKey()), elementValue.write(entry.getValue()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
return jsonObject.toString();
|
||||
}
|
||||
|
||||
public static void readCollection(Collection collection, String serializedString, TSerializerElementGeneral elementValue) {
|
||||
readCollection(collection, serializedString, elementValue.getSerializer());
|
||||
}
|
||||
|
||||
public static void readCollection(Collection collection, String serializedString, TSerializerElement elementValue) {
|
||||
try {
|
||||
JsonArray jsonArray = (JsonArray) new JsonParser().parse(serializedString);
|
||||
for (JsonElement jsonElement : jsonArray) {
|
||||
try {
|
||||
collection.add(elementValue.read(jsonElement.getAsString()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static String writeCollection(Collection collection, TSerializerElementGeneral elementValue) {
|
||||
return writeCollection(collection, elementValue.getSerializer());
|
||||
}
|
||||
|
||||
public static String writeCollection(Collection collection, TSerializerElement elementValue) {
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (Object object : collection) {
|
||||
try {
|
||||
jsonArray.add(new JsonPrimitive(elementValue.write(object)));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
return jsonArray.toString();
|
||||
}
|
||||
|
||||
public static String serializeCS(ConfigurationSerializable o) {
|
||||
YamlConfiguration y = new YamlConfiguration();
|
||||
y.set("value", o);
|
||||
return y.saveToString();
|
||||
}
|
||||
|
||||
public static <T extends ConfigurationSerializable> T deserializeCS(String s, Class<T> c) {
|
||||
YamlConfiguration y = new YamlConfiguration();
|
||||
try {
|
||||
y.loadFromString(s);
|
||||
} catch (InvalidConfigurationException var4) {
|
||||
return null;
|
||||
}
|
||||
Object o = y.get("value");
|
||||
return !c.isInstance(o) ? null : (T) o;
|
||||
}
|
||||
|
||||
public static TSerializerElement generateElement(Class<? extends TSerializable> serializable) {
|
||||
return generated.computeIfAbsent(serializable.getName(), n -> new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Object read(String value) {
|
||||
try {
|
||||
return serializable.newInstance().read(value);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return ((TSerializable) value).write();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static TSerializerElement checkCustom(Class type, TSerializerElementGeneral serializer) {
|
||||
return serializer == TSerializerElementGeneral.CUSTOM ? generateElement(type) : serializer.getSerializer();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package io.izzel.taboolib.module.serialize;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-03-08 17:28
|
||||
*/
|
||||
public interface TSerializerElement<T> {
|
||||
|
||||
T read(String value);
|
||||
|
||||
String write(T value);
|
||||
|
||||
boolean matches(Class objectClass);
|
||||
}
|
||||
@@ -1,234 +0,0 @@
|
||||
package io.izzel.taboolib.module.serialize;
|
||||
|
||||
import io.izzel.taboolib.origin.lite.Numbers;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-03-10 18:49
|
||||
*/
|
||||
public enum TSerializerElementGeneral {
|
||||
|
||||
STRING(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public String read(String value) {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return Objects.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return String.class.equals(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
INT(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Integer read(String value) {
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return Objects.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return Integer.class.equals(objectClass) || Integer.TYPE.equals(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
LONG(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Long read(String value) {
|
||||
try {
|
||||
return Long.parseLong(value);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return Objects.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return Long.class.equals(objectClass) || Long.TYPE.equals(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
SHORT(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Short read(String value) {
|
||||
try {
|
||||
return Short.parseShort(value);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return (short) 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return Objects.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return Short.class.equals(objectClass) || Short.TYPE.equals(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
DOUBLE(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Double read(String value) {
|
||||
try {
|
||||
return Double.parseDouble(value);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return 0D;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return Objects.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return Double.class.equals(objectClass) || Boolean.TYPE.equals(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
FLOAT(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Float read(String value) {
|
||||
try {
|
||||
return Float.parseFloat(value);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return 0f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return Objects.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return Float.class.equals(objectClass) || Float.TYPE.equals(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
BOOLEAN(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Boolean read(String value) {
|
||||
try {
|
||||
return Numbers.getBoolean(value);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return Objects.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return Boolean.class.equals(objectClass) || Boolean.TYPE.equals(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
ITEM_STACK(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public ItemStack read(String value) {
|
||||
return TSerializer.deserializeCS(value, ItemStack.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return TSerializer.serializeCS((ItemStack) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return ItemStack.class.isAssignableFrom(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
LOCATION(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Location read(String value) {
|
||||
return TSerializer.deserializeCS(value, Location.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return TSerializer.serializeCS((Location) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return Location.class.isAssignableFrom(objectClass);
|
||||
}
|
||||
}),
|
||||
|
||||
CUSTOM(new TSerializerElement() {
|
||||
|
||||
@Override
|
||||
public Object read(String value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String write(Object value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Class objectClass) {
|
||||
return TSerializable.class.isAssignableFrom(objectClass);
|
||||
}
|
||||
});
|
||||
|
||||
private TSerializerElement serializer;
|
||||
|
||||
TSerializerElementGeneral(TSerializerElement serializer) {
|
||||
this.serializer = serializer;
|
||||
}
|
||||
|
||||
public TSerializerElement getSerializer() {
|
||||
return serializer;
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package io.izzel.taboolib.module.serialize;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.util.ArrayUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author 坏黑
|
||||
* @Since 2019-04-01 17:49
|
||||
*/
|
||||
public class TSerializerExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 创建对象
|
||||
SimpleData data = new SimpleData();
|
||||
// 修改参数
|
||||
data.number = 9999;
|
||||
data.list.addAll(ArrayUtil.asList(111D, 222D));
|
||||
data.map.putAll(ImmutableMap.of("a", "b", "c", "d"));
|
||||
// 序列化
|
||||
String value = data.write();
|
||||
// 打印
|
||||
System.out.println(value);
|
||||
// 创建新的对象
|
||||
SimpleData dataCopy = new SimpleData();
|
||||
// 反序列化
|
||||
dataCopy.read(value);
|
||||
// 打印
|
||||
System.out.println(dataCopy);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建序列化类
|
||||
* 实现 TSerializable 接口
|
||||
*/
|
||||
public static class SimpleData implements TSerializable {
|
||||
|
||||
/**
|
||||
* 基本类型不需要手动进行序列化
|
||||
* 包含: String、int、short、long、double、float、boolean、ItemStack、Location
|
||||
*/
|
||||
private String text = "123";
|
||||
private int number = 100;
|
||||
|
||||
/**
|
||||
* 包含基本类型的容器需要通过标注来完成序列化
|
||||
*/
|
||||
@TSerializeCollection
|
||||
private List<Double> list = Lists.newArrayList();
|
||||
|
||||
@TSerializeMap
|
||||
private Map<String, String> map = Maps.newHashMap();
|
||||
|
||||
/**
|
||||
* 跳过序列化
|
||||
*/
|
||||
@DoNotSerialize
|
||||
private String ignoreSerialize = "aaa";
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleData{" +
|
||||
"text='" + text + '\'' +
|
||||
", number=" + number +
|
||||
", list=" + list +
|
||||
", map=" + map +
|
||||
", ignoreSerialize='" + ignoreSerialize + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.tellraw.internal;
|
||||
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.module.item.Items;
|
||||
import io.izzel.taboolib.util.item.Items;
|
||||
import io.izzel.taboolib.module.lite.SimpleReflection;
|
||||
import io.izzel.taboolib.module.packet.TPacketHandler;
|
||||
import io.izzel.taboolib.module.tellraw.TellrawVersion;
|
||||
|
||||
Reference in New Issue
Block a user