首次上传
This commit is contained in:
parent
a34df5a95b
commit
af29cbe4f0
288
src/main/src/me/skymc/taboolib/Main.java
Normal file
288
src/main/src/me/skymc/taboolib/Main.java
Normal file
@ -0,0 +1,288 @@
|
||||
package me.skymc.taboolib;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
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.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.skymc.taboolib.anvil.AnvilContainerAPI;
|
||||
import me.skymc.taboolib.client.LogClient;
|
||||
import me.skymc.taboolib.commands.MainCommands;
|
||||
import me.skymc.taboolib.commands.sub.itemlist.listener.ItemLibraryPatch;
|
||||
import me.skymc.taboolib.database.PlayerDataManager;
|
||||
import me.skymc.taboolib.database.GlobalDataManager;
|
||||
import me.skymc.taboolib.economy.EcoUtils;
|
||||
import me.skymc.taboolib.entity.EntityUtils;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.javashell.JavaShell;
|
||||
import me.skymc.taboolib.listener.ListenerPlayerCommand;
|
||||
import me.skymc.taboolib.listener.ListenerPlayerQuit;
|
||||
import me.skymc.taboolib.listener.ListenerPlayerJump;
|
||||
import me.skymc.taboolib.listener.ListenerPluginDisable;
|
||||
import me.skymc.taboolib.message.ChatCatcher;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.mysql.protect.MySQLConnection;
|
||||
import me.skymc.taboolib.sign.SignUtils;
|
||||
import me.skymc.taboolib.string.StringUtils;
|
||||
import me.skymc.taboolib.support.SupportPlaceholder;
|
||||
import me.skymc.taboolib.team.TagUtils;
|
||||
import me.skymc.taboolib.timecycle.TimeCycleManager;
|
||||
import me.skymc.taboolib.nms.item.DabItemUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.permission.PermissionUtils;
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class Main extends JavaPlugin implements Listener {
|
||||
|
||||
@Getter
|
||||
private static Plugin inst;
|
||||
@Getter
|
||||
private static String prefix = "§8[§3§lTabooLib§8] §7";
|
||||
@Getter
|
||||
@Setter
|
||||
private static Economy Economy;
|
||||
@Getter
|
||||
private static File playerDataFolder;
|
||||
@Getter
|
||||
private static File serverDataFolder;
|
||||
@Getter
|
||||
private static File docsFolder;
|
||||
@Getter
|
||||
private static StorageType storageType;
|
||||
@Getter
|
||||
private static boolean disable = false;
|
||||
@Getter
|
||||
private static MySQLConnection connection = null;
|
||||
@Getter
|
||||
private FileConfiguration config = null;
|
||||
@Getter
|
||||
private static LogClient client;
|
||||
|
||||
public static Random getRandom() {
|
||||
return NumberUtils.getRand();
|
||||
}
|
||||
|
||||
public static String getTablePrefix() {
|
||||
return inst.getConfig().getString("MYSQL.PREFIX");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveDefaultConfig() {
|
||||
reloadConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reloadConfig() {
|
||||
File file = new File(getDataFolder(), "config.yml");
|
||||
if (!file.exists()) {
|
||||
saveResource("config.yml", true);
|
||||
}
|
||||
config = ConfigUtils.load(this, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
inst = this; disable = false;
|
||||
|
||||
// 载入配置
|
||||
saveDefaultConfig();
|
||||
// 载入目录
|
||||
setupDataFolder();
|
||||
// 注册配置
|
||||
DataUtils.addPluginData("TabooLibrary", null);
|
||||
|
||||
// 启用数据库
|
||||
if (getConfig().getBoolean("MYSQL.ENABLE")) {
|
||||
// 连接数据库
|
||||
connection = new MySQLConnection(getConfig().getString("MYSQL.HOST"), getConfig().getString("MYSQL.USER"), getConfig().getString("MYSQL.POST"), getConfig().getString("MYSQL.PASSWORD"), getConfig().getString("MYSQL.DATABASE"), 30, this);
|
||||
// 连接成功
|
||||
if (connection.isConnection()) {
|
||||
// 创建表
|
||||
connection.createTable(getTablePrefix() + "_playerdata", "username", "configuration");
|
||||
connection.createTable(getTablePrefix() + "_plugindata", "name", "variable", "upgrade");
|
||||
connection.createTable(getTablePrefix() + "_serveruuid", "uuid", "hash");
|
||||
|
||||
// 如果没有数据
|
||||
if (!connection.isExists(getTablePrefix() + "_serveruuid", "uuid", TabooLib.getServerUID())) {
|
||||
connection.intoValue(getTablePrefix() + "_serveruuid", TabooLib.getServerUID(), StringUtils.hashKeyForDisk(getDataFolder().getPath()));
|
||||
}
|
||||
else {
|
||||
String hash = connection.getValue(getTablePrefix() + "_serveruuid", "uuid", TabooLib.getServerUID(), "hash").toString();
|
||||
// 如果这个值和我的值不同
|
||||
if (!hash.equals(StringUtils.hashKeyForDisk(getDataFolder().getPath()))) {
|
||||
MsgUtils.warn("检测到本服序列号与其他服务器相同, 已重新生成!");
|
||||
// 重新生成序列号
|
||||
TabooLib.resetServerUID();
|
||||
// 关服
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 提示
|
||||
MsgUtils.warn("数据库连接失败, 请检查配置是否正确!");
|
||||
// 关服
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
// 储存方式
|
||||
storageType = StorageType.SQL;
|
||||
}
|
||||
else {
|
||||
// 储存方式
|
||||
storageType = StorageType.LOCAL;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// 注册指令
|
||||
getCommand("taboolib").setExecutor(new MainCommands());
|
||||
|
||||
// 注册监听
|
||||
registerListener();
|
||||
// 载入经济
|
||||
EcoUtils.setupEconomy();
|
||||
// 载入权限
|
||||
PermissionUtils.loadRegisteredServiceProvider();
|
||||
// 物品名称
|
||||
ItemUtils.LoadLib();
|
||||
// 低层工具
|
||||
DabItemUtils.getInstance();
|
||||
// 载入周期管理器
|
||||
TimeCycleManager.load();
|
||||
// 启动脚本
|
||||
JavaShell.javaShellSetup();
|
||||
|
||||
// 启动数据库储存方法
|
||||
if (getStorageType() == StorageType.SQL) {
|
||||
GlobalDataManager.SQLMethod.startSQLMethod();
|
||||
}
|
||||
|
||||
// 载入完成
|
||||
MsgUtils.send("§7插件载入完成!");
|
||||
MsgUtils.send("§7插件版本: §f" + getDescription().getVersion());
|
||||
MsgUtils.send("§7插件作者: §f" + getDescription().getAuthors());
|
||||
MsgUtils.send("§7游戏版本: §f" + TabooLib.getVerint());
|
||||
|
||||
// 文件保存
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> DataUtils.saveAllCaches(), 0, 20 * 120);
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> PlayerDataManager.saveAllCaches(true, false), 0, 20 * 60);
|
||||
|
||||
// 插件联动
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
new SupportPlaceholder(getInst(), "taboolib").hook();
|
||||
}
|
||||
}
|
||||
}.runTask(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
disable = true;
|
||||
|
||||
// 保存数据
|
||||
Bukkit.getOnlinePlayers().forEach(x -> DataUtils.saveOnline(x.getName()));
|
||||
// 结束线程
|
||||
Bukkit.getScheduler().cancelTasks(this);
|
||||
// 保存插件数据
|
||||
DataUtils.saveAllCaches();
|
||||
// 保存玩家数据
|
||||
PlayerDataManager.saveAllPlayers(false, true);
|
||||
// 结束脚本
|
||||
JavaShell.javaShellCancel();
|
||||
|
||||
// 结束数据库储存方法
|
||||
if (getStorageType() == StorageType.SQL) {
|
||||
GlobalDataManager.SQLMethod.cancelSQLMethod();
|
||||
}
|
||||
|
||||
// 清理数据
|
||||
if (getStorageType() == StorageType.LOCAL && getConfig().getBoolean("DELETE-DATA")) {
|
||||
getPlayerDataFolder().delete();
|
||||
}
|
||||
// 清理数据
|
||||
if (getStorageType() == StorageType.SQL && getConfig().getBoolean("DELETE-VARIABLE")) {
|
||||
GlobalDataManager.clearInvalidVariables();
|
||||
}
|
||||
|
||||
// 提示信息
|
||||
MsgUtils.send("&c插件已卸载, 感谢您使用&4禁忌书库");
|
||||
MsgUtils.send("&c插件作者: &4坏黑");
|
||||
|
||||
// 清理头衔
|
||||
TagUtils.delete();
|
||||
|
||||
// 结束连接
|
||||
if (connection != null && connection.isConnection()) {
|
||||
connection.closeConnection();
|
||||
}
|
||||
|
||||
// 关闭服务器
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
|
||||
private void setupDataFolder() {
|
||||
playerDataFolder = new File(getConfig().getString("DATAURL.PLAYER-DATA"));
|
||||
if (!playerDataFolder.exists()) {
|
||||
playerDataFolder.mkdirs();
|
||||
}
|
||||
|
||||
serverDataFolder = new File(getConfig().getString("DATAURL.SERVER-DATA"));
|
||||
if (!serverDataFolder.exists()) {
|
||||
serverDataFolder.mkdirs();
|
||||
}
|
||||
|
||||
docsFolder = new File(getDataFolder(), "Document");
|
||||
if (!docsFolder.exists()) {
|
||||
docsFolder.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
private void registerListener() {
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPlayerCommand(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPlayerJump(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPlayerQuit(), this);
|
||||
getServer().getPluginManager().registerEvents(new ChatCatcher(), this);
|
||||
getServer().getPluginManager().registerEvents(new DataUtils(), this);
|
||||
getServer().getPluginManager().registerEvents(new AnvilContainerAPI(), this);
|
||||
getServer().getPluginManager().registerEvents(new ListenerPluginDisable(), this);
|
||||
getServer().getPluginManager().registerEvents(new PlayerDataManager(), this);
|
||||
getServer().getPluginManager().registerEvents(new ItemLibraryPatch(), this);
|
||||
|
||||
if (TabooLib.getVerint() > 10700) {
|
||||
getServer().getPluginManager().registerEvents(new EntityUtils(), this);
|
||||
getServer().getPluginManager().registerEvents(new SignUtils(), this);
|
||||
}
|
||||
}
|
||||
|
||||
public static enum StorageType {
|
||||
LOCAL, SQL;
|
||||
}
|
||||
}
|
86
src/main/src/me/skymc/taboolib/TabooLib.java
Normal file
86
src/main/src/me/skymc/taboolib/TabooLib.java
Normal file
@ -0,0 +1,86 @@
|
||||
package me.skymc.taboolib;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class TabooLib {
|
||||
|
||||
/**
|
||||
* 获取 NMS 版本
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getVersion() {
|
||||
return Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器 UID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getServerUID() {
|
||||
if (!DataUtils.getPluginData("TabooLibrary", null).contains("serverUID")) {
|
||||
DataUtils.getPluginData("TabooLibrary", null).set("serverUID", UUID.randomUUID().toString());
|
||||
}
|
||||
return DataUtils.getPluginData("TabooLibrary", null).getString("serverUID");
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置服务器 UID
|
||||
*/
|
||||
public static void resetServerUID() {
|
||||
DataUtils.getPluginData("TabooLibrary", null).set("serverUID", UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 向后台发送 DEBUG 信息
|
||||
*
|
||||
* @param plugin
|
||||
* @param ss
|
||||
*/
|
||||
public static void debug(Plugin plugin, String... ss) {
|
||||
if (Main.getInst().getConfig().getBoolean("DEBUG")) {
|
||||
for (String s : ss) {
|
||||
// [00:42:41 INFO]: [TabooLib - DEBUG][TabooPlugin] Example bug message
|
||||
Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "[TabooLib - DEBUG][" + plugin.getName() + "] " + ChatColor.RED + s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 NMS 版本(数字)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static int getVerint() {
|
||||
if (getVersion().startsWith("v1_7")) {
|
||||
return 10700;
|
||||
}
|
||||
else if (getVersion().startsWith("v1_8")) {
|
||||
return 10800;
|
||||
}
|
||||
else if (getVersion().startsWith("v1_9")) {
|
||||
return 10800;
|
||||
}
|
||||
else if (getVersion().startsWith("v1_10")) {
|
||||
return 11000;
|
||||
}
|
||||
else if (getVersion().startsWith("v1_11")) {
|
||||
return 11100;
|
||||
}
|
||||
else if (getVersion().startsWith("v1_12")) {
|
||||
return 11200;
|
||||
}
|
||||
else if (getVersion().startsWith("v1_13")) {
|
||||
return 11300;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
125
src/main/src/me/skymc/taboolib/anvil/AnvilContainerAPI.java
Normal file
125
src/main/src/me/skymc/taboolib/anvil/AnvilContainerAPI.java
Normal file
@ -0,0 +1,125 @@
|
||||
package me.skymc.taboolib.anvil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
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.InventoryType;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import me.skymc.taboolib.anvil.versions.AnvilContainer_V1_9_4;
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class AnvilContainerAPI implements Listener{
|
||||
|
||||
public static List<String> list = new ArrayList<>();
|
||||
public static ItemStack item = new ItemStack(Material.NAME_TAG);
|
||||
|
||||
public static HashMap<String, String> isOpen = new HashMap<>();
|
||||
public static AnvilContainerAPIEvent event;
|
||||
|
||||
public static void send(Player p, String type, String str, List<String> lorelist)
|
||||
{
|
||||
isOpen.put(p.getName(), type);
|
||||
|
||||
AnvilContainer_V1_9_4.openAnvil(p);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
|
||||
list.clear();
|
||||
if (lorelist == null)
|
||||
{
|
||||
list.add("");
|
||||
list.add("§7在上方文本框内输入信息");
|
||||
list.add("§7随后点击右侧输出物品");
|
||||
}
|
||||
else
|
||||
{
|
||||
list = lorelist;
|
||||
}
|
||||
meta.setLore(list);
|
||||
meta.setDisplayName(str);
|
||||
item.setItemMeta(meta);
|
||||
|
||||
p.getOpenInventory().setItem(0, item);
|
||||
p.playSound(p.getLocation(), Sound.BLOCK_ANVIL_PLACE, 1, 1);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void close(InventoryCloseEvent e)
|
||||
{
|
||||
if (isOpen.containsKey(e.getPlayer().getName()))
|
||||
{
|
||||
isOpen.remove(e.getPlayer().getName());
|
||||
if (e.getInventory().getType() == InventoryType.ANVIL)
|
||||
{
|
||||
e.getInventory().clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void click(InventoryClickEvent e)
|
||||
{
|
||||
if (!isOpen.containsKey(e.getWhoClicked().getName()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (e.getInventory().getType() != InventoryType.ANVIL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
e.setCancelled(true);
|
||||
|
||||
int slot = e.getRawSlot();
|
||||
if (slot != 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Inventory inv = e.getInventory();
|
||||
if (inv.getItem(2) == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (inv.getItem(2).getItemMeta().hasDisplayName())
|
||||
{
|
||||
event = new AnvilContainerAPIEvent(e, isOpen.get(e.getWhoClicked().getName()), inv.getItem(2).getItemMeta().getDisplayName());
|
||||
e.getWhoClicked().closeInventory();
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void example(PlayerCommandPreprocessEvent e)
|
||||
{
|
||||
if (e.getMessage().equals("/anvilexample"))
|
||||
{
|
||||
if (e.getPlayer().hasPermission("taboolib.admin"))
|
||||
{
|
||||
e.setCancelled(true);
|
||||
AnvilContainerAPI.send(e.getPlayer(), "EXAMPLE", "在这里输入文本", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void example2(AnvilContainerAPIEvent e)
|
||||
{
|
||||
if (e.type.equals("EXAMPLE"))
|
||||
{
|
||||
e.event.getWhoClicked().sendMessage(e.string);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package me.skymc.taboolib.anvil;
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
|
||||
public class AnvilContainerAPIEvent extends Event{
|
||||
|
||||
public static final HandlerList handlers = new HandlerList();
|
||||
public InventoryClickEvent event;
|
||||
public String string;
|
||||
public String type;
|
||||
|
||||
public AnvilContainerAPIEvent(InventoryClickEvent e, String t, String s)
|
||||
{
|
||||
event = e;
|
||||
string = s;
|
||||
type = t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers()
|
||||
{
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList()
|
||||
{
|
||||
return handlers;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package me.skymc.taboolib.anvil.versions;
|
||||
|
||||
import org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
import net.minecraft.server.v1_11_R1.BlockPosition;
|
||||
import net.minecraft.server.v1_11_R1.ChatMessage;
|
||||
import net.minecraft.server.v1_11_R1.ContainerAnvil;
|
||||
import net.minecraft.server.v1_11_R1.EntityHuman;
|
||||
import net.minecraft.server.v1_11_R1.EntityPlayer;
|
||||
import net.minecraft.server.v1_11_R1.PacketPlayOutOpenWindow;
|
||||
|
||||
public class AnvilContainer_V1_11_R1 extends ContainerAnvil {
|
||||
|
||||
public AnvilContainer_V1_11_R1(EntityHuman player)
|
||||
{
|
||||
super(player.inventory, player.world, new BlockPosition(0, 0, 0), player);
|
||||
}
|
||||
|
||||
public boolean a(EntityHuman player)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 方法已过期,已有新的方法
|
||||
*/
|
||||
public static void openAnvil(Player p)
|
||||
{
|
||||
EntityPlayer player = ((CraftPlayer)p).getHandle();
|
||||
AnvilContainer_V1_11_R1 container = new AnvilContainer_V1_11_R1(player);
|
||||
int c = player.nextContainerCounter();
|
||||
player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(c, "minecraft:anvil", new ChatMessage("Repairing", new Object[0]), 0));
|
||||
player.activeContainer = container;
|
||||
player.activeContainer.windowId = c;
|
||||
player.activeContainer.addSlotListener(player);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package me.skymc.taboolib.anvil.versions;
|
||||
|
||||
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
import net.minecraft.server.v1_8_R3.BlockPosition;
|
||||
import net.minecraft.server.v1_8_R3.ChatMessage;
|
||||
import net.minecraft.server.v1_8_R3.ContainerAnvil;
|
||||
import net.minecraft.server.v1_8_R3.EntityHuman;
|
||||
import net.minecraft.server.v1_8_R3.EntityPlayer;
|
||||
import net.minecraft.server.v1_8_R3.PacketPlayOutOpenWindow;
|
||||
|
||||
public class AnvilContainer_V1_8_R3 extends ContainerAnvil {
|
||||
|
||||
public AnvilContainer_V1_8_R3(EntityHuman player)
|
||||
{
|
||||
super(player.inventory, player.world, new BlockPosition(0, 0, 0), player);
|
||||
}
|
||||
|
||||
public boolean a(EntityHuman player)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 方法已过期,已有新的方法
|
||||
*/
|
||||
public static void openAnvil(Player p)
|
||||
{
|
||||
EntityPlayer player = ((CraftPlayer)p).getHandle();
|
||||
AnvilContainer_V1_8_R3 container = new AnvilContainer_V1_8_R3(player);
|
||||
int c = player.nextContainerCounter();
|
||||
player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(c, "minecraft:anvil", new ChatMessage("Repairing", new Object[0]), 0));
|
||||
player.activeContainer = container;
|
||||
player.activeContainer.windowId = c;
|
||||
player.activeContainer.addSlotListener(player);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package me.skymc.taboolib.anvil.versions;
|
||||
|
||||
import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
import net.minecraft.server.v1_9_R2.BlockPosition;
|
||||
import net.minecraft.server.v1_9_R2.ChatMessage;
|
||||
import net.minecraft.server.v1_9_R2.ContainerAnvil;
|
||||
import net.minecraft.server.v1_9_R2.EntityHuman;
|
||||
import net.minecraft.server.v1_9_R2.EntityPlayer;
|
||||
import net.minecraft.server.v1_9_R2.PacketPlayOutOpenWindow;
|
||||
|
||||
public class AnvilContainer_V1_9_4 extends ContainerAnvil {
|
||||
|
||||
public AnvilContainer_V1_9_4(EntityHuman player)
|
||||
{
|
||||
super(player.inventory, player.world, new BlockPosition(0, 0, 0), player);
|
||||
}
|
||||
|
||||
public boolean a(EntityHuman player)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 方法已过期,已有新的方法
|
||||
*/
|
||||
public static void openAnvil(Player p)
|
||||
{
|
||||
EntityPlayer player = ((CraftPlayer)p).getHandle();
|
||||
AnvilContainer_V1_9_4 container = new AnvilContainer_V1_9_4(player);
|
||||
int c = player.nextContainerCounter();
|
||||
player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(c, "minecraft:anvil", new ChatMessage("Repairing", new Object[0]), 0));
|
||||
player.activeContainer = container;
|
||||
player.activeContainer.windowId = c;
|
||||
player.activeContainer.addSlotListener(player);
|
||||
}
|
||||
|
||||
}
|
71
src/main/src/me/skymc/taboolib/client/LogClient.java
Normal file
71
src/main/src/me/skymc/taboolib/client/LogClient.java
Normal file
@ -0,0 +1,71 @@
|
||||
package me.skymc.taboolib.client;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.border.BevelBorder;
|
||||
|
||||
public class LogClient extends JFrame {
|
||||
|
||||
/**
|
||||
* DEFAULT VERSION UID
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private JTextArea textArea = new JTextArea();
|
||||
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
|
||||
|
||||
public LogClient(String title) {
|
||||
super(title);
|
||||
|
||||
// DEFAULT CLOSE OPERATION
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
// SETTINGS
|
||||
final JScrollPane scrollPane = new JScrollPane();
|
||||
scrollPane.setBorder(new BevelBorder(BevelBorder.RAISED));
|
||||
scrollPane.setViewportView(textArea);
|
||||
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
setSize(700, 500);
|
||||
setVisible(true);
|
||||
|
||||
// CON'T EDIT
|
||||
textArea.setEditable(false);
|
||||
textArea.setFont(new Font("şÚĚĺ", 0, 18));
|
||||
|
||||
textArea.setBackground(Color.black);
|
||||
textArea.setForeground(Color.LIGHT_GRAY);
|
||||
|
||||
addstr(title);
|
||||
addstr("");
|
||||
}
|
||||
|
||||
public void addString(String a) {
|
||||
|
||||
textArea.append("[" + sdf.format(System.currentTimeMillis()) + " NONE]: " + a + '\n');
|
||||
textArea.setSelectionStart(textArea.getText().length());
|
||||
}
|
||||
|
||||
public void addstr(String a) {
|
||||
|
||||
textArea.append(a + '\n');
|
||||
textArea.setSelectionStart(textArea.getText().length());
|
||||
}
|
||||
|
||||
public void info(String a) {
|
||||
|
||||
textArea.append("[" + sdf.format(System.currentTimeMillis()) + " INFO]: " + a + '\n');
|
||||
textArea.setSelectionStart(textArea.getText().length());
|
||||
}
|
||||
|
||||
public void warn(String a) {
|
||||
|
||||
textArea.append("[" + sdf.format(System.currentTimeMillis()) + " WARN]: " + a + '\n');
|
||||
textArea.setSelectionStart(textArea.getText().length());
|
||||
}
|
||||
}
|
111
src/main/src/me/skymc/taboolib/commands/MainCommands.java
Normal file
111
src/main/src/me/skymc/taboolib/commands/MainCommands.java
Normal file
@ -0,0 +1,111 @@
|
||||
package me.skymc.taboolib.commands;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.commands.sub.AttributesCommand;
|
||||
import me.skymc.taboolib.commands.sub.EnchantCommand;
|
||||
import me.skymc.taboolib.commands.sub.FlagCommand;
|
||||
import me.skymc.taboolib.commands.sub.ImportCommand;
|
||||
import me.skymc.taboolib.commands.sub.InfoCommand;
|
||||
import me.skymc.taboolib.commands.sub.ItemCommand;
|
||||
import me.skymc.taboolib.commands.sub.PotionCommand;
|
||||
import me.skymc.taboolib.commands.sub.SaveCommand;
|
||||
import me.skymc.taboolib.commands.sub.SlotCommand;
|
||||
import me.skymc.taboolib.commands.sub.VariableGetCommand;
|
||||
import me.skymc.taboolib.commands.sub.VariableSetCommand;
|
||||
import me.skymc.taboolib.commands.sub.itemlist.ItemListCommand;
|
||||
import me.skymc.taboolib.commands.sub.shell.ShellCommand;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class MainCommands implements CommandExecutor{
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lTaooLib Commands §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§7 /taboolib save [名称] §f- §8保存手中物品");
|
||||
sender.sendMessage("§7 /taboolib item/i [物品] <玩家> <数量> §f- §8给予玩家物品");
|
||||
sender.sendMessage("§7 /taboolib iteminfo §f- §8查看物品信息");
|
||||
sender.sendMessage("§7 /taboolib itemlist §f- §8查看所有物品");
|
||||
sender.sendMessage("§7 /taboolib itemreload/ireload §f- §8重载物品缓存");
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§7 /taboolib attributes §f- §8查看所有属性");
|
||||
sender.sendMessage("§7 /taboolib enchants §f- §8查看所有附魔");
|
||||
sender.sendMessage("§7 /taboolib potions §f- §8查看所有药水");
|
||||
sender.sendMessage("§7 /taboolib flags §f- §8查看所有标签");
|
||||
sender.sendMessage("§7 /taboolib slots §f- §8查看所有部位");
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§7 /taboolib getvariable [-s|a] [键] §f- §8查看变量");
|
||||
sender.sendMessage("§7 /taboolib setvariable [-s|a] [键] [值] §f- §8更改变量");
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§7 /taboolib shell/s load [脚本] §f- §8载入某个脚本");
|
||||
sender.sendMessage("§7 /taboolib shell/s unload [脚本] §f- §8卸载某个脚本");
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§c /taboolib importdata §f- §4向数据库导入本地数据 §8(该操作将会清空数据库)");
|
||||
sender.sendMessage("§f");
|
||||
return false;
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("itemreload") || args[0].equalsIgnoreCase("ireload")) {
|
||||
ItemUtils.reloadItemCache();
|
||||
ItemUtils.reloadItemName();
|
||||
MsgUtils.send(sender, "重载成功");
|
||||
return true;
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("save")) {
|
||||
return new SaveCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("enchants")) {
|
||||
return new EnchantCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("potions")) {
|
||||
return new PotionCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("flags")) {
|
||||
return new FlagCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("attributes")) {
|
||||
return new AttributesCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("slots")) {
|
||||
return new SlotCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("importdata")) {
|
||||
return new ImportCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("iteminfo")) {
|
||||
return new InfoCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("itemlist")) {
|
||||
return new ItemListCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("item") || args[0].equalsIgnoreCase("i")) {
|
||||
return new ItemCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("setvariable")) {
|
||||
return new VariableSetCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("getvariable")) {
|
||||
return new VariableGetCommand(sender, args).command();
|
||||
}
|
||||
else if (args[0].equalsIgnoreCase("shell") || args[0].equalsIgnoreCase("s")) {
|
||||
return new ShellCommand(sender, args).command();
|
||||
}
|
||||
else {
|
||||
MsgUtils.send(sender, "&4指令错误");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
38
src/main/src/me/skymc/taboolib/commands/SubCommand.java
Normal file
38
src/main/src/me/skymc/taboolib/commands/SubCommand.java
Normal file
@ -0,0 +1,38 @@
|
||||
package me.skymc.taboolib.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class SubCommand {
|
||||
|
||||
public CommandSender sender;
|
||||
public String[] args;
|
||||
|
||||
public boolean returnValue = false;
|
||||
|
||||
public SubCommand(CommandSender sender, String[] args) {
|
||||
this.sender = sender;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public boolean setReturn(boolean returnValue) {
|
||||
return this.returnValue = returnValue;
|
||||
}
|
||||
|
||||
public boolean isPlayer() {
|
||||
return sender instanceof Player;
|
||||
}
|
||||
|
||||
public boolean command() {
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public String getArgs(int size) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = size ; i < args.length ; i++) {
|
||||
sb.append(args[i]);
|
||||
sb.append(" ");
|
||||
}
|
||||
return sb.toString().substring(0, sb.length() - 1);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package me.skymc.taboolib.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@Deprecated
|
||||
public abstract interface SubCommandExecutor {
|
||||
|
||||
public abstract boolean command(CommandSender sender, String[] args);
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent;
|
||||
|
||||
public class AttributesCommand extends SubCommand {
|
||||
|
||||
public AttributesCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lItemStack Attributes §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
|
||||
String[] attributes = new String[] { "damage", "speed", "attackspeed", "health", "knockback", "armor", "luck" };
|
||||
|
||||
for (String name : attributes) {
|
||||
if (isPlayer()) {
|
||||
JSONFormatter json = new JSONFormatter();
|
||||
json.append(" §7- §f" + name);
|
||||
json.appendHoverClick(" §8(点击复制)", new ShowTextEvent("§f点击复制"), new SuggestCommandEvent(name));
|
||||
json.send((Player) sender);
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(" §7- §f" + name);
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent;
|
||||
|
||||
public class EnchantCommand extends SubCommand {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public EnchantCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lItemStack Enchantments §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
|
||||
for (Enchantment enchant : Enchantment.values()) {
|
||||
if (isPlayer()) {
|
||||
JSONFormatter json = new JSONFormatter();
|
||||
json.append(" §7- §f" + enchant.getId() + ". " + enchant.getName());
|
||||
json.appendHoverClick(" §8(点击复制)", new ShowTextEvent("§f点击复制"), new SuggestCommandEvent(enchant.getName()));
|
||||
json.send((Player) sender);
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(" §7- §f" + enchant.getId() + ". " + enchant.getName());
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
}
|
34
src/main/src/me/skymc/taboolib/commands/sub/FlagCommand.java
Normal file
34
src/main/src/me/skymc/taboolib/commands/sub/FlagCommand.java
Normal file
@ -0,0 +1,34 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent;
|
||||
|
||||
public class FlagCommand extends SubCommand {
|
||||
|
||||
public FlagCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lItemStack Flags §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
|
||||
for (ItemFlag flag : ItemFlag.values()) {
|
||||
if (isPlayer()) {
|
||||
JSONFormatter json = new JSONFormatter();
|
||||
json.append(" §7- §f" + flag.name());
|
||||
json.appendHoverClick(" §8(点击复制)", new ShowTextEvent("§f点击复制"), new SuggestCommandEvent(flag.name()));
|
||||
json.send((Player) sender);
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(" §7- §f" + flag.name());
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.Main.StorageType;
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class ImportCommand extends SubCommand {
|
||||
|
||||
public ImportCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
if (isPlayer()) {
|
||||
MsgUtils.warn("改命令只能由控制台输入");
|
||||
}
|
||||
else if (Main.getStorageType() == StorageType.LOCAL) {
|
||||
MsgUtils.warn("只有启用数据库储存时才能这么做");
|
||||
}
|
||||
else {
|
||||
MsgUtils.send("正在清空数据库...");
|
||||
Main.getConnection().truncateTable(Main.getTablePrefix() + "_playerdata");
|
||||
|
||||
MsgUtils.send("开始导入玩家数据...");
|
||||
int size = Main.getPlayerDataFolder().listFiles().length;
|
||||
int loop = 1;
|
||||
|
||||
for (File file : Main.getPlayerDataFolder().listFiles()) {
|
||||
FileConfiguration conf = YamlConfiguration.loadConfiguration(file);
|
||||
Main.getConnection().intoValue(Main.getTablePrefix() + "_playerdata", file.getName().replace(".yml", ""), ConfigUtils.encodeYAML(conf));
|
||||
|
||||
MsgUtils.send("导入玩家: &f" + file.getName().replace(".yml", "") + " &7进度: &f" + loop + "/" + size);
|
||||
loop++;
|
||||
}
|
||||
MsgUtils.send("导入完成!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
50
src/main/src/me/skymc/taboolib/commands/sub/InfoCommand.java
Normal file
50
src/main/src/me/skymc/taboolib/commands/sub/InfoCommand.java
Normal file
@ -0,0 +1,50 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTItem;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class InfoCommand extends SubCommand {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public InfoCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
if (isPlayer()) {
|
||||
Player player = (Player) sender;
|
||||
if (player.getItemInHand().getType().equals(Material.AIR)) {
|
||||
MsgUtils.send(player, "&7请手持正确物品");
|
||||
}
|
||||
else {
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lItemStack Info §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
|
||||
JSONFormatter json = new JSONFormatter();
|
||||
json.append("§7 - 物品材质: §f"); json.appendHoverClick("§f" + player.getItemInHand().getType().name(), new ShowTextEvent("§f点击复制"), new SuggestCommandEvent(player.getItemInHand().getType().name()));
|
||||
json.newLine();
|
||||
json.append("§7 - 物品名称: §f"); json.appendHoverClick("§f" + ItemUtils.getCustomName(player.getItemInHand()), new ShowTextEvent("§f点击复制"), new SuggestCommandEvent(ItemUtils.getCustomName(player.getItemInHand()).replace("§", "&")));
|
||||
json.newLine();
|
||||
json.append("§7 - 物品序号: §f" + player.getItemInHand().getTypeId() + ":" + player.getItemInHand().getDurability());
|
||||
json.send(player);
|
||||
|
||||
NBTItem nbt = new NBTItem(((Player) sender).getItemInHand());
|
||||
sender.sendMessage("§7 - 物品 NBT: §f");
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage(nbt.toString());
|
||||
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
77
src/main/src/me/skymc/taboolib/commands/sub/ItemCommand.java
Normal file
77
src/main/src/me/skymc/taboolib/commands/sub/ItemCommand.java
Normal file
@ -0,0 +1,77 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
|
||||
public class ItemCommand extends SubCommand {
|
||||
|
||||
/**
|
||||
* /TabooLib item 物品 玩家 数量
|
||||
*
|
||||
* @param sender
|
||||
* @param args
|
||||
*/
|
||||
public ItemCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
if (args.length < 2) {
|
||||
MsgUtils.send(sender, "请输入正确的物品名称");
|
||||
setReturn(false);
|
||||
}
|
||||
else {
|
||||
if (ItemUtils.getCacheItem(args[1]) == null) {
|
||||
MsgUtils.send(sender, "物品 &f" + args[1] + "&7 不存在");
|
||||
setReturn(false);
|
||||
return;
|
||||
}
|
||||
|
||||
Player player;
|
||||
Integer amount = 1;
|
||||
ItemStack item = ItemUtils.getCacheItem(args[1]).clone();
|
||||
|
||||
if (args.length > 2) {
|
||||
player = Bukkit.getPlayerExact(args[2]);
|
||||
if (player == null) {
|
||||
MsgUtils.send(sender, "玩家 &f" + args[2] + "&7 不在线");
|
||||
setReturn(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (sender instanceof Player) {
|
||||
player = (Player) sender;
|
||||
}
|
||||
else {
|
||||
MsgUtils.send(sender, "后台不允许这么做");
|
||||
setReturn(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.length > 3) {
|
||||
amount = NumberUtils.getInteger(args[3]);
|
||||
if (amount < 1) {
|
||||
MsgUtils.send(sender, "数量必须大于0");
|
||||
setReturn(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
item.setAmount(amount);
|
||||
|
||||
HashMap<Integer, ItemStack> map = player.getInventory().addItem(item);
|
||||
if (map.size() > 0) {
|
||||
player.getWorld().dropItem(player.getLocation(), item);
|
||||
}
|
||||
|
||||
MsgUtils.send(sender, "物品已发送至玩家 &f" + player.getName() + " &7的背包中");
|
||||
setReturn(true);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent;
|
||||
|
||||
public class PotionCommand extends SubCommand {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public PotionCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lPotionEffect Types §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
|
||||
for (PotionEffectType type : PotionEffectType.values()) {
|
||||
if (type != null) {
|
||||
if (isPlayer()) {
|
||||
JSONFormatter json = new JSONFormatter();
|
||||
json.append(" §7- §f" + type.getId() + ". " + type.getName());
|
||||
json.appendHoverClick(" §8(点击复制)", new ShowTextEvent("§f点击复制"), new SuggestCommandEvent(type.getName()));
|
||||
json.send((Player) sender);
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(" §7- §f" + type.getId() + ". " + type.getName() + "");
|
||||
}
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
}
|
89
src/main/src/me/skymc/taboolib/commands/sub/SaveCommand.java
Normal file
89
src/main/src/me/skymc/taboolib/commands/sub/SaveCommand.java
Normal file
@ -0,0 +1,89 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.message.ChatCatcher;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.message.ChatCatcher.Catcher;
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
|
||||
public class SaveCommand extends SubCommand {
|
||||
|
||||
public SaveCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
if (!(sender instanceof Player)) {
|
||||
MsgUtils.send(sender, "&4后台无法这么做");
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
MsgUtils.send(sender, "&4请输入正确的名称");
|
||||
return;
|
||||
}
|
||||
|
||||
if (((Player) sender).getItemInHand().getType().equals(Material.AIR)) {
|
||||
MsgUtils.send(sender, "&4你不能保存空气");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ItemUtils.getItemCachesFinal().containsKey(args[1])) {
|
||||
MsgUtils.send(sender, "&4该名称所对应的物品保存于固定物品库中, 无法覆盖");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ItemUtils.getItemCaches().containsKey(args[1])) {
|
||||
// 检查聊天引导
|
||||
if (ChatCatcher.contains((Player) sender)) {
|
||||
MsgUtils.send(sender, "&4你有一个正在进行的聊天引导, 请完成后在这么做");
|
||||
return;
|
||||
}
|
||||
|
||||
ChatCatcher.call((Player) sender, new ChatCatcher.Catcher() {
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
MsgUtils.send(sender, "&7退出引导");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Catcher before() {
|
||||
MsgUtils.send(sender, "物品 &f" + args[1] + "&7 已存在, 如果你想要覆盖它, 请在聊天框中输入 \"&f是&7\"");
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean after(String message) {
|
||||
if (message.equals("是")) {
|
||||
saveItem(args[1], ((Player) sender).getItemInHand());
|
||||
MsgUtils.send(sender, "物品 &f" + args[1] + " &7已替换");
|
||||
}
|
||||
else {
|
||||
MsgUtils.send(sender, "&7退出引导");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
saveItem(args[1], ((Player) sender).getItemInHand());
|
||||
MsgUtils.send(sender, "物品 &f" + args[1] + " &7已保存");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void saveItem(String name, ItemStack item) {
|
||||
FileConfiguration conf = ConfigUtils.load(Main.getInst(), ItemUtils.getItemCacheFile());
|
||||
conf.set(name + ".bukkit", item);
|
||||
DataUtils.saveConfiguration(conf, ItemUtils.getItemCacheFile());
|
||||
ItemUtils.reloadItemCache();
|
||||
}
|
||||
}
|
36
src/main/src/me/skymc/taboolib/commands/sub/SlotCommand.java
Normal file
36
src/main/src/me/skymc/taboolib/commands/sub/SlotCommand.java
Normal file
@ -0,0 +1,36 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent;
|
||||
|
||||
public class SlotCommand extends SubCommand {
|
||||
|
||||
public SlotCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
sender.sendMessage("§f");
|
||||
sender.sendMessage("§b§l----- §3§lAttribute Slots §b§l-----");
|
||||
sender.sendMessage("§f");
|
||||
|
||||
String[] attributes = new String[] { "mainhand", "offhand", "feet", "legs", "chest", "head", "all" };
|
||||
|
||||
for (String name : attributes) {
|
||||
if (isPlayer()) {
|
||||
JSONFormatter json = new JSONFormatter();
|
||||
json.append(" §7- §f" + name);
|
||||
json.appendHoverClick(" §8(点击复制)", new ShowTextEvent("§f点击复制"), new SuggestCommandEvent(name));
|
||||
json.send((Player) sender);
|
||||
}
|
||||
else {
|
||||
sender.sendMessage(" §7- §f" + name);
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§f");
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.database.GlobalDataManager;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class VariableGetCommand extends SubCommand {
|
||||
|
||||
public VariableGetCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
if (args.length < 3) {
|
||||
MsgUtils.send(sender, "&4请输入正确的指令 ");
|
||||
}
|
||||
else if (!(args[1].equals("-a") || args[1].equals("-s"))) {
|
||||
MsgUtils.send(sender, "&4请输入正确的读取方式");
|
||||
}
|
||||
|
||||
Long time = System.currentTimeMillis();
|
||||
String value = null;
|
||||
|
||||
if (args[1].equals("-s")) {
|
||||
value = GlobalDataManager.getVariable(args[2], null);
|
||||
}
|
||||
else if (args[1].equals("-a")) {
|
||||
value = GlobalDataManager.getVariableAsynchronous(args[2], null);
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
MsgUtils.send(sender, "读取完成, 耗时: &f" + (System.currentTimeMillis() - time) + " &7(ms)");
|
||||
MsgUtils.send(sender, "变量 &f" + args[2] + " &7不存在");
|
||||
}
|
||||
else {
|
||||
MsgUtils.send(sender, "读取完成, 耗时: &f" + (System.currentTimeMillis() - time) + " &7(ms)");
|
||||
MsgUtils.send(sender, "变量 &f" + args[2] + " &7的值为 &f" + value);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package me.skymc.taboolib.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.database.GlobalDataManager;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class VariableSetCommand extends SubCommand {
|
||||
|
||||
public VariableSetCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
|
||||
if (args.length < 4) {
|
||||
MsgUtils.send(sender, "&4请输入正确的指令 ");
|
||||
}
|
||||
else if (!(args[1].equals("-a") || args[1].equals("-s"))) {
|
||||
MsgUtils.send(sender, "&4请输入正确的写入方式");
|
||||
}
|
||||
|
||||
Long time = System.currentTimeMillis();
|
||||
String value = getArgs(3);
|
||||
|
||||
if (args[1].equals("-s")) {
|
||||
GlobalDataManager.setVariable(args[2], value);
|
||||
}
|
||||
else if (args[1].equals("-a")) {
|
||||
GlobalDataManager.setVariableAsynchronous(args[2], value);
|
||||
}
|
||||
|
||||
MsgUtils.send(sender, "写入完成, 耗时: &f" + (System.currentTimeMillis() - time) + " &7(ms)");
|
||||
setReturn(true);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package me.skymc.taboolib.commands.sub.itemlist;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.commands.sub.itemlist.listener.ItemLibraryPatch;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018Äê2ÔÂ4ÈÕ ÏÂÎç8:08:22
|
||||
*/
|
||||
public class ItemListCommand extends SubCommand {
|
||||
|
||||
/**
|
||||
* @param sender
|
||||
* @param args
|
||||
*/
|
||||
public ItemListCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
if (isPlayer()) {
|
||||
if (args.length == 1) {
|
||||
ItemLibraryPatch.openInventory((Player) sender, 1);
|
||||
}
|
||||
else {
|
||||
ItemLibraryPatch.openInventory((Player) sender, NumberUtils.getInteger(args[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
package me.skymc.taboolib.commands.sub.itemlist.listener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import me.skymc.taboolib.inventory.InventoryUtil;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月4日 下午4:35:00
|
||||
*/
|
||||
public class ItemLibraryPatch implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void inventoryClick(InventoryClickEvent e) {
|
||||
if (e.getInventory().getHolder() instanceof ItemLibraryHolder) {
|
||||
e.setCancelled(true);
|
||||
|
||||
if (e.getCurrentItem() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.getRawSlot() == 47) {
|
||||
openInventory((Player) e.getWhoClicked(), ((ItemLibraryHolder) e.getInventory().getHolder()).PAGE - 1);
|
||||
}
|
||||
else if (e.getRawSlot() == 51) {
|
||||
openInventory((Player) e.getWhoClicked(), ((ItemLibraryHolder) e.getInventory().getHolder()).PAGE + 1);
|
||||
}
|
||||
else {
|
||||
e.getWhoClicked().getInventory().addItem(ItemUtils.getCacheItem(((ItemLibraryHolder) e.getInventory().getHolder()).ITEMS_DATA.get(e.getRawSlot())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开物品库界面
|
||||
*
|
||||
* @param player
|
||||
* @param page
|
||||
*/
|
||||
public static void openInventory(Player player, int page) {
|
||||
ItemLibraryHolder holder = new ItemLibraryHolder(page);
|
||||
Inventory inventory = Bukkit.createInventory(holder, 54, "物品库");
|
||||
|
||||
LinkedHashMap<String, ItemStack> map = new LinkedHashMap<>();
|
||||
map.putAll(ItemUtils.getItemCachesFinal());
|
||||
map.putAll(ItemUtils.getItemCaches());
|
||||
|
||||
int loop = 0;
|
||||
Iterator<String> iterator = map.keySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String name = iterator.next();
|
||||
if (loop >= (page - 1) * 28) {
|
||||
if (loop < page * 28) {
|
||||
int slot = InventoryUtil.SLOT_OF_CENTENTS.get(loop - ((page - 1) * 28));
|
||||
ItemStack item = map.get(name).clone(); {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
|
||||
lore.add("§f");
|
||||
lore.add("§f§m ");
|
||||
lore.add("§f序列号: §8" + name);
|
||||
meta.setLore(lore);
|
||||
item.setItemMeta(meta);
|
||||
inventory.setItem(slot, item);
|
||||
}
|
||||
holder.ITEMS_DATA.put(slot, name);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
loop++;
|
||||
}
|
||||
|
||||
if (page > 1) {
|
||||
inventory.setItem(47, ItemUtils.setName(new ItemStack(Material.ARROW), "§f上一页"));
|
||||
}
|
||||
if (((int) Math.ceil(ItemUtils.getItemCaches().size() / 28D)) > page) {
|
||||
inventory.setItem(51, ItemUtils.setName(new ItemStack(Material.ARROW), "§f下一页"));
|
||||
}
|
||||
player.openInventory(inventory);
|
||||
}
|
||||
|
||||
public static class ItemLibraryHolder implements InventoryHolder {
|
||||
|
||||
public final int PAGE;
|
||||
public final HashMap<Integer, String> ITEMS_DATA = new HashMap<>();
|
||||
|
||||
public ItemLibraryHolder(int page) {
|
||||
this.PAGE = page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package me.skymc.taboolib.commands.sub.shell;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.javashell.JavaShell;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class ShellCommand extends SubCommand {
|
||||
|
||||
public ShellCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
if (args.length > 1) {
|
||||
if (args[1].equalsIgnoreCase("load")) {
|
||||
new ShellLoadCommand(sender, args);
|
||||
}
|
||||
else if (args[1].equalsIgnoreCase("unload")) {
|
||||
new ShellUnloadCommand(sender, args);
|
||||
}
|
||||
}
|
||||
else {
|
||||
MsgUtils.send(sender, "&4Ö¸Áî´íÎó");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean command() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package me.skymc.taboolib.commands.sub.shell;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.javashell.JavaShell;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class ShellLoadCommand extends SubCommand {
|
||||
|
||||
public ShellLoadCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
if (args.length < 3) {
|
||||
MsgUtils.send(sender, "&c请输入正确的脚本名称");
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(JavaShell.getScriptFolder(), args[2].contains(".java") ? args[2] : args[2] + ".java");
|
||||
if (!file.exists()) {
|
||||
MsgUtils.send(sender, "&c脚本 &4" + args[2] + "&c 不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
if (JavaShell.reloadShell(args[2])) {
|
||||
MsgUtils.send(sender, "脚本 " + args[2] + " 已载入");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean command() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package me.skymc.taboolib.commands.sub.shell;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.javashell.JavaShell;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class ShellUnloadCommand extends SubCommand {
|
||||
|
||||
public ShellUnloadCommand(CommandSender sender, String[] args) {
|
||||
super(sender, args);
|
||||
if (args.length < 3) {
|
||||
MsgUtils.send(sender, "&c请输入正确的脚本名称");
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(JavaShell.getScriptFolder(), args[2].contains(".java") ? args[2] : args[2] + ".java");
|
||||
if (!file.exists()) {
|
||||
MsgUtils.send(sender, "&c脚本 &4" + args[2] + "&c 不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
JavaShell.unloadShell(args[2]);
|
||||
MsgUtils.send(sender, "脚本 " + args[2] + " 已卸载");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean command() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
62
src/main/src/me/skymc/taboolib/cooldown/CooldownPack.java
Normal file
62
src/main/src/me/skymc/taboolib/cooldown/CooldownPack.java
Normal file
@ -0,0 +1,62 @@
|
||||
package me.skymc.taboolib.cooldown;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@Deprecated
|
||||
public class CooldownPack {
|
||||
|
||||
private String plugin;
|
||||
private String name;
|
||||
private int seconds;
|
||||
|
||||
private HashMap<String, Long> data = new HashMap<>();
|
||||
|
||||
public CooldownPack(String n, int s) {
|
||||
this.name = n;
|
||||
this.seconds = s;
|
||||
this.plugin = "null";
|
||||
}
|
||||
|
||||
public String getPackName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPackSeconds() {
|
||||
return seconds;
|
||||
}
|
||||
|
||||
public String getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public void setPlugin(String p) {
|
||||
this.plugin = p;
|
||||
}
|
||||
|
||||
public void unRegister(String player) {
|
||||
data.remove(player);
|
||||
}
|
||||
|
||||
public int getCooldown(String player) {
|
||||
if (!data.containsKey(player)) {
|
||||
return 0;
|
||||
}
|
||||
int difference = (int) ((System.currentTimeMillis() - data.get(player)) / 1000);
|
||||
|
||||
return difference >= seconds ? 0 : seconds - difference;
|
||||
}
|
||||
|
||||
public boolean isCooldown(String player, int cutseconds) {
|
||||
if (!data.containsKey(player)) {
|
||||
data.put(player, System.currentTimeMillis());
|
||||
return false;
|
||||
}
|
||||
if ((getCooldown(player) - (cutseconds*1000)) <= 0) {
|
||||
data.put(player, System.currentTimeMillis());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
60
src/main/src/me/skymc/taboolib/cooldown/CooldownUtils.java
Normal file
60
src/main/src/me/skymc/taboolib/cooldown/CooldownUtils.java
Normal file
@ -0,0 +1,60 @@
|
||||
package me.skymc.taboolib.cooldown;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
@Deprecated
|
||||
public class CooldownUtils implements Listener {
|
||||
|
||||
private static ConcurrentHashMap<String, CooldownPack> packlist = new ConcurrentHashMap<>();
|
||||
|
||||
public static void register(CooldownPack pack) {
|
||||
packlist.put(pack.getPackName(), pack);
|
||||
MsgUtils.send("注册冷却包: " + pack.getPackName() + ", 时间: " + pack.getPackSeconds() + " 秒 (匿名注册)");
|
||||
}
|
||||
|
||||
public static void register(CooldownPack pack, Plugin plugin) {
|
||||
pack.setPlugin(plugin.getName());
|
||||
|
||||
packlist.put(pack.getPackName(), pack);
|
||||
MsgUtils.send("注册冷却包: " + pack.getPackName() + ", 时间: " + pack.getPackSeconds() + " 秒 (" + plugin.getName() + ")");
|
||||
}
|
||||
|
||||
public static void unregister(String name) {
|
||||
packlist.remove(name);
|
||||
|
||||
MsgUtils.send("注销冷却包: " + name + " (主动注销)");
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
for (CooldownPack pack : packlist.values()) {
|
||||
if (!pack.isCooldown(e.getPlayer().getName(), 0)) {
|
||||
pack.unRegister(e.getPlayer().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void disable(PluginDisableEvent e) {
|
||||
for (CooldownPack pack : packlist.values()) {
|
||||
if (pack.getPlugin().equals(e.getPlugin().getName())) {
|
||||
packlist.remove(pack.getPackName());
|
||||
|
||||
MsgUtils.send("注销冷却包: " + pack.getPackName() + " (自动注销)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package me.skymc.taboolib.cooldown.seconds;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class CooldownPack2 {
|
||||
|
||||
private String plugin;
|
||||
private String name;
|
||||
private int seconds;
|
||||
|
||||
private HashMap<String, Long> data = new HashMap<>();
|
||||
|
||||
public CooldownPack2(String n, int s) {
|
||||
this.name = n;
|
||||
this.seconds = s;
|
||||
this.plugin = "null";
|
||||
}
|
||||
|
||||
public String getPackName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPackSeconds() {
|
||||
return seconds;
|
||||
}
|
||||
|
||||
public String getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public void setPlugin(String p) {
|
||||
this.plugin = p;
|
||||
}
|
||||
|
||||
public void unRegister(String player) {
|
||||
data.remove(player);
|
||||
}
|
||||
|
||||
public int getCooldown(String player, int cutseconds) {
|
||||
if (!data.containsKey(player)) {
|
||||
return 0;
|
||||
}
|
||||
int difference = (int) ((System.currentTimeMillis() + cutseconds) - data.get(player));
|
||||
return difference >= seconds ? 0 : seconds - difference;
|
||||
}
|
||||
|
||||
public boolean isCooldown(String player, int cutseconds) {
|
||||
if (!data.containsKey(player)) {
|
||||
data.put(player, System.currentTimeMillis());
|
||||
return false;
|
||||
}
|
||||
if (getCooldown(player, cutseconds) <= 0) {
|
||||
data.put(player, System.currentTimeMillis());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package me.skymc.taboolib.cooldown.seconds;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class CooldownUtils2 implements Listener {
|
||||
|
||||
private static ConcurrentHashMap<String, CooldownPack2> packlist = new ConcurrentHashMap<>();
|
||||
|
||||
public static void register(CooldownPack2 pack) {
|
||||
packlist.put(pack.getPackName(), pack);
|
||||
MsgUtils.send("注册冷却包: " + pack.getPackName() + ", 时间: " + pack.getPackSeconds() + " 秒 (匿名注册)");
|
||||
}
|
||||
|
||||
public static void register(CooldownPack2 pack, Plugin plugin) {
|
||||
pack.setPlugin(plugin.getName());
|
||||
|
||||
packlist.put(pack.getPackName(), pack);
|
||||
MsgUtils.send("注册冷却包: " + pack.getPackName() + ", 时间: " + pack.getPackSeconds() + " 秒 (" + plugin.getName() + ")");
|
||||
}
|
||||
|
||||
public static void unregister(String name) {
|
||||
packlist.remove(name);
|
||||
|
||||
MsgUtils.send("注销冷却包: " + name + " (主动注销)");
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
for (CooldownPack2 pack : packlist.values()) {
|
||||
if (!pack.isCooldown(e.getPlayer().getName(), 0)) {
|
||||
pack.unRegister(e.getPlayer().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void disable(PluginDisableEvent e) {
|
||||
for (CooldownPack2 pack : packlist.values()) {
|
||||
if (pack.getPlugin().equals(e.getPlugin().getName())) {
|
||||
packlist.remove(pack.getPackName());
|
||||
|
||||
MsgUtils.send("注销冷却包: " + pack.getPackName() + " (自动注销)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1110
src/main/src/me/skymc/taboolib/csvutils/CsvReader.java
Normal file
1110
src/main/src/me/skymc/taboolib/csvutils/CsvReader.java
Normal file
File diff suppressed because it is too large
Load Diff
327
src/main/src/me/skymc/taboolib/csvutils/CsvWriter.java
Normal file
327
src/main/src/me/skymc/taboolib/csvutils/CsvWriter.java
Normal file
@ -0,0 +1,327 @@
|
||||
package me.skymc.taboolib.csvutils;
|
||||
|
||||
import java.nio.charset.*;
|
||||
import java.io.*;
|
||||
|
||||
public class CsvWriter
|
||||
{
|
||||
private Writer outputStream;
|
||||
private String fileName;
|
||||
private boolean firstColumn;
|
||||
private boolean useCustomRecordDelimiter;
|
||||
private Charset charset;
|
||||
private UserSettings userSettings;
|
||||
private boolean initialized;
|
||||
private boolean closed;
|
||||
private String systemRecordDelimiter;
|
||||
public static final int ESCAPE_MODE_DOUBLED = 1;
|
||||
public static final int ESCAPE_MODE_BACKSLASH = 2;
|
||||
|
||||
public CsvWriter(final String fileName, final char delimiter, final Charset charset) {
|
||||
this.outputStream = null;
|
||||
this.fileName = null;
|
||||
this.firstColumn = true;
|
||||
this.useCustomRecordDelimiter = false;
|
||||
this.charset = null;
|
||||
this.userSettings = new UserSettings();
|
||||
this.initialized = false;
|
||||
this.closed = false;
|
||||
this.systemRecordDelimiter = System.getProperty("line.separator");
|
||||
if (fileName == null) {
|
||||
throw new IllegalArgumentException("Parameter fileName can not be null.");
|
||||
}
|
||||
if (charset == null) {
|
||||
throw new IllegalArgumentException("Parameter charset can not be null.");
|
||||
}
|
||||
this.fileName = fileName;
|
||||
this.userSettings.Delimiter = delimiter;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
public CsvWriter(final String s) {
|
||||
this(s, ',', Charset.forName("ISO-8859-1"));
|
||||
}
|
||||
|
||||
public CsvWriter(final Writer outputStream, final char delimiter) {
|
||||
this.outputStream = null;
|
||||
this.fileName = null;
|
||||
this.firstColumn = true;
|
||||
this.useCustomRecordDelimiter = false;
|
||||
this.charset = null;
|
||||
this.userSettings = new UserSettings();
|
||||
this.initialized = false;
|
||||
this.closed = false;
|
||||
this.systemRecordDelimiter = System.getProperty("line.separator");
|
||||
if (outputStream == null) {
|
||||
throw new IllegalArgumentException("Parameter outputStream can not be null.");
|
||||
}
|
||||
this.outputStream = outputStream;
|
||||
this.userSettings.Delimiter = delimiter;
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
public CsvWriter(final OutputStream outputStream, final char c, final Charset charset) {
|
||||
this(new OutputStreamWriter(outputStream, charset), c);
|
||||
}
|
||||
|
||||
public char getDelimiter() {
|
||||
return this.userSettings.Delimiter;
|
||||
}
|
||||
|
||||
public void setDelimiter(final char delimiter) {
|
||||
this.userSettings.Delimiter = delimiter;
|
||||
}
|
||||
|
||||
public char getRecordDelimiter() {
|
||||
return this.userSettings.RecordDelimiter;
|
||||
}
|
||||
|
||||
public void setRecordDelimiter(final char recordDelimiter) {
|
||||
this.useCustomRecordDelimiter = true;
|
||||
this.userSettings.RecordDelimiter = recordDelimiter;
|
||||
}
|
||||
|
||||
public char getTextQualifier() {
|
||||
return this.userSettings.TextQualifier;
|
||||
}
|
||||
|
||||
public void setTextQualifier(final char textQualifier) {
|
||||
this.userSettings.TextQualifier = textQualifier;
|
||||
}
|
||||
|
||||
public boolean getUseTextQualifier() {
|
||||
return this.userSettings.UseTextQualifier;
|
||||
}
|
||||
|
||||
public void setUseTextQualifier(final boolean useTextQualifier) {
|
||||
this.userSettings.UseTextQualifier = useTextQualifier;
|
||||
}
|
||||
|
||||
public int getEscapeMode() {
|
||||
return this.userSettings.EscapeMode;
|
||||
}
|
||||
|
||||
public void setEscapeMode(final int escapeMode) {
|
||||
this.userSettings.EscapeMode = escapeMode;
|
||||
}
|
||||
|
||||
public void setComment(final char comment) {
|
||||
this.userSettings.Comment = comment;
|
||||
}
|
||||
|
||||
public char getComment() {
|
||||
return this.userSettings.Comment;
|
||||
}
|
||||
|
||||
public boolean getForceQualifier() {
|
||||
return this.userSettings.ForceQualifier;
|
||||
}
|
||||
|
||||
public void setForceQualifier(final boolean forceQualifier) {
|
||||
this.userSettings.ForceQualifier = forceQualifier;
|
||||
}
|
||||
|
||||
public void write(String s, final boolean b) throws IOException {
|
||||
this.checkClosed();
|
||||
this.checkInit();
|
||||
if (s == null) {
|
||||
s = "";
|
||||
}
|
||||
if (!this.firstColumn) {
|
||||
this.outputStream.write(this.userSettings.Delimiter);
|
||||
}
|
||||
int forceQualifier = this.userSettings.ForceQualifier ? 1 : 0;
|
||||
if (!b && s.length() > 0) {
|
||||
s = s.trim();
|
||||
}
|
||||
if (forceQualifier == 0 && this.userSettings.UseTextQualifier && (s.indexOf(this.userSettings.TextQualifier) > -1 || s.indexOf(this.userSettings.Delimiter) > -1 || (!this.useCustomRecordDelimiter && (s.indexOf(10) > -1 || s.indexOf(13) > -1)) || (this.useCustomRecordDelimiter && s.indexOf(this.userSettings.RecordDelimiter) > -1) || (this.firstColumn && s.length() > 0 && s.charAt(0) == this.userSettings.Comment) || (this.firstColumn && s.length() == 0))) {
|
||||
forceQualifier = 1;
|
||||
}
|
||||
if (this.userSettings.UseTextQualifier && forceQualifier == 0 && s.length() > 0 && b) {
|
||||
final char char1 = s.charAt(0);
|
||||
if (char1 == ' ' || char1 == '\t') {
|
||||
forceQualifier = 1;
|
||||
}
|
||||
if (forceQualifier == 0 && s.length() > 1) {
|
||||
final char char2 = s.charAt(s.length() - 1);
|
||||
if (char2 == ' ' || char2 == '\t') {
|
||||
forceQualifier = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (forceQualifier != 0) {
|
||||
this.outputStream.write(this.userSettings.TextQualifier);
|
||||
if (this.userSettings.EscapeMode == 2) {
|
||||
s = replace(s, "\\", "\\\\");
|
||||
s = replace(s, "" + this.userSettings.TextQualifier, "\\" + this.userSettings.TextQualifier);
|
||||
}
|
||||
else {
|
||||
s = replace(s, "" + this.userSettings.TextQualifier, "" + this.userSettings.TextQualifier + this.userSettings.TextQualifier);
|
||||
}
|
||||
}
|
||||
else if (this.userSettings.EscapeMode == 2) {
|
||||
s = replace(s, "\\", "\\\\");
|
||||
s = replace(s, "" + this.userSettings.Delimiter, "\\" + this.userSettings.Delimiter);
|
||||
if (this.useCustomRecordDelimiter) {
|
||||
s = replace(s, "" + this.userSettings.RecordDelimiter, "\\" + this.userSettings.RecordDelimiter);
|
||||
}
|
||||
else {
|
||||
s = replace(s, "\r", "\\\r");
|
||||
s = replace(s, "\n", "\\\n");
|
||||
}
|
||||
if (this.firstColumn && s.length() > 0 && s.charAt(0) == this.userSettings.Comment) {
|
||||
if (s.length() > 1) {
|
||||
s = "\\" + this.userSettings.Comment + s.substring(1);
|
||||
}
|
||||
else {
|
||||
s = "\\" + this.userSettings.Comment;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.outputStream.write(s);
|
||||
if (forceQualifier != 0) {
|
||||
this.outputStream.write(this.userSettings.TextQualifier);
|
||||
}
|
||||
this.firstColumn = false;
|
||||
}
|
||||
|
||||
public void write(final String s) throws IOException {
|
||||
this.write(s, false);
|
||||
}
|
||||
|
||||
public void writeComment(final String s) throws IOException {
|
||||
this.checkClosed();
|
||||
this.checkInit();
|
||||
this.outputStream.write(this.userSettings.Comment);
|
||||
this.outputStream.write(s);
|
||||
if (this.useCustomRecordDelimiter) {
|
||||
this.outputStream.write(this.userSettings.RecordDelimiter);
|
||||
}
|
||||
else {
|
||||
this.outputStream.write(this.systemRecordDelimiter);
|
||||
}
|
||||
this.firstColumn = true;
|
||||
}
|
||||
|
||||
public void writeRecord(final String[] array, final boolean b) throws IOException {
|
||||
if (array != null && array.length > 0) {
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
this.write(array[i], b);
|
||||
}
|
||||
this.endRecord();
|
||||
}
|
||||
}
|
||||
|
||||
public void writeRecord(final String[] array) throws IOException {
|
||||
this.writeRecord(array, false);
|
||||
}
|
||||
|
||||
public void endRecord() throws IOException {
|
||||
this.checkClosed();
|
||||
this.checkInit();
|
||||
if (this.useCustomRecordDelimiter) {
|
||||
this.outputStream.write(this.userSettings.RecordDelimiter);
|
||||
}
|
||||
else {
|
||||
this.outputStream.write(this.systemRecordDelimiter);
|
||||
}
|
||||
this.firstColumn = true;
|
||||
}
|
||||
|
||||
private void checkInit() throws IOException {
|
||||
if (!this.initialized) {
|
||||
if (this.fileName != null) {
|
||||
this.outputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.fileName), this.charset));
|
||||
}
|
||||
this.initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
this.outputStream.flush();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (!this.closed) {
|
||||
this.close(true);
|
||||
this.closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void close(final boolean b) {
|
||||
if (!this.closed) {
|
||||
if (b) {
|
||||
this.charset = null;
|
||||
}
|
||||
try {
|
||||
if (this.initialized) {
|
||||
this.outputStream.close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {}
|
||||
this.outputStream = null;
|
||||
this.closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkClosed() throws IOException {
|
||||
if (this.closed) {
|
||||
throw new IOException("This instance of the CsvWriter class has already been closed.");
|
||||
}
|
||||
}
|
||||
|
||||
protected void finalize() {
|
||||
this.close(false);
|
||||
}
|
||||
|
||||
public static String replace(final String s, final String s2, final String s3) {
|
||||
final int length = s2.length();
|
||||
int i = s.indexOf(s2);
|
||||
if (i > -1) {
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
int n;
|
||||
for (n = 0; i != -1; i = s.indexOf(s2, n)) {
|
||||
sb.append(s.substring(n, i));
|
||||
sb.append(s3);
|
||||
n = i + length;
|
||||
}
|
||||
sb.append(s.substring(n));
|
||||
return sb.toString();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
private class UserSettings
|
||||
{
|
||||
public char TextQualifier;
|
||||
public boolean UseTextQualifier;
|
||||
public char Delimiter;
|
||||
public char RecordDelimiter;
|
||||
public char Comment;
|
||||
public int EscapeMode;
|
||||
public boolean ForceQualifier;
|
||||
|
||||
public UserSettings() {
|
||||
this.TextQualifier = '\"';
|
||||
this.UseTextQualifier = true;
|
||||
this.Delimiter = ',';
|
||||
this.RecordDelimiter = '\0';
|
||||
this.Comment = '#';
|
||||
this.EscapeMode = 1;
|
||||
this.ForceQualifier = false;
|
||||
}
|
||||
}
|
||||
|
||||
private class Letters
|
||||
{
|
||||
public static final char LF = '\n';
|
||||
public static final char CR = '\r';
|
||||
public static final char QUOTE = '\"';
|
||||
public static final char COMMA = ',';
|
||||
public static final char SPACE = ' ';
|
||||
public static final char TAB = '\t';
|
||||
public static final char POUND = '#';
|
||||
public static final char BACKSLASH = '\\';
|
||||
public static final char NULL = '\0';
|
||||
}
|
||||
}
|
73
src/main/src/me/skymc/taboolib/damage/DamageUtils.java
Normal file
73
src/main/src/me/skymc/taboolib/damage/DamageUtils.java
Normal file
@ -0,0 +1,73 @@
|
||||
package me.skymc.taboolib.damage;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
|
||||
public class DamageUtils {
|
||||
|
||||
public static void damage(Player damager, LivingEntity victim, double damage)
|
||||
{
|
||||
dmg(damager, victim, damage);
|
||||
}
|
||||
|
||||
public static void damage(Player damager, Entity victim, double damage)
|
||||
{
|
||||
if (victim instanceof LivingEntity) {
|
||||
dmg(damager, (LivingEntity) victim, damage);
|
||||
}
|
||||
}
|
||||
|
||||
public static void dmg(LivingEntity paramLivingEntity1, LivingEntity paramLivingEntity2, double paramDouble)
|
||||
{
|
||||
if ((paramLivingEntity2.hasMetadata("NPC")) || (paramLivingEntity1.hasMetadata("NPC"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
Object localObject1 = null;
|
||||
try
|
||||
{
|
||||
localObject1 = paramLivingEntity1.getClass().getDeclaredMethod("getHandle", new Class[0]).invoke(paramLivingEntity1, new Object[0]);
|
||||
}
|
||||
catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException|NoSuchMethodException|SecurityException localIllegalAccessException1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Object localObject2 = null;
|
||||
try
|
||||
{
|
||||
localObject2 = paramLivingEntity2.getClass().getDeclaredMethod("getHandle", new Class[0]).invoke(paramLivingEntity2, new Object[0]);
|
||||
}
|
||||
catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException|NoSuchMethodException|SecurityException localIllegalAccessException2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Class<?> DamageSource = nmsClass("DamageSource");
|
||||
Object localObject3 = DamageSource.getDeclaredMethod("playerAttack", new Class[] { nmsClass("EntityHuman") }).invoke(DamageSource, new Object[] { localObject1 });
|
||||
|
||||
localObject2.getClass().getDeclaredMethod("damageEntity", new Class[] { DamageSource, Float.TYPE }).invoke(localObject2, new Object[] { localObject3, Float.valueOf((float) paramDouble) });
|
||||
}
|
||||
catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException|NoSuchMethodException|SecurityException localIllegalAccessException3)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> nmsClass(String paramString)
|
||||
{
|
||||
String str = "net.minecraft.server." + TabooLib.getVersion() + "." + paramString;
|
||||
try {
|
||||
return Class.forName(str);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
23
src/main/src/me/skymc/taboolib/damage/GetDamager.java
Normal file
23
src/main/src/me/skymc/taboolib/damage/GetDamager.java
Normal file
@ -0,0 +1,23 @@
|
||||
package me.skymc.taboolib.damage;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Projectile;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
|
||||
public class GetDamager {
|
||||
|
||||
public static Player get(EntityDamageByEntityEvent e) {
|
||||
Player p = null;
|
||||
if (e.getDamager() instanceof Projectile) {
|
||||
Projectile arrow = (Projectile) e.getDamager();
|
||||
if (arrow.getShooter() instanceof Player) {
|
||||
p = (Player) arrow.getShooter();
|
||||
}
|
||||
}
|
||||
else if (e.getDamager() instanceof Player) {
|
||||
p = (Player) e.getDamager();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
26
src/main/src/me/skymc/taboolib/damage/GetKiller.java
Normal file
26
src/main/src/me/skymc/taboolib/damage/GetKiller.java
Normal file
@ -0,0 +1,26 @@
|
||||
package me.skymc.taboolib.damage;
|
||||
|
||||
import org.bukkit.entity.Arrow;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Projectile;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class GetKiller {
|
||||
|
||||
public static Player get(EntityDeathEvent e) {
|
||||
Player p = null;
|
||||
if (e.getEntity().getKiller() instanceof Projectile) {
|
||||
Projectile arrow = (Projectile) e.getEntity().getKiller();
|
||||
if (arrow.getShooter() instanceof Player) {
|
||||
p = (Player) arrow.getShooter();
|
||||
}
|
||||
}
|
||||
else if (e.getEntity().getKiller() instanceof Player) {
|
||||
p = e.getEntity().getKiller();
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
424
src/main/src/me/skymc/taboolib/database/GlobalDataManager.java
Normal file
424
src/main/src/me/skymc/taboolib/database/GlobalDataManager.java
Normal file
@ -0,0 +1,424 @@
|
||||
package me.skymc.taboolib.database;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.Main.StorageType;
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
|
||||
public class GlobalDataManager {
|
||||
|
||||
public static FileConfiguration data = DataUtils.addPluginData("TabooLibrary-Variable.yml", null);
|
||||
|
||||
/**
|
||||
* 获取变量
|
||||
*
|
||||
* @param name 名称
|
||||
* @param defaultVariable 默认值
|
||||
* @return
|
||||
*/
|
||||
public static String getVariable(String name, String defaultVariable) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
Object obj = Main.getConnection().getValueLast(Main.getTablePrefix() + "_plugindata", "name", name, "variable");
|
||||
return obj != null ? obj.toString().equals("null") ? defaultVariable : obj.toString() : defaultVariable;
|
||||
}
|
||||
else {
|
||||
return data.contains(name.replace(":", "-")) ? data.getString(name.replace(":", "-")) : defaultVariable;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存变量(该方法仅限数据库储存方式)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param defaultVariable 默认值
|
||||
* @return
|
||||
*/
|
||||
public static String getVariableAsynchronous(String name, String defaultVariable) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
SQLVariable variable = SQLMethod.getSQLVariable(name);
|
||||
return variable == null ? defaultVariable : variable.getVariable().equals("null") ? defaultVariable : variable.getVariable();
|
||||
}
|
||||
else {
|
||||
return getVariable(name, defaultVariable);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置变量
|
||||
*
|
||||
* @param name 名称
|
||||
* @param variable 变量
|
||||
*/
|
||||
public static void setVariable(String name, String variable) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
Main.getConnection().intoValue(Main.getTablePrefix() + "_plugindata", name, variable == null ? "null" : variable, TabooLib.getServerUID());
|
||||
}
|
||||
else {
|
||||
data.set(name.replace(":", "-"), variable);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存变量(该方法仅限数据库储存方式)
|
||||
*
|
||||
* @param name
|
||||
* @param variable
|
||||
*/
|
||||
public static void setVariableAsynchronous(String name, String variable) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
SQLVariable _variable = SQLMethod.contains(name) ? SQLMethod.getSQLVariable(name).setVariable(variable == null ? "null" : variable) : SQLMethod.addSQLVariable(name, variable == null ? "null" : variable);
|
||||
// 更新数据
|
||||
SQLMethod.uploadVariable(_variable, true);
|
||||
}
|
||||
else {
|
||||
setVariable(name, variable);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查变量是否存在
|
||||
*
|
||||
* @param name 名称
|
||||
*/
|
||||
public static boolean contains(String name) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
return getVariable(name, null) == null ? false : true;
|
||||
}
|
||||
else {
|
||||
return data.contains(name.replace(":", "-"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查变量是否被缓存(该方法仅限数据库储存方式)
|
||||
*
|
||||
* @param name 名称
|
||||
* @return
|
||||
*/
|
||||
public static boolean containsAsynchronous(String name) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
return getVariableAsynchronous(name, null) == null ? false : true;
|
||||
}
|
||||
else {
|
||||
return contains(name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有失效的变量
|
||||
* 该方法仅限数据库储存时有效
|
||||
*/
|
||||
public static void clearInvalidVariables() {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
HashMap<String, String> map = getVariables();
|
||||
Main.getConnection().truncateTable(Main.getTablePrefix() + "_plugindata");
|
||||
for (String name : map.keySet()) {
|
||||
Main.getConnection().intoValue(Main.getTablePrefix() + "_plugindata", name, map.get(name), TabooLib.getServerUID());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有有效变量
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static HashMap<String, String> getVariables() {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
LinkedList<HashMap<String, Object>> list = Main.getConnection().getValues(Main.getTablePrefix() + "_plugindata", "id", -1, false, "name", "variable");
|
||||
for (HashMap<String, Object> _map : list) {
|
||||
if (!_map.get("variable").toString().equals("null")) {
|
||||
map.put(_map.get("name").toString(), _map.get("variable").toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (String name : data.getConfigurationSection("").getKeys(false)) {
|
||||
map.put(name, data.getString(name));
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存变量(该方法仅限数据库储存方式)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static HashMap<String, String> getVariablesAsynchronous() {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
for (SQLVariable variable : SQLMethod.getSQLVariables()) {
|
||||
if (!variable.getVariable().equals("null")) {
|
||||
map.put(variable.getName(), variable.getVariable());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
else {
|
||||
return getVariables();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库变量
|
||||
*
|
||||
* @author sky
|
||||
*
|
||||
*/
|
||||
public static class SQLVariable {
|
||||
|
||||
public String name = "";
|
||||
public String variable = "";
|
||||
public String upgradeUID = "";
|
||||
|
||||
public SQLVariable(String name, String variable, String upgradeUID) {
|
||||
this.name = name;
|
||||
this.variable = variable;
|
||||
this.upgradeUID = upgradeUID;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getVariable() {
|
||||
return variable;
|
||||
}
|
||||
|
||||
public SQLVariable setVariable(String args) {
|
||||
this.variable = args;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getUpgradeUID() {
|
||||
return upgradeUID;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库方法
|
||||
*
|
||||
* @author sky
|
||||
*
|
||||
*/
|
||||
public static class SQLMethod {
|
||||
|
||||
private static ConcurrentHashMap<String, SQLVariable> variables = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param name 名字
|
||||
*/
|
||||
public static SQLVariable getSQLVariable(String name) {
|
||||
return variables.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有变量
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Collection<SQLVariable> getSQLVariables() {
|
||||
return variables.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个变量
|
||||
*
|
||||
* @param name 名字
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public static SQLVariable addSQLVariable(String name, String value) {
|
||||
SQLVariable variable = new SQLVariable(name, value, TabooLib.getServerUID());
|
||||
variables.put(name, variable);
|
||||
return variable;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除一个变量
|
||||
*
|
||||
* @param name 名字
|
||||
* @return
|
||||
*/
|
||||
public static SQLVariable removeSQLVariable(String name) {
|
||||
if (variables.contains(name)) {
|
||||
variables.get(name).setVariable("null");
|
||||
}
|
||||
return variables.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含变量
|
||||
*
|
||||
* @param name 名字
|
||||
* @return
|
||||
*/
|
||||
public static boolean contains(String name) {
|
||||
return variables.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入数据库中的所有变量缓存
|
||||
*
|
||||
* @param sync 是否异步
|
||||
*/
|
||||
public static void loadVariables(boolean sync) {
|
||||
if (Main.getStorageType() == StorageType.LOCAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
BukkitRunnable runnable = new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
LinkedList<HashMap<String, Object>> list = Main.getConnection().getValues(Main.getTablePrefix() + "_plugindata", "id", -1, false, "name", "variable", "upgrade");
|
||||
for (HashMap<String, Object> _map : list) {
|
||||
if (!_map.get("variable").toString().equals("null")) {
|
||||
variables.put(_map.get("name").toString(), new SQLVariable(_map.get("name").toString(), _map.get("variable").toString(), _map.get("upgrade").toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前变量是否被其他服务器更新
|
||||
*
|
||||
* @param sync 是否异步
|
||||
*/
|
||||
public static void checkVariable(boolean sync) {
|
||||
if (Main.getStorageType() == StorageType.LOCAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
BukkitRunnable runnable = new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (String name : variables.keySet()) {
|
||||
// 获取数据
|
||||
HashMap<String, Object> value = Main.getConnection().getValueLast(Main.getTablePrefix() + "_plugindata", "name", name, "variable", "upgrade");
|
||||
try {
|
||||
// 检查更新服务器的名称是与本服不同
|
||||
if (!value.get("upgrade").toString().equals(variables.get(name).getUpgradeUID())) {
|
||||
if (value.get("variable").toString().equals("null")) {
|
||||
variables.remove(name);
|
||||
}
|
||||
else {
|
||||
variables.get(name).setVariable(value.get("variable").toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// 移除
|
||||
variables.remove(name);
|
||||
// 提示
|
||||
MsgUtils.warn("变量出现异常: &4" + name);
|
||||
MsgUtils.warn("原因: &4" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向数据库上传所有数据
|
||||
*
|
||||
* @param sync 是否异步
|
||||
*/
|
||||
public static void uploadVariables(boolean sync) {
|
||||
if (Main.getStorageType() == StorageType.LOCAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (SQLVariable variable : variables.values()) {
|
||||
uploadVariable(variable, sync);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向数据库上传当前数据
|
||||
*
|
||||
* @param variable 数据
|
||||
* @param sync 是否异步
|
||||
*/
|
||||
public static void uploadVariable(SQLVariable variable, boolean sync) {
|
||||
if (Main.getStorageType() == StorageType.LOCAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
BukkitRunnable runnable = new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Main.getConnection().intoValue(Main.getTablePrefix() + "_plugindata", variable.getName(), variable.getVariable() == null ? "null" : variable.getVariable(), TabooLib.getServerUID());
|
||||
}
|
||||
};
|
||||
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动数据库储存方法
|
||||
*
|
||||
*/
|
||||
public static void startSQLMethod() {
|
||||
long time = System.currentTimeMillis();
|
||||
// 载入数据
|
||||
loadVariables(false);
|
||||
// 提示信息
|
||||
MsgUtils.send("从数据库中获取 &f" + variables.size() + " &7个变量, 耗时: &f" + (System.currentTimeMillis() - time) + " &7(ms)");
|
||||
|
||||
// 检查更新
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
checkVariable(true);
|
||||
}
|
||||
}.runTaskTimerAsynchronously(Main.getInst(), Main.getInst().getConfig().getInt("PluginData.CHECK-DELAY") * 20, Main.getInst().getConfig().getInt("PluginData.CHECK-DELAY") * 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* 结束数据库储存方法
|
||||
*
|
||||
*/
|
||||
public static void cancelSQLMethod() {
|
||||
// 上传数据
|
||||
uploadVariables(false);
|
||||
}
|
||||
}
|
||||
}
|
271
src/main/src/me/skymc/taboolib/database/PlayerDataManager.java
Normal file
271
src/main/src/me/skymc/taboolib/database/PlayerDataManager.java
Normal file
@ -0,0 +1,271 @@
|
||||
package me.skymc.taboolib.database;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
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 org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.Main.StorageType;
|
||||
import me.skymc.taboolib.events.PlayerLoadedEvent;
|
||||
import me.skymc.taboolib.exception.PlayerOfflineException;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class PlayerDataManager implements Listener {
|
||||
|
||||
private static final ConcurrentHashMap<String, FileConfiguration> PLAYER_DATA = new ConcurrentHashMap<>();
|
||||
|
||||
public static enum UsernameType {
|
||||
UUID, USERNAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户储存方式
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static UsernameType getUsernameType() {
|
||||
return Main.getInst().getConfig().getBoolean("ENABLE-UUID") ? UsernameType.UUID : UsernameType.USERNAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家数据
|
||||
*
|
||||
* @param player 玩家
|
||||
* @return
|
||||
* @throws PlayerOfflineException
|
||||
*/
|
||||
public static FileConfiguration getPlayerData(Player player) {
|
||||
if (getUsernameType() == UsernameType.UUID) {
|
||||
return getPlayerData(player.getUniqueId().toString(), false);
|
||||
}
|
||||
else {
|
||||
return getPlayerData(player.getName(), false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家数据
|
||||
*
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
public static FileConfiguration getPlayerData(OfflinePlayer player) {
|
||||
if (!player.isOnline()) {
|
||||
return null;
|
||||
}
|
||||
if (getUsernameType() == UsernameType.UUID) {
|
||||
return getPlayerData(player.getUniqueId().toString(), false);
|
||||
}
|
||||
else {
|
||||
return getPlayerData(player.getName(), false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取玩家数据
|
||||
*
|
||||
* @param username 玩家
|
||||
* @return
|
||||
* @throws PlayerOfflineException
|
||||
*/
|
||||
public static FileConfiguration getPlayerData(String username, boolean offline) {
|
||||
if (PLAYER_DATA.containsKey(username)) {
|
||||
return PLAYER_DATA.get(username);
|
||||
}
|
||||
else if (offline) {
|
||||
if (Main.getStorageType() == StorageType.SQL) {
|
||||
throw new PlayerOfflineException("不允许在储存模式为数据库的情况下获取离线玩家数据");
|
||||
}
|
||||
return loadPlayerData(username);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入玩家数据
|
||||
*
|
||||
* @param username 玩家
|
||||
* @return
|
||||
*/
|
||||
public static FileConfiguration loadPlayerData(String username) {
|
||||
// 本地储存
|
||||
if (Main.getStorageType() == StorageType.LOCAL) {
|
||||
// 读取文件
|
||||
File file = FileUtils.file(Main.getPlayerDataFolder(), username + ".yml");
|
||||
// 载入配置
|
||||
PLAYER_DATA.put(username, YamlConfiguration.loadConfiguration(file));
|
||||
}
|
||||
else {
|
||||
// 数据是否存在
|
||||
if (Main.getConnection().isExists(Main.getTablePrefix() + "_playerdata", "username", username)) {
|
||||
// 获取数据
|
||||
String code = Main.getConnection().getValue(Main.getTablePrefix() + "_playerdata", "username", username, "configuration").toString();
|
||||
try {
|
||||
// 载入配置
|
||||
PLAYER_DATA.put(username, ConfigUtils.decodeYAML(code));
|
||||
}
|
||||
catch (Exception e) {
|
||||
// 创建空数据
|
||||
PLAYER_DATA.put(username, new YamlConfiguration());
|
||||
// 反馈信息
|
||||
MsgUtils.warn("玩家 &4" + username + " &c的数据载入出现异常: &4" + e.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 创建空数据
|
||||
PLAYER_DATA.put(username, new YamlConfiguration());
|
||||
}
|
||||
}
|
||||
return PLAYER_DATA.get(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存玩家数据
|
||||
*
|
||||
* @param username 玩家
|
||||
* @param remove 是否移除缓存
|
||||
*/
|
||||
public static void savePlayerData(String username, boolean remove) {
|
||||
// 没有数据
|
||||
if (!PLAYER_DATA.containsKey(username)) {
|
||||
return;
|
||||
}
|
||||
// 本地储存
|
||||
if (Main.getStorageType() == StorageType.LOCAL) {
|
||||
// 读取文件
|
||||
File file = FileUtils.file(Main.getPlayerDataFolder(), username + ".yml");
|
||||
// 保存配置
|
||||
try {
|
||||
PLAYER_DATA.get(username).save(file);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
// 如果是数据库储存且有数据
|
||||
else if (PLAYER_DATA.get(username).getConfigurationSection("").getKeys(false).size() > 0) {
|
||||
// 数据是否存在
|
||||
if (Main.getConnection().isExists(Main.getTablePrefix() + "_playerdata", "username", username)) {
|
||||
// 写入数据
|
||||
Main.getConnection().setValue(Main.getTablePrefix() + "_playerdata", "username", username, "configuration", ConfigUtils.encodeYAML(PLAYER_DATA.get(username)));
|
||||
}
|
||||
else {
|
||||
// 插入数据
|
||||
Main.getConnection().intoValue(Main.getTablePrefix() + "_playerdata", username, ConfigUtils.encodeYAML(PLAYER_DATA.get(username)));
|
||||
}
|
||||
}
|
||||
// 获取这个属性对应的玩家
|
||||
Player player;
|
||||
if (getUsernameType() == UsernameType.UUID) {
|
||||
player = Bukkit.getPlayer(UUID.fromString(username));
|
||||
}
|
||||
else {
|
||||
player = Bukkit.getPlayerExact(username);
|
||||
}
|
||||
// 如果移除数据 或 玩家不在线
|
||||
if (remove || player == null) {
|
||||
PLAYER_DATA.remove(username);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存所有玩家的缓存
|
||||
*
|
||||
* @param sync 是否异步进行
|
||||
* @param remove 是否移除数据
|
||||
*/
|
||||
public static void saveAllCaches(boolean sync, boolean remove) {
|
||||
BukkitRunnable runnable = new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long time = System.currentTimeMillis();
|
||||
// 保存
|
||||
for (String name : PLAYER_DATA.keySet()) {
|
||||
savePlayerData(name, false);
|
||||
}
|
||||
// 提示
|
||||
if (!Main.getInst().getConfig().getBoolean("HIDE-NOTIFY")) {
|
||||
MsgUtils.send("保存 &f" + PLAYER_DATA.size() + " &7条玩家数据, 耗时: &f" + (System.currentTimeMillis() - time) + " &7(ms)");
|
||||
}
|
||||
}
|
||||
};
|
||||
// 如果异步
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
// 如果同步
|
||||
else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存所有玩家的数据
|
||||
*
|
||||
* @param sync 是否异步进行
|
||||
* @param remove 是否移除数据
|
||||
*/
|
||||
public static void saveAllPlayers(boolean sync, boolean remove) {
|
||||
// 创建任务
|
||||
BukkitRunnable runnable = new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
savePlayerData(Main.getInst().getConfig().getBoolean("ENABLE-UUID") ? player.getUniqueId().toString() : player.getName(), remove);
|
||||
}
|
||||
}
|
||||
};
|
||||
// 如果异步
|
||||
if (sync) {
|
||||
runnable.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
// 如果同步
|
||||
else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void join(PlayerJoinEvent e) {
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// 载入数据
|
||||
loadPlayerData(Main.getInst().getConfig().getBoolean("ENABLE-UUID") ? e.getPlayer().getUniqueId().toString() : e.getPlayer().getName());
|
||||
// 载入完成
|
||||
Bukkit.getPluginManager().callEvent(new PlayerLoadedEvent(e.getPlayer()));
|
||||
}
|
||||
}.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
if (!Main.isDisable()) {
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// 保存数据
|
||||
savePlayerData(Main.getInst().getConfig().getBoolean("ENABLE-UUID") ? e.getPlayer().getUniqueId().toString() : e.getPlayer().getName(), true);
|
||||
}
|
||||
}.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
}
|
||||
}
|
57
src/main/src/me/skymc/taboolib/display/ActionUtils.java
Normal file
57
src/main/src/me/skymc/taboolib/display/ActionUtils.java
Normal file
@ -0,0 +1,57 @@
|
||||
package me.skymc.taboolib.display;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class ActionUtils {
|
||||
|
||||
private static void sendPacket(Player player, Object packet)
|
||||
{
|
||||
try
|
||||
{
|
||||
Object handle = player.getClass().getMethod("getHandle", new Class[0]).invoke(player, new Object[0]);
|
||||
Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
|
||||
playerConnection.getClass().getMethod("sendPacket", new Class[] { getNMSClass("Packet") }).invoke(playerConnection, new Object[] { packet });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> getNMSClass(String class_name)
|
||||
{
|
||||
String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
try
|
||||
{
|
||||
return Class.forName("net.minecraft.server." + version + "." + class_name);
|
||||
}
|
||||
catch (ClassNotFoundException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void send(Player p, String msg)
|
||||
{
|
||||
if (msg == null) {
|
||||
msg = "";
|
||||
}
|
||||
try
|
||||
{
|
||||
Object ab = getNMSClass("ChatComponentText").getConstructor(new Class[] { String.class }).newInstance(new Object[] { msg });
|
||||
Constructor<?> ac = getNMSClass("PacketPlayOutChat").getConstructor(new Class[] { getNMSClass("IChatBaseComponent"), Byte.TYPE });
|
||||
Object abPacket = ac.newInstance(new Object[] { ab, Byte.valueOf((byte) 2) });
|
||||
sendPacket(p, abPacket);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
83
src/main/src/me/skymc/taboolib/display/TitleUtils.java
Normal file
83
src/main/src/me/skymc/taboolib/display/TitleUtils.java
Normal file
@ -0,0 +1,83 @@
|
||||
package me.skymc.taboolib.display;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class TitleUtils {
|
||||
|
||||
private static void sendPacket(Player player, Object packet)
|
||||
{
|
||||
try
|
||||
{
|
||||
Object handle = player.getClass().getMethod("getHandle", new Class[0]).invoke(player, new Object[0]);
|
||||
Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
|
||||
playerConnection.getClass().getMethod("sendPacket", new Class[] { getNMSClass("Packet") }).invoke(playerConnection, new Object[] { packet });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> getNMSClass(String class_name)
|
||||
{
|
||||
String version = org.bukkit.Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
try
|
||||
{
|
||||
return Class.forName("net.minecraft.server." + version + "." + class_name);
|
||||
}
|
||||
catch (ClassNotFoundException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void sendTitle(Player p, String title, int fadeint, int stayt, int fadeoutt, String subtitle, int fadeinst, int stayst, int fadeoutst)
|
||||
{
|
||||
if (title == null) {
|
||||
title = "";
|
||||
}
|
||||
if (subtitle == null) {
|
||||
subtitle = "";
|
||||
}
|
||||
try
|
||||
{
|
||||
if (title != null)
|
||||
{
|
||||
Object e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TIMES").get(null);
|
||||
Object chatTitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + title + "\"}" });
|
||||
Constructor<?> subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[] { getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE });
|
||||
Object titlePacket = subtitleConstructor.newInstance(new Object[] { e, chatTitle, Integer.valueOf(fadeint), Integer.valueOf(stayt), Integer.valueOf(fadeoutt) });
|
||||
sendPacket(p, titlePacket);
|
||||
|
||||
e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TITLE").get(null);
|
||||
chatTitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + title + "\"}" });
|
||||
subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[] { getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent") });
|
||||
titlePacket = subtitleConstructor.newInstance(new Object[] { e, chatTitle });
|
||||
sendPacket(p, titlePacket);
|
||||
}
|
||||
if (subtitle != null)
|
||||
{
|
||||
Object e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TIMES").get(null);
|
||||
Object chatSubtitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + title + "\"}" });
|
||||
Constructor<?> subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[] { getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE });
|
||||
Object subtitlePacket = subtitleConstructor.newInstance(new Object[] { e, chatSubtitle, Integer.valueOf(fadeinst), Integer.valueOf(stayst), Integer.valueOf(fadeoutst) });
|
||||
sendPacket(p, subtitlePacket);
|
||||
|
||||
e = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("SUBTITLE").get(null);
|
||||
chatSubtitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", new Class[] { String.class }).invoke(null, new Object[] { "{\"text\":\"" + subtitle + "\"}" });
|
||||
subtitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(new Class[] { getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), Integer.TYPE, Integer.TYPE, Integer.TYPE });
|
||||
subtitlePacket = subtitleConstructor.newInstance(new Object[] { e, chatSubtitle, Integer.valueOf(fadeinst), Integer.valueOf(stayst), Integer.valueOf(fadeoutst) });
|
||||
sendPacket(p, subtitlePacket);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
34
src/main/src/me/skymc/taboolib/economy/EcoUtils.java
Normal file
34
src/main/src/me/skymc/taboolib/economy/EcoUtils.java
Normal file
@ -0,0 +1,34 @@
|
||||
package me.skymc.taboolib.economy;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
public class EcoUtils {
|
||||
|
||||
public static void setupEconomy() {
|
||||
RegisteredServiceProvider<Economy> l = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (l != null) {
|
||||
Main.setEconomy(l.getProvider());
|
||||
}
|
||||
}
|
||||
|
||||
public static void remove(OfflinePlayer p, double d) {
|
||||
Main.getEconomy().withdrawPlayer(p, d);
|
||||
}
|
||||
|
||||
public static void add(OfflinePlayer p, double d) {
|
||||
Main.getEconomy().depositPlayer(p, d);
|
||||
}
|
||||
|
||||
public static double get(OfflinePlayer p) {
|
||||
return Main.getEconomy().getBalance(p);
|
||||
}
|
||||
|
||||
public static void create(OfflinePlayer p) {
|
||||
Main.getEconomy().createPlayerAccount(p);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package me.skymc.taboolib.enchantment;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
|
||||
public class EnchantmentUtils {
|
||||
|
||||
public static void setAcceptingNew(boolean value) {
|
||||
try {
|
||||
Field f = Enchantment.class.getDeclaredField("acceptingNew");
|
||||
f.setAccessible(true);
|
||||
f.set(null, value);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
92
src/main/src/me/skymc/taboolib/entity/EntityUtils.java
Normal file
92
src/main/src/me/skymc/taboolib/entity/EntityUtils.java
Normal file
@ -0,0 +1,92 @@
|
||||
package me.skymc.taboolib.entity;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntitySpawnEvent;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class EntityUtils implements Listener{
|
||||
|
||||
public static Entity lastSpawned = null;
|
||||
|
||||
@EventHandler
|
||||
public void spawn(EntitySpawnEvent e) {
|
||||
lastSpawned = e.getEntity();
|
||||
}
|
||||
|
||||
public static Entity getEntityWithUUID(UUID u) {
|
||||
for (World w : Bukkit.getWorlds()) {
|
||||
for (Entity e : w.getLivingEntities()) {
|
||||
if (e.getUniqueId().equals(u)) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Entity getEntityWithUUID_World(UUID u, World world) {
|
||||
for (Entity e : world.getLivingEntities()) {
|
||||
if (e.getUniqueId().equals(u)) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置生物发光(ProcotolLib)
|
||||
*
|
||||
* @param player
|
||||
* @param entity
|
||||
*/
|
||||
public static void addGlow(Player player,Entity entity) {
|
||||
PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA);
|
||||
packet.getIntegers().write(0, entity.getEntityId());
|
||||
WrappedDataWatcher watcher = new WrappedDataWatcher();
|
||||
WrappedDataWatcher.Serializer serializer = WrappedDataWatcher.Registry.get(Byte.class);
|
||||
watcher.setEntity(player);
|
||||
watcher.setObject(0, serializer, (byte) (0x40));
|
||||
packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消生物发光(ProcotolLib)
|
||||
*
|
||||
* @param player
|
||||
* @param entity
|
||||
*/
|
||||
public static void delGlow(Player player,Entity entity) {
|
||||
PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA);
|
||||
packet.getIntegers().write(0, entity.getEntityId());
|
||||
WrappedDataWatcher watcher = new WrappedDataWatcher();
|
||||
WrappedDataWatcher.Serializer serializer = WrappedDataWatcher.Registry.get(Byte.class);
|
||||
watcher.setEntity(player);
|
||||
watcher.setObject(0, serializer, (byte) (0x0));
|
||||
packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
28
src/main/src/me/skymc/taboolib/events/DefaultEvent.java
Normal file
28
src/main/src/me/skymc/taboolib/events/DefaultEvent.java
Normal file
@ -0,0 +1,28 @@
|
||||
package me.skymc.taboolib.events;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
public class DefaultEvent extends Event {
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private Player player;
|
||||
|
||||
public DefaultEvent(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
51
src/main/src/me/skymc/taboolib/events/DefaultEvent2.java
Normal file
51
src/main/src/me/skymc/taboolib/events/DefaultEvent2.java
Normal file
@ -0,0 +1,51 @@
|
||||
package me.skymc.taboolib.events;
|
||||
|
||||
import org.bukkit.event.player.*;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.event.*;
|
||||
|
||||
public class DefaultEvent2 extends PlayerEvent
|
||||
{
|
||||
private static final HandlerList handlers;
|
||||
|
||||
static {
|
||||
handlers = new HandlerList();
|
||||
}
|
||||
|
||||
private DefaultEvent2(final Player who) {
|
||||
super(who);
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return DefaultEvent2.handlers;
|
||||
}
|
||||
|
||||
public HandlerList getHandlers() {
|
||||
return DefaultEvent2.handlers;
|
||||
}
|
||||
|
||||
public static class Pre extends DefaultEvent2 implements Cancellable
|
||||
{
|
||||
private boolean cancelled;
|
||||
|
||||
public Pre(Player who) {
|
||||
super(who);
|
||||
this.cancelled = false;
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
public void setCancelled(final boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Post extends DefaultEvent2
|
||||
{
|
||||
public Post(Player who) {
|
||||
super(who);
|
||||
}
|
||||
}
|
||||
}
|
46
src/main/src/me/skymc/taboolib/events/PlayerJumpEvent.java
Normal file
46
src/main/src/me/skymc/taboolib/events/PlayerJumpEvent.java
Normal file
@ -0,0 +1,46 @@
|
||||
package me.skymc.taboolib.events;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
public class PlayerJumpEvent
|
||||
extends Event
|
||||
implements Cancellable
|
||||
{
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private boolean isCancelled;
|
||||
private Player player;
|
||||
|
||||
public PlayerJumpEvent(boolean b, Player player)
|
||||
{
|
||||
this.isCancelled = false;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public Player getPlayer()
|
||||
{
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public boolean isCancelled()
|
||||
{
|
||||
return this.isCancelled;
|
||||
}
|
||||
|
||||
public void setCancelled(boolean e)
|
||||
{
|
||||
this.isCancelled = e;
|
||||
}
|
||||
|
||||
public HandlerList getHandlers()
|
||||
{
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList()
|
||||
{
|
||||
return handlers;
|
||||
}
|
||||
}
|
28
src/main/src/me/skymc/taboolib/events/PlayerLoadedEvent.java
Normal file
28
src/main/src/me/skymc/taboolib/events/PlayerLoadedEvent.java
Normal file
@ -0,0 +1,28 @@
|
||||
package me.skymc.taboolib.events;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
public class PlayerLoadedEvent extends Event {
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private Player player;
|
||||
|
||||
public PlayerLoadedEvent(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package me.skymc.taboolib.exception;
|
||||
|
||||
public class PlayerOfflineException extends Error {
|
||||
|
||||
private static final long serialVersionUID = 4129402767538548807L;
|
||||
|
||||
public PlayerOfflineException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
53
src/main/src/me/skymc/taboolib/fileutils/ConfigUtils.java
Normal file
53
src/main/src/me/skymc/taboolib/fileutils/ConfigUtils.java
Normal file
@ -0,0 +1,53 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class ConfigUtils {
|
||||
|
||||
public static FileConfiguration decodeYAML(String args) {
|
||||
return YamlConfiguration.loadConfiguration(new StringReader(Base64Coder.decodeString(args)));
|
||||
}
|
||||
|
||||
public static String encodeYAML(FileConfiguration file) {
|
||||
return Base64Coder.encodeLines(file.saveToString().getBytes()).replaceAll("\\s+", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 UTF-8 的格式载入配置文件
|
||||
*
|
||||
* @param main
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
public static YamlConfiguration load(Plugin plugin, File file) {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
try {
|
||||
yaml = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8));
|
||||
} catch (FileNotFoundException e) {
|
||||
MsgUtils.warn("配置文件载入失败!");
|
||||
MsgUtils.warn("插件: &4" + plugin.getName());
|
||||
MsgUtils.warn("文件: &4" + file.getName());
|
||||
}
|
||||
return yaml;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static YamlConfiguration load(Plugin plugin, String file) {
|
||||
return load(plugin, FileUtils.file(file));
|
||||
}
|
||||
}
|
98
src/main/src/me/skymc/taboolib/fileutils/CopyUtils.java
Normal file
98
src/main/src/me/skymc/taboolib/fileutils/CopyUtils.java
Normal file
@ -0,0 +1,98 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class CopyUtils {
|
||||
|
||||
public static long Copy(File file1, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file1.exists()) {
|
||||
file1.createNewFile();
|
||||
}
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileInputStream in = new FileInputStream(file1);
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static long Copy(FileInputStream in, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
124
src/main/src/me/skymc/taboolib/fileutils/EncodeUtils.java
Normal file
124
src/main/src/me/skymc/taboolib/fileutils/EncodeUtils.java
Normal file
@ -0,0 +1,124 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
public class EncodeUtils {
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param fileName 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(file, fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
File[] fileList = null;
|
||||
if (filter == null) {
|
||||
fileList = file.listFiles();
|
||||
} else {
|
||||
fileList = file.listFiles(filter);
|
||||
}
|
||||
for (File f : fileList) {
|
||||
convert(f, fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
} else {
|
||||
if (filter == null
|
||||
|| filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file,
|
||||
fromCharsetName);
|
||||
saveFile2Charset(file, toCharsetName, fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以指定编码方式读取文件,返回文件内容
|
||||
*
|
||||
* @param file 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String getFileContentFromCharset(File file, String fromCharsetName) throws Exception {
|
||||
if (!Charset.isSupported(fromCharsetName)) {
|
||||
throw new UnsupportedCharsetException(fromCharsetName);
|
||||
}
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream,
|
||||
fromCharsetName);
|
||||
char[] chs = new char[(int) file.length()];
|
||||
reader.read(chs);
|
||||
String str = new String(chs).trim();
|
||||
reader.close();
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以指定编码方式写文本文件,存在会覆盖
|
||||
*
|
||||
* @param file 要写入的文件
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param content 文件内容
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void saveFile2Charset(File file, String toCharsetName, String content) throws Exception {
|
||||
if (!Charset.isSupported(toCharsetName)) {
|
||||
throw new UnsupportedCharsetException(toCharsetName);
|
||||
}
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
OutputStreamWriter outWrite = new OutputStreamWriter(outputStream, toCharsetName);
|
||||
outWrite.write(content);
|
||||
outWrite.close();
|
||||
|
||||
System.out.println("[Encodeing...] 更改文件: " + file.getPath());
|
||||
}
|
||||
}
|
209
src/main/src/me/skymc/taboolib/fileutils/FileUtils.java
Normal file
209
src/main/src/me/skymc/taboolib/fileutils/FileUtils.java
Normal file
@ -0,0 +1,209 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import me.skymc.taboolib.client.LogClient;
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
public static String ip() {
|
||||
try {
|
||||
InputStream ins = null;
|
||||
URL url = new URL("http://1212.ip138.com/ic.asp");
|
||||
URLConnection con = url.openConnection();
|
||||
ins = con.getInputStream();
|
||||
InputStreamReader isReader = new InputStreamReader(ins, "GB2312");
|
||||
BufferedReader bReader = new BufferedReader(isReader);
|
||||
StringBuffer webContent = new StringBuffer();
|
||||
String str = null;
|
||||
while ((str = bReader.readLine()) != null) {
|
||||
webContent.append(str);
|
||||
}
|
||||
int start = webContent.indexOf("[") + 1;
|
||||
int end = webContent.indexOf("]");
|
||||
ins.close();
|
||||
return webContent.substring(start, end);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
return "[IP ERROR]";
|
||||
}
|
||||
|
||||
public static File file(String filePath) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public static File file(File Path, String filePath) {
|
||||
File file = new File(Path, filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public static String getStringFromInputStream(InputStream in, int size, String encode) {
|
||||
try {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
while ((i = in.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getStringFromFile(File file, int size, String encode) {
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
BufferedInputStream bin = new BufferedInputStream(fin);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
while ((i = bin.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
bin.close();
|
||||
fin.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getStringFromURL(String url, int size) {
|
||||
try {
|
||||
URLConnection conn = new URL(url).openConnection();
|
||||
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
while ((i = bin.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
bin.close();
|
||||
return new String(bos.toByteArray(), conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a UTF8 file
|
||||
*
|
||||
* @param strs list of lines
|
||||
* @param f file to write
|
||||
*
|
||||
*/
|
||||
public FileUtils(List<String> strs, File f) throws IOException
|
||||
{
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
||||
new FileOutputStream(f), "UTF8"));
|
||||
for(String s : strs) {
|
||||
out.write(s+"\n");
|
||||
}
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
public FileUtils(String str, File f) throws IOException
|
||||
{
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
||||
new FileOutputStream(f), "UTF8"));
|
||||
out.write(str);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static void download(String urlStr, String filename, File saveDir) {
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
// 超时时间
|
||||
conn.setConnectTimeout(5 * 1000);
|
||||
// 防止屏蔽程序抓取而返回 403 错误
|
||||
conn.setRequestProperty("User-Agent", "Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)");
|
||||
|
||||
// 得到输入流
|
||||
InputStream inputStream = conn.getInputStream();
|
||||
// 获取数组
|
||||
byte[] data = read(inputStream);
|
||||
|
||||
// 创建文件夹
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.mkdirs();
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
File file = new File(saveDir, filename);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
|
||||
// 写入文件
|
||||
fos.write(data);
|
||||
|
||||
// 结束
|
||||
fos.close();
|
||||
inputStream.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] read(InputStream in) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try {
|
||||
while((len = in.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
bos.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bos.toByteArray();
|
||||
}
|
||||
}
|
60
src/main/src/me/skymc/taboolib/fileutils/LogUtils.java
Normal file
60
src/main/src/me/skymc/taboolib/fileutils/LogUtils.java
Normal file
@ -0,0 +1,60 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
import me.skymc.taboolib.other.DateUtils;
|
||||
|
||||
@Deprecated
|
||||
public class LogUtils {
|
||||
|
||||
public static void Log(String s, String s2)
|
||||
{
|
||||
try
|
||||
{
|
||||
File file = new File(Main.getInst().getDataFolder(), s2+".txt");
|
||||
if(!file.exists())
|
||||
{
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write(s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.getInst().getLogger().warning(s2+":"+s);
|
||||
}
|
||||
}
|
||||
|
||||
public static void newLog(Plugin main, String s, String s2)
|
||||
{
|
||||
try
|
||||
{
|
||||
File file = new File(main.getDataFolder(), s2+".txt");
|
||||
if(!file.exists())
|
||||
{
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write("["+DateUtils.CH_ALL.format(System.currentTimeMillis())+"]"+s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.getInst().getLogger().warning(s2+":"+s);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
51
src/main/src/me/skymc/taboolib/inventory/DropUtils.java
Normal file
51
src/main/src/me/skymc/taboolib/inventory/DropUtils.java
Normal file
@ -0,0 +1,51 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
|
||||
public class DropUtils {
|
||||
|
||||
public static Item drop(Player player, ItemStack itemStack, double bulletSpread, double radius) {
|
||||
Location location = player.getLocation();
|
||||
location.setY(location.getY() + 1.5);
|
||||
|
||||
Item item = player.getWorld().dropItem(location, itemStack);
|
||||
|
||||
double yaw = Math.toRadians(-player.getLocation().getYaw() - 90.0F);
|
||||
double pitch = Math.toRadians(-player.getLocation().getPitch());
|
||||
|
||||
double x = 0;
|
||||
double y = 0;
|
||||
double z = 0;
|
||||
|
||||
if (bulletSpread > 0) {
|
||||
double[] spread = { 1.0D, 1.0D, 1.0D };
|
||||
for (int t = 0; t < 3; t++) {
|
||||
spread[t] = ((NumberUtils.getRand().nextDouble() - NumberUtils.getRand().nextDouble()) * bulletSpread * 0.1D);
|
||||
}
|
||||
|
||||
x = Math.cos(pitch) * Math.cos(yaw) + spread[0];
|
||||
y = Math.sin(pitch) + spread[1];
|
||||
z = -Math.sin(yaw) * Math.cos(pitch) + spread[2];
|
||||
}
|
||||
else {
|
||||
x = Math.cos(pitch) * Math.cos(yaw);
|
||||
y = Math.sin(pitch);
|
||||
z = -Math.sin(yaw) * Math.cos(pitch);
|
||||
}
|
||||
Vector dirVel = new Vector(x, y, z);
|
||||
dirVel.normalize().multiply(radius);
|
||||
|
||||
item.setVelocity(dirVel);
|
||||
return item;
|
||||
}
|
||||
|
||||
}
|
140
src/main/src/me/skymc/taboolib/inventory/InventoryUtil.java
Normal file
140
src/main/src/me/skymc/taboolib/inventory/InventoryUtil.java
Normal file
@ -0,0 +1,140 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class InventoryUtil {
|
||||
|
||||
public final static LinkedList<Integer> SLOT_OF_CENTENTS = new LinkedList<>(Arrays.asList(
|
||||
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));
|
||||
|
||||
@Deprecated
|
||||
public static boolean isEmpey(Player p) {
|
||||
return isEmpty(p, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查背包是否有空位
|
||||
*
|
||||
* @param p 玩家
|
||||
* @param i 起始位置
|
||||
*/
|
||||
public static boolean isEmpty(Player p, int i) {
|
||||
while (i < 35) {
|
||||
if (p.getInventory().getItem(i) == null) {
|
||||
return true;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测玩家是否有指定物品
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param item 物品
|
||||
* @param amount 数量
|
||||
* @param remove 是否删除
|
||||
*/
|
||||
public static boolean hasItem(Player player, ItemStack item, int amount, boolean remove) {
|
||||
int hasAmount = 0;
|
||||
for (ItemStack _item : player.getInventory()) {
|
||||
if (item.isSimilar(_item)) {
|
||||
hasAmount += _item.getAmount();
|
||||
}
|
||||
}
|
||||
if (hasAmount < amount) {
|
||||
return false;
|
||||
}
|
||||
int requireAmount = amount;
|
||||
for (int i = 0; i < player.getInventory().getSize() && remove; i++) {
|
||||
ItemStack _item = player.getInventory().getItem(i);
|
||||
if (_item != null && _item.isSimilar(item)) {
|
||||
/**
|
||||
* 如果循环到的物品数量 小于 需要的数量
|
||||
* 则 删除物品,减少需要的数量
|
||||
*/
|
||||
if (_item.getAmount() < requireAmount) {
|
||||
player.getInventory().setItem(i, null);
|
||||
requireAmount -= _item.getAmount();
|
||||
}
|
||||
/**
|
||||
* 如果循环到的物品数量 等于 需要的数量
|
||||
* 则 删除物品,直接结束
|
||||
*/
|
||||
else if (_item.getAmount() == requireAmount) {
|
||||
player.getInventory().setItem(i, null);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 如果循环到的物品数量 大于 需要的数量
|
||||
* 则扣除 需要的数量
|
||||
*/
|
||||
else {
|
||||
_item.setAmount(_item.getAmount() - requireAmount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean hasItem(Inventory targetInventory, ItemStack targetItem, Integer amount) {
|
||||
int inventoryAmount = 0;
|
||||
for (ItemStack item : targetInventory) {
|
||||
if (item != null) {
|
||||
if (item.isSimilar(targetItem)) {
|
||||
inventoryAmount += item.getAmount();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inventoryAmount >= amount) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean takeItem2(Inventory inv, ItemStack takeitem, Integer amount) {
|
||||
for (int i = 0; i < inv.getSize(); ++i) {
|
||||
if (amount <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ItemStack item = inv.getItem(i);
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
if (!item.isSimilar(takeitem)) {
|
||||
continue;
|
||||
}
|
||||
if (item.getAmount() >= amount) {
|
||||
if (item.getAmount() - amount == 0) {
|
||||
inv.setItem(i, null);
|
||||
}
|
||||
else {
|
||||
item.setAmount(item.getAmount() - amount);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
amount -= item.getAmount();
|
||||
inv.setItem(i, null);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
614
src/main/src/me/skymc/taboolib/inventory/ItemUtils.java
Normal file
614
src/main/src/me/skymc/taboolib/inventory/ItemUtils.java
Normal file
@ -0,0 +1,614 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
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.scheduler.BukkitRunnable;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.skymc.taboocode.TabooCodeItem;
|
||||
import me.skymc.taboocode4.manager.ItemManager;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTItem;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTList;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTListCompound;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTType;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.string.Language;
|
||||
|
||||
public class ItemUtils {
|
||||
|
||||
@Getter
|
||||
private static FileConfiguration itemdir = null;
|
||||
|
||||
@Getter
|
||||
private static FileConfiguration itemCache = null;
|
||||
|
||||
@Getter
|
||||
private static File finalItemsFolder;
|
||||
|
||||
@Getter
|
||||
private static LinkedHashMap<String, String> itemlib = new LinkedHashMap<>();
|
||||
|
||||
@Getter
|
||||
private static LinkedHashMap<String, ItemStack> itemCaches = new LinkedHashMap<>();
|
||||
|
||||
@Getter
|
||||
private static LinkedHashMap<String, ItemStack> itemCachesFinal = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取物品缓存
|
||||
* 检测顺序:
|
||||
* 1. 固定物品库
|
||||
* 2. 动态物品库
|
||||
*
|
||||
* @param name 物品名称
|
||||
* @return
|
||||
*/
|
||||
public static ItemStack getCacheItem(String name) {
|
||||
// 检测固定物品库是否存在该物品
|
||||
if (itemCachesFinal.containsKey(name)) {
|
||||
return itemCachesFinal.get(name);
|
||||
}
|
||||
// 返回动态物品库
|
||||
return itemCaches.get(name);
|
||||
}
|
||||
|
||||
public static boolean isExists(String name) {
|
||||
return itemCachesFinal.containsKey(name) || itemCaches.containsKey(name);
|
||||
}
|
||||
|
||||
public static void LoadLib() {
|
||||
itemdir = YamlConfiguration.loadConfiguration(new File(Main.getInst().getConfig().getString("DATAURL.ITEMDIR")));
|
||||
reloadItemName();
|
||||
reloadItemCache();
|
||||
}
|
||||
|
||||
public static void loadItemsFile(File file, boolean finalFile) {
|
||||
YamlConfiguration conf = ConfigUtils.load(Main.getInst(), file);
|
||||
for (String name : conf.getConfigurationSection("").getKeys(false)) {
|
||||
if (isExists(name)) {
|
||||
MsgUtils.warn("无法载入载入物品 &4" + name + "&c, 因为它已经存在了");
|
||||
} else if (finalFile) {
|
||||
itemCachesFinal.put(name, loadItem(conf, name));
|
||||
} else {
|
||||
itemCaches.put(name, loadItem(conf, name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void reloadItemCache() {
|
||||
itemCaches.clear();
|
||||
itemCachesFinal.clear();
|
||||
loadItemsFile(getItemCacheFile(), false);
|
||||
// 创建固定物品库
|
||||
finalItemsFolder = new File(Main.getInst().getDataFolder(), "FinalItems");
|
||||
if (!finalItemsFolder.exists()) {
|
||||
finalItemsFolder.mkdir();
|
||||
}
|
||||
// 检查固定物品库中的物品
|
||||
for (File file : finalItemsFolder.listFiles()) {
|
||||
loadItemsFile(file, true);
|
||||
}
|
||||
MsgUtils.send("载入 " + (itemCaches.size() + itemCachesFinal.size()) + " 项缓存物品");
|
||||
}
|
||||
|
||||
public static void reloadItemName() {
|
||||
FileConfiguration conf = new Language("ITEM_NAME", Main.getInst(), true).getConfiguration();
|
||||
itemlib.clear();
|
||||
for (String a : conf.getConfigurationSection("").getKeys(false)) {
|
||||
itemlib.put(a, conf.getString(a));
|
||||
}
|
||||
MsgUtils.send("载入 " + itemlib.size() + " 项物品名称");
|
||||
}
|
||||
|
||||
public static File getItemCacheFile() {
|
||||
File itemCacheFile = new File(Main.getInst().getDataFolder(), "items.yml");
|
||||
if (!itemCacheFile.exists()) {
|
||||
Main.getInst().saveResource("items.yml", true);
|
||||
}
|
||||
return itemCacheFile;
|
||||
}
|
||||
|
||||
public static String getCustomName(ItemStack item) {
|
||||
if (item == null || item.getType().equals(Material.AIR)) {
|
||||
return "空";
|
||||
}
|
||||
int data = item.getType().getMaxDurability() == 0 ? item.getDurability() : 0;
|
||||
return item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : itemlib.get(item.getType() + ":" + data) == null ? item.getType().toString() : itemlib.get(item.getType() + ":" + data);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static ItemStack getItemFromDir(String name) {
|
||||
ItemStack item = new ItemStack(Material.STONE);
|
||||
if (Bukkit.getPluginManager().getPlugin("TabooCode").isEnabled()) {
|
||||
item = TabooCodeItem.getItem(name, true);
|
||||
}
|
||||
else if (Bukkit.getPluginManager().getPlugin("TabooCode4").isEnabled()) {
|
||||
item = ItemManager.getFinishItem(name);
|
||||
}
|
||||
if (item == null && itemdir != null) {
|
||||
item = itemdir.getItemStack("item." + name);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static ItemStack item(int n, int a, int d) {
|
||||
ItemStack item = new ItemStack(n, a, (short)d);
|
||||
return item;
|
||||
}
|
||||
|
||||
public static ItemStack setName(ItemStack i, String n) {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
meta.setDisplayName(n);
|
||||
i.setItemMeta(meta);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static ItemStack Enchant(ItemStack i, Enchantment e, int l) {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
meta.addEnchant(e, l, false);
|
||||
i.setItemMeta(meta);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static ItemStack addFlag(ItemStack i, ItemFlag f) {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
meta.addItemFlags(f);
|
||||
i.setItemMeta(meta);
|
||||
return i;
|
||||
}
|
||||
|
||||
public static boolean isName(ItemStack i, String a) {
|
||||
if (!isNamed(i) || i.getItemMeta() == null || i.getItemMeta().getDisplayName() == null || !i.getItemMeta().getDisplayName().equals(a)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isNameAs(ItemStack i, String a) {
|
||||
if (!isNamed(i) || !i.getItemMeta().getDisplayName().contains(a)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String asString(String args, Player placeholderPlayer) {
|
||||
if (placeholderPlayer == null) {
|
||||
return args.replace("&", "§");
|
||||
}
|
||||
return PlaceholderAPI.setPlaceholders(placeholderPlayer, args.replace("&", "§"));
|
||||
}
|
||||
|
||||
public static List<String> asString(List<String> args, Player placeholderPlayer) {
|
||||
for (int i = 0 ; i < args.size() ; i ++) {
|
||||
args.set(i, asString(args.get(i), placeholderPlayer));
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
public static ItemFlag asItemFlag(String flag) {
|
||||
try {
|
||||
return ItemFlag.valueOf(flag);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Material asMaterial(String args) {
|
||||
try {
|
||||
Material material = Material.getMaterial(args);
|
||||
return material != null ? material : Material.getMaterial(Integer.valueOf(args));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return Material.STONE;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "deprecation" })
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
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 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) {
|
||||
if (name.toLowerCase().equals("damage")) {
|
||||
return "generic.attackDamage";
|
||||
}
|
||||
else if (name.toLowerCase().equals("attackspeed")) {
|
||||
return "generic.attackSpeed";
|
||||
}
|
||||
else if (name.toLowerCase().equals("health")) {
|
||||
return "generic.maxHealth";
|
||||
}
|
||||
else if (name.toLowerCase().equals("speed")) {
|
||||
return "generic.movementSpeed";
|
||||
}
|
||||
else if (name.toLowerCase().equals("knockback")) {
|
||||
return "generic.knockbackResistance";
|
||||
}
|
||||
else if (name.toLowerCase().equals("armor")) {
|
||||
return "generic.armor";
|
||||
}
|
||||
else if (name.toLowerCase().equals("luck")) {
|
||||
return "generic.luck";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 包含介绍
|
||||
*
|
||||
* @param i 物品
|
||||
* @param a 关键字
|
||||
*/
|
||||
public static boolean hasLore(ItemStack i, String a) {
|
||||
if (!isLored(i) || !i.getItemMeta().getLore().toString().contains(a)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果已描述
|
||||
*
|
||||
* @param i
|
||||
* @return
|
||||
*/
|
||||
public static boolean isLored(ItemStack i) {
|
||||
if (i == null || i.getItemMeta() == null || i.getItemMeta().getLore() == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果已命名
|
||||
*
|
||||
* @param i
|
||||
* @return
|
||||
*/
|
||||
public static boolean isNamed(ItemStack i) {
|
||||
if (i == null || i.getItemMeta() == null || i.getItemMeta().getDisplayName() == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加描述
|
||||
*
|
||||
* @param is 物品
|
||||
* @param lore 描述
|
||||
*/
|
||||
public static ItemStack addLore(ItemStack is, String lore) {
|
||||
ItemMeta meta = is.getItemMeta();
|
||||
|
||||
List<String> _lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
|
||||
_lore.add(lore.replaceAll("&", "§"));
|
||||
|
||||
is.setItemMeta(meta);
|
||||
return is;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除描述
|
||||
*
|
||||
* @param is 物品
|
||||
* @param line 行数
|
||||
*/
|
||||
public static ItemStack delLore(ItemStack is, int line) {
|
||||
ItemMeta meta = is.getItemMeta();
|
||||
if (meta.hasLore()) {
|
||||
List<String> l = meta.getLore();
|
||||
if (l.size() >= line) {
|
||||
l.remove(line);
|
||||
meta.setLore(l);
|
||||
is.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取介绍所在行数
|
||||
*
|
||||
* @param i 物品
|
||||
* @param a 关键字
|
||||
*/
|
||||
public static int getLore(ItemStack i, String a) {
|
||||
if (isLored(i)) {
|
||||
for (int j = 0; j < i.getItemMeta().getLore().size() ; j++) {
|
||||
if (i.getItemMeta().getLore().get(j).contains(a)) {
|
||||
return j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加耐久
|
||||
*
|
||||
* @param i 物品
|
||||
* @param d 耐久
|
||||
*/
|
||||
public static ItemStack addDurability(ItemStack i, int d) {
|
||||
i.setDurability((short) (i.getDurability() + d));
|
||||
int min = i.getDurability();
|
||||
int max = i.getType().getMaxDurability();
|
||||
if (min >= max) {
|
||||
i.setType(Material.AIR);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换描述
|
||||
*
|
||||
* @param i 物品
|
||||
* @param l1 关键字1
|
||||
* @param l2 关键字2
|
||||
*/
|
||||
public static ItemStack repalceLore(ItemStack i, String l1, String l2) {
|
||||
if (!isLored(i)) {
|
||||
return i;
|
||||
}
|
||||
else {
|
||||
ItemMeta meta = i.getItemMeta();
|
||||
List<String> lore = meta.getLore();
|
||||
for (int j = 0 ; j < lore.size() ; j++) {
|
||||
lore.set(j, lore.get(j).replace(l1, l2));
|
||||
}
|
||||
meta.setLore(lore);
|
||||
i.setItemMeta(meta);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
public static ItemStack loadItem(FileConfiguration f, String s) {
|
||||
return loadItem(f, s, null);
|
||||
}
|
||||
|
||||
public static ItemStack loadItem(FileConfiguration f, String s, Player papiPlayer) {
|
||||
return loadItem(f.getConfigurationSection(s), papiPlayer);
|
||||
}
|
||||
|
||||
public static ItemStack loadItem(ConfigurationSection section, Player papiPlayer) {
|
||||
if (section.get("bukkit") instanceof ItemStack) {
|
||||
return section.getItemStack("bukkit");
|
||||
}
|
||||
// 材质
|
||||
ItemStack item = new ItemStack(asMaterial(section.get("material").toString()));
|
||||
// 数量
|
||||
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(asString(section.getString("name"), papiPlayer));
|
||||
}
|
||||
// 描述
|
||||
if (section.contains("lore")) {
|
||||
meta.setLore(asString(section.getStringList("lore"), papiPlayer));
|
||||
}
|
||||
// 附魔
|
||||
if (section.contains("enchants")) {
|
||||
for (String preEnchant : section.getConfigurationSection("enchants").getKeys(false)) {
|
||||
Enchantment enchant = asEnchantment(preEnchant);
|
||||
if (enchant != null) {
|
||||
meta.addEnchant(enchant, section.getInt("enchants." + preEnchant), true);
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + preEnchant + " &c不是一个有效的附魔名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib enchants&c 查看所有附魔");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 标签
|
||||
if (section.contains("flags") && TabooLib.getVerint() > 10700) {
|
||||
for (String preFlag : section.getStringList("flags")) {
|
||||
ItemFlag flag = asItemFlag(preFlag);
|
||||
if (flag != null) {
|
||||
meta.addItemFlags(flag);
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + preFlag + " &c不是一个有效的标签名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib flags&c 查看所有标签");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 皮革
|
||||
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,
|
||||
NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[0]),
|
||||
NumberUtils.getInteger(section.getString("potions." + prePotionName).split("-")[1]) - 1), true);
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + potionEffectType + " &c不是一个有效的药水名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib potions&c 查看所有药水");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 元数据
|
||||
item.setItemMeta(meta);
|
||||
// 数据
|
||||
NBTItem nbt = new NBTItem(item);
|
||||
// 物品标签
|
||||
if (section.contains("nbt")) {
|
||||
for (String name : section.getConfigurationSection("nbt").getKeys(false)) {
|
||||
Object obj = section.get("nbt." + name);
|
||||
if (obj instanceof String) {
|
||||
nbt.setString(name, obj.toString());
|
||||
}
|
||||
else if (obj instanceof Double) {
|
||||
nbt.setDouble(name, Double.valueOf(obj.toString()));
|
||||
}
|
||||
else if (obj instanceof Integer) {
|
||||
nbt.setInteger(name, Integer.valueOf(obj.toString()));
|
||||
}
|
||||
else if (obj instanceof Long) {
|
||||
nbt.setLong(name, Long.valueOf(obj.toString()));
|
||||
}
|
||||
else {
|
||||
nbt.setObject(name, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 物品属性
|
||||
if (section.contains("attributes")) {
|
||||
NBTList attr = nbt.getList("AttributeModifiers", NBTType.NBTTagCompound);
|
||||
for (String hand : section.getConfigurationSection("attributes").getKeys(false)) {
|
||||
for (String name : section.getConfigurationSection("attributes." + hand).getKeys(false)) {
|
||||
if (asAttribute(name) != null) {
|
||||
try {
|
||||
NBTListCompound _attr = attr.addCompound();
|
||||
Object num = section.get("attributes." + hand + "." + name);
|
||||
if (num.toString().contains("%")) {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
|
||||
_attr.setInteger("Operation", 1);
|
||||
}
|
||||
else {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString()));
|
||||
_attr.setInteger("Operation", 0);
|
||||
}
|
||||
_attr.setString("AttributeName", asAttribute(name));
|
||||
_attr.setInteger("UUIDMost", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setInteger("UUIDLeast", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setString("Name", asAttribute(name));
|
||||
if (!hand.equals("all")) {
|
||||
_attr.setString("Slot", hand);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
MsgUtils.warn("&8" + name + " &c属性载入失败: &8" + e.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + name + " &c不是一个有效的属性名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib attributes&c 查看所有属性");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nbt.getItem();
|
||||
}
|
||||
|
||||
public static NBTItem setAttribute(NBTItem nbt, String name, Object num, String hand) {
|
||||
NBTList attr = nbt.getList("AttributeModifiers", NBTType.NBTTagCompound);
|
||||
if (asAttribute(name) != null) {
|
||||
try {
|
||||
NBTListCompound _attr = attr.addCompound();
|
||||
for (int i = 0 ; i < attr.size() ; i++) {
|
||||
NBTListCompound nlc = attr.getCompound(i);
|
||||
if (nlc.getString("AttributeName").equals("name")) {
|
||||
_attr = nlc;
|
||||
}
|
||||
}
|
||||
if (num.toString().contains("%")) {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString().replace("%", "")) / 100D);
|
||||
_attr.setInteger("Operation", 1);
|
||||
}
|
||||
else {
|
||||
_attr.setDouble("Amount", Double.valueOf(num.toString()));
|
||||
_attr.setInteger("Operation", 0);
|
||||
}
|
||||
_attr.setString("AttributeName", asAttribute(name));
|
||||
_attr.setInteger("UUIDMost", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setInteger("UUIDLeast", NumberUtils.getRand().nextInt(Integer.MAX_VALUE));
|
||||
_attr.setString("Name", asAttribute(name));
|
||||
if (!hand.equals("all")) {
|
||||
_attr.setString("Slot", hand);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
MsgUtils.warn("&8" + name + " &c属性载入失败: &8" + e.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
MsgUtils.warn("&8" + name + " &c不是一个有效的属性名称");
|
||||
MsgUtils.warn("&c输入 &4/taboolib attributes&c 查看所有属性");
|
||||
}
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void putO(ItemStack item, Inventory inv, int i) {
|
||||
inv.setItem(i, item);
|
||||
inv.setItem(i+1, item);
|
||||
inv.setItem(i+2, item);
|
||||
inv.setItem(i+9, item);
|
||||
inv.setItem(i+10, null);
|
||||
inv.setItem(i+11, item);
|
||||
inv.setItem(i+18, item);
|
||||
inv.setItem(i+19, item);
|
||||
inv.setItem(i+20, item);
|
||||
}
|
||||
}
|
192
src/main/src/me/skymc/taboolib/itemnbtapi/NBTCompound.java
Normal file
192
src/main/src/me/skymc/taboolib/itemnbtapi/NBTCompound.java
Normal file
@ -0,0 +1,192 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
|
||||
public class NBTCompound {
|
||||
|
||||
private String compundName;
|
||||
private NBTCompound parent;
|
||||
|
||||
protected NBTCompound(NBTCompound owner, String name) {
|
||||
this.compundName = name;
|
||||
this.parent = owner;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return compundName;
|
||||
}
|
||||
|
||||
protected Object getCompound() {
|
||||
return parent.getCompound();
|
||||
}
|
||||
|
||||
protected void setCompound(Object compound) {
|
||||
parent.setCompound(compound);
|
||||
}
|
||||
|
||||
public NBTCompound getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void mergeCompound(NBTCompound comp){
|
||||
NBTReflectionUtil.addOtherNBTCompound(this, comp);
|
||||
}
|
||||
|
||||
public void setString(String key, String value) {
|
||||
NBTReflectionUtil.setString(this, key, value);
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
return NBTReflectionUtil.getString(this, key);
|
||||
}
|
||||
|
||||
protected String getContent(String key) {
|
||||
return NBTReflectionUtil.getContent(this, key);
|
||||
}
|
||||
|
||||
public void setInteger(String key, Integer value) {
|
||||
NBTReflectionUtil.setInt(this, key, value);
|
||||
}
|
||||
|
||||
public Integer getInteger(String key) {
|
||||
return NBTReflectionUtil.getInt(this, key);
|
||||
}
|
||||
|
||||
public void setDouble(String key, Double value) {
|
||||
NBTReflectionUtil.setDouble(this, key, value);
|
||||
}
|
||||
|
||||
public Double getDouble(String key) {
|
||||
return NBTReflectionUtil.getDouble(this, key);
|
||||
}
|
||||
|
||||
public void setByte(String key, Byte value) {
|
||||
NBTReflectionUtil.setByte(this, key, value);
|
||||
}
|
||||
|
||||
public Byte getByte(String key) {
|
||||
return NBTReflectionUtil.getByte(this, key);
|
||||
}
|
||||
|
||||
public void setShort(String key, Short value) {
|
||||
NBTReflectionUtil.setShort(this, key, value);
|
||||
}
|
||||
|
||||
public Short getShort(String key) {
|
||||
return NBTReflectionUtil.getShort(this, key);
|
||||
}
|
||||
|
||||
public void setLong(String key, Long value) {
|
||||
NBTReflectionUtil.setLong(this, key, value);
|
||||
}
|
||||
|
||||
public Long getLong(String key) {
|
||||
return NBTReflectionUtil.getLong(this, key);
|
||||
}
|
||||
|
||||
public void setFloat(String key, Float value) {
|
||||
NBTReflectionUtil.setFloat(this, key, value);
|
||||
}
|
||||
|
||||
public Float getFloat(String key) {
|
||||
return NBTReflectionUtil.getFloat(this, key);
|
||||
}
|
||||
|
||||
public void setByteArray(String key, byte[] value) {
|
||||
NBTReflectionUtil.setByteArray(this, key, value);
|
||||
}
|
||||
|
||||
public byte[] getByteArray(String key) {
|
||||
return NBTReflectionUtil.getByteArray(this, key);
|
||||
}
|
||||
|
||||
public void setIntArray(String key, int[] value) {
|
||||
NBTReflectionUtil.setIntArray(this, key, value);
|
||||
}
|
||||
|
||||
public int[] getIntArray(String key) {
|
||||
return NBTReflectionUtil.getIntArray(this, key);
|
||||
}
|
||||
|
||||
public void setBoolean(String key, Boolean value) {
|
||||
NBTReflectionUtil.setBoolean(this, key, value);
|
||||
}
|
||||
|
||||
protected void set(String key, Object val) {
|
||||
NBTReflectionUtil.set(this, key, val);
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key) {
|
||||
return NBTReflectionUtil.getBoolean(this, key);
|
||||
}
|
||||
|
||||
public void setObject(String key, Object value) {
|
||||
NBTReflectionUtil.setObject(this, key, value);
|
||||
}
|
||||
|
||||
public <T> T getObject(String key, Class<T> type) {
|
||||
return NBTReflectionUtil.getObject(this, key, type);
|
||||
}
|
||||
|
||||
public Boolean hasKey(String key) {
|
||||
return NBTReflectionUtil.hasKey(this, key);
|
||||
}
|
||||
|
||||
public void removeKey(String key) {
|
||||
NBTReflectionUtil.remove(this, key);
|
||||
}
|
||||
|
||||
public Set<String> getKeys() {
|
||||
return NBTReflectionUtil.getKeys(this);
|
||||
}
|
||||
|
||||
public NBTCompound addCompound(String name) {
|
||||
NBTReflectionUtil.addNBTTagCompound(this, name);
|
||||
return getCompound(name);
|
||||
}
|
||||
|
||||
public NBTCompound getCompound(String name) {
|
||||
NBTCompound next = new NBTCompound(this, name);
|
||||
if (NBTReflectionUtil.valideCompound(next)) return next;
|
||||
return null;
|
||||
}
|
||||
|
||||
public NBTList getList(String name, NBTType type) {
|
||||
return NBTReflectionUtil.getList(this, name, type);
|
||||
}
|
||||
|
||||
public NBTType getType(String name) {
|
||||
if (TabooLib.getVerint() == 10700) return NBTType.NBTTagEnd;
|
||||
return NBTType.valueOf(NBTReflectionUtil.getType(this, name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (String key : getKeys()) {
|
||||
result.append(toString(key));
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public String toString(String key) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
NBTCompound compound = this;
|
||||
while (compound.getParent() != null) {
|
||||
result.append(" ");
|
||||
compound = compound.getParent();
|
||||
}
|
||||
if (this.getType(key) == NBTType.NBTTagCompound) {
|
||||
return this.getCompound(key).toString();
|
||||
} else {
|
||||
return result + "-" + key + ": " + getContent(key) + System.lineSeparator();
|
||||
}
|
||||
}
|
||||
|
||||
public String asNBTString(){
|
||||
return getCompound().toString();
|
||||
}
|
||||
|
||||
}
|
35
src/main/src/me/skymc/taboolib/itemnbtapi/NBTContainer.java
Normal file
35
src/main/src/me/skymc/taboolib/itemnbtapi/NBTContainer.java
Normal file
@ -0,0 +1,35 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
public class NBTContainer extends NBTCompound{
|
||||
|
||||
private Object nbt;
|
||||
|
||||
public NBTContainer() {
|
||||
super(null, null);
|
||||
nbt = NBTReflectionUtil.getNewNBTTag();
|
||||
}
|
||||
|
||||
protected NBTContainer(Object nbt){
|
||||
super(null, null);
|
||||
this.nbt = nbt;
|
||||
}
|
||||
|
||||
public NBTContainer(String nbtString) throws IllegalArgumentException {
|
||||
super(null, null);
|
||||
try{
|
||||
nbt = NBTReflectionUtil.parseNBT(nbtString);
|
||||
}catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
throw new IllegalArgumentException("Malformed Json: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected Object getCompound() {
|
||||
return nbt;
|
||||
}
|
||||
|
||||
protected void setCompound(Object tag) {
|
||||
nbt = tag;
|
||||
}
|
||||
|
||||
}
|
22
src/main/src/me/skymc/taboolib/itemnbtapi/NBTEntity.java
Normal file
22
src/main/src/me/skymc/taboolib/itemnbtapi/NBTEntity.java
Normal file
@ -0,0 +1,22 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import org.bukkit.entity.Entity;
|
||||
|
||||
public class NBTEntity extends NBTCompound {
|
||||
|
||||
private final Entity ent;
|
||||
|
||||
public NBTEntity(Entity entity) {
|
||||
super(null, null);
|
||||
ent = entity;
|
||||
}
|
||||
|
||||
protected Object getCompound() {
|
||||
return NBTReflectionUtil.getEntityNBTTagCompound(NBTReflectionUtil.getNMSEntity(ent));
|
||||
}
|
||||
|
||||
protected void setCompound(Object compound) {
|
||||
NBTReflectionUtil.setEntityNBTTag(compound, NBTReflectionUtil.getNMSEntity(ent));
|
||||
}
|
||||
|
||||
}
|
46
src/main/src/me/skymc/taboolib/itemnbtapi/NBTFile.java
Normal file
46
src/main/src/me/skymc/taboolib/itemnbtapi/NBTFile.java
Normal file
@ -0,0 +1,46 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class NBTFile extends NBTCompound {
|
||||
|
||||
private final File file;
|
||||
private Object nbt;
|
||||
|
||||
public NBTFile(File file) throws IOException {
|
||||
super(null, null);
|
||||
this.file = file;
|
||||
if (file.exists()) {
|
||||
FileInputStream inputsteam = new FileInputStream(file);
|
||||
nbt = NBTReflectionUtil.readNBTFile(inputsteam);
|
||||
} else {
|
||||
nbt = NBTReflectionUtil.getNewNBTTag();
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
public void save() throws IOException {
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
}
|
||||
FileOutputStream outStream = new FileOutputStream(file);
|
||||
NBTReflectionUtil.saveNBTFile(nbt, outStream);
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
protected Object getCompound() {
|
||||
return nbt;
|
||||
}
|
||||
|
||||
protected void setCompound(Object compound) {
|
||||
nbt = compound;
|
||||
}
|
||||
|
||||
}
|
38
src/main/src/me/skymc/taboolib/itemnbtapi/NBTItem.java
Normal file
38
src/main/src/me/skymc/taboolib/itemnbtapi/NBTItem.java
Normal file
@ -0,0 +1,38 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class NBTItem extends NBTCompound {
|
||||
|
||||
private ItemStack bukkitItem;
|
||||
|
||||
public NBTItem(ItemStack item) {
|
||||
super(null, null);
|
||||
bukkitItem = item.clone();
|
||||
}
|
||||
|
||||
protected Object getCompound() {
|
||||
return NBTReflectionUtil.getItemRootNBTTagCompound(NBTReflectionUtil.getNMSItemStack(bukkitItem));
|
||||
}
|
||||
|
||||
protected void setCompound(Object compound) {
|
||||
bukkitItem = NBTReflectionUtil.getBukkitItemStack(NBTReflectionUtil.setNBTTag(compound, NBTReflectionUtil.getNMSItemStack(bukkitItem)));
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return bukkitItem;
|
||||
}
|
||||
|
||||
protected void setItem(ItemStack item) {
|
||||
bukkitItem = item;
|
||||
}
|
||||
|
||||
public static NBTContainer convertItemtoNBT(ItemStack item){
|
||||
return NBTReflectionUtil.convertNMSItemtoNBTCompound(NBTReflectionUtil.getNMSItemStack(item));
|
||||
}
|
||||
|
||||
public static ItemStack convertNBTtoItem(NBTCompound comp){
|
||||
return NBTReflectionUtil.getBukkitItemStack(NBTReflectionUtil.convertNBTCompoundtoNMSItem(comp));
|
||||
}
|
||||
|
||||
}
|
128
src/main/src/me/skymc/taboolib/itemnbtapi/NBTList.java
Normal file
128
src/main/src/me/skymc/taboolib/itemnbtapi/NBTList.java
Normal file
@ -0,0 +1,128 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import me.skymc.taboolib.itemnbtapi.utils.MethodNames;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class NBTList {
|
||||
|
||||
private String listName;
|
||||
private NBTCompound parent;
|
||||
private NBTType type;
|
||||
private Object listObject;
|
||||
|
||||
protected NBTList(NBTCompound owner, String name, NBTType type, Object list) {
|
||||
parent = owner;
|
||||
listName = name;
|
||||
this.type = type;
|
||||
this.listObject = list;
|
||||
if (!(type == NBTType.NBTTagString || type == NBTType.NBTTagCompound)) {
|
||||
System.err.println("List types != String/Compound are currently not implemented!");
|
||||
}
|
||||
}
|
||||
|
||||
protected void save() {
|
||||
parent.set(listName, listObject);
|
||||
}
|
||||
|
||||
public NBTListCompound addCompound() {
|
||||
if (type != NBTType.NBTTagCompound) {
|
||||
new Throwable("Using Compound method on a non Compound list!").printStackTrace();
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Method method = listObject.getClass().getMethod("add", NBTReflectionUtil.getNBTBase());
|
||||
Object compound = NBTReflectionUtil.getNBTTagCompound().newInstance();
|
||||
method.invoke(listObject, compound);
|
||||
return new NBTListCompound(this, compound);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public NBTListCompound getCompound(int id) {
|
||||
if (type != NBTType.NBTTagCompound) {
|
||||
new Throwable("Using Compound method on a non Compound list!").printStackTrace();
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Method method = listObject.getClass().getMethod("get", int.class);
|
||||
Object compound = method.invoke(listObject, id);
|
||||
return new NBTListCompound(this, compound);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getString(int i) {
|
||||
if (type != NBTType.NBTTagString) {
|
||||
new Throwable("Using String method on a non String list!").printStackTrace();
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Method method = listObject.getClass().getMethod("getString", int.class);
|
||||
return (String) method.invoke(listObject, i);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addString(String s) {
|
||||
if (type != NBTType.NBTTagString) {
|
||||
new Throwable("Using String method on a non String list!").printStackTrace();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Method method = listObject.getClass().getMethod("add", NBTReflectionUtil.getNBTBase());
|
||||
method.invoke(listObject, NBTReflectionUtil.getNBTTagString().getConstructor(String.class).newInstance(s));
|
||||
save();
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setString(int i, String s) {
|
||||
if (type != NBTType.NBTTagString) {
|
||||
new Throwable("Using String method on a non String list!").printStackTrace();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Method method = listObject.getClass().getMethod("a", int.class, NBTReflectionUtil.getNBTBase());
|
||||
method.invoke(listObject, i, NBTReflectionUtil.getNBTTagString().getConstructor(String.class).newInstance(s));
|
||||
save();
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(int i) {
|
||||
try {
|
||||
Method method = listObject.getClass().getMethod(MethodNames.getRemoveMethodName(), int.class);
|
||||
method.invoke(listObject, i);
|
||||
save();
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
try {
|
||||
Method method = listObject.getClass().getMethod("size");
|
||||
return (int) method.invoke(listObject);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public NBTType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
104
src/main/src/me/skymc/taboolib/itemnbtapi/NBTListCompound.java
Normal file
104
src/main/src/me/skymc/taboolib/itemnbtapi/NBTListCompound.java
Normal file
@ -0,0 +1,104 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class NBTListCompound {
|
||||
|
||||
private NBTList owner;
|
||||
private Object compound;
|
||||
|
||||
protected NBTListCompound(NBTList parent, Object obj) {
|
||||
owner = parent;
|
||||
compound = obj;
|
||||
}
|
||||
|
||||
public void setString(String key, String value) {
|
||||
if (value == null) {
|
||||
remove(key);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
compound.getClass().getMethod("setString", String.class, String.class).invoke(compound, key, value);
|
||||
owner.save();
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void setInteger(String key, int value) {
|
||||
try {
|
||||
compound.getClass().getMethod("setInt", String.class, int.class).invoke(compound, key, value);
|
||||
owner.save();
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public int getInteger(String value) {
|
||||
try {
|
||||
return (int) compound.getClass().getMethod("getInt", String.class).invoke(compound, value);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setDouble(String key, double value) {
|
||||
try {
|
||||
compound.getClass().getMethod("setDouble", String.class, double.class).invoke(compound, key, value);
|
||||
owner.save();
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public double getDouble(String key) {
|
||||
try {
|
||||
return (double) compound.getClass().getMethod("getDouble", String.class).invoke(compound, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public String getString(String key) {
|
||||
try {
|
||||
return (String) compound.getClass().getMethod("getString", String.class).invoke(compound, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public boolean hasKey(String key) {
|
||||
try {
|
||||
return (boolean) compound.getClass().getMethod("hasKey", String.class).invoke(compound, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<String> getKeys() {
|
||||
try {
|
||||
return (Set<String>) compound.getClass().getMethod("c").invoke(compound);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return new HashSet<>();
|
||||
}
|
||||
|
||||
public void remove(String key) {
|
||||
try {
|
||||
compound.getClass().getMethod("remove", String.class).invoke(compound, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
978
src/main/src/me/skymc/taboolib/itemnbtapi/NBTReflectionUtil.java
Normal file
978
src/main/src/me/skymc/taboolib/itemnbtapi/NBTReflectionUtil.java
Normal file
@ -0,0 +1,978 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.itemnbtapi.utils.GsonWrapper;
|
||||
import me.skymc.taboolib.itemnbtapi.utils.MethodNames;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
// TODO: finish codestyle cleanup -sgdc3
|
||||
public class NBTReflectionUtil {
|
||||
|
||||
private static final String version = TabooLib.getVersion();
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Class getCraftItemStack() {
|
||||
|
||||
try {
|
||||
Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Class getCraftEntity() {
|
||||
try {
|
||||
Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".entity.CraftEntity");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getNBTBase() {
|
||||
try {
|
||||
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTBase");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getNBTTagString() {
|
||||
try {
|
||||
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTTagString");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getNMSItemStack() {
|
||||
try {
|
||||
Class clazz = Class.forName("net.minecraft.server." + version + ".ItemStack");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getNBTTagCompound() {
|
||||
try {
|
||||
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTTagCompound");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getNBTCompressedStreamTools() {
|
||||
try {
|
||||
Class clazz = Class.forName("net.minecraft.server." + version + ".NBTCompressedStreamTools");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getMojangsonParser() {
|
||||
try {
|
||||
Class c = Class.forName("net.minecraft.server." + version + ".MojangsonParser");
|
||||
return c;
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getTileEntity() {
|
||||
try {
|
||||
Class clazz = Class.forName("net.minecraft.server." + version + ".TileEntity");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected static Class getCraftWorld() {
|
||||
try {
|
||||
Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".CraftWorld");
|
||||
return clazz;
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getNewNBTTag() {
|
||||
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
|
||||
try {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class c = Class.forName("net.minecraft.server." + version + ".NBTTagCompound");
|
||||
return c.newInstance();
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object getNewBlockPosition(int x, int y, int z) {
|
||||
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
|
||||
try {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = Class.forName("net.minecraft.server." + version + ".BlockPosition");
|
||||
return clazz.getConstructor(int.class, int.class, int.class).newInstance(x, y, z);
|
||||
} catch (Exception ex) {
|
||||
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object setNBTTag(Object NBTTag, Object NMSItem) {
|
||||
try {
|
||||
Method method;
|
||||
method = NMSItem.getClass().getMethod("setTag", NBTTag.getClass());
|
||||
method.invoke(NMSItem, NBTTag);
|
||||
return NMSItem;
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Object getNMSItemStack(ItemStack item) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = getCraftItemStack();
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod("asNMSCopy", ItemStack.class);
|
||||
Object answer = method.invoke(clazz, item);
|
||||
return answer;
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Object getNMSEntity(Entity entity) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = getCraftEntity();
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod("getHandle");
|
||||
return method.invoke(getCraftEntity().cast(entity));
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static Object parseNBT(String json) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class cis = getMojangsonParser();
|
||||
java.lang.reflect.Method method;
|
||||
try {
|
||||
method = cis.getMethod("parse", String.class);
|
||||
return method.invoke(null, json);
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static Object readNBTFile(FileInputStream stream) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = getNBTCompressedStreamTools();
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod("a", InputStream.class);
|
||||
return method.invoke(clazz, stream);
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static Object saveNBTFile(Object nbt, FileOutputStream stream) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = getNBTCompressedStreamTools();
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod("a", getNBTTagCompound(), OutputStream.class);
|
||||
return method.invoke(clazz, nbt, stream);
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static ItemStack getBukkitItemStack(Object item) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = getCraftItemStack();
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod("asCraftMirror", item.getClass());
|
||||
Object answer = method.invoke(clazz, item);
|
||||
return (ItemStack) answer;
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static Object getItemRootNBTTagCompound(Object nmsitem) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = nmsitem.getClass();
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod("getTag");
|
||||
Object answer = method.invoke(nmsitem);
|
||||
return answer;
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = getNMSItemStack();
|
||||
try {
|
||||
Object nmsstack = clazz.getConstructor(getNBTTagCompound()).newInstance(nbtcompound.getCompound());
|
||||
return nmsstack;
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static NBTContainer convertNMSItemtoNBTCompound(Object nmsitem) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class clazz = nmsitem.getClass();
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod("save", getNBTTagCompound());
|
||||
Object answer = method.invoke(nmsitem, getNewNBTTag());
|
||||
return new NBTContainer(answer);
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static Object getEntityNBTTagCompound(Object nmsitem) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class c = nmsitem.getClass();
|
||||
Method method;
|
||||
try {
|
||||
method = c.getMethod(MethodNames.getEntityNbtGetterMethodName(), getNBTTagCompound());
|
||||
Object nbt = getNBTTagCompound().newInstance();
|
||||
Object answer = method.invoke(nmsitem, nbt);
|
||||
if (answer == null)
|
||||
answer = nbt;
|
||||
return answer;
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object setEntityNBTTag(Object NBTTag, Object NMSItem) {
|
||||
try {
|
||||
Method method;
|
||||
method = NMSItem.getClass().getMethod(MethodNames.getEntityNbtSetterMethodName(), getNBTTagCompound());
|
||||
method.invoke(NMSItem, NBTTag);
|
||||
return NMSItem;
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object getTileEntityNBTTagCompound(BlockState tile) {
|
||||
Method method;
|
||||
try {
|
||||
Object pos = getNewBlockPosition(tile.getX(), tile.getY(), tile.getZ());
|
||||
Object cworld = getCraftWorld().cast(tile.getWorld());
|
||||
Object nmsworld = cworld.getClass().getMethod("getHandle").invoke(cworld);
|
||||
Object o = nmsworld.getClass().getMethod("getTileEntity", pos.getClass()).invoke(nmsworld, pos);
|
||||
method = getTileEntity().getMethod(MethodNames.getTileDataMethodName(), getNBTTagCompound());
|
||||
Object tag = getNBTTagCompound().newInstance();
|
||||
Object answer = method.invoke(o, tag);
|
||||
if (answer == null)
|
||||
answer = tag;
|
||||
return answer;
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setTileEntityNBTTagCompound(BlockState tile, Object comp) {
|
||||
Method method;
|
||||
try {
|
||||
Object pos = getNewBlockPosition(tile.getX(), tile.getY(), tile.getZ());
|
||||
Object cworld = getCraftWorld().cast(tile.getWorld());
|
||||
Object nmsworld = cworld.getClass().getMethod("getHandle").invoke(cworld);
|
||||
Object o = nmsworld.getClass().getMethod("getTileEntity", pos.getClass()).invoke(nmsworld, pos);
|
||||
method = getTileEntity().getMethod("a", getNBTTagCompound());
|
||||
method.invoke(o, comp);
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Object getSubNBTTagCompound(Object compound, String name) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class c = compound.getClass();
|
||||
Method method;
|
||||
try {
|
||||
method = c.getMethod("getCompound", String.class);
|
||||
Object answer = method.invoke(compound, name);
|
||||
return answer;
|
||||
} catch (Exception e) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void addNBTTagCompound(NBTCompound comp, String name) {
|
||||
if (name == null) {
|
||||
remove(comp, name);
|
||||
return;
|
||||
}
|
||||
Object nbttag = comp.getCompound();
|
||||
if (nbttag == null) {
|
||||
nbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(nbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("set", String.class, getNBTBase());
|
||||
method.invoke(workingtag, name, getNBTTagCompound().newInstance());
|
||||
comp.setCompound(nbttag);
|
||||
return;
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public static Boolean valideCompound(NBTCompound comp) {
|
||||
Object root = comp.getCompound();
|
||||
if (root == null) {
|
||||
root = getNewNBTTag();
|
||||
}
|
||||
return (gettoCompount(root, comp)) != null;
|
||||
}
|
||||
|
||||
private static Object gettoCompount(Object nbttag, NBTCompound comp) {
|
||||
Stack<String> structure = new Stack<>();
|
||||
while (comp.getParent() != null) {
|
||||
structure.add(comp.getName());
|
||||
comp = comp.getParent();
|
||||
}
|
||||
while (!structure.isEmpty()) {
|
||||
nbttag = getSubNBTTagCompound(nbttag, structure.pop());
|
||||
if (nbttag == null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return nbttag;
|
||||
}
|
||||
|
||||
public static void addOtherNBTCompound(NBTCompound comp, NBTCompound nbtcompound) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("a", getNBTTagCompound());
|
||||
method.invoke(workingtag, nbtcompound.getCompound());
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void setString(NBTCompound comp, String key, String text) {
|
||||
if (text == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setString", String.class, String.class);
|
||||
method.invoke(workingtag, key, text);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String getString(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getString", String.class);
|
||||
return (String) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getContent(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("get", String.class);
|
||||
return method.invoke(workingtag, key).toString();
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setInt(NBTCompound comp, String key, Integer i) {
|
||||
if (i == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setInt", String.class, int.class);
|
||||
method.invoke(workingtag, key, i);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer getInt(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getInt", String.class);
|
||||
return (Integer) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setByteArray(NBTCompound comp, String key, byte[] b) {
|
||||
if (b == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setByteArray", String.class, byte[].class);
|
||||
method.invoke(workingtag, key, b);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public static byte[] getByteArray(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getByteArray", String.class);
|
||||
return (byte[]) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setIntArray(NBTCompound comp, String key, int[] i) {
|
||||
if (i == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setIntArray", String.class, int[].class);
|
||||
method.invoke(workingtag, key, i);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] getIntArray(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getIntArray", String.class);
|
||||
return (int[]) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setFloat(NBTCompound comp, String key, Float f) {
|
||||
if (f == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setFloat", String.class, float.class);
|
||||
method.invoke(workingtag, key, (float) f);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Float getFloat(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getFloat", String.class);
|
||||
return (Float) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setLong(NBTCompound comp, String key, Long f) {
|
||||
if (f == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setLong", String.class, long.class);
|
||||
method.invoke(workingtag, key, (long) f);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Long getLong(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getLong", String.class);
|
||||
return (Long) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setShort(NBTCompound comp, String key, Short f) {
|
||||
if (f == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setShort", String.class, short.class);
|
||||
method.invoke(workingtag, key, (short) f);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Short getShort(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getShort", String.class);
|
||||
return (Short) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setByte(NBTCompound comp, String key, Byte f) {
|
||||
if (f == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setByte", String.class, byte.class);
|
||||
method.invoke(workingtag, key, (byte) f);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Byte getByte(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getByte", String.class);
|
||||
return (Byte) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setDouble(NBTCompound comp, String key, Double d) {
|
||||
if (d == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setDouble", String.class, double.class);
|
||||
method.invoke(workingtag, key, d);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Double getDouble(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getDouble", String.class);
|
||||
return (Double) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte getType(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return 0;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod(MethodNames.getTypeMethodName(), String.class);
|
||||
return (byte) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void setBoolean(NBTCompound comp, String key, Boolean d) {
|
||||
if (d == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("setBoolean", String.class, boolean.class);
|
||||
method.invoke(workingtag, key, d);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean getBoolean(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getBoolean", String.class);
|
||||
return (Boolean) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void set(NBTCompound comp, String key, Object val) {
|
||||
if (val == null) {
|
||||
remove(comp, key);
|
||||
return;
|
||||
}
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) {
|
||||
new Throwable("InvalideCompound").printStackTrace();
|
||||
return;
|
||||
}
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("set", String.class, getNBTBase());
|
||||
method.invoke(workingtag, key, val);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static NBTList getList(NBTCompound comp, String key, NBTType type) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("getList", String.class, int.class);
|
||||
return new NBTList(comp, key, type, method.invoke(workingtag, key, type.getId()));
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setObject(NBTCompound comp, String key, Object value) {
|
||||
try {
|
||||
String json = GsonWrapper.getString(value);
|
||||
setString(comp, key, json);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T getObject(NBTCompound comp, String key, Class<T> type) {
|
||||
String json = getString(comp, key);
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
return GsonWrapper.deserializeJson(json, type);
|
||||
}
|
||||
|
||||
public static void remove(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("remove", String.class);
|
||||
method.invoke(workingtag, key);
|
||||
comp.setCompound(rootnbttag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean hasKey(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("hasKey", String.class);
|
||||
return (Boolean) method.invoke(workingtag, key);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Set<String> getKeys(NBTCompound comp) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = getNewNBTTag();
|
||||
}
|
||||
if (!valideCompound(comp)) return null;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Method method;
|
||||
try {
|
||||
method = workingtag.getClass().getMethod("c");
|
||||
return (Set<String>) method.invoke(workingtag);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT 操作出现异常: §7" + ex.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
22
src/main/src/me/skymc/taboolib/itemnbtapi/NBTTileEntity.java
Normal file
22
src/main/src/me/skymc/taboolib/itemnbtapi/NBTTileEntity.java
Normal file
@ -0,0 +1,22 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import org.bukkit.block.BlockState;
|
||||
|
||||
public class NBTTileEntity extends NBTCompound {
|
||||
|
||||
private final BlockState tile;
|
||||
|
||||
public NBTTileEntity(BlockState tile) {
|
||||
super(null, null);
|
||||
this.tile = tile;
|
||||
}
|
||||
|
||||
protected Object getCompound() {
|
||||
return NBTReflectionUtil.getTileEntityNBTTagCompound(tile);
|
||||
}
|
||||
|
||||
protected void setCompound(Object compound) {
|
||||
NBTReflectionUtil.setTileEntityNBTTagCompound(tile, compound);
|
||||
}
|
||||
|
||||
}
|
34
src/main/src/me/skymc/taboolib/itemnbtapi/NBTType.java
Normal file
34
src/main/src/me/skymc/taboolib/itemnbtapi/NBTType.java
Normal file
@ -0,0 +1,34 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
public enum NBTType {
|
||||
NBTTagEnd(0),
|
||||
NBTTagByte(1),
|
||||
NBTTagShort(2),
|
||||
NBTTagInt(3),
|
||||
NBTTagLong(4),
|
||||
NBTTagFloat(5),
|
||||
NBTTagDouble(6),
|
||||
NBTTagByteArray(7),
|
||||
NBTTagIntArray(11),
|
||||
NBTTagString(8),
|
||||
NBTTagList(9),
|
||||
NBTTagCompound(10);
|
||||
|
||||
NBTType(int i) {
|
||||
id = i;
|
||||
}
|
||||
|
||||
private final int id;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public static NBTType valueOf(int id) {
|
||||
for (NBTType t : values())
|
||||
if (t.getId() == id)
|
||||
return t;
|
||||
return NBTType.NBTTagEnd;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package me.skymc.taboolib.itemnbtapi.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class GsonWrapper {
|
||||
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
public static String getString(Object obj) {
|
||||
return gson.toJson(obj);
|
||||
}
|
||||
|
||||
public static <T> T deserializeJson(String json, Class<T> type) {
|
||||
try {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
T obj = gson.fromJson(json, type);
|
||||
return type.cast(obj);
|
||||
} catch (Exception ex) {
|
||||
MsgUtils.warn("NBT ²Ù×÷³öÏÖÒì³£: ¡ì7" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package me.skymc.taboolib.itemnbtapi.utils;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
|
||||
public class MethodNames {
|
||||
|
||||
public static String getEntityNbtGetterMethodName() {
|
||||
return "b";
|
||||
}
|
||||
|
||||
public static String getEntityNbtSetterMethodName() {
|
||||
return "a";
|
||||
}
|
||||
|
||||
public static String getTileDataMethodName() {
|
||||
if (TabooLib.getVerint() <= 10800) {
|
||||
return "b";
|
||||
}
|
||||
return "save";
|
||||
}
|
||||
|
||||
public static String getTypeMethodName() {
|
||||
if (TabooLib.getVerint() <= 10800) {
|
||||
return "b";
|
||||
}
|
||||
return "d";
|
||||
}
|
||||
|
||||
public static String getRemoveMethodName() {
|
||||
if (TabooLib.getVerint() <= 10800) {
|
||||
return "a";
|
||||
}
|
||||
return "remove";
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package me.skymc.taboolib.javascript;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
|
||||
public class JavaScriptUtils {
|
||||
|
||||
private static ScriptEngineManager manager = new ScriptEngineManager();
|
||||
|
||||
public static ScriptEngineManager getScriptManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public static void invokeJavaScript(File jsFile, String method, Object... o) {
|
||||
ScriptEngine engine = manager.getEngineByName("javascript");
|
||||
try {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Thread.currentThread().setContextClassLoader(Main.class.getClassLoader());
|
||||
|
||||
FileReader reader = new FileReader(jsFile);
|
||||
engine.eval(reader);
|
||||
|
||||
// TODO run
|
||||
|
||||
Thread.currentThread().setContextClassLoader(classLoader);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object JavaScriptInterface(String jsFile, Object... o) {
|
||||
|
||||
ScriptEngine engine = manager.getEngineByName("javascript");
|
||||
try {
|
||||
FileReader reader = new FileReader(jsFile);
|
||||
engine.eval(reader);
|
||||
|
||||
if (engine instanceof Invocable) {
|
||||
return ((Invocable) engine).invokeFunction("main", o);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ScriptException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void JavaScriptExecute(String jsFile, Object... o) {
|
||||
|
||||
ScriptEngine engine = manager.getEngineByName("javascript");
|
||||
try {
|
||||
FileReader reader = new FileReader(jsFile);
|
||||
engine.eval(reader);
|
||||
|
||||
if (engine instanceof Invocable) {
|
||||
((Invocable) engine).invokeFunction("main", o);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ScriptException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
211
src/main/src/me/skymc/taboolib/javashell/JavaShell.java
Normal file
211
src/main/src/me/skymc/taboolib/javashell/JavaShell.java
Normal file
@ -0,0 +1,211 @@
|
||||
package me.skymc.taboolib.javashell;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.RegisteredListener;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.javashell.utils.JarUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class JavaShell {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static String paths = "";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File javaShellFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File scriptFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File cacheFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static File libFolder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static HashMap<String, Class<?>> shells = new HashMap<>();
|
||||
|
||||
public static void javaShellSetup() {
|
||||
File dataFolder = Main.getInst().getDataFolder();
|
||||
File pluginsFolder = dataFolder.getParentFile();
|
||||
File serverRoot = Bukkit.getWorldContainer();
|
||||
|
||||
File[] rootJars = serverRoot.listFiles(new FilenameFilter() {
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.toLowerCase().endsWith("jar");
|
||||
}
|
||||
});
|
||||
|
||||
File[] pluginJars = pluginsFolder.listFiles(new FilenameFilter() {
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.toLowerCase().endsWith("jar");
|
||||
}
|
||||
});
|
||||
|
||||
for (File file : (File[]) ArrayUtils.addAll(rootJars, pluginJars)) {
|
||||
String path = file.getAbsolutePath();
|
||||
paths += File.pathSeparator + path;
|
||||
}
|
||||
|
||||
javaShellFolder = new File(Main.getInst().getDataFolder(), "JavaShells");
|
||||
if (!javaShellFolder.exists()) {
|
||||
Main.getInst().saveResource("JavaShells/lib/com.sun.tools.jar", true);
|
||||
Main.getInst().saveResource("JavaShells/scripts/-testshell.java", true);
|
||||
}
|
||||
|
||||
scriptFolder = new File(javaShellFolder, "scripts");
|
||||
if (!scriptFolder.exists()) {
|
||||
scriptFolder.mkdir();
|
||||
}
|
||||
|
||||
cacheFolder = new File(javaShellFolder, "cache");
|
||||
if (!cacheFolder.exists()) {
|
||||
cacheFolder.mkdir();
|
||||
}
|
||||
|
||||
libFolder = new File(javaShellFolder, "lib");
|
||||
if (!libFolder.exists()) {
|
||||
libFolder.mkdir();
|
||||
}
|
||||
|
||||
loadLibrary();
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long time = System.currentTimeMillis();
|
||||
for (File file : scriptFolder.listFiles()) {
|
||||
if (!file.getName().startsWith("-")) {
|
||||
reloadShell(file.getName());
|
||||
}
|
||||
}
|
||||
MsgUtils.send("载入 " + shells.size() + " 个脚本, 耗时 &f" + (System.currentTimeMillis() - time) + "ms");
|
||||
}
|
||||
}.runTask(Main.getInst());
|
||||
}
|
||||
|
||||
public static void javaShellCancel() {
|
||||
for (File cacheFile : cacheFolder.listFiles()) {
|
||||
cacheFile.delete();
|
||||
}
|
||||
for (String name : shells.keySet()) {
|
||||
invokeMethod(name, "onDisable");
|
||||
}
|
||||
}
|
||||
|
||||
public static void invokeMethod(String name, String method) {
|
||||
if (shells.containsKey(name)) {
|
||||
Class<?> clazz = shells.get(name);
|
||||
try {
|
||||
Method disableMethod = clazz.getMethod(method, new Class[0]);
|
||||
if (disableMethod != null) {
|
||||
disableMethod.invoke(clazz.newInstance());
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void unloadShell(String shell) {
|
||||
invokeMethod(shell, "onDisable");
|
||||
Class<?> clazz = shells.remove(shell);
|
||||
try {
|
||||
if (clazz.newInstance() instanceof Listener) {
|
||||
for (RegisteredListener listener : HandlerList.getRegisteredListeners(Main.getInst())) {
|
||||
if (listener.getListener().getClass().getName().equals(clazz.getName())) {
|
||||
HandlerList.unregisterAll(listener.getListener());
|
||||
}
|
||||
}
|
||||
MsgUtils.send("已为脚本 &f" + shell + " &7注销监听器");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean reloadShell(String shell) {
|
||||
unloadShell(shell = shell.replace(".java", ""));
|
||||
|
||||
try {
|
||||
Class.forName("com.sun.tools.javac.main.Main");
|
||||
}
|
||||
catch (Exception e) {
|
||||
MsgUtils.warn("&4JavaShell &c工具的必要依赖 &4com.sun.tools.jar &c丢失, 无法载入!");
|
||||
return false;
|
||||
}
|
||||
|
||||
File javaFile = new File(scriptFolder, shell + ".java");
|
||||
if (!javaFile.exists()) {
|
||||
MsgUtils.send("&c脚本 &4" + shell + "&c 不存在");
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] args = {
|
||||
"-nowarn",
|
||||
"-classpath", "." + File.pathSeparator + JavaShell.getPaths(),
|
||||
"-d", cacheFolder.getAbsolutePath() + File.separator,
|
||||
javaFile.getAbsolutePath()
|
||||
};
|
||||
|
||||
int code = new com.sun.tools.javac.main.Main("javac").compile(args).exitCode;
|
||||
if (code == 0) {
|
||||
MsgUtils.send("&f" + shell + "&7 载入成功");
|
||||
try {
|
||||
URL[] urls = { cacheFolder.toURI().toURL() };
|
||||
URLClassLoader sysloader = new URLClassLoader(urls, Main.class.getClassLoader());
|
||||
Class<?> clazz = sysloader.loadClass(shell);
|
||||
shells.put(shell, clazz);
|
||||
sysloader.close();
|
||||
|
||||
invokeMethod(shell, "onEnable");
|
||||
if (clazz.newInstance() instanceof Listener) {
|
||||
Bukkit.getPluginManager().registerEvents((Listener) clazz.newInstance(), Main.getInst());
|
||||
MsgUtils.send("已为脚本 &f" + shell + " &7注册监听器");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
MsgUtils.send("&4" + shell + "&c 载入失败");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadLibrary() {
|
||||
for (File jar : libFolder.listFiles()) {
|
||||
try {
|
||||
JarUtils.addClassPath(JarUtils.getJarUrl(jar));
|
||||
MsgUtils.send("成功载入 &f" + jar.getName() + " &7到运行库");
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
104
src/main/src/me/skymc/taboolib/javashell/utils/JarUtils.java
Normal file
104
src/main/src/me/skymc/taboolib/javashell/utils/JarUtils.java
Normal file
@ -0,0 +1,104 @@
|
||||
package me.skymc.taboolib.javashell.utils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
public class JarUtils {
|
||||
|
||||
public static boolean extractFromJar(final String fileName, final String dest) throws IOException {
|
||||
if (getRunningJar() == null) {
|
||||
return false;
|
||||
}
|
||||
final File file = new File(dest);
|
||||
if (file.isDirectory()) {
|
||||
file.mkdir();
|
||||
return false;
|
||||
}
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
final JarFile jar = getRunningJar();
|
||||
final Enumeration<JarEntry> e = jar.entries();
|
||||
while (e.hasMoreElements()) {
|
||||
final JarEntry je = e.nextElement();
|
||||
if (!je.getName().contains(fileName)) {
|
||||
continue;
|
||||
}
|
||||
final InputStream in = new BufferedInputStream(jar.getInputStream(je));
|
||||
final OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
|
||||
copyInputStream(in, out);
|
||||
jar.close();
|
||||
return true;
|
||||
}
|
||||
jar.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
private final static void copyInputStream(final InputStream in, final OutputStream out) throws IOException {
|
||||
try {
|
||||
final byte[] buff = new byte[4096];
|
||||
int n;
|
||||
while ((n = in.read(buff)) > 0) {
|
||||
out.write(buff, 0, n);
|
||||
}
|
||||
} finally {
|
||||
out.flush();
|
||||
out.close();
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static URL getJarUrl(final File file) throws IOException {
|
||||
return new URL("jar:" + file.toURI().toURL().toExternalForm() + "!/");
|
||||
}
|
||||
|
||||
public static JarFile getRunningJar() throws IOException {
|
||||
if (!RUNNING_FROM_JAR) {
|
||||
return null; // null if not running from jar
|
||||
}
|
||||
String path = new File(JarUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath())
|
||||
.getAbsolutePath();
|
||||
path = URLDecoder.decode(path, "UTF-8");
|
||||
return new JarFile(path);
|
||||
}
|
||||
|
||||
private static boolean RUNNING_FROM_JAR = false;
|
||||
|
||||
static {
|
||||
final URL resource = JarUtils.class.getClassLoader().getResource("plugin.yml");
|
||||
if (resource != null) {
|
||||
RUNNING_FROM_JAR = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void addClassPath(final URL url) throws IOException {
|
||||
final URLClassLoader sysloader = (URLClassLoader) Bukkit.class.getClassLoader();
|
||||
final Class<URLClassLoader> sysclass = URLClassLoader.class;
|
||||
try {
|
||||
final Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
|
||||
method.setAccessible(true);
|
||||
method.invoke(sysloader, new Object[] { url });
|
||||
} catch (Throwable t) {
|
||||
MsgUtils.warn("ÎÞ·¨Ìí¼ÓÌí¼Ó &4" + url + "&c µ½ÔËÐпâ");
|
||||
MsgUtils.warn(t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
158
src/main/src/me/skymc/taboolib/json/CDL.java
Normal file
158
src/main/src/me/skymc/taboolib/json/CDL.java
Normal file
@ -0,0 +1,158 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
public class CDL {
|
||||
|
||||
private static String getValue(JSONTokener x) throws JSONException {
|
||||
char c;
|
||||
char q;
|
||||
StringBuffer sb;
|
||||
do {
|
||||
c = x.next();
|
||||
} while (c == ' ' || c == '\t');
|
||||
switch (c) {
|
||||
case 0:
|
||||
return null;
|
||||
case '"':
|
||||
case '\'':
|
||||
q = c;
|
||||
sb = new StringBuffer();
|
||||
for (;;) {
|
||||
c = x.next();
|
||||
if (c == q) {
|
||||
break;
|
||||
}
|
||||
if (c == 0 || c == '\n' || c == '\r') {
|
||||
throw x.syntaxError("Missing close quote '" + q + "'.");
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
case ',':
|
||||
x.back();
|
||||
return "";
|
||||
default:
|
||||
x.back();
|
||||
return x.nextTo(',');
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
|
||||
JSONArray ja = new JSONArray();
|
||||
for (;;) {
|
||||
String value = getValue(x);
|
||||
char c = x.next();
|
||||
if (value == null ||
|
||||
(ja.length() == 0 && value.length() == 0 && c != ',')) {
|
||||
return null;
|
||||
}
|
||||
ja.put(value);
|
||||
for (;;) {
|
||||
if (c == ',') {
|
||||
break;
|
||||
}
|
||||
if (c != ' ') {
|
||||
if (c == '\n' || c == '\r' || c == 0) {
|
||||
return ja;
|
||||
}
|
||||
throw x.syntaxError("Bad character '" + c + "' (" +
|
||||
(int)c + ").");
|
||||
}
|
||||
c = x.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
|
||||
throws JSONException {
|
||||
JSONArray ja = rowToJSONArray(x);
|
||||
return ja != null ? ja.toJSONObject(names) : null;
|
||||
}
|
||||
|
||||
public static String rowToString(JSONArray ja) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < ja.length(); i += 1) {
|
||||
if (i > 0) {
|
||||
sb.append(',');
|
||||
}
|
||||
Object object = ja.opt(i);
|
||||
if (object != null) {
|
||||
String string = object.toString();
|
||||
if (string.length() > 0 && (string.indexOf(',') >= 0 ||
|
||||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
||||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
|
||||
sb.append('"');
|
||||
int length = string.length();
|
||||
for (int j = 0; j < length; j += 1) {
|
||||
char c = string.charAt(j);
|
||||
if (c >= ' ' && c != '"') {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
sb.append('"');
|
||||
} else {
|
||||
sb.append(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.append('\n');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||
return toJSONArray(new JSONTokener(string));
|
||||
}
|
||||
|
||||
public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
|
||||
return toJSONArray(rowToJSONArray(x), x);
|
||||
}
|
||||
|
||||
public static JSONArray toJSONArray(JSONArray names, String string)
|
||||
throws JSONException {
|
||||
return toJSONArray(names, new JSONTokener(string));
|
||||
}
|
||||
|
||||
public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
|
||||
throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONArray ja = new JSONArray();
|
||||
for (;;) {
|
||||
JSONObject jo = rowToJSONObject(names, x);
|
||||
if (jo == null) {
|
||||
break;
|
||||
}
|
||||
ja.put(jo);
|
||||
}
|
||||
if (ja.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
|
||||
public static String toString(JSONArray ja) throws JSONException {
|
||||
JSONObject jo = ja.optJSONObject(0);
|
||||
if (jo != null) {
|
||||
JSONArray names = jo.names();
|
||||
if (names != null) {
|
||||
return rowToString(names) + toString(names, ja);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String toString(JSONArray names, JSONArray ja)
|
||||
throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < ja.length(); i += 1) {
|
||||
JSONObject jo = ja.optJSONObject(i);
|
||||
if (jo != null) {
|
||||
sb.append(rowToString(jo.toJSONArray(names)));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
92
src/main/src/me/skymc/taboolib/json/Cookie.java
Normal file
92
src/main/src/me/skymc/taboolib/json/Cookie.java
Normal file
@ -0,0 +1,92 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
public class Cookie {
|
||||
|
||||
public static String escape(String string) {
|
||||
char c;
|
||||
String s = string.trim();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int length = s.length();
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
c = s.charAt(i);
|
||||
if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
|
||||
sb.append('%');
|
||||
sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));
|
||||
sb.append(Character.forDigit((char)(c & 0x0f), 16));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
String name;
|
||||
JSONObject jo = new JSONObject();
|
||||
Object value;
|
||||
JSONTokener x = new JSONTokener(string);
|
||||
jo.put("name", x.nextTo('='));
|
||||
x.next('=');
|
||||
jo.put("value", x.nextTo(';'));
|
||||
x.next();
|
||||
while (x.more()) {
|
||||
name = unescape(x.nextTo("=;"));
|
||||
if (x.next() != '=') {
|
||||
if (name.equals("secure")) {
|
||||
value = Boolean.TRUE;
|
||||
} else {
|
||||
throw x.syntaxError("Missing '=' in cookie parameter.");
|
||||
}
|
||||
} else {
|
||||
value = unescape(x.nextTo(';'));
|
||||
x.next();
|
||||
}
|
||||
jo.put(name, value);
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
sb.append(escape(jo.getString("name")));
|
||||
sb.append("=");
|
||||
sb.append(escape(jo.getString("value")));
|
||||
if (jo.has("expires")) {
|
||||
sb.append(";expires=");
|
||||
sb.append(jo.getString("expires"));
|
||||
}
|
||||
if (jo.has("domain")) {
|
||||
sb.append(";domain=");
|
||||
sb.append(escape(jo.getString("domain")));
|
||||
}
|
||||
if (jo.has("path")) {
|
||||
sb.append(";path=");
|
||||
sb.append(escape(jo.getString("path")));
|
||||
}
|
||||
if (jo.optBoolean("secure")) {
|
||||
sb.append(";secure");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String unescape(String string) {
|
||||
int length = string.length();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < length; ++i) {
|
||||
char c = string.charAt(i);
|
||||
if (c == '+') {
|
||||
c = ' ';
|
||||
} else if (c == '%' && i + 2 < length) {
|
||||
int d = JSONTokener.dehexchar(string.charAt(i + 1));
|
||||
int e = JSONTokener.dehexchar(string.charAt(i + 2));
|
||||
if (d >= 0 && e >= 0) {
|
||||
c = (char)(d * 16 + e);
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
39
src/main/src/me/skymc/taboolib/json/CookieList.java
Normal file
39
src/main/src/me/skymc/taboolib/json/CookieList.java
Normal file
@ -0,0 +1,39 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class CookieList {
|
||||
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
JSONTokener x = new JSONTokener(string);
|
||||
while (x.more()) {
|
||||
String name = Cookie.unescape(x.nextTo('='));
|
||||
x.next('=');
|
||||
jo.put(name, Cookie.unescape(x.nextTo(';')));
|
||||
x.next();
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
boolean b = false;
|
||||
Iterator keys = jo.keys();
|
||||
String string;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (keys.hasNext()) {
|
||||
string = keys.next().toString();
|
||||
if (!jo.isNull(string)) {
|
||||
if (b) {
|
||||
sb.append(';');
|
||||
}
|
||||
sb.append(Cookie.escape(string));
|
||||
sb.append("=");
|
||||
sb.append(Cookie.escape(jo.getString(string)));
|
||||
b = true;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
71
src/main/src/me/skymc/taboolib/json/HTTP.java
Normal file
71
src/main/src/me/skymc/taboolib/json/HTTP.java
Normal file
@ -0,0 +1,71 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class HTTP {
|
||||
|
||||
public static final String CRLF = "\r\n";
|
||||
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
HTTPTokener x = new HTTPTokener(string);
|
||||
String token;
|
||||
|
||||
token = x.nextToken();
|
||||
if (token.toUpperCase().startsWith("HTTP")) {
|
||||
jo.put("HTTP-Version", token);
|
||||
jo.put("Status-Code", x.nextToken());
|
||||
jo.put("Reason-Phrase", x.nextTo('\0'));
|
||||
x.next();
|
||||
} else {
|
||||
jo.put("Method", token);
|
||||
jo.put("Request-URI", x.nextToken());
|
||||
jo.put("HTTP-Version", x.nextToken());
|
||||
}
|
||||
while (x.more()) {
|
||||
String name = x.nextTo(':');
|
||||
x.next(':');
|
||||
jo.put(name, x.nextTo('\0'));
|
||||
x.next();
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
Iterator keys = jo.keys();
|
||||
String string;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Status-Code"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Reason-Phrase"));
|
||||
} else if (jo.has("Method") && jo.has("Request-URI")) {
|
||||
sb.append(jo.getString("Method"));
|
||||
sb.append(' ');
|
||||
sb.append('"');
|
||||
sb.append(jo.getString("Request-URI"));
|
||||
sb.append('"');
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
} else {
|
||||
throw new JSONException("Not enough material for an HTTP header.");
|
||||
}
|
||||
sb.append(CRLF);
|
||||
while (keys.hasNext()) {
|
||||
string = keys.next().toString();
|
||||
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) &&
|
||||
!"Reason-Phrase".equals(string) && !"Method".equals(string) &&
|
||||
!"Request-URI".equals(string) && !jo.isNull(string)) {
|
||||
sb.append(string);
|
||||
sb.append(": ");
|
||||
sb.append(jo.getString(string));
|
||||
sb.append(CRLF);
|
||||
}
|
||||
}
|
||||
sb.append(CRLF);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
37
src/main/src/me/skymc/taboolib/json/HTTPTokener.java
Normal file
37
src/main/src/me/skymc/taboolib/json/HTTPTokener.java
Normal file
@ -0,0 +1,37 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
public class HTTPTokener extends JSONTokener {
|
||||
|
||||
public HTTPTokener(String string) {
|
||||
super(string);
|
||||
}
|
||||
|
||||
public String nextToken() throws JSONException {
|
||||
char c;
|
||||
char q;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
if (c == '"' || c == '\'') {
|
||||
q = c;
|
||||
for (;;) {
|
||||
c = next();
|
||||
if (c < ' ') {
|
||||
throw syntaxError("Unterminated string.");
|
||||
}
|
||||
if (c == q) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
for (;;) {
|
||||
if (c == 0 || Character.isWhitespace(c)) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
c = next();
|
||||
}
|
||||
}
|
||||
}
|
414
src/main/src/me/skymc/taboolib/json/JSONArray.java
Normal file
414
src/main/src/me/skymc/taboolib/json/JSONArray.java
Normal file
@ -0,0 +1,414 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public class JSONArray {
|
||||
|
||||
private final ArrayList myArrayList;
|
||||
|
||||
public JSONArray() {
|
||||
this.myArrayList = new ArrayList();
|
||||
}
|
||||
|
||||
public JSONArray(JSONTokener x) throws JSONException {
|
||||
this();
|
||||
if (x.nextClean() != '[') {
|
||||
throw x.syntaxError("A JSONArray text must start with '['");
|
||||
}
|
||||
if (x.nextClean() != ']') {
|
||||
x.back();
|
||||
for (;;) {
|
||||
if (x.nextClean() == ',') {
|
||||
x.back();
|
||||
this.myArrayList.add(JSONObject.NULL);
|
||||
} else {
|
||||
x.back();
|
||||
this.myArrayList.add(x.nextValue());
|
||||
}
|
||||
switch (x.nextClean()) {
|
||||
case ';':
|
||||
case ',':
|
||||
if (x.nextClean() == ']') {
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
case ']':
|
||||
return;
|
||||
default:
|
||||
throw x.syntaxError("Expected a ',' or ']'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray(String source) throws JSONException {
|
||||
this(new JSONTokener(source));
|
||||
}
|
||||
|
||||
public JSONArray(Collection collection) {
|
||||
this.myArrayList = new ArrayList();
|
||||
if (collection != null) {
|
||||
Iterator iter = collection.iterator();
|
||||
while (iter.hasNext()) {
|
||||
this.myArrayList.add(JSONObject.wrap(iter.next()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray(Object array) throws JSONException {
|
||||
this();
|
||||
if (array.getClass().isArray()) {
|
||||
int length = Array.getLength(array);
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
this.put(JSONObject.wrap(Array.get(array, i)));
|
||||
}
|
||||
} else {
|
||||
throw new JSONException("JSONArray initial value should be a string or collection or array.");
|
||||
}
|
||||
}
|
||||
|
||||
public Object get(int index) throws JSONException {
|
||||
Object object = this.opt(index);
|
||||
if (object == null) {
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
public boolean getBoolean(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
|
||||
}
|
||||
|
||||
public double getDouble(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).doubleValue()
|
||||
: Double.parseDouble((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public int getInt(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getJSONArray(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof JSONArray) {
|
||||
return (JSONArray)object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a JSONArray.");
|
||||
}
|
||||
|
||||
public JSONObject getJSONObject(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof JSONObject) {
|
||||
return (JSONObject)object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a JSONObject.");
|
||||
}
|
||||
|
||||
public long getLong(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof String) {
|
||||
return (String)object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] not a string.");
|
||||
}
|
||||
|
||||
public boolean isNull(int index) {
|
||||
return JSONObject.NULL.equals(this.opt(index));
|
||||
}
|
||||
|
||||
public String join(String separator) throws JSONException {
|
||||
int len = this.length();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < len; i += 1) {
|
||||
if (i > 0) {
|
||||
sb.append(separator);
|
||||
}
|
||||
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return this.myArrayList.size();
|
||||
}
|
||||
|
||||
public Object opt(int index) {
|
||||
return (index < 0 || index >= this.length())
|
||||
? null
|
||||
: this.myArrayList.get(index);
|
||||
}
|
||||
|
||||
public boolean optBoolean(int index) {
|
||||
return this.optBoolean(index, false);
|
||||
}
|
||||
|
||||
public boolean optBoolean(int index, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public double optDouble(int index) {
|
||||
return this.optDouble(index, Double.NaN);
|
||||
}
|
||||
|
||||
public double optDouble(int index, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public int optInt(int index) {
|
||||
return this.optInt(index, 0);
|
||||
}
|
||||
|
||||
public int optInt(int index, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray optJSONArray(int index) {
|
||||
Object o = this.opt(index);
|
||||
return o instanceof JSONArray ? (JSONArray)o : null;
|
||||
}
|
||||
|
||||
public JSONObject optJSONObject(int index) {
|
||||
Object o = this.opt(index);
|
||||
return o instanceof JSONObject ? (JSONObject)o : null;
|
||||
}
|
||||
|
||||
public long optLong(int index) {
|
||||
return this.optLong(index, 0);
|
||||
}
|
||||
|
||||
public long optLong(int index, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public String optString(int index) {
|
||||
return this.optString(index, "");
|
||||
}
|
||||
|
||||
public String optString(int index, String defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
return JSONObject.NULL.equals(object)
|
||||
? defaultValue : object
|
||||
.toString();
|
||||
}
|
||||
|
||||
public JSONArray put(boolean value) {
|
||||
this.put(value ? Boolean.TRUE : Boolean.FALSE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(Collection value) {
|
||||
this.put(new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(double value) throws JSONException {
|
||||
Double d = new Double(value);
|
||||
JSONObject.testValidity(d);
|
||||
this.put(d);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int value) {
|
||||
this.put(new Integer(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(long value) {
|
||||
this.put(new Long(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(Map value) {
|
||||
this.put(new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(Object value) {
|
||||
this.myArrayList.add(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, boolean value) throws JSONException {
|
||||
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, Collection value) throws JSONException {
|
||||
this.put(index, new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, double value) throws JSONException {
|
||||
this.put(index, new Double(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, int value) throws JSONException {
|
||||
this.put(index, new Integer(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, long value) throws JSONException {
|
||||
this.put(index, new Long(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, Map value) throws JSONException {
|
||||
this.put(index, new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONArray put(int index, Object value) throws JSONException {
|
||||
JSONObject.testValidity(value);
|
||||
if (index < 0) {
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
}
|
||||
if (index < this.length()) {
|
||||
this.myArrayList.set(index, value);
|
||||
} else {
|
||||
while (index != this.length()) {
|
||||
this.put(JSONObject.NULL);
|
||||
}
|
||||
this.put(value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Object remove(int index) {
|
||||
Object o = this.opt(index);
|
||||
this.myArrayList.remove(index);
|
||||
return o;
|
||||
}
|
||||
|
||||
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
||||
if (names == null || names.length() == 0 || this.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONObject jo = new JSONObject();
|
||||
for (int i = 0; i < names.length(); i += 1) {
|
||||
jo.put(names.getString(i), this.opt(i));
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
try {
|
||||
return '[' + this.join(",") + ']';
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
Writer write(Writer writer, int indentFactor, int indent)
|
||||
throws JSONException {
|
||||
try {
|
||||
boolean commanate = false;
|
||||
int length = this.length();
|
||||
writer.write('[');
|
||||
|
||||
if (length == 1) {
|
||||
JSONObject.writeValue(writer, this.myArrayList.get(0),
|
||||
indentFactor, indent);
|
||||
} else if (length != 0) {
|
||||
final int newindent = indent + indentFactor;
|
||||
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
if (commanate) {
|
||||
writer.write(',');
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, newindent);
|
||||
JSONObject.writeValue(writer, this.myArrayList.get(i),
|
||||
indentFactor, newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, indent);
|
||||
}
|
||||
writer.write(']');
|
||||
return writer;
|
||||
} catch (IOException e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
}
|
||||
}
|
21
src/main/src/me/skymc/taboolib/json/JSONException.java
Normal file
21
src/main/src/me/skymc/taboolib/json/JSONException.java
Normal file
@ -0,0 +1,21 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
|
||||
public class JSONException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 0;
|
||||
private Throwable cause;
|
||||
|
||||
public JSONException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JSONException(Throwable cause) {
|
||||
super(cause.getMessage());
|
||||
this.cause = cause;
|
||||
}
|
||||
|
||||
public Throwable getCause() {
|
||||
return this.cause;
|
||||
}
|
||||
}
|
311
src/main/src/me/skymc/taboolib/json/JSONML.java
Normal file
311
src/main/src/me/skymc/taboolib/json/JSONML.java
Normal file
@ -0,0 +1,311 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public class JSONML {
|
||||
|
||||
private static Object parse(
|
||||
XMLTokener x,
|
||||
boolean arrayForm,
|
||||
JSONArray ja
|
||||
) throws JSONException {
|
||||
String attribute;
|
||||
char c;
|
||||
String closeTag = null;
|
||||
int i;
|
||||
JSONArray newja = null;
|
||||
JSONObject newjo = null;
|
||||
Object token;
|
||||
String tagName = null;
|
||||
|
||||
while (true) {
|
||||
if (!x.more()) {
|
||||
throw x.syntaxError("Bad XML");
|
||||
}
|
||||
token = x.nextContent();
|
||||
if (token == XML.LT) {
|
||||
token = x.nextToken();
|
||||
if (token instanceof Character) {
|
||||
if (token == XML.SLASH) {
|
||||
token = x.nextToken();
|
||||
if (!(token instanceof String)) {
|
||||
throw new JSONException(
|
||||
"Expected a closing name instead of '" +
|
||||
token + "'.");
|
||||
}
|
||||
if (x.nextToken() != XML.GT) {
|
||||
throw x.syntaxError("Misshaped close tag");
|
||||
}
|
||||
return token;
|
||||
} else if (token == XML.BANG) {
|
||||
c = x.next();
|
||||
if (c == '-') {
|
||||
if (x.next() == '-') {
|
||||
x.skipPast("-->");
|
||||
} else {
|
||||
x.back();
|
||||
}
|
||||
} else if (c == '[') {
|
||||
token = x.nextToken();
|
||||
if (token.equals("CDATA") && x.next() == '[') {
|
||||
if (ja != null) {
|
||||
ja.put(x.nextCDATA());
|
||||
}
|
||||
} else {
|
||||
throw x.syntaxError("Expected 'CDATA['");
|
||||
}
|
||||
} else {
|
||||
i = 1;
|
||||
do {
|
||||
token = x.nextMeta();
|
||||
if (token == null) {
|
||||
throw x.syntaxError("Missing '>' after '<!'.");
|
||||
} else if (token == XML.LT) {
|
||||
i += 1;
|
||||
} else if (token == XML.GT) {
|
||||
i -= 1;
|
||||
}
|
||||
} while (i > 0);
|
||||
}
|
||||
} else if (token == XML.QUEST) {
|
||||
x.skipPast("?>");
|
||||
} else {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
} else {
|
||||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Bad tagName '" + token + "'.");
|
||||
}
|
||||
tagName = (String)token;
|
||||
newja = new JSONArray();
|
||||
newjo = new JSONObject();
|
||||
if (arrayForm) {
|
||||
newja.put(tagName);
|
||||
if (ja != null) {
|
||||
ja.put(newja);
|
||||
}
|
||||
} else {
|
||||
newjo.put("tagName", tagName);
|
||||
if (ja != null) {
|
||||
ja.put(newjo);
|
||||
}
|
||||
}
|
||||
token = null;
|
||||
for (;;) {
|
||||
if (token == null) {
|
||||
token = x.nextToken();
|
||||
}
|
||||
if (token == null) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (!(token instanceof String)) {
|
||||
break;
|
||||
}
|
||||
attribute = (String)token;
|
||||
if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
|
||||
throw x.syntaxError("Reserved attribute.");
|
||||
}
|
||||
token = x.nextToken();
|
||||
if (token == XML.EQ) {
|
||||
token = x.nextToken();
|
||||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
newjo.accumulate(attribute, XML.stringToValue((String)token));
|
||||
token = null;
|
||||
} else {
|
||||
newjo.accumulate(attribute, "");
|
||||
}
|
||||
}
|
||||
if (arrayForm && newjo.length() > 0) {
|
||||
newja.put(newjo);
|
||||
}
|
||||
if (token == XML.SLASH) {
|
||||
if (x.nextToken() != XML.GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (ja == null) {
|
||||
if (arrayForm) {
|
||||
return newja;
|
||||
} else {
|
||||
return newjo;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (token != XML.GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
closeTag = (String)parse(x, arrayForm, newja);
|
||||
if (closeTag != null) {
|
||||
if (!closeTag.equals(tagName)) {
|
||||
throw x.syntaxError("Mismatched '" + tagName +
|
||||
"' and '" + closeTag + "'");
|
||||
}
|
||||
tagName = null;
|
||||
if (!arrayForm && newja.length() > 0) {
|
||||
newjo.put("childNodes", newja);
|
||||
}
|
||||
if (ja == null) {
|
||||
if (arrayForm) {
|
||||
return newja;
|
||||
} else {
|
||||
return newjo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (ja != null) {
|
||||
ja.put(token instanceof String
|
||||
? XML.stringToValue((String)token)
|
||||
: token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||
return toJSONArray(new XMLTokener(string));
|
||||
}
|
||||
|
||||
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
||||
return (JSONArray)parse(x, true, null);
|
||||
}
|
||||
|
||||
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
||||
return (JSONObject)parse(x, false, null);
|
||||
}
|
||||
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
return toJSONObject(new XMLTokener(string));
|
||||
}
|
||||
|
||||
public static String toString(JSONArray ja) throws JSONException {
|
||||
int i;
|
||||
JSONObject jo;
|
||||
String key;
|
||||
Iterator keys;
|
||||
int length;
|
||||
Object object;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
String tagName;
|
||||
String value;
|
||||
tagName = ja.getString(0);
|
||||
XML.noSpace(tagName);
|
||||
tagName = XML.escape(tagName);
|
||||
sb.append('<');
|
||||
sb.append(tagName);
|
||||
|
||||
object = ja.opt(1);
|
||||
if (object instanceof JSONObject) {
|
||||
i = 2;
|
||||
jo = (JSONObject)object;
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
key = keys.next().toString();
|
||||
XML.noSpace(key);
|
||||
value = jo.optString(key);
|
||||
if (value != null) {
|
||||
sb.append(' ');
|
||||
sb.append(XML.escape(key));
|
||||
sb.append('=');
|
||||
sb.append('"');
|
||||
sb.append(XML.escape(value));
|
||||
sb.append('"');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
i = 1;
|
||||
}
|
||||
length = ja.length();
|
||||
if (i >= length) {
|
||||
sb.append('/');
|
||||
sb.append('>');
|
||||
} else {
|
||||
sb.append('>');
|
||||
do {
|
||||
object = ja.get(i);
|
||||
i += 1;
|
||||
if (object != null) {
|
||||
if (object instanceof String) {
|
||||
sb.append(XML.escape(object.toString()));
|
||||
} else if (object instanceof JSONObject) {
|
||||
sb.append(toString((JSONObject)object));
|
||||
} else if (object instanceof JSONArray) {
|
||||
sb.append(toString((JSONArray)object));
|
||||
}
|
||||
}
|
||||
} while (i < length);
|
||||
sb.append('<');
|
||||
sb.append('/');
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int i;
|
||||
JSONArray ja;
|
||||
String key;
|
||||
Iterator keys;
|
||||
int length;
|
||||
Object object;
|
||||
String tagName;
|
||||
String value;
|
||||
tagName = jo.optString("tagName");
|
||||
if (tagName == null) {
|
||||
return XML.escape(jo.toString());
|
||||
}
|
||||
XML.noSpace(tagName);
|
||||
tagName = XML.escape(tagName);
|
||||
sb.append('<');
|
||||
sb.append(tagName);
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
key = keys.next().toString();
|
||||
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
|
||||
XML.noSpace(key);
|
||||
value = jo.optString(key);
|
||||
if (value != null) {
|
||||
sb.append(' ');
|
||||
sb.append(XML.escape(key));
|
||||
sb.append('=');
|
||||
sb.append('"');
|
||||
sb.append(XML.escape(value));
|
||||
sb.append('"');
|
||||
}
|
||||
}
|
||||
}
|
||||
ja = jo.optJSONArray("childNodes");
|
||||
if (ja == null) {
|
||||
sb.append('/');
|
||||
sb.append('>');
|
||||
} else {
|
||||
sb.append('>');
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
object = ja.get(i);
|
||||
if (object != null) {
|
||||
if (object instanceof String) {
|
||||
sb.append(XML.escape(object.toString()));
|
||||
} else if (object instanceof JSONObject) {
|
||||
sb.append(toString((JSONObject)object));
|
||||
} else if (object instanceof JSONArray) {
|
||||
sb.append(toString((JSONArray)object));
|
||||
} else {
|
||||
sb.append(object.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.append('<');
|
||||
sb.append('/');
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
884
src/main/src/me/skymc/taboolib/json/JSONObject.java
Normal file
884
src/main/src/me/skymc/taboolib/json/JSONObject.java
Normal file
@ -0,0 +1,884 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public class JSONObject {
|
||||
|
||||
private static final class Null {
|
||||
|
||||
protected final Object clone() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == null || object == this;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
|
||||
private final Map map;
|
||||
|
||||
public static final Object NULL = new Null();
|
||||
|
||||
public JSONObject() {
|
||||
this.map = new HashMap();
|
||||
}
|
||||
|
||||
public JSONObject(JSONObject jo, String[] names) {
|
||||
this();
|
||||
for (int i = 0; i < names.length; i += 1) {
|
||||
try {
|
||||
this.putOnce(names[i], jo.opt(names[i]));
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject(JSONTokener x) throws JSONException {
|
||||
this();
|
||||
char c;
|
||||
String key;
|
||||
|
||||
if (x.nextClean() != '{') {
|
||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||
}
|
||||
for (;;) {
|
||||
c = x.nextClean();
|
||||
switch (c) {
|
||||
case 0:
|
||||
throw x.syntaxError("A JSONObject text must end with '}'");
|
||||
case '}':
|
||||
return;
|
||||
default:
|
||||
x.back();
|
||||
key = x.nextValue().toString();
|
||||
}
|
||||
c = x.nextClean();
|
||||
if (c == '=') {
|
||||
if (x.next() != '>') {
|
||||
x.back();
|
||||
}
|
||||
} else if (c != ':') {
|
||||
throw x.syntaxError("Expected a ':' after a key");
|
||||
}
|
||||
this.putOnce(key, x.nextValue());
|
||||
switch (x.nextClean()) {
|
||||
case ';':
|
||||
case ',':
|
||||
if (x.nextClean() == '}') {
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
case '}':
|
||||
return;
|
||||
default:
|
||||
throw x.syntaxError("Expected a ',' or '}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject(Map map) {
|
||||
this.map = new HashMap();
|
||||
if (map != null) {
|
||||
Iterator i = map.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
Map.Entry e = (Map.Entry)i.next();
|
||||
Object value = e.getValue();
|
||||
if (value != null) {
|
||||
this.map.put(e.getKey(), wrap(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject(Object bean) {
|
||||
this();
|
||||
this.populateMap(bean);
|
||||
}
|
||||
|
||||
public JSONObject(Object object, String names[]) {
|
||||
this();
|
||||
Class c = object.getClass();
|
||||
for (int i = 0; i < names.length; i += 1) {
|
||||
String name = names[i];
|
||||
try {
|
||||
this.putOpt(name, c.getField(name).get(object));
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject(String source) throws JSONException {
|
||||
this(new JSONTokener(source));
|
||||
}
|
||||
|
||||
public JSONObject(String baseName, Locale locale) throws JSONException {
|
||||
this();
|
||||
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
|
||||
Thread.currentThread().getContextClassLoader());
|
||||
Enumeration keys = bundle.getKeys();
|
||||
while (keys.hasMoreElements()) {
|
||||
Object key = keys.nextElement();
|
||||
if (key instanceof String) {
|
||||
String[] path = ((String)key).split("\\.");
|
||||
int last = path.length - 1;
|
||||
JSONObject target = this;
|
||||
for (int i = 0; i < last; i += 1) {
|
||||
String segment = path[i];
|
||||
JSONObject nextTarget = target.optJSONObject(segment);
|
||||
if (nextTarget == null) {
|
||||
nextTarget = new JSONObject();
|
||||
target.put(segment, nextTarget);
|
||||
}
|
||||
target = nextTarget;
|
||||
}
|
||||
target.put(path[last], bundle.getString((String)key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject accumulate(
|
||||
String key,
|
||||
Object value
|
||||
) throws JSONException {
|
||||
testValidity(value);
|
||||
Object object = this.opt(key);
|
||||
if (object == null) {
|
||||
this.put(key, value instanceof JSONArray
|
||||
? new JSONArray().put(value)
|
||||
: value);
|
||||
} else if (object instanceof JSONArray) {
|
||||
((JSONArray)object).put(value);
|
||||
} else {
|
||||
this.put(key, new JSONArray().put(object).put(value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject append(String key, Object value) throws JSONException {
|
||||
testValidity(value);
|
||||
Object object = this.opt(key);
|
||||
if (object == null) {
|
||||
this.put(key, new JSONArray().put(value));
|
||||
} else if (object instanceof JSONArray) {
|
||||
this.put(key, ((JSONArray)object).put(value));
|
||||
} else {
|
||||
throw new JSONException("JSONObject[" + key +
|
||||
"] is not a JSONArray.");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String doubleToString(double d) {
|
||||
if (Double.isInfinite(d) || Double.isNaN(d)) {
|
||||
return "null";
|
||||
}
|
||||
String string = Double.toString(d);
|
||||
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
string.indexOf('E') < 0) {
|
||||
while (string.endsWith("0")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
if (string.endsWith(".")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public Object get(String key) throws JSONException {
|
||||
if (key == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
Object object = this.opt(key);
|
||||
if (object == null) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] not found.");
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not a Boolean.");
|
||||
}
|
||||
|
||||
public double getDouble(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).doubleValue()
|
||||
: Double.parseDouble((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
public int getInt(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not an int.");
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getJSONArray(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if (object instanceof JSONArray) {
|
||||
return (JSONArray)object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not a JSONArray.");
|
||||
}
|
||||
|
||||
public JSONObject getJSONObject(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if (object instanceof JSONObject) {
|
||||
return (JSONObject)object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not a JSONObject.");
|
||||
}
|
||||
|
||||
public long getLong(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not a long.");
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] getNames(JSONObject jo) {
|
||||
int length = jo.length();
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
Iterator iterator = jo.keys();
|
||||
String[] names = new String[length];
|
||||
int i = 0;
|
||||
while (iterator.hasNext()) {
|
||||
names[i] = (String)iterator.next();
|
||||
i += 1;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public static String[] getNames(Object object) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
Class klass = object.getClass();
|
||||
Field[] fields = klass.getFields();
|
||||
int length = fields.length;
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
String[] names = new String[length];
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
names[i] = fields[i].getName();
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public String getString(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
if (object instanceof String) {
|
||||
return (String)object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] not a string.");
|
||||
}
|
||||
|
||||
public boolean has(String key) {
|
||||
return this.map.containsKey(key);
|
||||
}
|
||||
|
||||
public JSONObject increment(String key) throws JSONException {
|
||||
Object value = this.opt(key);
|
||||
if (value == null) {
|
||||
this.put(key, 1);
|
||||
} else if (value instanceof Integer) {
|
||||
this.put(key, ((Integer)value).intValue() + 1);
|
||||
} else if (value instanceof Long) {
|
||||
this.put(key, ((Long)value).longValue() + 1);
|
||||
} else if (value instanceof Double) {
|
||||
this.put(key, ((Double)value).doubleValue() + 1);
|
||||
} else if (value instanceof Float) {
|
||||
this.put(key, ((Float)value).floatValue() + 1);
|
||||
} else {
|
||||
throw new JSONException("Unable to increment [" + quote(key) + "].");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isNull(String key) {
|
||||
return JSONObject.NULL.equals(this.opt(key));
|
||||
}
|
||||
|
||||
public Iterator keys() {
|
||||
return this.map.keySet().iterator();
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return this.map.size();
|
||||
}
|
||||
|
||||
public JSONArray names() {
|
||||
JSONArray ja = new JSONArray();
|
||||
Iterator keys = this.keys();
|
||||
while (keys.hasNext()) {
|
||||
ja.put(keys.next());
|
||||
}
|
||||
return ja.length() == 0 ? null : ja;
|
||||
}
|
||||
|
||||
public static String numberToString(Number number)
|
||||
throws JSONException {
|
||||
if (number == null) {
|
||||
throw new JSONException("Null pointer");
|
||||
}
|
||||
testValidity(number);
|
||||
String string = number.toString();
|
||||
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
string.indexOf('E') < 0) {
|
||||
while (string.endsWith("0")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
if (string.endsWith(".")) {
|
||||
string = string.substring(0, string.length() - 1);
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public Object opt(String key) {
|
||||
return key == null ? null : this.map.get(key);
|
||||
}
|
||||
|
||||
public boolean optBoolean(String key) {
|
||||
return this.optBoolean(key, false);
|
||||
}
|
||||
|
||||
public boolean optBoolean(String key, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public double optDouble(String key) {
|
||||
return this.optDouble(key, Double.NaN);
|
||||
}
|
||||
|
||||
public double optDouble(String key, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public int optInt(String key) {
|
||||
return this.optInt(key, 0);
|
||||
}
|
||||
|
||||
public int optInt(String key, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray optJSONArray(String key) {
|
||||
Object o = this.opt(key);
|
||||
return o instanceof JSONArray ? (JSONArray)o : null;
|
||||
}
|
||||
|
||||
public JSONObject optJSONObject(String key) {
|
||||
Object object = this.opt(key);
|
||||
return object instanceof JSONObject ? (JSONObject)object : null;
|
||||
}
|
||||
|
||||
public long optLong(String key) {
|
||||
return this.optLong(key, 0);
|
||||
}
|
||||
|
||||
public long optLong(String key, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public String optString(String key) {
|
||||
return this.optString(key, "");
|
||||
}
|
||||
|
||||
public String optString(String key, String defaultValue) {
|
||||
Object object = this.opt(key);
|
||||
return NULL.equals(object) ? defaultValue : object.toString();
|
||||
}
|
||||
|
||||
|
||||
private void populateMap(Object bean) {
|
||||
Class klass = bean.getClass();
|
||||
boolean includeSuperClass = klass.getClassLoader() != null;
|
||||
|
||||
Method[] methods = includeSuperClass
|
||||
? klass.getMethods()
|
||||
: klass.getDeclaredMethods();
|
||||
for (int i = 0; i < methods.length; i += 1) {
|
||||
try {
|
||||
Method method = methods[i];
|
||||
if (Modifier.isPublic(method.getModifiers())) {
|
||||
String name = method.getName();
|
||||
String key = "";
|
||||
if (name.startsWith("get")) {
|
||||
if ("getClass".equals(name) ||
|
||||
"getDeclaringClass".equals(name)) {
|
||||
key = "";
|
||||
} else {
|
||||
key = name.substring(3);
|
||||
}
|
||||
} else if (name.startsWith("is")) {
|
||||
key = name.substring(2);
|
||||
}
|
||||
if (key.length() > 0 &&
|
||||
Character.isUpperCase(key.charAt(0)) &&
|
||||
method.getParameterTypes().length == 0) {
|
||||
if (key.length() == 1) {
|
||||
key = key.toLowerCase();
|
||||
} else if (!Character.isUpperCase(key.charAt(1))) {
|
||||
key = key.substring(0, 1).toLowerCase() +
|
||||
key.substring(1);
|
||||
}
|
||||
|
||||
Object result = method.invoke(bean, (Object[])null);
|
||||
if (result != null) {
|
||||
this.map.put(key, wrap(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONObject put(String key, boolean value) throws JSONException {
|
||||
this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(String key, Collection value) throws JSONException {
|
||||
this.put(key, new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(String key, double value) throws JSONException {
|
||||
this.put(key, new Double(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(String key, int value) throws JSONException {
|
||||
this.put(key, new Integer(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(String key, long value) throws JSONException {
|
||||
this.put(key, new Long(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(String key, Map value) throws JSONException {
|
||||
this.put(key, new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject put(String key, Object value) throws JSONException {
|
||||
if (key == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
if (value != null) {
|
||||
testValidity(value);
|
||||
this.map.put(key, value);
|
||||
} else {
|
||||
this.remove(key);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject putOnce(String key, Object value) throws JSONException {
|
||||
if (key != null && value != null) {
|
||||
if (this.opt(key) != null) {
|
||||
throw new JSONException("Duplicate key \"" + key + "\"");
|
||||
}
|
||||
this.put(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONObject putOpt(String key, Object value) throws JSONException {
|
||||
if (key != null && value != null) {
|
||||
this.put(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String quote(String string) {
|
||||
StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
try {
|
||||
return quote(string, sw).toString();
|
||||
} catch (IOException ignored) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Writer quote(String string, Writer w) throws IOException {
|
||||
if (string == null || string.length() == 0) {
|
||||
w.write("\"\"");
|
||||
return w;
|
||||
}
|
||||
|
||||
char b;
|
||||
char c = 0;
|
||||
String hhhh;
|
||||
int i;
|
||||
int len = string.length();
|
||||
|
||||
w.write('"');
|
||||
for (i = 0; i < len; i += 1) {
|
||||
b = c;
|
||||
c = string.charAt(i);
|
||||
switch (c) {
|
||||
case '\\':
|
||||
case '"':
|
||||
w.write('\\');
|
||||
w.write(c);
|
||||
break;
|
||||
case '/':
|
||||
if (b == '<') {
|
||||
w.write('\\');
|
||||
}
|
||||
w.write(c);
|
||||
break;
|
||||
case '\b':
|
||||
w.write("\\b");
|
||||
break;
|
||||
case '\t':
|
||||
w.write("\\t");
|
||||
break;
|
||||
case '\n':
|
||||
w.write("\\n");
|
||||
break;
|
||||
case '\f':
|
||||
w.write("\\f");
|
||||
break;
|
||||
case '\r':
|
||||
w.write("\\r");
|
||||
break;
|
||||
default:
|
||||
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|
||||
|| (c >= '\u2000' && c < '\u2100')) {
|
||||
hhhh = "000" + Integer.toHexString(c);
|
||||
w.write("\\u" + hhhh.substring(hhhh.length() - 4));
|
||||
} else {
|
||||
w.write(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
w.write('"');
|
||||
return w;
|
||||
}
|
||||
|
||||
public Object remove(String key) {
|
||||
return this.map.remove(key);
|
||||
}
|
||||
|
||||
public static Object stringToValue(String string) {
|
||||
Double d;
|
||||
if (string.equals("")) {
|
||||
return string;
|
||||
}
|
||||
if (string.equalsIgnoreCase("true")) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("false")) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("null")) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
char b = string.charAt(0);
|
||||
if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
|
||||
try {
|
||||
if (string.indexOf('.') > -1 ||
|
||||
string.indexOf('e') > -1 || string.indexOf('E') > -1) {
|
||||
d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
Long myLong = new Long(string);
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return new Integer(myLong.intValue());
|
||||
} else {
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public static void testValidity(Object o) throws JSONException {
|
||||
if (o != null) {
|
||||
if (o instanceof Double) {
|
||||
if (((Double)o).isInfinite() || ((Double)o).isNaN()) {
|
||||
throw new JSONException(
|
||||
"JSON does not allow non-finite numbers.");
|
||||
}
|
||||
} else if (o instanceof Float) {
|
||||
if (((Float)o).isInfinite() || ((Float)o).isNaN()) {
|
||||
throw new JSONException(
|
||||
"JSON does not allow non-finite numbers.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray toJSONArray(JSONArray names) throws JSONException {
|
||||
if (names == null || names.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONArray ja = new JSONArray();
|
||||
for (int i = 0; i < names.length(); i += 1) {
|
||||
ja.put(this.opt(names.getString(i)));
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter w = new StringWriter();
|
||||
synchronized (w.getBuffer()) {
|
||||
return this.write(w, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static String valueToString(Object value) throws JSONException {
|
||||
if (value == null || value.equals(null)) {
|
||||
return "null";
|
||||
}
|
||||
if (value instanceof JSONString) {
|
||||
Object object;
|
||||
try {
|
||||
object = ((JSONString)value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if (object instanceof String) {
|
||||
return (String)object;
|
||||
}
|
||||
throw new JSONException("Bad value from toJSONString: " + object);
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return numberToString((Number) value);
|
||||
}
|
||||
if (value instanceof Boolean || value instanceof JSONObject ||
|
||||
value instanceof JSONArray) {
|
||||
return value.toString();
|
||||
}
|
||||
if (value instanceof Map) {
|
||||
return new JSONObject((Map)value).toString();
|
||||
}
|
||||
if (value instanceof Collection) {
|
||||
return new JSONArray((Collection)value).toString();
|
||||
}
|
||||
if (value.getClass().isArray()) {
|
||||
return new JSONArray(value).toString();
|
||||
}
|
||||
return quote(value.toString());
|
||||
}
|
||||
|
||||
public static Object wrap(Object object) {
|
||||
try {
|
||||
if (object == null) {
|
||||
return NULL;
|
||||
}
|
||||
if (object instanceof JSONObject || object instanceof JSONArray ||
|
||||
NULL.equals(object) || object instanceof JSONString ||
|
||||
object instanceof Byte || object instanceof Character ||
|
||||
object instanceof Short || object instanceof Integer ||
|
||||
object instanceof Long || object instanceof Boolean ||
|
||||
object instanceof Float || object instanceof Double ||
|
||||
object instanceof String || object instanceof Enum) {
|
||||
return object;
|
||||
}
|
||||
|
||||
if (object instanceof Collection) {
|
||||
return new JSONArray((Collection)object);
|
||||
}
|
||||
if (object.getClass().isArray()) {
|
||||
return new JSONArray(object);
|
||||
}
|
||||
if (object instanceof Map) {
|
||||
return new JSONObject((Map)object);
|
||||
}
|
||||
Package objectPackage = object.getClass().getPackage();
|
||||
String objectPackageName = objectPackage != null
|
||||
? objectPackage.getName()
|
||||
: "";
|
||||
if (
|
||||
objectPackageName.startsWith("java.") ||
|
||||
objectPackageName.startsWith("javax.") ||
|
||||
object.getClass().getClassLoader() == null
|
||||
) {
|
||||
return object.toString();
|
||||
}
|
||||
return new JSONObject(object);
|
||||
} catch(Exception exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
static final Writer writeValue(Writer writer, Object value,
|
||||
int indentFactor, int indent) throws JSONException, IOException {
|
||||
if (value == null || value.equals(null)) {
|
||||
writer.write("null");
|
||||
} else if (value instanceof JSONObject) {
|
||||
((JSONObject) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof JSONArray) {
|
||||
((JSONArray) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Map) {
|
||||
new JSONObject((Map) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Collection) {
|
||||
new JSONArray((Collection) value).write(writer, indentFactor,
|
||||
indent);
|
||||
} else if (value.getClass().isArray()) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
writer.write(numberToString((Number) value));
|
||||
} else if (value instanceof Boolean) {
|
||||
writer.write(value.toString());
|
||||
} else if (value instanceof JSONString) {
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
||||
} else {
|
||||
quote(value.toString(), writer);
|
||||
}
|
||||
return writer;
|
||||
}
|
||||
|
||||
static final void indent(Writer writer, int indent) throws IOException {
|
||||
for (int i = 0; i < indent; i += 1) {
|
||||
writer.write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
Writer write(Writer writer, int indentFactor, int indent)
|
||||
throws JSONException {
|
||||
try {
|
||||
boolean commanate = false;
|
||||
final int length = this.length();
|
||||
Iterator keys = this.keys();
|
||||
writer.write('{');
|
||||
|
||||
if (length == 1) {
|
||||
Object key = keys.next();
|
||||
writer.write(quote(key.toString()));
|
||||
writer.write(':');
|
||||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
writeValue(writer, this.map.get(key), indentFactor, indent);
|
||||
} else if (length != 0) {
|
||||
final int newindent = indent + indentFactor;
|
||||
while (keys.hasNext()) {
|
||||
Object key = keys.next();
|
||||
if (commanate) {
|
||||
writer.write(',');
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
indent(writer, newindent);
|
||||
writer.write(quote(key.toString()));
|
||||
writer.write(':');
|
||||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
writeValue(writer, this.map.get(key), indentFactor,
|
||||
newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
indent(writer, indent);
|
||||
}
|
||||
writer.write('}');
|
||||
return writer;
|
||||
} catch (IOException exception) {
|
||||
throw new JSONException(exception);
|
||||
}
|
||||
}
|
||||
}
|
6
src/main/src/me/skymc/taboolib/json/JSONString.java
Normal file
6
src/main/src/me/skymc/taboolib/json/JSONString.java
Normal file
@ -0,0 +1,6 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
public interface JSONString {
|
||||
|
||||
public String toJSONString();
|
||||
}
|
14
src/main/src/me/skymc/taboolib/json/JSONStringer.java
Normal file
14
src/main/src/me/skymc/taboolib/json/JSONStringer.java
Normal file
@ -0,0 +1,14 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
public class JSONStringer extends JSONWriter {
|
||||
|
||||
public JSONStringer() {
|
||||
super(new StringWriter());
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.mode == 'd' ? this.writer.toString() : null;
|
||||
}
|
||||
}
|
287
src/main/src/me/skymc/taboolib/json/JSONTokener.java
Normal file
287
src/main/src/me/skymc/taboolib/json/JSONTokener.java
Normal file
@ -0,0 +1,287 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
public class JSONTokener {
|
||||
|
||||
private long character;
|
||||
private boolean eof;
|
||||
private long index;
|
||||
private long line;
|
||||
private char previous;
|
||||
private Reader reader;
|
||||
private boolean usePrevious;
|
||||
|
||||
public JSONTokener(Reader reader) {
|
||||
this.reader = reader.markSupported()
|
||||
? reader
|
||||
: new BufferedReader(reader);
|
||||
this.eof = false;
|
||||
this.usePrevious = false;
|
||||
this.previous = 0;
|
||||
this.index = 0;
|
||||
this.character = 1;
|
||||
this.line = 1;
|
||||
}
|
||||
|
||||
public JSONTokener(InputStream inputStream) throws JSONException {
|
||||
this(new InputStreamReader(inputStream));
|
||||
}
|
||||
|
||||
public JSONTokener(String s) {
|
||||
this(new StringReader(s));
|
||||
}
|
||||
|
||||
public void back() throws JSONException {
|
||||
if (this.usePrevious || this.index <= 0) {
|
||||
throw new JSONException("Stepping back two steps is not supported");
|
||||
}
|
||||
this.index -= 1;
|
||||
this.character -= 1;
|
||||
this.usePrevious = true;
|
||||
this.eof = false;
|
||||
}
|
||||
|
||||
public static int dehexchar(char c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
return c - ('A' - 10);
|
||||
}
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
return c - ('a' - 10);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean end() {
|
||||
return this.eof && !this.usePrevious;
|
||||
}
|
||||
|
||||
public boolean more() throws JSONException {
|
||||
this.next();
|
||||
if (this.end()) {
|
||||
return false;
|
||||
}
|
||||
this.back();
|
||||
return true;
|
||||
}
|
||||
|
||||
public char next() throws JSONException {
|
||||
int c;
|
||||
if (this.usePrevious) {
|
||||
this.usePrevious = false;
|
||||
c = this.previous;
|
||||
} else {
|
||||
try {
|
||||
c = this.reader.read();
|
||||
} catch (IOException exception) {
|
||||
throw new JSONException(exception);
|
||||
}
|
||||
|
||||
if (c <= 0) {
|
||||
this.eof = true;
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
this.index += 1;
|
||||
if (this.previous == '\r') {
|
||||
this.line += 1;
|
||||
this.character = c == '\n' ? 0 : 1;
|
||||
} else if (c == '\n') {
|
||||
this.line += 1;
|
||||
this.character = 0;
|
||||
} else {
|
||||
this.character += 1;
|
||||
}
|
||||
this.previous = (char) c;
|
||||
return this.previous;
|
||||
}
|
||||
|
||||
public char next(char c) throws JSONException {
|
||||
char n = this.next();
|
||||
if (n != c) {
|
||||
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
||||
n + "'");
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public String next(int n) throws JSONException {
|
||||
if (n == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
char[] chars = new char[n];
|
||||
int pos = 0;
|
||||
|
||||
while (pos < n) {
|
||||
chars[pos] = this.next();
|
||||
if (this.end()) {
|
||||
throw this.syntaxError("Substring bounds error");
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
public char nextClean() throws JSONException {
|
||||
for (;;) {
|
||||
char c = this.next();
|
||||
if (c == 0 || c > ' ') {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String nextString(char quote) throws JSONException {
|
||||
char c;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
c = this.next();
|
||||
switch (c) {
|
||||
case 0:
|
||||
case '\n':
|
||||
case '\r':
|
||||
throw this.syntaxError("Unterminated string");
|
||||
case '\\':
|
||||
c = this.next();
|
||||
switch (c) {
|
||||
case 'b':
|
||||
sb.append('\b');
|
||||
break;
|
||||
case 't':
|
||||
sb.append('\t');
|
||||
break;
|
||||
case 'n':
|
||||
sb.append('\n');
|
||||
break;
|
||||
case 'f':
|
||||
sb.append('\f');
|
||||
break;
|
||||
case 'r':
|
||||
sb.append('\r');
|
||||
break;
|
||||
case 'u':
|
||||
sb.append((char)Integer.parseInt(this.next(4), 16));
|
||||
break;
|
||||
case '"':
|
||||
case '\'':
|
||||
case '\\':
|
||||
case '/':
|
||||
sb.append(c);
|
||||
break;
|
||||
default:
|
||||
throw this.syntaxError("Illegal escape.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (c == quote) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String nextTo(char delimiter) throws JSONException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
char c = this.next();
|
||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||
if (c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
public String nextTo(String delimiters) throws JSONException {
|
||||
char c;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
c = this.next();
|
||||
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
||||
c == '\n' || c == '\r') {
|
||||
if (c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
public Object nextValue() throws JSONException {
|
||||
char c = this.nextClean();
|
||||
String string;
|
||||
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
return this.nextString(c);
|
||||
case '{':
|
||||
this.back();
|
||||
return new JSONObject(this);
|
||||
case '[':
|
||||
this.back();
|
||||
return new JSONArray(this);
|
||||
}
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||
sb.append(c);
|
||||
c = this.next();
|
||||
}
|
||||
this.back();
|
||||
|
||||
string = sb.toString().trim();
|
||||
if ("".equals(string)) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
return JSONObject.stringToValue(string);
|
||||
}
|
||||
|
||||
public char skipTo(char to) throws JSONException {
|
||||
char c;
|
||||
try {
|
||||
long startIndex = this.index;
|
||||
long startCharacter = this.character;
|
||||
long startLine = this.line;
|
||||
this.reader.mark(1000000);
|
||||
do {
|
||||
c = this.next();
|
||||
if (c == 0) {
|
||||
this.reader.reset();
|
||||
this.index = startIndex;
|
||||
this.character = startCharacter;
|
||||
this.line = startLine;
|
||||
return c;
|
||||
}
|
||||
} while (c != to);
|
||||
} catch (IOException exc) {
|
||||
throw new JSONException(exc);
|
||||
}
|
||||
|
||||
this.back();
|
||||
return c;
|
||||
}
|
||||
|
||||
public JSONException syntaxError(String message) {
|
||||
return new JSONException(message + this.toString());
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return " at " + this.index + " [character " + this.character + " line " +
|
||||
this.line + "]";
|
||||
}
|
||||
}
|
160
src/main/src/me/skymc/taboolib/json/JSONWriter.java
Normal file
160
src/main/src/me/skymc/taboolib/json/JSONWriter.java
Normal file
@ -0,0 +1,160 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public class JSONWriter {
|
||||
|
||||
private static final int maxdepth = 200;
|
||||
|
||||
private boolean comma;
|
||||
|
||||
protected char mode;
|
||||
|
||||
private final JSONObject stack[];
|
||||
|
||||
private int top;
|
||||
|
||||
protected Writer writer;
|
||||
|
||||
public JSONWriter(Writer w) {
|
||||
this.comma = false;
|
||||
this.mode = 'i';
|
||||
this.stack = new JSONObject[maxdepth];
|
||||
this.top = 0;
|
||||
this.writer = w;
|
||||
}
|
||||
|
||||
private JSONWriter append(String string) throws JSONException {
|
||||
if (string == null) {
|
||||
throw new JSONException("Null pointer");
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
try {
|
||||
if (this.comma && this.mode == 'a') {
|
||||
this.writer.write(',');
|
||||
}
|
||||
this.writer.write(string);
|
||||
} catch (IOException e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if (this.mode == 'o') {
|
||||
this.mode = 'k';
|
||||
}
|
||||
this.comma = true;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Value out of sequence.");
|
||||
}
|
||||
|
||||
public JSONWriter array() throws JSONException {
|
||||
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
|
||||
this.push(null);
|
||||
this.append("[");
|
||||
this.comma = false;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Misplaced array.");
|
||||
}
|
||||
|
||||
private JSONWriter end(char mode, char c) throws JSONException {
|
||||
if (this.mode != mode) {
|
||||
throw new JSONException(mode == 'a'
|
||||
? "Misplaced endArray."
|
||||
: "Misplaced endObject.");
|
||||
}
|
||||
this.pop(mode);
|
||||
try {
|
||||
this.writer.write(c);
|
||||
} catch (IOException e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
this.comma = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONWriter endArray() throws JSONException {
|
||||
return this.end('a', ']');
|
||||
}
|
||||
|
||||
public JSONWriter endObject() throws JSONException {
|
||||
return this.end('k', '}');
|
||||
}
|
||||
|
||||
public JSONWriter key(String string) throws JSONException {
|
||||
if (string == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
if (this.mode == 'k') {
|
||||
try {
|
||||
this.stack[this.top - 1].putOnce(string, Boolean.TRUE);
|
||||
if (this.comma) {
|
||||
this.writer.write(',');
|
||||
}
|
||||
this.writer.write(JSONObject.quote(string));
|
||||
this.writer.write(':');
|
||||
this.comma = false;
|
||||
this.mode = 'o';
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
}
|
||||
throw new JSONException("Misplaced key.");
|
||||
}
|
||||
|
||||
public JSONWriter object() throws JSONException {
|
||||
if (this.mode == 'i') {
|
||||
this.mode = 'o';
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
this.append("{");
|
||||
this.push(new JSONObject());
|
||||
this.comma = false;
|
||||
return this;
|
||||
}
|
||||
throw new JSONException("Misplaced object.");
|
||||
|
||||
}
|
||||
|
||||
private void pop(char c) throws JSONException {
|
||||
if (this.top <= 0) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
char m = this.stack[this.top - 1] == null ? 'a' : 'k';
|
||||
if (m != c) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
this.top -= 1;
|
||||
this.mode = this.top == 0
|
||||
? 'd'
|
||||
: this.stack[this.top - 1] == null
|
||||
? 'a'
|
||||
: 'k';
|
||||
}
|
||||
|
||||
private void push(JSONObject jo) throws JSONException {
|
||||
if (this.top >= maxdepth) {
|
||||
throw new JSONException("Nesting too deep.");
|
||||
}
|
||||
this.stack[this.top] = jo;
|
||||
this.mode = jo == null ? 'a' : 'k';
|
||||
this.top += 1;
|
||||
}
|
||||
|
||||
public JSONWriter value(boolean b) throws JSONException {
|
||||
return this.append(b ? "true" : "false");
|
||||
}
|
||||
|
||||
public JSONWriter value(double d) throws JSONException {
|
||||
return this.value(new Double(d));
|
||||
}
|
||||
|
||||
public JSONWriter value(long l) throws JSONException {
|
||||
return this.append(Long.toString(l));
|
||||
}
|
||||
|
||||
public JSONWriter value(Object object) throws JSONException {
|
||||
return this.append(JSONObject.valueToString(object));
|
||||
}
|
||||
}
|
344
src/main/src/me/skymc/taboolib/json/XML.java
Normal file
344
src/main/src/me/skymc/taboolib/json/XML.java
Normal file
@ -0,0 +1,344 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public class XML {
|
||||
|
||||
public static final Character AMP = new Character('&');
|
||||
|
||||
public static final Character APOS = new Character('\'');
|
||||
|
||||
public static final Character BANG = new Character('!');
|
||||
|
||||
public static final Character EQ = new Character('=');
|
||||
|
||||
public static final Character GT = new Character('>');
|
||||
|
||||
public static final Character LT = new Character('<');
|
||||
|
||||
public static final Character QUEST = new Character('?');
|
||||
|
||||
public static final Character QUOT = new Character('"');
|
||||
|
||||
public static final Character SLASH = new Character('/');
|
||||
|
||||
public static String escape(String string) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0, length = string.length(); i < length; i++) {
|
||||
char c = string.charAt(i);
|
||||
switch (c) {
|
||||
case '&':
|
||||
sb.append("&");
|
||||
break;
|
||||
case '<':
|
||||
sb.append("<");
|
||||
break;
|
||||
case '>':
|
||||
sb.append(">");
|
||||
break;
|
||||
case '"':
|
||||
sb.append(""");
|
||||
break;
|
||||
case '\'':
|
||||
sb.append("'");
|
||||
break;
|
||||
default:
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void noSpace(String string) throws JSONException {
|
||||
int i, length = string.length();
|
||||
if (length == 0) {
|
||||
throw new JSONException("Empty string.");
|
||||
}
|
||||
for (i = 0; i < length; i += 1) {
|
||||
if (Character.isWhitespace(string.charAt(i))) {
|
||||
throw new JSONException("'" + string +
|
||||
"' contains a space character.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean parse(XMLTokener x, JSONObject context,
|
||||
String name) throws JSONException {
|
||||
char c;
|
||||
int i;
|
||||
JSONObject jsonobject = null;
|
||||
String string;
|
||||
String tagName;
|
||||
Object token;
|
||||
token = x.nextToken();
|
||||
if (token == BANG) {
|
||||
c = x.next();
|
||||
if (c == '-') {
|
||||
if (x.next() == '-') {
|
||||
x.skipPast("-->");
|
||||
return false;
|
||||
}
|
||||
x.back();
|
||||
} else if (c == '[') {
|
||||
token = x.nextToken();
|
||||
if ("CDATA".equals(token)) {
|
||||
if (x.next() == '[') {
|
||||
string = x.nextCDATA();
|
||||
if (string.length() > 0) {
|
||||
context.accumulate("content", string);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
throw x.syntaxError("Expected 'CDATA['");
|
||||
}
|
||||
i = 1;
|
||||
do {
|
||||
token = x.nextMeta();
|
||||
if (token == null) {
|
||||
throw x.syntaxError("Missing '>' after '<!'.");
|
||||
} else if (token == LT) {
|
||||
i += 1;
|
||||
} else if (token == GT) {
|
||||
i -= 1;
|
||||
}
|
||||
} while (i > 0);
|
||||
return false;
|
||||
} else if (token == QUEST) {
|
||||
x.skipPast("?>");
|
||||
return false;
|
||||
} else if (token == SLASH) {
|
||||
token = x.nextToken();
|
||||
if (name == null) {
|
||||
throw x.syntaxError("Mismatched close tag " + token);
|
||||
}
|
||||
if (!token.equals(name)) {
|
||||
throw x.syntaxError("Mismatched " + name + " and " + token);
|
||||
}
|
||||
if (x.nextToken() != GT) {
|
||||
throw x.syntaxError("Misshaped close tag");
|
||||
}
|
||||
return true;
|
||||
|
||||
} else if (token instanceof Character) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
} else {
|
||||
tagName = (String)token;
|
||||
token = null;
|
||||
jsonobject = new JSONObject();
|
||||
for (;;) {
|
||||
if (token == null) {
|
||||
token = x.nextToken();
|
||||
}
|
||||
if (token instanceof String) {
|
||||
string = (String)token;
|
||||
token = x.nextToken();
|
||||
if (token == EQ) {
|
||||
token = x.nextToken();
|
||||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
jsonobject.accumulate(string,
|
||||
XML.stringToValue((String)token));
|
||||
token = null;
|
||||
} else {
|
||||
jsonobject.accumulate(string, "");
|
||||
}
|
||||
} else if (token == SLASH) {
|
||||
if (x.nextToken() != GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (jsonobject.length() > 0) {
|
||||
context.accumulate(tagName, jsonobject);
|
||||
} else {
|
||||
context.accumulate(tagName, "");
|
||||
}
|
||||
return false;
|
||||
} else if (token == GT) {
|
||||
for (;;) {
|
||||
token = x.nextContent();
|
||||
if (token == null) {
|
||||
if (tagName != null) {
|
||||
throw x.syntaxError("Unclosed tag " + tagName);
|
||||
}
|
||||
return false;
|
||||
} else if (token instanceof String) {
|
||||
string = (String)token;
|
||||
if (string.length() > 0) {
|
||||
jsonobject.accumulate("content",
|
||||
XML.stringToValue(string));
|
||||
}
|
||||
} else if (token == LT) {
|
||||
if (parse(x, jsonobject, tagName)) {
|
||||
if (jsonobject.length() == 0) {
|
||||
context.accumulate(tagName, "");
|
||||
} else if (jsonobject.length() == 1 &&
|
||||
jsonobject.opt("content") != null) {
|
||||
context.accumulate(tagName,
|
||||
jsonobject.opt("content"));
|
||||
} else {
|
||||
context.accumulate(tagName, jsonobject);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Object stringToValue(String string) {
|
||||
if ("".equals(string)) {
|
||||
return string;
|
||||
}
|
||||
if ("true".equalsIgnoreCase(string)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if ("false".equalsIgnoreCase(string)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if ("null".equalsIgnoreCase(string)) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
if ("0".equals(string)) {
|
||||
return new Integer(0);
|
||||
}
|
||||
try {
|
||||
char initial = string.charAt(0);
|
||||
boolean negative = false;
|
||||
if (initial == '-') {
|
||||
initial = string.charAt(1);
|
||||
negative = true;
|
||||
}
|
||||
if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {
|
||||
return string;
|
||||
}
|
||||
if ((initial >= '0' && initial <= '9')) {
|
||||
if (string.indexOf('.') >= 0) {
|
||||
return Double.valueOf(string);
|
||||
} else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) {
|
||||
Long myLong = new Long(string);
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return new Integer(myLong.intValue());
|
||||
} else {
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
XMLTokener x = new XMLTokener(string);
|
||||
while (x.more() && x.skipPast("<")) {
|
||||
parse(x, jo, null);
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
public static String toString(Object object) throws JSONException {
|
||||
return toString(object, null);
|
||||
}
|
||||
|
||||
public static String toString(Object object, String tagName)
|
||||
throws JSONException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int i;
|
||||
JSONArray ja;
|
||||
JSONObject jo;
|
||||
String key;
|
||||
Iterator keys;
|
||||
int length;
|
||||
String string;
|
||||
Object value;
|
||||
if (object instanceof JSONObject) {
|
||||
if (tagName != null) {
|
||||
sb.append('<');
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
}
|
||||
jo = (JSONObject)object;
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
key = keys.next().toString();
|
||||
value = jo.opt(key);
|
||||
if (value == null) {
|
||||
value = "";
|
||||
}
|
||||
if (value instanceof String) {
|
||||
string = (String)value;
|
||||
} else {
|
||||
string = null;
|
||||
}
|
||||
if ("content".equals(key)) {
|
||||
if (value instanceof JSONArray) {
|
||||
ja = (JSONArray)value;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
if (i > 0) {
|
||||
sb.append('\n');
|
||||
}
|
||||
sb.append(escape(ja.get(i).toString()));
|
||||
}
|
||||
} else {
|
||||
sb.append(escape(value.toString()));
|
||||
}
|
||||
} else if (value instanceof JSONArray) {
|
||||
ja = (JSONArray)value;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
value = ja.get(i);
|
||||
if (value instanceof JSONArray) {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append('>');
|
||||
sb.append(toString(value));
|
||||
sb.append("</");
|
||||
sb.append(key);
|
||||
sb.append('>');
|
||||
} else {
|
||||
sb.append(toString(value, key));
|
||||
}
|
||||
}
|
||||
} else if ("".equals(value)) {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append("/>");
|
||||
} else {
|
||||
sb.append(toString(value, key));
|
||||
}
|
||||
}
|
||||
if (tagName != null) {
|
||||
sb.append("</");
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
}
|
||||
return sb.toString();
|
||||
} else {
|
||||
if (object.getClass().isArray()) {
|
||||
object = new JSONArray(object);
|
||||
}
|
||||
if (object instanceof JSONArray) {
|
||||
ja = (JSONArray)object;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
|
||||
}
|
||||
return sb.toString();
|
||||
} else {
|
||||
string = (object == null) ? "null" : escape(object.toString());
|
||||
return (tagName == null) ? "\"" + string + "\"" :
|
||||
(string.length() == 0) ? "<" + tagName + "/>" :
|
||||
"<" + tagName + ">" + string + "</" + tagName + ">";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
252
src/main/src/me/skymc/taboolib/json/XMLTokener.java
Normal file
252
src/main/src/me/skymc/taboolib/json/XMLTokener.java
Normal file
@ -0,0 +1,252 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public class XMLTokener extends JSONTokener {
|
||||
|
||||
public static final java.util.HashMap entity;
|
||||
|
||||
static {
|
||||
entity = new java.util.HashMap(8);
|
||||
entity.put("amp", XML.AMP);
|
||||
entity.put("apos", XML.APOS);
|
||||
entity.put("gt", XML.GT);
|
||||
entity.put("lt", XML.LT);
|
||||
entity.put("quot", XML.QUOT);
|
||||
}
|
||||
|
||||
public XMLTokener(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public String nextCDATA() throws JSONException {
|
||||
char c;
|
||||
int i;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
c = next();
|
||||
if (end()) {
|
||||
throw syntaxError("Unclosed CDATA");
|
||||
}
|
||||
sb.append(c);
|
||||
i = sb.length() - 3;
|
||||
if (i >= 0 && sb.charAt(i) == ']' &&
|
||||
sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') {
|
||||
sb.setLength(i);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object nextContent() throws JSONException {
|
||||
char c;
|
||||
StringBuffer sb;
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
if (c == 0) {
|
||||
return null;
|
||||
}
|
||||
if (c == '<') {
|
||||
return XML.LT;
|
||||
}
|
||||
sb = new StringBuffer();
|
||||
for (;;) {
|
||||
if (c == '<' || c == 0) {
|
||||
back();
|
||||
return sb.toString().trim();
|
||||
}
|
||||
if (c == '&') {
|
||||
sb.append(nextEntity(c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
c = next();
|
||||
}
|
||||
}
|
||||
|
||||
public Object nextEntity(char ampersand) throws JSONException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (;;) {
|
||||
char c = next();
|
||||
if (Character.isLetterOrDigit(c) || c == '#') {
|
||||
sb.append(Character.toLowerCase(c));
|
||||
} else if (c == ';') {
|
||||
break;
|
||||
} else {
|
||||
throw syntaxError("Missing ';' in XML entity: &" + sb);
|
||||
}
|
||||
}
|
||||
String string = sb.toString();
|
||||
Object object = entity.get(string);
|
||||
return object != null ? object : ampersand + string + ";";
|
||||
}
|
||||
|
||||
public Object nextMeta() throws JSONException {
|
||||
char c;
|
||||
char q;
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
switch (c) {
|
||||
case 0:
|
||||
throw syntaxError("Misshaped meta tag");
|
||||
case '<':
|
||||
return XML.LT;
|
||||
case '>':
|
||||
return XML.GT;
|
||||
case '/':
|
||||
return XML.SLASH;
|
||||
case '=':
|
||||
return XML.EQ;
|
||||
case '!':
|
||||
return XML.BANG;
|
||||
case '?':
|
||||
return XML.QUEST;
|
||||
case '"':
|
||||
case '\'':
|
||||
q = c;
|
||||
for (;;) {
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
throw syntaxError("Unterminated string");
|
||||
}
|
||||
if (c == q) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
default:
|
||||
for (;;) {
|
||||
c = next();
|
||||
if (Character.isWhitespace(c)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
switch (c) {
|
||||
case 0:
|
||||
case '<':
|
||||
case '>':
|
||||
case '/':
|
||||
case '=':
|
||||
case '!':
|
||||
case '?':
|
||||
case '"':
|
||||
case '\'':
|
||||
back();
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object nextToken() throws JSONException {
|
||||
char c;
|
||||
char q;
|
||||
StringBuffer sb;
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
switch (c) {
|
||||
case 0:
|
||||
throw syntaxError("Misshaped element");
|
||||
case '<':
|
||||
throw syntaxError("Misplaced '<'");
|
||||
case '>':
|
||||
return XML.GT;
|
||||
case '/':
|
||||
return XML.SLASH;
|
||||
case '=':
|
||||
return XML.EQ;
|
||||
case '!':
|
||||
return XML.BANG;
|
||||
case '?':
|
||||
return XML.QUEST;
|
||||
case '"':
|
||||
case '\'':
|
||||
q = c;
|
||||
sb = new StringBuffer();
|
||||
for (;;) {
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
throw syntaxError("Unterminated string");
|
||||
}
|
||||
if (c == q) {
|
||||
return sb.toString();
|
||||
}
|
||||
if (c == '&') {
|
||||
sb.append(nextEntity(c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
default:
|
||||
sb = new StringBuffer();
|
||||
for (;;) {
|
||||
sb.append(c);
|
||||
c = next();
|
||||
if (Character.isWhitespace(c)) {
|
||||
return sb.toString();
|
||||
}
|
||||
switch (c) {
|
||||
case 0:
|
||||
return sb.toString();
|
||||
case '>':
|
||||
case '/':
|
||||
case '=':
|
||||
case '!':
|
||||
case '?':
|
||||
case '[':
|
||||
case ']':
|
||||
back();
|
||||
return sb.toString();
|
||||
case '<':
|
||||
case '"':
|
||||
case '\'':
|
||||
throw syntaxError("Bad character in a name");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean skipPast(String to) throws JSONException {
|
||||
boolean b;
|
||||
char c;
|
||||
int i;
|
||||
int j;
|
||||
int offset = 0;
|
||||
int length = to.length();
|
||||
char[] circle = new char[length];
|
||||
|
||||
for (i = 0; i < length; i += 1) {
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
return false;
|
||||
}
|
||||
circle[i] = c;
|
||||
}
|
||||
for (;;) {
|
||||
j = offset;
|
||||
b = true;
|
||||
for (i = 0; i < length; i += 1) {
|
||||
if (circle[j] != to.charAt(i)) {
|
||||
b = false;
|
||||
break;
|
||||
}
|
||||
j += 1;
|
||||
if (j >= length) {
|
||||
j -= length;
|
||||
}
|
||||
}
|
||||
if (b) {
|
||||
return true;
|
||||
}
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
return false;
|
||||
}
|
||||
circle[offset] = c;
|
||||
offset += 1;
|
||||
if (offset >= length) {
|
||||
offset -= length;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
431
src/main/src/me/skymc/taboolib/jsonformatter/JSONFormatter.java
Normal file
431
src/main/src/me/skymc/taboolib/jsonformatter/JSONFormatter.java
Normal file
@ -0,0 +1,431 @@
|
||||
package me.skymc.taboolib.jsonformatter;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.skymc.taboolib.json.JSONArray;
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
import me.skymc.taboolib.jsonformatter.click.ClickEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.HoverEvent;
|
||||
import me.skymc.taboolib.nms.NMSUtils;
|
||||
|
||||
public class JSONFormatter {
|
||||
|
||||
private JSONArray ja = new JSONArray();
|
||||
private Builder builder = new Builder();
|
||||
private String color = "";
|
||||
private List<JSONArray> all = new ArrayList<JSONArray>();
|
||||
private boolean newline = true;
|
||||
|
||||
public JSONFormatter(){
|
||||
}
|
||||
|
||||
public JSONFormatter(boolean newline){
|
||||
this.newline = newline;
|
||||
}
|
||||
|
||||
public JSONFormatter append(JSONFormatter json){
|
||||
if(json.ja.length() == 0)
|
||||
return this;
|
||||
try{
|
||||
if(newline && json.newline){
|
||||
all.addAll(json.all);
|
||||
}
|
||||
for(int i = 0; i < json.ja.length(); i++){
|
||||
add(json.ja.get(i));
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getSize(){
|
||||
if(newline){ return 1; }
|
||||
return all.size() + 1;
|
||||
}
|
||||
|
||||
public JSONFormatter newLine(){
|
||||
if(newline){
|
||||
append("\n");
|
||||
}else{
|
||||
all.add(ja);
|
||||
ja = new JSONArray();
|
||||
}
|
||||
resetAll();
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONFormatter newLine(int amount){
|
||||
for(int i = 0; i < amount; i++)
|
||||
newLine();
|
||||
return this;
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
ja = new JSONArray();
|
||||
builder = new Builder();
|
||||
color = "";
|
||||
}
|
||||
|
||||
public JSONFormatter resetAll(){
|
||||
return resetColors().resetModifiers();
|
||||
}
|
||||
|
||||
public JSONFormatter resetColors(){
|
||||
color = "";
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONFormatter resetModifiers(){
|
||||
builder = new Builder();
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toJSON(){
|
||||
JSONObject jo = new JSONObject();
|
||||
try{
|
||||
if(ja.length() > 0)
|
||||
jo.put("extra", ja);
|
||||
jo.put("text", "");
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jo.toString();
|
||||
}
|
||||
|
||||
public List<String> toJSONList(){
|
||||
List<String> list = new ArrayList<String>();
|
||||
try{
|
||||
for(JSONArray ja : all){
|
||||
JSONObject jo = new JSONObject();
|
||||
if(ja.length() > 0)
|
||||
jo.put("extra", ja);
|
||||
jo.put("text", "");
|
||||
list.add(jo.toString());
|
||||
}
|
||||
JSONObject jo = new JSONObject();
|
||||
if(ja.length() > 0)
|
||||
jo.put("extra", ja);
|
||||
jo.put("text", "");
|
||||
list.add(jo.toString());
|
||||
return list;
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object toSerialized(){
|
||||
try{
|
||||
return a.invoke(null, toJSON());
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Object> toSerializedList(){
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
try{
|
||||
for(String s : toJSONList()){
|
||||
list.add(a.invoke(null, s));
|
||||
}
|
||||
return list;
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public JSONFormatter send(Player player){
|
||||
JSONFormatter.send(player, this);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void add(Object jo){
|
||||
if(ja == null)
|
||||
ja = new JSONArray();
|
||||
if(jo != null)
|
||||
ja.put(jo);
|
||||
}
|
||||
|
||||
private JSONFormatter append(String text, BuilderMaker bm){
|
||||
builder = new Builder(builder);
|
||||
for(int i = 0; i < text.length(); i++){
|
||||
char c = text.charAt(i);
|
||||
switch(c){
|
||||
case '<27>':{
|
||||
if((i + 1) == text.length()){
|
||||
builder.append(c);
|
||||
continue;
|
||||
}
|
||||
ChatColor cc = ChatColor.getByChar(text.charAt(i + 1));
|
||||
if(cc == null){
|
||||
builder.append(c);
|
||||
break;
|
||||
}
|
||||
add(bm.make());
|
||||
switch(cc){
|
||||
case BOLD:
|
||||
builder = new Builder(builder);
|
||||
builder.bold = true;
|
||||
break;
|
||||
case ITALIC:
|
||||
builder = new Builder(builder);
|
||||
builder.italic = true;
|
||||
break;
|
||||
case MAGIC:
|
||||
builder = new Builder(builder);
|
||||
builder.magic = true;
|
||||
break;
|
||||
case RESET:
|
||||
builder = new Builder();
|
||||
color = "";
|
||||
break;
|
||||
case STRIKETHROUGH:
|
||||
builder = new Builder(builder);
|
||||
builder.strikethrough = true;
|
||||
break;
|
||||
case UNDERLINE:
|
||||
builder = new Builder(builder);
|
||||
builder.underline = true;
|
||||
break;
|
||||
default:{
|
||||
builder = new Builder();
|
||||
color = cc.name().toLowerCase();
|
||||
break;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
default:{
|
||||
builder.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
add(bm.make());
|
||||
return this;
|
||||
}
|
||||
|
||||
public JSONFormatter append(String text){
|
||||
return append(text, new BuilderMaker(){
|
||||
@Override
|
||||
public JSONObject make(){
|
||||
return builder.toString(color);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JSONFormatter appendHover(String text, final HoverEvent hevent){
|
||||
return append(text, new BuilderMaker(){
|
||||
@Override
|
||||
public JSONObject make(){
|
||||
return builder.toStringHover(color, hevent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JSONFormatter appendClick(String text, final ClickEvent cevent){
|
||||
return append(text, new BuilderMaker(){
|
||||
@Override
|
||||
public JSONObject make(){
|
||||
return builder.toStringClick(color, cevent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JSONFormatter appendHoverClick(String text, final HoverEvent hevent, final ClickEvent cevent){
|
||||
return append(text, new BuilderMaker(){
|
||||
@Override
|
||||
public JSONObject make(){
|
||||
return builder.toStringHoverClick(color, hevent, cevent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Object getPacket(){
|
||||
try{
|
||||
return ppocc.newInstance(toSerialized());
|
||||
}catch(Exception e){
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Object> getPacketList(){
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
try{
|
||||
for(Object o : toSerializedList()){
|
||||
list.add(ppocc.newInstance(o));
|
||||
}
|
||||
return list;
|
||||
}catch(Exception e){
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Class<?> cs = NMSUtils.getNMSClassSilent("ChatSerializer", "IChatBaseComponent"), icbc = NMSUtils.getNMSClassSilent("IChatBaseComponent"), ppoc = NMSUtils.getNMSClassSilent("PacketPlayOutChat"), pc = NMSUtils.getNMSClassSilent("PlayerConnection"),
|
||||
p = NMSUtils.getNMSClassSilent("Packet"), ep = NMSUtils.getNMSClassSilent("EntityPlayer");
|
||||
private static Method a = NMSUtils.getMethodSilent(cs, "a", String.class), sp = NMSUtils.getMethodSilent(pc, "sendPacket", p);
|
||||
private static Field ppc = NMSUtils.getFieldSilent(ep, "playerConnection");
|
||||
private static Constructor<?> ppocc = NMSUtils.getConstructorSilent(ppoc, icbc);
|
||||
private static boolean b = check(cs, icbc, ppoc, pc, p, ep, a, sp, ppc, ppocc);
|
||||
|
||||
private static boolean check(Object... o){
|
||||
for(Object a : o){
|
||||
if(a == null)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void send(Player player, JSONFormatter jf){
|
||||
if(!jf.newline){
|
||||
send1(player, jf);
|
||||
}else if(b){
|
||||
try{
|
||||
Object entityplayer = NMSUtils.getHandle(player);
|
||||
Object ppco = ppc.get(entityplayer);
|
||||
sp.invoke(ppco, jf.getPacket());
|
||||
}catch(Exception e){
|
||||
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + jf.toJSON());
|
||||
}
|
||||
}else{
|
||||
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + jf.toJSON());
|
||||
}
|
||||
}
|
||||
|
||||
private static void send1(Player player, JSONFormatter jf){
|
||||
if(b){
|
||||
try{
|
||||
Object entityplayer = NMSUtils.getHandle(player);
|
||||
Object ppco = ppc.get(entityplayer);
|
||||
List<Object> packets = jf.getPacketList();
|
||||
List<String> jsons = null;
|
||||
for(int i = 0; i < packets.size(); i++){
|
||||
try{
|
||||
sp.invoke(ppco, packets.get(i));
|
||||
}catch(Exception e){
|
||||
if(jsons == null){
|
||||
jsons = jf.toJSONList();
|
||||
}
|
||||
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + jsons.get(i));
|
||||
}
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else{
|
||||
for(String json : jf.toJSONList()){
|
||||
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Builder{
|
||||
|
||||
private StringBuilder sb = new StringBuilder("");
|
||||
private boolean bold = false, italic = false, magic = false, strikethrough = false, underline = false, changed = false;
|
||||
|
||||
public Builder(){
|
||||
}
|
||||
|
||||
public Builder(Builder b){
|
||||
bold = b.bold;
|
||||
italic = b.italic;
|
||||
magic = b.magic;
|
||||
strikethrough = b.strikethrough;
|
||||
underline = b.underline;
|
||||
}
|
||||
|
||||
public void append(char c){
|
||||
sb.append(c);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
private JSONObject toString(String color, BuilderHelper bh){
|
||||
String string = sb.toString();
|
||||
if(!changed)
|
||||
return null;
|
||||
if(string.length() == 0)
|
||||
return null;
|
||||
JSONObject jo = new JSONObject();
|
||||
try{
|
||||
if(!color.equals(""))
|
||||
jo.put("color", color);
|
||||
if(bold)
|
||||
jo.put("bold", true);
|
||||
if(italic)
|
||||
jo.put("italic", true);
|
||||
if(magic)
|
||||
jo.put("obfuscated", true);
|
||||
if(strikethrough)
|
||||
jo.put("strikethrough", true);
|
||||
if(underline)
|
||||
jo.put("underlined", true);
|
||||
bh.add(jo);
|
||||
jo.put("text", string);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
public JSONObject toString(String color){
|
||||
return toString(color, new BuilderHelper(){
|
||||
@Override
|
||||
public void add(JSONObject jo) throws Exception{
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JSONObject toStringHover(String color, final HoverEvent event){
|
||||
return toString(color, new BuilderHelper(){
|
||||
@Override
|
||||
public void add(JSONObject jo) throws Exception{
|
||||
if(event.getEvent().length() > 1)
|
||||
jo.put("hoverEvent", event.getEvent());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JSONObject toStringClick(String color, final ClickEvent event){
|
||||
return toString(color, new BuilderHelper(){
|
||||
@Override
|
||||
public void add(JSONObject jo) throws Exception{
|
||||
if(event.getEvent().length() > 1)
|
||||
jo.put("clickEvent", event.getEvent());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JSONObject toStringHoverClick(String color, final HoverEvent hevent, final ClickEvent cevent){
|
||||
return toString(color, new BuilderHelper(){
|
||||
@Override
|
||||
public void add(JSONObject jo) throws Exception{
|
||||
if(hevent.getEvent().length() > 1)
|
||||
jo.put("hoverEvent", hevent.getEvent());
|
||||
if(cevent.getEvent().length() > 1)
|
||||
jo.put("clickEvent", cevent.getEvent());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private abstract class BuilderMaker{
|
||||
public abstract JSONObject make();
|
||||
}
|
||||
|
||||
private abstract class BuilderHelper{
|
||||
public abstract void add(JSONObject jo) throws Exception;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package me.skymc.taboolib.jsonformatter.click;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public abstract class ClickEvent{
|
||||
|
||||
public abstract JSONObject getEvent();
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package me.skymc.taboolib.jsonformatter.click;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public class OpenUrlEvent extends ClickEvent{
|
||||
|
||||
private JSONObject object = new JSONObject();
|
||||
|
||||
public OpenUrlEvent(String suggest){
|
||||
try{
|
||||
object.put("action", "open_url");
|
||||
object.put("value", suggest);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getEvent(){
|
||||
return object;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package me.skymc.taboolib.jsonformatter.click;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public class RunCommandEvent extends ClickEvent{
|
||||
|
||||
private JSONObject object = new JSONObject();
|
||||
|
||||
public RunCommandEvent(String command){
|
||||
try{
|
||||
object.put("action", "run_command");
|
||||
object.put("value", command);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getEvent(){
|
||||
return object;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package me.skymc.taboolib.jsonformatter.click;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public class SuggestCommandEvent extends ClickEvent{
|
||||
|
||||
private JSONObject object = new JSONObject();
|
||||
|
||||
public SuggestCommandEvent(String suggest){
|
||||
try{
|
||||
object.put("action", "suggest_command");
|
||||
object.put("value", suggest);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getEvent(){
|
||||
return object;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package me.skymc.taboolib.jsonformatter.hover;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public abstract class HoverEvent{
|
||||
|
||||
public abstract JSONObject getEvent();
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package me.skymc.taboolib.jsonformatter.hover;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public class ShowAchievementEvent extends HoverEvent{
|
||||
|
||||
private JSONObject object = new JSONObject();
|
||||
|
||||
public ShowAchievementEvent(String achievement){
|
||||
try{
|
||||
object.put("action", "show_achievement");
|
||||
object.put("value", achievement);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getEvent(){
|
||||
return object;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package me.skymc.taboolib.jsonformatter.hover;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
import me.skymc.taboolib.nms.NMSUtil19;
|
||||
import me.skymc.taboolib.nms.item.DabItemUtils;
|
||||
|
||||
public class ShowItemEvent extends HoverEvent{
|
||||
|
||||
private JSONObject object = new JSONObject();
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public ShowItemEvent(ItemStack is){
|
||||
try{
|
||||
object.put("action", "show_item");
|
||||
String display = DabItemUtils.getName(is);
|
||||
String raw = DabItemUtils.getRawName(is);
|
||||
ItemMeta im = is.getItemMeta();
|
||||
List<String> lore = im.hasLore() ? im.getLore() : new ArrayList<String>();
|
||||
Map<Enchantment, Integer> enchants = is.getItemMeta().getEnchants();
|
||||
boolean utag = !display.equals(raw) || lore.size() > 0 || enchants.size() > 0;
|
||||
String tag = "";
|
||||
if(utag){
|
||||
tag = ",tag:{";
|
||||
if(!display.equals(raw)){
|
||||
tag = tag + "display:{Name:" + display;
|
||||
if(lore.size() > 0){
|
||||
tag = tag + ",Lore:[";
|
||||
for(String s : lore){
|
||||
tag = tag + "\"" + s + "\",";
|
||||
}
|
||||
tag = tag.substring(0, tag.length() - 1);
|
||||
tag = tag + "]";
|
||||
}
|
||||
tag = tag + "}";
|
||||
}else{
|
||||
if(lore.size() > 0){
|
||||
tag = tag + "display:{Lore:[";
|
||||
for(String s : lore){
|
||||
tag = tag + "\"" + s + "\",";
|
||||
}
|
||||
tag = tag.substring(0, tag.length() - 1);
|
||||
tag = tag + "]}";
|
||||
}
|
||||
}
|
||||
if(enchants.size() > 0){
|
||||
if(tag.length() > 6){
|
||||
tag = tag + ",";
|
||||
}
|
||||
tag = tag + "ench:[";
|
||||
for(Entry<Enchantment, Integer> e : enchants.entrySet()){
|
||||
tag = tag + "{id:" + e.getKey().getId() + ",lvl:" + e.getValue() + "},";
|
||||
}
|
||||
tag = tag.substring(0, tag.length() - 1);
|
||||
tag = tag + "]";
|
||||
}
|
||||
tag = tag + "}";
|
||||
}
|
||||
String name = DabItemUtils.getMinecraftName(is);
|
||||
object.put("value", "{id:" + name + ",Count:" + is.getAmount() + tag + "}");
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getEvent(){
|
||||
return object;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package me.skymc.taboolib.jsonformatter.hover;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public class ShowTextEvent extends HoverEvent{
|
||||
|
||||
private JSONObject object = new JSONObject();
|
||||
|
||||
public ShowTextEvent(String text){
|
||||
try{
|
||||
object.put("action", "show_text");
|
||||
object.put("value", text);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getEvent(){
|
||||
return object;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package me.skymc.taboolib.listener;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.server.ServerCommandEvent;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.database.PlayerDataManager;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTCompound;
|
||||
import me.skymc.taboolib.itemnbtapi.NBTItem;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.jsonformatter.click.RunCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent;
|
||||
import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.permission.PermissionUtils;
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
|
||||
public class ListenerPlayerCommand implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void cmd(ServerCommandEvent e) {
|
||||
if (e.getCommand().equals("savefile")) {
|
||||
if (TabooLib.getVerint() > 10700) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
Bukkit.getScheduler().runTask(Main.getInst(), () -> DataUtils.saveAllCaches());
|
||||
Bukkit.getScheduler().runTask(Main.getInst(), () -> PlayerDataManager.saveAllCaches(true, false));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@EventHandler
|
||||
public void cmd(PlayerCommandPreprocessEvent e) {
|
||||
if (e.getMessage().equals("/unbreakable") && PermissionUtils.hasPermission(e.getPlayer(), "taboolib.unbreakable")) {
|
||||
e.setCancelled(true);
|
||||
|
||||
NBTItem nbti = new NBTItem(e.getPlayer().getItemInHand());
|
||||
nbti.setInteger("Unbreakable", 1);
|
||||
e.getPlayer().setItemInHand(nbti.getItem());
|
||||
|
||||
MsgUtils.send(e.getPlayer(), "Success!");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package me.skymc.taboolib.listener;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import me.skymc.taboolib.events.PlayerJumpEvent;
|
||||
|
||||
public class ListenerPlayerJump
|
||||
implements Listener
|
||||
{
|
||||
public HashMap<Player, Long> cooldown = new HashMap<>();
|
||||
|
||||
@EventHandler(priority=EventPriority.HIGH)
|
||||
public void onJump(PlayerMoveEvent event)
|
||||
{
|
||||
if ((!event.getPlayer().isFlying()) && (event.getPlayer().getGameMode() != GameMode.CREATIVE) && (event.getFrom().getY() + 0.5D != event.getTo().getY()) && (event.getFrom().getY() + 0.419D < event.getTo().getY())) {
|
||||
Location loc = event.getFrom();
|
||||
loc.setY(event.getFrom().getY() - 1.0D);
|
||||
if (loc.getBlock().getType() != Material.AIR)
|
||||
{
|
||||
if (!this.cooldown.containsKey(event.getPlayer()))
|
||||
{
|
||||
this.cooldown.put(event.getPlayer(), Long.valueOf(System.currentTimeMillis() + 350L));
|
||||
PlayerJumpEvent evt = new PlayerJumpEvent(event.isCancelled(), event.getPlayer());
|
||||
Bukkit.getPluginManager().callEvent(evt);
|
||||
if (evt.isCancelled())
|
||||
{
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
else if (((Long)this.cooldown.get(event.getPlayer())).longValue() <= System.currentTimeMillis())
|
||||
{
|
||||
this.cooldown.put(event.getPlayer(), Long.valueOf(System.currentTimeMillis() + 350L));
|
||||
PlayerJumpEvent evt = new PlayerJumpEvent(event.isCancelled(), event.getPlayer());
|
||||
|
||||
Bukkit.getPluginManager().callEvent(evt);
|
||||
if (evt.isCancelled())
|
||||
{
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event)
|
||||
{
|
||||
this.cooldown.remove(event.getPlayer());
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package me.skymc.taboolib.listener;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.database.PlayerDataManager;
|
||||
import me.skymc.taboolib.playerdata.DataUtils;
|
||||
|
||||
public class ListenerPlayerQuit implements Listener{
|
||||
|
||||
@EventHandler (priority = EventPriority.MONITOR)
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
DataUtils.saveOnline(e.getPlayer().getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package me.skymc.taboolib.listener;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.mysql.MysqlUtils;
|
||||
import me.skymc.taboolib.mysql.protect.MySQLConnection;
|
||||
|
||||
public class ListenerPluginDisable implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void disable(PluginDisableEvent e) {
|
||||
int i = 0;
|
||||
for (MySQLConnection conn : MysqlUtils.CONNECTIONS) {
|
||||
if (conn.getPlugin().equals(e.getPlugin())) {
|
||||
MysqlUtils.CONNECTIONS.remove(conn);
|
||||
conn.closeConnection();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (i > 0) {
|
||||
MsgUtils.send("已停止插件 &f" + e.getPlugin().getName() + "&7 的 &f" + i + "&7 条数据库连接");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
69
src/main/src/me/skymc/taboolib/location/LocationUtils.java
Normal file
69
src/main/src/me/skymc/taboolib/location/LocationUtils.java
Normal file
@ -0,0 +1,69 @@
|
||||
package me.skymc.taboolib.location;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
import me.skymc.taboolib.methods.MethodsUtils;
|
||||
|
||||
public class LocationUtils {
|
||||
|
||||
/**
|
||||
* ÐòÁл¯
|
||||
*
|
||||
* @param location
|
||||
* @return
|
||||
*/
|
||||
public static String fromString(Location location) {
|
||||
return location.getWorld().getName() + "," + location.getX() + "," + location.getY() + "," + location.getZ() + "," + location.getYaw() + "," + location.getPitch();
|
||||
}
|
||||
|
||||
/**
|
||||
* ·´ÐòÁл¯
|
||||
*
|
||||
* @param string
|
||||
* @return
|
||||
*/
|
||||
public static Location toString(String string) {
|
||||
Location location = new Location(null, 0, 0, 0);
|
||||
try {
|
||||
location.setWorld(Bukkit.getWorld(string.split(",")[0]));
|
||||
location.setX(Double.valueOf(string.split(",")[1]));
|
||||
location.setY(Double.valueOf(string.split(",")[2]));
|
||||
location.setZ(Double.valueOf(string.split(",")[3]));
|
||||
location.setYaw(Float.valueOf(string.split(",")[4]));
|
||||
location.setPitch(Float.valueOf(string.split(",")[5]));
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
public static double getBetween(Location l1, Location l2) {
|
||||
if (l1.getWorld().equals(l2.getWorld())) {
|
||||
return Math.ceil(l1.distance(l2));
|
||||
}
|
||||
return -1D;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Block findBlockByLocation(Location l) {
|
||||
while(l.getY() < 255 && l.getBlock().getType() != Material.AIR) {
|
||||
l.add(0, 1, 0);
|
||||
}
|
||||
return l.getY() < 255 && l.getBlock().getType() == Material.AIR ? l.getBlock() : null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String formatToString(Location l) {
|
||||
return l.getWorld().getName() + "," + String.valueOf(l.getX()).replace(".", "#") + "," + String.valueOf(l.getY()).replace(".", "#") + "," + String.valueOf(l.getZ()).replace(".", "#");
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Location parsedToLocation(String string) {
|
||||
String[] values = string.split(",");
|
||||
return new Location(Bukkit.getWorld(values[0]), Double.valueOf(values[1].replace("#", ".")), Double.valueOf(values[2].replace("#", ".")), Double.valueOf(values[3].replace("#", ".")));
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user