TabooLib v4.55
+ 对插件本体进行大面积整改,将一些过失或不再更新的工具类转移至 TabooLibDeprecated.jar 中。 + 对 SimpleCommandBuilder 进行了微调,动态注册后的命令前缀将显示为注册插件。 + 对 SimpleVersionControl 进行了微调,允许在 translate 类中选择所属插件。 + 对 ItemBuilder 进行了微调,允许使用 ItemStack 作为构造参数。 + ...
This commit is contained in:
parent
64aba798c3
commit
d793444dd5
68
pom.xml
68
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.skymc</groupId>
|
||||
<artifactId>TabooLib</artifactId>
|
||||
<version>4.53</version>
|
||||
<version>4.55</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@ -36,6 +36,42 @@
|
||||
<useIncrementalCompilation>true</useIncrementalCompilation>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<useDefaultDelimiters>false</useDefaultDelimiters>
|
||||
<nonFilteredFileExtensions>
|
||||
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
|
||||
</nonFilteredFileExtensions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>net.alchim31.maven</groupId>
|
||||
<artifactId>scala-maven-plugin</artifactId>
|
||||
<version>3.4.2</version>
|
||||
<!--<executions>-->
|
||||
<!--<execution>-->
|
||||
<!--<phase>compile</phase>-->
|
||||
<!--<goals>-->
|
||||
<!--<goal>compile</goal>-->
|
||||
<!--</goals>-->
|
||||
<!--</execution>-->
|
||||
<!--</executions>-->
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>me.skymc.taboolib.socket.TabooLibServer</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
@ -59,29 +95,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>net.alchim31.maven</groupId>
|
||||
<artifactId>scala-maven-plugin</artifactId>
|
||||
<version>3.4.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>me.skymc.taboolib.socket.TabooLibServer</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
@ -218,6 +231,13 @@
|
||||
<scope>system</scope>
|
||||
<systemPath>${basedir}/libs/BossBarAPI.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>deprecated</groupId>
|
||||
<artifactId>deprecated</artifactId>
|
||||
<version>1</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${basedir}/libs/TabooLibDeprecated.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -4,9 +4,11 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.annotations.Dependency;
|
||||
import com.ilummc.tlib.dependency.TDependencyLoader;
|
||||
import com.ilummc.tlib.inject.TDependencyInjector;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.bstats.Metrics;
|
||||
import me.skymc.taboolib.deprecated.TabooLibDeprecated;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import me.skymc.taboolib.listener.TListenerHandler;
|
||||
@ -32,11 +34,13 @@ import java.util.*;
|
||||
@TListener
|
||||
public class TabooLibLoader implements Listener {
|
||||
|
||||
static TabooLibDeprecated tabooLibDeprecated;
|
||||
static Map<String, List<Class>> pluginClasses = Maps.newHashMap();
|
||||
static List<Loader> loaders = Lists.newArrayList();
|
||||
|
||||
static void setup() {
|
||||
testInternet();
|
||||
setupAddons();
|
||||
setupDataFolder();
|
||||
setupDatabase();
|
||||
setupLibraries();
|
||||
@ -44,15 +48,24 @@ public class TabooLibLoader implements Listener {
|
||||
|
||||
static void register() {
|
||||
setupClasses();
|
||||
loadClasses();
|
||||
registerListener();
|
||||
registerMetrics();
|
||||
loadClasses();
|
||||
try {
|
||||
tabooLibDeprecated = new TabooLibDeprecated();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
static void unregister() {
|
||||
unloadClasses();
|
||||
}
|
||||
|
||||
public static TabooLibDeprecated getTabooLibDeprecated() {
|
||||
return tabooLibDeprecated;
|
||||
}
|
||||
|
||||
public static Optional<List<Class>> getPluginClasses(Plugin plugin) {
|
||||
return Optional.ofNullable(pluginClasses.get(plugin.getName()));
|
||||
}
|
||||
@ -84,6 +97,14 @@ public class TabooLibLoader implements Listener {
|
||||
metrics.addCustomChart(new Metrics.SingleLineChart("plugins_using_taboolib", () -> Math.toIntExact(Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(plugin -> plugin.getDescription().getDepend().contains("TabooLib")).count())));
|
||||
}
|
||||
|
||||
static void setupAddons() {
|
||||
TabooLib.instance().saveResource("Addons/TabooLibDeprecated.jar", true);
|
||||
File file = new File(TabooLib.instance().getDataFolder(), "Addons");
|
||||
if (file.exists()) {
|
||||
Arrays.stream(file.listFiles()).forEach(listFile -> TDependencyLoader.addToPath(TabooLib.instance(), listFile));
|
||||
}
|
||||
}
|
||||
|
||||
static void setupDataFolder() {
|
||||
Main.setPlayerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.PLAYER-DATA")));
|
||||
Main.setServerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.SERVER-DATA")));
|
||||
|
@ -2,7 +2,7 @@ package me.skymc.taboolib.anvil;
|
||||
|
||||
import com.ilummc.tlib.util.asm.AsmClassLoader;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.object.Instantiable;
|
||||
import me.skymc.taboolib.common.loader.Instantiable;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
|
@ -31,6 +31,7 @@ public class SimpleCommandBuilder {
|
||||
private String permissionMessage;
|
||||
private CompleterTab completerTab = EMPTY_COMPLETER_TAB;
|
||||
private CompleterCommand completerCommand = EMPTY_COMPLETER_COMMAND;
|
||||
private boolean silence;
|
||||
|
||||
SimpleCommandBuilder(String command, Plugin plugin) {
|
||||
this.command = command;
|
||||
@ -38,6 +39,7 @@ public class SimpleCommandBuilder {
|
||||
this.description = "";
|
||||
this.usage = "/" + command;
|
||||
this.aliases = new ArrayList<>();
|
||||
this.silence = false;
|
||||
}
|
||||
|
||||
public static SimpleCommandBuilder create(String command, Plugin plugin) {
|
||||
@ -79,6 +81,11 @@ public class SimpleCommandBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder silence() {
|
||||
this.silence = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder build() {
|
||||
Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分");
|
||||
Preconditions.checkNotNull(completerTab, "缺少 \"CompleterTab\" 部分");
|
||||
@ -91,7 +98,8 @@ public class SimpleCommandBuilder {
|
||||
permission,
|
||||
permissionMessage,
|
||||
(sender, command, s, args) -> completerCommand.execute(sender, args),
|
||||
(sender, command, s, args) -> completerTab.execute(sender, args));
|
||||
(sender, command, s, args) -> completerTab.execute(sender, args),
|
||||
silence);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,9 @@ import org.bukkit.plugin.Plugin;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
@ -70,6 +72,10 @@ public class TCommandHandler implements Listener {
|
||||
return registerPluginCommand(plugin, command, description, usage, aliases, null, null, commandExecutor, tabCompleter);
|
||||
}
|
||||
|
||||
public static boolean registerPluginCommand(Plugin plugin, String command, String description, String usage, List<String> aliases, String permission, String permissionMessage, CommandExecutor commandExecutor, TabCompleter tabCompleter) {
|
||||
return registerPluginCommand(plugin, command, description, usage, aliases, permission, permissionMessage, commandExecutor, tabCompleter, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向服务端动态注册命令
|
||||
*
|
||||
@ -81,10 +87,11 @@ public class TCommandHandler implements Listener {
|
||||
* @param permission 权限
|
||||
* @param permissionMessage 权限提示
|
||||
* @param commandExecutor 命令执行器
|
||||
* @param tabCompleter 补全执行器
|
||||
* @param tabCompleter 补全执行器
|
||||
* @param silence 是否屏蔽提示
|
||||
* @return 注册结果(boolean)
|
||||
*/
|
||||
public static boolean registerPluginCommand(Plugin plugin, String command, String description, String usage, List<String> aliases, String permission, String permissionMessage, CommandExecutor commandExecutor, TabCompleter tabCompleter) {
|
||||
public static boolean registerPluginCommand(Plugin plugin, String command, String description, String usage, List<String> aliases, String permission, String permissionMessage, CommandExecutor commandExecutor, TabCompleter tabCompleter, boolean silence) {
|
||||
try {
|
||||
Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
|
||||
constructor.setAccessible(true);
|
||||
@ -93,17 +100,18 @@ public class TCommandHandler implements Listener {
|
||||
pluginCommand.setTabCompleter(tabCompleter);
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "description", description);
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "usageMessage", usage);
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "aliases", aliases);
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "activeAliases", aliases);
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "aliases", aliases.stream().map(String::toLowerCase).collect(Collectors.toList()));
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "activeAliases", aliases.stream().map(String::toLowerCase).collect(Collectors.toList()));
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "permission", permission);
|
||||
ReflectionUtils.setValue(pluginCommand, pluginCommand.getClass().getSuperclass(), true, "permissionMessage", permissionMessage);
|
||||
commandMap.register(command, pluginCommand);
|
||||
if (!TabooLib.isTabooLib(plugin)) {
|
||||
commandMap.register(plugin.getName(), pluginCommand);
|
||||
if (!TabooLib.isTabooLib(plugin) && !silence) {
|
||||
TLocale.Logger.info("COMMANDS.INTERNAL.COMMAND-CREATE", plugin.getName(), command);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.info("COMMANDS.INTERNAL.COMMAND-CREATE-FAILED", plugin.getName(), command, e.getMessage());
|
||||
TLocale.Logger.info("COMMANDS.INTERNAL.COMMAND-CREATE-FAILED", plugin.getName(), command, e.toString());
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.commands.builder.SimpleCommandBuilder;
|
||||
import me.skymc.taboolib.object.Instantiable;
|
||||
import me.skymc.taboolib.common.loader.Instantiable;
|
||||
import me.skymc.taboolib.string.language2.Language2Value;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
|
@ -1,10 +1,8 @@
|
||||
package me.skymc.taboolib.commands.taboolib;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.skymc.taboolib.commands.SubCommand;
|
||||
import me.skymc.taboolib.itagapi.TagDataHandler;
|
||||
import me.skymc.taboolib.team.TagAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -3,6 +3,7 @@ package me.skymc.taboolib.common.inject;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.logger.TLogger;
|
||||
import me.skymc.taboolib.TabooLibLoader;
|
||||
import me.skymc.taboolib.commands.builder.SimpleCommandBuilder;
|
||||
import me.skymc.taboolib.common.configuration.TConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@ -39,6 +40,14 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
// SimpleCommandBuilder Inject
|
||||
injectTypes.put(SimpleCommandBuilder.class, (plugin, field, args) -> {
|
||||
try {
|
||||
((SimpleCommandBuilder) field.get(null)).build();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,4 +1,4 @@
|
||||
package me.skymc.taboolib.object;
|
||||
package me.skymc.taboolib.common.loader;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
@ -1,4 +1,4 @@
|
||||
package me.skymc.taboolib.object;
|
||||
package me.skymc.taboolib.common.loader;
|
||||
|
||||
import com.ilummc.tlib.util.Ref;
|
||||
import me.skymc.taboolib.TabooLibLoader;
|
@ -1,7 +1,7 @@
|
||||
package me.skymc.taboolib.common.pathfinder;
|
||||
|
||||
import me.skymc.taboolib.common.versioncontrol.SimpleVersionControl;
|
||||
import me.skymc.taboolib.object.Instantiable;
|
||||
import me.skymc.taboolib.common.loader.Instantiable;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
|
@ -15,8 +15,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 我不信 ClassNotFound 的邪,自己写了一个发现还是一样。。。
|
||||
*
|
||||
* @Author sky
|
||||
* @Since 2018-09-19 21:05
|
||||
*/
|
||||
@ -64,6 +62,10 @@ public class SimpleVersionControl {
|
||||
}
|
||||
|
||||
public Class<?> translate() throws IOException {
|
||||
return translate(plugin);
|
||||
}
|
||||
|
||||
public Class<?> translate(Plugin plugin) throws IOException {
|
||||
ClassReader classReader = new ClassReader(FileUtils.getResource(plugin, target.replace(".", "/") + ".class"));
|
||||
ClassWriter classWriter = new ClassWriter(0);
|
||||
ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter);
|
||||
|
@ -1,60 +0,0 @@
|
||||
package me.skymc.taboolib.cooldown;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package me.skymc.taboolib.cooldown;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
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 java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@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);
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-REGISTER-ANONYMOUS", pack.getPackName(), String.valueOf(pack.getPackSeconds()));
|
||||
}
|
||||
|
||||
public static void register(CooldownPack pack, Plugin plugin) {
|
||||
pack.setPlugin(plugin.getName());
|
||||
packlist.put(pack.getPackName(), pack);
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-REGISTER", pack.getPackName(), String.valueOf(pack.getPackSeconds()), plugin.getName());
|
||||
}
|
||||
|
||||
public static void unregister(String name) {
|
||||
packlist.remove(name);
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-UNREGISTER", name);
|
||||
}
|
||||
|
||||
private static void unregister(CooldownPack pack) {
|
||||
packlist.remove(pack.getPackName());
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-UNREGISTER-AUTO", pack.getPackName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
packlist.values().stream().filter(pack -> !pack.isCooldown(e.getPlayer().getName(), 0)).forEach(pack -> pack.unRegister(e.getPlayer().getName()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void disable(PluginDisableEvent e) {
|
||||
packlist.values().stream().filter(pack -> pack.getPlugin().equals(e.getPlugin().getName())).forEach(CooldownUtils::unregister);
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
package me.skymc.taboolib.cooldown.seconds;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package me.skymc.taboolib.cooldown.seconds;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
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 java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class CooldownUtils2 implements Listener {
|
||||
|
||||
private static ConcurrentHashMap<String, CooldownPack2> packlist = new ConcurrentHashMap<>();
|
||||
|
||||
public static ConcurrentHashMap<String, CooldownPack2> getCooldownPacks() {
|
||||
return packlist;
|
||||
}
|
||||
|
||||
public static void register(CooldownPack2 pack) {
|
||||
packlist.put(pack.getPackName(), pack);
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-REGISTER-ANONYMOUS", pack.getPackName(), String.valueOf(pack.getPackSeconds()));
|
||||
}
|
||||
|
||||
public static void register(CooldownPack2 pack, Plugin plugin) {
|
||||
pack.setPlugin(plugin.getName());
|
||||
packlist.put(pack.getPackName(), pack);
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-REGISTER", pack.getPackName(), String.valueOf(pack.getPackSeconds()), plugin.getName());
|
||||
}
|
||||
|
||||
public static void unregister(String name) {
|
||||
packlist.remove(name);
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-UNREGISTER", name);
|
||||
}
|
||||
|
||||
private static void unregister(CooldownPack2 pack) {
|
||||
packlist.remove(pack.getPackName());
|
||||
TLocale.Logger.info("COOLDOWNPACK.PACK-UNREGISTER-AUTO", pack.getPackName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
packlist.values().stream().filter(pack -> !pack.isCooldown(e.getPlayer().getName(), 0)).forEach(pack -> pack.unRegister(e.getPlayer().getName()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void disable(PluginDisableEvent e) {
|
||||
packlist.values().stream().filter(pack -> pack.getPlugin().equals(e.getPlugin().getName())).forEach(CooldownUtils2::unregister);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,320 +0,0 @@
|
||||
package me.skymc.taboolib.csvutils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
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 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 StringBuilder sb = new StringBuilder();
|
||||
int n;
|
||||
for (n = 0; i != -1; i = s.indexOf(s2, n)) {
|
||||
sb.append(s, n, i);
|
||||
sb.append(s3);
|
||||
n = i + length;
|
||||
}
|
||||
sb.append(s.substring(n));
|
||||
return sb.toString();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public void writeRecord(final String[] array, final boolean b) throws IOException {
|
||||
if (array != null && array.length > 0) {
|
||||
for (String anArray : array) {
|
||||
this.write(anArray, b);
|
||||
}
|
||||
this.endRecord();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkClosed() throws IOException {
|
||||
if (this.closed) {
|
||||
throw new IOException("This instance of the CsvWriter class has already been closed.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() {
|
||||
this.close(false);
|
||||
}
|
||||
|
||||
private void close(final boolean b) {
|
||||
if (!this.closed) {
|
||||
if (b) {
|
||||
this.charset = null;
|
||||
}
|
||||
try {
|
||||
if (this.initialized) {
|
||||
this.outputStream.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
this.outputStream = null;
|
||||
this.closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package me.skymc.taboolib.damage;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Projectile;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
|
||||
@Deprecated
|
||||
public class GetDamager {
|
||||
|
||||
public static Player get(EntityDamageByEntityEvent e) {
|
||||
return DamageUtils.getAttackerInDamageEvent(e);
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package me.skymc.taboolib.damage;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
|
||||
@Deprecated
|
||||
public class GetKiller {
|
||||
|
||||
public static Player get(EntityDeathEvent e) {
|
||||
return e.getEntity().getKiller();
|
||||
}
|
||||
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package me.skymc.taboolib.display;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.nms.NMSUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
/**
|
||||
* @author Bkm016
|
||||
* @since 2018-04-26
|
||||
*/
|
||||
public class ActionUtils {
|
||||
|
||||
private static Class<?> Packet = NMSUtils.getNMSClass("Packet");
|
||||
private static Class<?> ChatComponentText = NMSUtils.getNMSClass("ChatComponentText");
|
||||
private static Class<?> ChatMessageType = NMSUtils.getNMSClass("ChatMessageType");
|
||||
private static Class<?> PacketPlayOutChat = NMSUtils.getNMSClass("PacketPlayOutChat");
|
||||
private static Class<?> IChatBaseComponent = NMSUtils.getNMSClass("IChatBaseComponent");
|
||||
|
||||
public static void send(Player player, String action) {
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Object ab = ChatComponentText.getConstructor(String.class).newInstance(action);
|
||||
Constructor<?> ac;
|
||||
Object abPacket;
|
||||
if (TabooLib.getVerint() > 11100) {
|
||||
ac = PacketPlayOutChat.getConstructor(IChatBaseComponent, ChatMessageType);
|
||||
abPacket = ac.newInstance(ab, ChatMessageType.getMethod("a", Byte.TYPE).invoke(null, (byte) 2));
|
||||
} else {
|
||||
ac = PacketPlayOutChat.getConstructor(IChatBaseComponent, Byte.TYPE);
|
||||
abPacket = ac.newInstance(ab, (byte) 2);
|
||||
}
|
||||
sendPacket(player, abPacket);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
private static void sendPacket(Player player, Object packet) {
|
||||
try {
|
||||
Object handle = player.getClass().getMethod("getHandle", new Class[0]).invoke(player);
|
||||
Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
|
||||
playerConnection.getClass().getMethod("sendPacket", Packet).invoke(playerConnection, packet);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
package me.skymc.taboolib.display;
|
||||
|
||||
import me.skymc.taboolib.nms.NMSUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
/**
|
||||
* @author Bkm016
|
||||
* @since 2018-04-26
|
||||
*/
|
||||
public class TitleUtils {
|
||||
|
||||
private static Class<?> Packet = NMSUtils.getNMSClass("Packet");
|
||||
private static Class<?> PacketPlayOutTitle = NMSUtils.getNMSClass("PacketPlayOutTitle");
|
||||
private static Class<?> IChatBaseComponent = NMSUtils.getNMSClass("IChatBaseComponent");
|
||||
private static Class<?> EnumTitleAction = PacketPlayOutTitle.getDeclaredClasses()[0];
|
||||
|
||||
public static void sendTitle(Player p, String title, String subtitle, int fadein, int stay, int fadeout) {
|
||||
sendTitle(p, title, fadein, stay, fadeout, subtitle, fadein, stay, fadeout);
|
||||
}
|
||||
|
||||
public static void sendTitle(Player p, String title, int fadeint, int stayt, int fadeoutt, String subtitle, int fadeinst, int stayst, int fadeoutst) {
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (title != null) {
|
||||
Object times = EnumTitleAction.getField("TIMES").get(null);
|
||||
Object chatTitle = IChatBaseComponent.getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + title + "\"}");
|
||||
Constructor<?> subtitleConstructor = PacketPlayOutTitle.getConstructor(EnumTitleAction, IChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE);
|
||||
Object titlePacket = subtitleConstructor.newInstance(times, chatTitle, fadeint, stayt, fadeoutt);
|
||||
sendPacket(p, titlePacket);
|
||||
|
||||
times = EnumTitleAction.getField("TITLE").get(null);
|
||||
chatTitle = IChatBaseComponent.getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + title + "\"}");
|
||||
subtitleConstructor = PacketPlayOutTitle.getConstructor(EnumTitleAction, IChatBaseComponent);
|
||||
titlePacket = subtitleConstructor.newInstance(times, chatTitle);
|
||||
sendPacket(p, titlePacket);
|
||||
}
|
||||
if (subtitle != null) {
|
||||
Object times = EnumTitleAction.getField("TIMES").get(null);
|
||||
Object chatSubtitle = IChatBaseComponent.getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + title + "\"}");
|
||||
Constructor<?> subtitleConstructor = PacketPlayOutTitle.getConstructor(EnumTitleAction, IChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE);
|
||||
Object subtitlePacket = subtitleConstructor.newInstance(times, chatSubtitle, fadeinst, stayst, fadeoutst);
|
||||
sendPacket(p, subtitlePacket);
|
||||
|
||||
times = EnumTitleAction.getField("SUBTITLE").get(null);
|
||||
chatSubtitle = IChatBaseComponent.getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + subtitle + "\"}");
|
||||
subtitleConstructor = PacketPlayOutTitle.getConstructor(EnumTitleAction, IChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE);
|
||||
subtitlePacket = subtitleConstructor.newInstance(times, chatSubtitle, fadeinst, stayst, fadeoutst);
|
||||
sendPacket(p, subtitlePacket);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
private static void sendPacket(Player player, Object packet) {
|
||||
try {
|
||||
Object handle = player.getClass().getDeclaredMethod("getHandle").invoke(player);
|
||||
Object playerConnection = handle.getClass().getDeclaredField("playerConnection").get(handle);
|
||||
playerConnection.getClass().getDeclaredMethod("sendPacket", Packet).invoke(playerConnection, packet);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package me.skymc.taboolib.economy;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package me.skymc.taboolib.enchantment;
|
||||
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,279 +0,0 @@
|
||||
package me.skymc.taboolib.entity;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 伪 - MetaData
|
||||
*
|
||||
* @author sky
|
||||
* @since 2018-03-11 11:43:41
|
||||
*/
|
||||
public class EntityTag {
|
||||
|
||||
private static EntityTag inst;
|
||||
private static ConcurrentHashMap<UUID, ConcurrentHashMap<String, Object>> entityData = new ConcurrentHashMap<>();
|
||||
|
||||
private EntityTag() {
|
||||
new BukkitRunnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (UUID uuid : entityData.keySet()) {
|
||||
if (EntityUtils.getEntityWithUUID(uuid) == null) {
|
||||
entityData.remove(uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}.runTaskTimerAsynchronously(Main.getInst(), 20 * 180, 20 * 180);
|
||||
}
|
||||
|
||||
public static EntityTag getInst() {
|
||||
if (inst == null) {
|
||||
synchronized (EntityTag.class) {
|
||||
if (inst == null) {
|
||||
inst = new EntityTag();
|
||||
}
|
||||
}
|
||||
}
|
||||
return inst;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标签
|
||||
*
|
||||
* @param entity 实体
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public void set(String key, Object value, Entity entity) {
|
||||
if (contains(entity)) {
|
||||
entityData.get(entity.getUniqueId()).put(key, value);
|
||||
} else {
|
||||
ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
|
||||
map.put(key, value);
|
||||
entityData.put(entity.getUniqueId(), map);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标签
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public void set(String key, Object value, Entity... entities) {
|
||||
for (Entity entity : entities) {
|
||||
set(key, value, entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标签
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public void set(String key, Object value, List<Entity> entities) {
|
||||
for (Entity entity : entities) {
|
||||
set(key, value, entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除标签
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
*/
|
||||
public Object remove(String key, Entity entity) {
|
||||
if (contains(entity)) {
|
||||
entityData.get(entity.getUniqueId()).remove(key);
|
||||
if (entityData.get(entity.getUniqueId()).size() == 0) {
|
||||
return entityData.remove(entity.getUniqueId());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除标签
|
||||
*
|
||||
* @param key 键
|
||||
* @param entities 实体
|
||||
*/
|
||||
public void remove(String key, Entity... entities) {
|
||||
for (Entity entity : entities) {
|
||||
remove(key, entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除标签
|
||||
*
|
||||
* @param key 键
|
||||
* @param entities 实体
|
||||
*/
|
||||
public void remove(String key, List<Entity> entities) {
|
||||
for (Entity entity : entities) {
|
||||
remove(key, entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数据
|
||||
*
|
||||
* @param entity 实体
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(Entity entity) {
|
||||
return entityData.containsKey(entity.getUniqueId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查标签
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean hasKey(String key, Entity entity) {
|
||||
return contains(entity) && entityData.get(entity.getUniqueId()).containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
* @return Object
|
||||
*/
|
||||
public Object get(String key, Entity entity) {
|
||||
if (contains(entity)) {
|
||||
return entityData.get(entity.getUniqueId()).get(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 值
|
||||
* @return String
|
||||
*/
|
||||
public String getString(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
if (object instanceof String) {
|
||||
return (String) object;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 值
|
||||
* @return int
|
||||
*/
|
||||
public int getInteger(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
if (object != null) {
|
||||
return (int) object;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 值
|
||||
* @return long
|
||||
*/
|
||||
public long getLong(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
if (object != null) {
|
||||
return (long) object;
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean getBoolean(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
return object != null && (boolean) object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
* @return double
|
||||
*/
|
||||
public double getDouble(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
if (object != null) {
|
||||
return (double) object;
|
||||
}
|
||||
return 0D;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
* @return float
|
||||
*/
|
||||
public double getFloat(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
if (object != null) {
|
||||
return (float) object;
|
||||
}
|
||||
return 0F;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
* @return short
|
||||
*/
|
||||
public short getShort(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
if (object != null) {
|
||||
return (short) object;
|
||||
}
|
||||
return (short) 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param entity 实体
|
||||
* @return byte
|
||||
*/
|
||||
public byte getByte(String key, Entity entity) {
|
||||
Object object = get(key, entity);
|
||||
if (object != null) {
|
||||
return (byte) object;
|
||||
}
|
||||
return (byte) 0;
|
||||
}
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
package me.skymc.taboolib.entity;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
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 java.lang.reflect.InvocationTargetException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
@TListener(condition = "check")
|
||||
public class EntityUtils implements Listener {
|
||||
|
||||
private static Entity lastSpawnedEntity = null;
|
||||
|
||||
public static Entity getLastSpawnedEntity() {
|
||||
return lastSpawnedEntity;
|
||||
}
|
||||
|
||||
public static boolean check() {
|
||||
return TabooLib.getVerint() > 10700;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 UUID 获取生物
|
||||
*
|
||||
* @param u
|
||||
* @return
|
||||
*/
|
||||
public static Entity getEntityWithUUID(UUID u) {
|
||||
return Bukkit.getWorlds().stream().flatMap(w -> w.getLivingEntities().stream()).filter(e -> e.getUniqueId().equals(u)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 UUID 获取生物(单世界)
|
||||
*
|
||||
* @param u
|
||||
* @param world
|
||||
* @return
|
||||
*/
|
||||
public static Entity getEntityWithUUID(UUID u, World world) {
|
||||
return world.getLivingEntities().stream().filter(e -> e.getUniqueId().equals(u)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置生物发光(ProcotolLib)
|
||||
*
|
||||
* @param player
|
||||
* @param entity
|
||||
*/
|
||||
public static void addGlow(Player player, Entity entity) {
|
||||
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) {
|
||||
TLocale.sendToConsole("ENTITY-UTILS.NOTFOUND-PROTOCOLLIB");
|
||||
}
|
||||
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) {
|
||||
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) {
|
||||
TLocale.sendToConsole("ENTITY-UTILS.NOTFOUND-PROTOCOLLIB");
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void spawn(EntitySpawnEvent e) {
|
||||
lastSpawnedEntity = e.getEntity();
|
||||
}
|
||||
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
package me.skymc.taboolib.entity;
|
||||
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-06-24 16:32
|
||||
*/
|
||||
public class VectorUtils {
|
||||
|
||||
/**
|
||||
* 物品丢弃
|
||||
* <p>
|
||||
* 常用参数:
|
||||
* itemDrop(player, itemStack, 0.2, 0.5)
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param itemStack 丢弃物品
|
||||
* @param bulletSpread 视角偏移
|
||||
* @param radius 距离
|
||||
* @return {@link Item}
|
||||
*/
|
||||
public static Item itemDrop(Player player, ItemStack itemStack, double bulletSpread, double radius) {
|
||||
Location location = player.getLocation().add(0, 1.5, 0);
|
||||
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;
|
||||
double y;
|
||||
double z;
|
||||
|
||||
if (bulletSpread > 0) {
|
||||
double[] spread = {1.0D, 1.0D, 1.0D};
|
||||
IntStream.range(0, 3).forEach(t -> spread[t] = ((NumberUtils.getRandom().nextDouble() - NumberUtils.getRandom().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);
|
||||
item.setVelocity(dirVel.normalize().multiply(radius));
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生物抛射
|
||||
* <p>
|
||||
* 常用参数:
|
||||
* entityPush(entity, location, 15)
|
||||
*
|
||||
* @param entity 目标生物
|
||||
* @param to 目标坐标
|
||||
* @param velocity 力量
|
||||
*/
|
||||
public static void entityPush(Entity entity, Location to, double velocity) {
|
||||
Location from = entity.getLocation();
|
||||
|
||||
Vector test = to.clone().subtract(from).toVector();
|
||||
double elevation = test.getY();
|
||||
|
||||
Double launchAngle = calculateLaunchAngle(from, to, velocity, elevation, 20.0D);
|
||||
double distance = Math.sqrt(Math.pow(test.getX(), 2.0D) + Math.pow(test.getZ(), 2.0D));
|
||||
if (distance == 0.0D) {
|
||||
return;
|
||||
}
|
||||
if (launchAngle == null) {
|
||||
launchAngle = Math.atan((40.0D * elevation + Math.pow(velocity, 2.0D)) / (40.0D * elevation + 2.0D * Math.pow(velocity, 2.0D)));
|
||||
}
|
||||
double hangTime = calculateHangTime(launchAngle, velocity, elevation, 20.0D);
|
||||
|
||||
test.setY(Math.tan(launchAngle) * distance);
|
||||
test = normalizeVector(test);
|
||||
|
||||
Vector noise = Vector.getRandom();
|
||||
noise = noise.multiply(1 / 10.0D);
|
||||
test.add(noise);
|
||||
|
||||
velocity = velocity + 1.188D * Math.pow(hangTime, 2.0D) + (NumberUtils.getRandom().nextDouble() - 0.8D) / 2.0D;
|
||||
test = test.multiply(velocity / 20.0D);
|
||||
|
||||
entity.setVelocity(test);
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Private Methods
|
||||
//
|
||||
// *********************************
|
||||
|
||||
private static double calculateHangTime(double launchAngle, double v, double elev, double g) {
|
||||
double a = v * Math.sin(launchAngle);
|
||||
double b = -2.0D * g * elev;
|
||||
return Math.pow(a, 2.0D) + b < 0.0D ? 0.0D : (a + Math.sqrt(Math.pow(a, 2.0D) + b)) / g;
|
||||
}
|
||||
|
||||
private static Vector normalizeVector(Vector victor) {
|
||||
double mag = Math.sqrt(Math.pow(victor.getX(), 2.0D) + Math.pow(victor.getY(), 2.0D) + Math.pow(victor.getZ(), 2.0D));
|
||||
return mag != 0.0D ? victor.multiply(1.0D / mag) : victor.multiply(0);
|
||||
}
|
||||
|
||||
private static Double calculateLaunchAngle(Location from, Location to, double v, double elevation, double g) {
|
||||
Vector vector = from.clone().subtract(to).toVector();
|
||||
double distance = Math.sqrt(Math.pow(vector.getX(), 2.0D) + Math.pow(vector.getZ(), 2.0D));
|
||||
double v2 = Math.pow(v, 2.0D);
|
||||
double v4 = Math.pow(v, 4.0D);
|
||||
double check = g * (g * Math.pow(distance, 2.0D) + 2.0D * elevation * v2);
|
||||
return v4 < check ? null : Math.atan((v2 - Math.sqrt(v4 - check)) / (g * distance));
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package me.skymc.taboolib.events;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
@ -160,6 +160,20 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源文件
|
||||
*
|
||||
* @param plugin 所属插件
|
||||
* @param path 地址
|
||||
* @param replace 是否替换
|
||||
*/
|
||||
public static void releaseResource(Plugin plugin, String path, boolean replace) {
|
||||
File file = new File(plugin.getDataFolder(), path);
|
||||
if (!file.exists() || replace) {
|
||||
inputStreamToFile(getResource(plugin, path), file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测文件并创建
|
||||
*
|
||||
|
@ -1,20 +0,0 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import me.skymc.taboolib.entity.VectorUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Deprecated
|
||||
public class DropUtils {
|
||||
|
||||
public static Item drop(Player player, ItemStack itemStack, double bulletSpread, double radius) {
|
||||
return VectorUtils.itemDrop(player, itemStack, bulletSpread, radius);
|
||||
}
|
||||
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
package me.skymc.taboolib.inventory;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class InventoryUtil {
|
||||
|
||||
public final static LinkedList<Integer> SLOT_OF_CENTENTS = new LinkedList<>(Arrays.asList(
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
return inventoryAmount >= amount;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
@ -42,6 +42,11 @@ public class ItemBuilder {
|
||||
itemMeta = itemStack.getItemMeta();
|
||||
}
|
||||
|
||||
public ItemBuilder(ItemStack itemStack) {
|
||||
this.itemStack = itemStack;
|
||||
this.itemMeta = itemStack.getItemMeta();
|
||||
}
|
||||
|
||||
public ItemBuilder(OfflinePlayer player) {
|
||||
this(Material.SKULL_ITEM, 1, 3);
|
||||
this.skullOwner(player.getName());
|
||||
|
@ -1,46 +0,0 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:35:42
|
||||
*/
|
||||
public interface AbstractSpecialItem {
|
||||
|
||||
/**
|
||||
* 当接口被载入
|
||||
*/
|
||||
default void onEnable() {}
|
||||
|
||||
/**
|
||||
* 当接口被卸载
|
||||
*/
|
||||
default void onDisable() {}
|
||||
|
||||
/**
|
||||
* 获取识别名称
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* 获取载入插件
|
||||
*
|
||||
* @return {@link Plugin}
|
||||
*/
|
||||
Plugin getPlugin();
|
||||
|
||||
/**
|
||||
* 是否进行点击事件
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param currentItem 点击物品
|
||||
* @param cursorItem 持有物品
|
||||
* @return {@link SpecialItemResult[]}
|
||||
*/
|
||||
SpecialItemResult[] isCorrectClick(Player player, ItemStack currentItem, ItemStack cursorItem);
|
||||
}
|
@ -1,184 +0,0 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:34:12
|
||||
*/
|
||||
public class SpecialItem implements Listener {
|
||||
|
||||
private static SpecialItem specialItem = null;
|
||||
|
||||
private final List<AbstractSpecialItem> ITEM_DATA = new CopyOnWriteArrayList<>();
|
||||
|
||||
private boolean isLoaded;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
private SpecialItem() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工具对象
|
||||
*
|
||||
* @return {@link SpecialItem}
|
||||
*/
|
||||
public static SpecialItem getInst() {
|
||||
if (specialItem == null) {
|
||||
synchronized (SpecialItem.class) {
|
||||
if (specialItem == null) {
|
||||
specialItem = new SpecialItem();
|
||||
// 注册监听器
|
||||
Bukkit.getPluginManager().registerEvents(specialItem, Main.getInst());
|
||||
}
|
||||
}
|
||||
}
|
||||
return specialItem;
|
||||
}
|
||||
|
||||
public boolean isLoaded() {
|
||||
return isLoaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册接口
|
||||
*
|
||||
* @param item 接口对象
|
||||
*/
|
||||
public void register(AbstractSpecialItem item) {
|
||||
if (contains(item.getName())) {
|
||||
MsgUtils.warn("特殊物品接口已存在, 检查名称 &4" + item.getName() + " &c是否重复");
|
||||
} else {
|
||||
ITEM_DATA.add(item);
|
||||
if (isLoaded) {
|
||||
item.onEnable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销接口
|
||||
*
|
||||
* @param name 注册名称
|
||||
*/
|
||||
public void cancel(String name) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getName() != null && specialitem.getName().equals(specialitem.getName())) {
|
||||
specialitem.onDisable();
|
||||
ITEM_DATA.remove(specialitem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销接口
|
||||
*
|
||||
* @param plugin 注册插件
|
||||
*/
|
||||
public void cancel(Plugin plugin) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getPlugin() != null && specialitem.getPlugin().equals(plugin)) {
|
||||
specialitem.onDisable();
|
||||
ITEM_DATA.remove(specialitem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断名称是否存在
|
||||
*
|
||||
* @param name 注册名称
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(String name) {
|
||||
for (AbstractSpecialItem specialitem : ITEM_DATA) {
|
||||
if (specialitem.getName().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入所有已注册接口
|
||||
*/
|
||||
public void loadItems() {
|
||||
ITEM_DATA.forEach(AbstractSpecialItem::onEnable);
|
||||
isLoaded = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销所有已注册接口
|
||||
*/
|
||||
public void unloadItems() {
|
||||
ITEM_DATA.forEach(AbstractSpecialItem::onDisable);
|
||||
ITEM_DATA.clear();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onDisable(PluginDisableEvent e) {
|
||||
cancel(e.getPlugin());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void click(InventoryClickEvent e) {
|
||||
if (e.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
if (ItemUtils.isNull(e.getCurrentItem()) || ItemUtils.isNull(e.getCursor())) {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) e.getWhoClicked();
|
||||
for (AbstractSpecialItem specialItem : ITEM_DATA) {
|
||||
for (SpecialItemResult result : specialItem.isCorrectClick(player, e.getCurrentItem(), e.getCursor())) {
|
||||
if (result == null) {
|
||||
break;
|
||||
}
|
||||
switch (result) {
|
||||
case CANCEL:
|
||||
e.setCancelled(true);
|
||||
break;
|
||||
case BREAK:
|
||||
return;
|
||||
case REMOVE_ITEM_CURRENT:
|
||||
e.setCurrentItem(null);
|
||||
break;
|
||||
case REMOVE_ITEM_CURSOR:
|
||||
e.getWhoClicked().setItemOnCursor(null);
|
||||
break;
|
||||
case REMOVE_ITEM_CURRENT_AMOUNT_1:
|
||||
if (e.getCurrentItem().getAmount() > 1) {
|
||||
e.getCurrentItem().setAmount(e.getCurrentItem().getAmount() - 1);
|
||||
} else {
|
||||
e.setCurrentItem(null);
|
||||
}
|
||||
break;
|
||||
case REMOVE_ITEM_CURSOR_AMOUNT_1:
|
||||
if (e.getCursor().getAmount() > 1) {
|
||||
e.getCursor().setAmount(e.getCursor().getAmount() - 1);
|
||||
} else {
|
||||
e.getWhoClicked().setItemOnCursor(null);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package me.skymc.taboolib.inventory.speciaitem;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
* @since 2018年2月17日 下午8:55:36
|
||||
*/
|
||||
public enum SpecialItemResult {
|
||||
|
||||
/**
|
||||
* 停止接口检测
|
||||
*/
|
||||
BREAK,
|
||||
|
||||
/**
|
||||
* 取消点击事件
|
||||
*/
|
||||
CANCEL,
|
||||
|
||||
/**
|
||||
* 移除点击物品
|
||||
*/
|
||||
REMOVE_ITEM_CURRENT,
|
||||
|
||||
/**
|
||||
* 移除鼠标物品
|
||||
*/
|
||||
REMOVE_ITEM_CURSOR,
|
||||
|
||||
/**
|
||||
* 移除一个点击物品
|
||||
*/
|
||||
REMOVE_ITEM_CURRENT_AMOUNT_1,
|
||||
|
||||
/**
|
||||
* 移除一个鼠标物品
|
||||
*/
|
||||
REMOVE_ITEM_CURSOR_AMOUNT_1
|
||||
|
||||
}
|
@ -37,7 +37,7 @@ class TagPacket implements Listener {
|
||||
}
|
||||
|
||||
public static void inst() {
|
||||
assert !loaded : "TagAPI is already instanced!";
|
||||
Preconditions.checkArgument(!loaded, "TagAPI is already instanced!");
|
||||
loaded = true;
|
||||
|
||||
Bukkit.getServer().getOnlinePlayers().forEach(player -> entityIdMap.put(player.getEntityId(), player));
|
||||
@ -79,7 +79,6 @@ class TagPacket implements Listener {
|
||||
Preconditions.checkState(Main.getInst().isEnabled(), "Not Enabled!");
|
||||
Preconditions.checkNotNull(player, "player");
|
||||
Preconditions.checkNotNull(forWhom, "forWhom");
|
||||
|
||||
if (player != forWhom && player.getWorld() == forWhom.getWorld() && forWhom.canSee(player)) {
|
||||
forWhom.hidePlayer(player);
|
||||
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getInst(), () -> forWhom.showPlayer(player), 2);
|
||||
@ -90,13 +89,11 @@ class TagPacket implements Listener {
|
||||
Preconditions.checkState(Main.getInst().isEnabled(), "Not Enabled!");
|
||||
Preconditions.checkNotNull(player, "player");
|
||||
Preconditions.checkNotNull(forWhom, "forWhom");
|
||||
|
||||
forWhom.forEach(playerFor -> refreshPlayer(player, playerFor));
|
||||
}
|
||||
|
||||
private static WrappedGameProfile getSentName(int sentEntityId, WrappedGameProfile sent, Player destinationPlayer) {
|
||||
Preconditions.checkState(Bukkit.getServer().isPrimaryThread(), "Can only process events on main thread.");
|
||||
|
||||
// Preconditions.checkState(Bukkit.getServer().isPrimaryThread(), "Can only process events on main thread.");
|
||||
Player namedPlayer = entityIdMap.get(sentEntityId);
|
||||
if (namedPlayer == null) {
|
||||
// They probably were dead when we reloaded
|
||||
|
@ -1,196 +0,0 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
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() == null ? "" : getCompound().toString();
|
||||
}
|
||||
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getCompound() {
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setCompound(Object tag) {
|
||||
nbt = tag;
|
||||
}
|
||||
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getCompound() {
|
||||
return NBTReflectionUtil.getEntityNBTTagCompound(NBTReflectionUtil.getNMSEntity(ent));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setCompound(Object compound) {
|
||||
NBTReflectionUtil.setEntityNBTTag(compound, NBTReflectionUtil.getNMSEntity(ent));
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getCompound() {
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setCompound(Object compound) {
|
||||
nbt = compound;
|
||||
}
|
||||
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
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();
|
||||
}
|
||||
|
||||
public static NBTContainer convertItemtoNBT(ItemStack item) {
|
||||
return NBTReflectionUtil.convertNMSItemtoNBTCompound(NBTReflectionUtil.getNMSItemStack(item));
|
||||
}
|
||||
|
||||
public static ItemStack convertNBTtoItem(NBTCompound comp) {
|
||||
return NBTReflectionUtil.getBukkitItemStack(NBTReflectionUtil.convertNBTCompoundtoNMSItem(comp));
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return bukkitItem;
|
||||
}
|
||||
|
||||
protected void setItem(ItemStack item) {
|
||||
bukkitItem = item;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getCompound() {
|
||||
return NBTReflectionUtil.getItemRootNBTTagCompound(NBTReflectionUtil.getNMSItemStack(bukkitItem));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setCompound(Object compound) {
|
||||
bukkitItem = NBTReflectionUtil.getBukkitItemStack(NBTReflectionUtil.setNBTTag(compound, NBTReflectionUtil.getNMSItemStack(bukkitItem)));
|
||||
}
|
||||
|
||||
}
|
@ -1,128 +0,0 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import me.skymc.taboolib.itemnbtapi.utils.MethodNames;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
package me.skymc.taboolib.itemnbtapi;
|
||||
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,24 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getCompound() {
|
||||
return NBTReflectionUtil.getTileEntityNBTTagCompound(tile);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setCompound(Object compound) {
|
||||
NBTReflectionUtil.setTileEntityNBTTagCompound(tile, compound);
|
||||
}
|
||||
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
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";
|
||||
}
|
||||
}
|
@ -1,155 +0,0 @@
|
||||
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);
|
||||
while (c != ',') {
|
||||
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) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
public class Cookie {
|
||||
|
||||
public static String escape(String string) {
|
||||
char c;
|
||||
String s = string.trim();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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 ("secure".equals(name)) {
|
||||
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 {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
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();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,413 +0,0 @@
|
||||
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.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) {
|
||||
for (Object aCollection : collection) {
|
||||
this.myArrayList.add(JSONObject.wrap(aCollection));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 &&
|
||||
"false".equalsIgnoreCase((String) object))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
"true".equalsIgnoreCase((String) object))) {
|
||||
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();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
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 = 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Throwable getCause() {
|
||||
return this.cause;
|
||||
}
|
||||
}
|
@ -1,310 +0,0 @@
|
||||
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;
|
||||
int i;
|
||||
JSONArray newja;
|
||||
JSONObject newjo;
|
||||
Object token;
|
||||
String tagName;
|
||||
|
||||
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();
|
||||
switch (c) {
|
||||
case '-':
|
||||
if (x.next() == '-') {
|
||||
x.skipPast("-->");
|
||||
} else {
|
||||
x.back();
|
||||
}
|
||||
break;
|
||||
case '[':
|
||||
token = x.nextToken();
|
||||
if ("CDATA".equals(token) && x.next() == '[') {
|
||||
if (ja != null) {
|
||||
ja.put(x.nextCDATA());
|
||||
}
|
||||
} else {
|
||||
throw x.syntaxError("Expected 'CDATA['");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
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);
|
||||
break;
|
||||
}
|
||||
} 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 + "'");
|
||||
}
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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 {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,872 +0,0 @@
|
||||
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.*;
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public class JSONObject {
|
||||
|
||||
private static final class Null {
|
||||
|
||||
@Override
|
||||
protected final Object clone() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
return object == null || object == this;
|
||||
}
|
||||
|
||||
@Override
|
||||
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 (String name : names) {
|
||||
try {
|
||||
this.putOnce(name, jo.opt(name));
|
||||
} 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) {
|
||||
for (Object o : map.entrySet()) {
|
||||
Map.Entry e = (Map.Entry) o;
|
||||
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 (String name : names) {
|
||||
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 void 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));
|
||||
}
|
||||
}
|
||||
|
||||
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 &&
|
||||
"false".equalsIgnoreCase((String) object))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
"true".equalsIgnoreCase((String) object))) {
|
||||
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 static Object stringToValue(String string) {
|
||||
Double d;
|
||||
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;
|
||||
}
|
||||
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 == myLong.intValue()) {
|
||||
return myLong.intValue();
|
||||
} else {
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public static String valueToString(Object value) throws JSONException {
|
||||
if (value == null || value == null) {
|
||||
return "null";
|
||||
}
|
||||
if (value instanceof JSONString) {
|
||||
Object object;
|
||||
try {
|
||||
object = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if (object != null) {
|
||||
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 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);
|
||||
}
|
||||
|
||||
static void writeValue(Writer writer, Object value, int indentFactor, int indent) throws JSONException, IOException {
|
||||
if (value == 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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
static void indent(Writer writer, int indent) throws IOException {
|
||||
for (int i = 0; i < indent; i += 1) {
|
||||
writer.write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 + 1);
|
||||
} else if (value instanceof Long) {
|
||||
this.put(key, (Long) value + 1);
|
||||
} else if (value instanceof Double) {
|
||||
this.put(key, (Double) value + 1);
|
||||
} else if (value instanceof Float) {
|
||||
this.put(key, (Float) value + 1);
|
||||
} else {
|
||||
throw new JSONException("Unable to increment [" + quote(key) + "].");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private void populateMap(Object bean) {
|
||||
Class klass = bean.getClass();
|
||||
boolean includeSuperClass = klass.getClassLoader() != null;
|
||||
|
||||
Method[] methods = includeSuperClass
|
||||
? klass.getMethods()
|
||||
: klass.getDeclaredMethods();
|
||||
for (Method method1 : methods) {
|
||||
try {
|
||||
Method method = method1;
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-07-01 11:10
|
||||
*/
|
||||
public class JSONReader {
|
||||
|
||||
private static Pattern pattern = Pattern.compile("[\t\n]");
|
||||
|
||||
public static String formatJson(String content) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
int index = 0;
|
||||
int count = 0;
|
||||
while (index < content.length()) {
|
||||
char ch = content.charAt(index);
|
||||
if (ch == '{' || ch == '[') {
|
||||
builder.append(ch);
|
||||
builder.append('\n');
|
||||
count++;
|
||||
for (int i = 0; i < count; i++) {
|
||||
builder.append('\t');
|
||||
}
|
||||
} else if (ch == '}' || ch == ']') {
|
||||
builder.append('\n');
|
||||
count--;
|
||||
for (int i = 0; i < count; i++) {
|
||||
builder.append('\t');
|
||||
}
|
||||
builder.append(ch);
|
||||
} else if (ch == ',') {
|
||||
builder.append(ch);
|
||||
builder.append('\n');
|
||||
for (int i = 0; i < count; i++) {
|
||||
builder.append('\t');
|
||||
}
|
||||
} else {
|
||||
builder.append(ch);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return compactJson(builder.toString());
|
||||
}
|
||||
|
||||
private static String compactJson(String content) {
|
||||
return pattern.matcher(content).replaceAll("").trim();
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
public interface JSONString {
|
||||
|
||||
String toJSONString();
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
public class JSONStringer extends JSONWriter {
|
||||
|
||||
public JSONStringer() {
|
||||
super(new StringWriter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.mode == 'd' ? this.writer.toString() : null;
|
||||
}
|
||||
}
|
@ -1,283 +0,0 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
|
||||
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) {
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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 {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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);
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return " at " + this.index + " [character " + this.character + " line " +
|
||||
this.line + "]";
|
||||
}
|
||||
}
|
@ -1,160 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
@ -1,338 +0,0 @@
|
||||
package me.skymc.taboolib.json;
|
||||
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public class XML {
|
||||
|
||||
public static final Character AMP = '&';
|
||||
|
||||
public static final Character APOS = '\'';
|
||||
|
||||
public static final Character BANG = '!';
|
||||
|
||||
public static final Character EQ = '=';
|
||||
|
||||
public static final Character GT = '>';
|
||||
|
||||
public static final Character LT = '<';
|
||||
|
||||
public static final Character QUEST = '?';
|
||||
|
||||
public static final Character QUOT = '"';
|
||||
|
||||
public static final Character SLASH = '/';
|
||||
|
||||
public static String escape(String string) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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 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 == myLong.intValue()) {
|
||||
return 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 {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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 ("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 = escape(object.toString());
|
||||
return (tagName == null) ? "\"" + string + "\"" :
|
||||
(string.length() == 0) ? "<" + tagName + "/>" :
|
||||
"<" + tagName + ">" + string + "</" + tagName + ">";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,252 +0,0 @@
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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 {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,465 +0,0 @@
|
||||
package me.skymc.taboolib.jsonformatter;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
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;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Unknown
|
||||
*/
|
||||
@Deprecated
|
||||
public class JSONFormatter {
|
||||
|
||||
public static void sendRawMessage(Player player, String message) {
|
||||
TLocale.Tellraw.send(player, message);
|
||||
}
|
||||
|
||||
private JSONArray ja = new JSONArray();
|
||||
private Builder builder = new Builder();
|
||||
private String color = "";
|
||||
private List<JSONArray> all = new ArrayList<>();
|
||||
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<>();
|
||||
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<>();
|
||||
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 '§': {
|
||||
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 ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Object> getPacketList() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
try {
|
||||
for (Object o : toSerializedList()) {
|
||||
list.add(ppocc.newInstance(o));
|
||||
}
|
||||
return list;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Class<?> cs = NMSUtils.getNMSClassSilent("ChatSerializer", "IChatBaseComponent");
|
||||
private static Class<?> icbc = NMSUtils.getNMSClassSilent("IChatBaseComponent");
|
||||
private static Class<?> ppoc = NMSUtils.getNMSClassSilent("PacketPlayOutChat");
|
||||
private static Class<?> pc = NMSUtils.getNMSClassSilent("PlayerConnection");
|
||||
private static Class<?> p = NMSUtils.getNMSClassSilent("Packet");
|
||||
private static Class<?> 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 (!"".equals(color)) {
|
||||
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) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package me.skymc.taboolib.jsonformatter.click;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public abstract class ClickEvent{
|
||||
|
||||
public abstract JSONObject getEvent();
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package me.skymc.taboolib.jsonformatter.hover;
|
||||
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
|
||||
public abstract class HoverEvent{
|
||||
|
||||
public abstract JSONObject getEvent();
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
package me.skymc.taboolib.jsonformatter.hover;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
import me.skymc.taboolib.json.tellraw.TellrawJson;
|
||||
import me.skymc.taboolib.nms.item.DabItemUtils;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class ShowItemEvent extends HoverEvent {
|
||||
|
||||
private JSONObject object = new JSONObject();
|
||||
|
||||
public Object getItemTag(ItemStack item) {
|
||||
try {
|
||||
return DabItemUtils.getInstance().getTag(DabItemUtils.getInstance().getNMSCopy(item));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public ShowItemEvent(ItemStack is) {
|
||||
if (TabooLib.getVerint() > 10700) {
|
||||
try {
|
||||
object.put("action", "show_item");
|
||||
object.put("value", TellrawJson.create().getItemComponent(is));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
object.put("action", "show_item");
|
||||
StringBuilder tag = new StringBuilder();
|
||||
Object itemTag = getItemTag(is);
|
||||
if (itemTag != null) {
|
||||
tag.append(",tag:").append(itemTag);
|
||||
} else {
|
||||
ItemMeta im = is.getItemMeta();
|
||||
List<String> lore = im.hasLore() ? im.getLore() : new ArrayList<>();
|
||||
Map<Enchantment, Integer> enchants = is.getItemMeta().getEnchants();
|
||||
tag.append(",tag:{display:{Name:").append(enchants.size() > 0 ? "§b§o" : "§f").append(ItemUtils.getCustomName(is));
|
||||
if (lore.size() > 0) {
|
||||
tag.append(",Lore:[");
|
||||
for (String s : lore) {
|
||||
tag.append("\"").append(s).append("\",");
|
||||
}
|
||||
tag.delete(tag.length() - 1, tag.length());
|
||||
tag.append("]");
|
||||
}
|
||||
tag.append("}");
|
||||
if (enchants.size() > 0) {
|
||||
if (tag.length() > 6) {
|
||||
tag.append(",");
|
||||
}
|
||||
tag.append("ench:[");
|
||||
for (Entry<Enchantment, Integer> e : enchants.entrySet()) {
|
||||
tag.append("{id:").append(e.getKey().getId()).append(",lvl:").append(e.getValue()).append("},");
|
||||
}
|
||||
tag.delete(tag.length() - 1, tag.length());
|
||||
tag.append("]");
|
||||
}
|
||||
tag.append("}");
|
||||
}
|
||||
object.put("value", "{id:" + (TabooLib.getVerint() > 10700 ? DabItemUtils.getMinecraftName(is) : is.getTypeId()) + ",Count:" + is.getAmount() + tag.toString() + "}");
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getEvent() {
|
||||
return object;
|
||||
}
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
package me.skymc.taboolib.location;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
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("#", ".")));
|
||||
}
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package me.skymc.taboolib.message;
|
||||
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
@TListener
|
||||
public class ChatCatcher implements Listener {
|
||||
|
||||
private static HashMap<String, LinkedList<Catcher>> playerdata = new HashMap<>();
|
||||
|
||||
public static HashMap<String, LinkedList<Catcher>> getPlayerdata() {
|
||||
return playerdata;
|
||||
}
|
||||
|
||||
public static boolean contains(Player player) {
|
||||
return playerdata.containsKey(player.getName()) && playerdata.get(player.getName()).size() > 0;
|
||||
}
|
||||
|
||||
public static void call(Player player, Catcher catcher) {
|
||||
if (!playerdata.containsKey(player.getName())) {
|
||||
playerdata.put(player.getName(), new LinkedList<>());
|
||||
}
|
||||
playerdata.get(player.getName()).add(catcher.before());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void quit(PlayerQuitEvent e) {
|
||||
playerdata.remove(e.getPlayer().getName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void chat(AsyncPlayerChatEvent e) {
|
||||
if (playerdata.containsKey(e.getPlayer().getName()) && playerdata.get(e.getPlayer().getName()).size() > 0) {
|
||||
e.setCancelled(true);
|
||||
|
||||
if ("quit()".equalsIgnoreCase(e.getMessage())) {
|
||||
// 退出引导
|
||||
playerdata.get(e.getPlayer().getName()).removeFirst().cancel();
|
||||
// 清理数据
|
||||
clearData(e.getPlayer());
|
||||
} else {
|
||||
Catcher catcher = playerdata.get(e.getPlayer().getName()).getFirst();
|
||||
// 如果终止引导
|
||||
if (!catcher.after(e.getMessage())) {
|
||||
// 移除
|
||||
playerdata.get(e.getPlayer().getName()).removeFirst();
|
||||
// 清理
|
||||
clearData(e.getPlayer());
|
||||
} else {
|
||||
catcher.before();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void clearData(Player player) {
|
||||
if (playerdata.containsKey(player.getName()) && playerdata.get(player.getName()).size() == 0) {
|
||||
playerdata.remove(player.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public interface Catcher {
|
||||
|
||||
Catcher before();
|
||||
|
||||
boolean after(String message);
|
||||
|
||||
void cancel();
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
package me.skymc.taboolib.message;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@Deprecated
|
||||
public class MsgUtils {
|
||||
|
||||
public static void send(CommandSender sender, String s) {
|
||||
sender.sendMessage(Main.getPrefix() + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
public static void send(org.bukkit.entity.Player player, String s) {
|
||||
player.sendMessage(Main.getPrefix() + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
public static void send(String s) {
|
||||
Bukkit.getConsoleSender().sendMessage(Main.getPrefix() + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
public static void warn(String s) {
|
||||
warn(s, Main.getInst());
|
||||
}
|
||||
|
||||
public static void send(String s, Plugin plugin) {
|
||||
Bukkit.getConsoleSender().sendMessage("§8[§3" + plugin.getName() + "§8] §7" + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
public static void warn(String s, Plugin plugin) {
|
||||
Bukkit.getConsoleSender().sendMessage("§4[§c" + plugin.getName() + "§4][WARN #!] §c" + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void Console(String s) {
|
||||
Bukkit.getConsoleSender().sendMessage(Main.getPrefix() + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void System(String s) {
|
||||
System.out.println("[TabooLib] " + s);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void Sender(CommandSender p, String s) {
|
||||
p.sendMessage(Main.getPrefix() + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void Player(org.bukkit.entity.Player p, String s) {
|
||||
p.sendMessage(Main.getPrefix() + s.replaceAll("&", "§"));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String noPe() {
|
||||
String s = Main.getInst().getConfig().getString("NO-PERMISSION-MESSAGE").replaceAll("&", "§");
|
||||
if ("".equals(s)) {
|
||||
s = "§cCONFIG ERROR §8(NO-PERMISSION-MESSAGE)";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String noClaim(String a) {
|
||||
String s = Main.getInst().getConfig().getString("NO-CLAIM-MESSAGE").replaceAll("&", "§").replaceAll("%s%", a);
|
||||
if ("".equals(s)) {
|
||||
s = "§cCONFIG ERROR §8(NO-CLAIM-MESSAGE)";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package me.skymc.taboolib.methods;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@Deprecated
|
||||
public class MethodsUtils {
|
||||
|
||||
public static boolean checkUser(String packagename, String current) {
|
||||
return current.substring(0, 8).equals(packagename);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static <T> Object[] a(T classname, String methodname, Class[] classes, Object[] objects) {
|
||||
if (!checkUser(new String(new byte[]{'m', 'e', '.', 's', 'k', 'y', 'm', 'c'}), new Exception().getStackTrace()[1].getClassName())) {
|
||||
throw new Error("未经允许的方法调用");
|
||||
}
|
||||
|
||||
Class<?> clazz = classname.getClass();
|
||||
Method method = null;
|
||||
try {
|
||||
method = clazz.getDeclaredMethod(methodname, classes);
|
||||
method.setAccessible(true);
|
||||
return new Object[]{method.invoke(classname, objects)};
|
||||
} catch (SecurityException | InvocationTargetException | IllegalAccessException | NoSuchMethodException | IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> Object b(T classname, String fieldname) {
|
||||
if (!checkUser(new String(new byte[]{'m', 'e', '.', 's', 'k', 'y', 'm', 'c'}), new Exception().getStackTrace()[1].getClassName())) {
|
||||
throw new Error("未经允许的方法调用");
|
||||
}
|
||||
|
||||
Class<?> clazz = classname.getClass();
|
||||
Field field = null;
|
||||
Object object = null;
|
||||
try {
|
||||
field = clazz.getDeclaredField(fieldname);
|
||||
field.setAccessible(true);
|
||||
object = field.get(classname);
|
||||
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return object;
|
||||
}
|
||||
}
|
@ -1,329 +0,0 @@
|
||||
package me.skymc.taboolib.mysql;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@Deprecated
|
||||
public class MysqlConnection {
|
||||
|
||||
/**
|
||||
* Create by Bkm016
|
||||
* <p>
|
||||
* 2017-7-22 23:25:55
|
||||
*/
|
||||
|
||||
private Connection connection = null;
|
||||
|
||||
private Statement statement = null;
|
||||
|
||||
private Boolean isConnection = false;
|
||||
|
||||
public MysqlConnection(String ip, String port, String table, String user, String pass) {
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
System("载入 MYSQL 系统库成功");
|
||||
} catch (ClassNotFoundException e) {
|
||||
System("载入 MYSQL 系统库失败");
|
||||
}
|
||||
|
||||
// TODO STATE THE URL AND CONNECTION
|
||||
String url = "jdbc:mysql://" + ip + ":" + port + "/" + table + "?characterEncoding=utf-8";
|
||||
|
||||
// TODO CONNECTION
|
||||
try {
|
||||
connection = DriverManager.getConnection(url, user, pass);
|
||||
statement = connection.createStatement();
|
||||
|
||||
isConnection = true;
|
||||
System("连接 MYSQL 数据库成功");
|
||||
|
||||
Executors.newFixedThreadPool(1).execute(() -> {
|
||||
while (isConnection) {
|
||||
try {
|
||||
if (connection.isClosed()) {
|
||||
connection = DriverManager.getConnection(url, user, pass);
|
||||
System("数据库连接关闭, 正在重新连接... [Connection Closed]");
|
||||
}
|
||||
|
||||
Thread.sleep(30000);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (SQLException e) {
|
||||
System("连接 MYSQL 数据库失败 详细信息: " + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
try {
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
isConnection = false;
|
||||
System("结束 MYSQL 连接成功");
|
||||
} catch (SQLException e) {
|
||||
System("结束 MYSQL 连接失败 详细信息: " + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
public Boolean isConnection() {
|
||||
try {
|
||||
if (statement.isClosed()) {
|
||||
statement = null;
|
||||
statement = connection.createStatement();
|
||||
System("数据库连接关闭, 正在重新连接... [Statement Closed]");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return isConnection;
|
||||
}
|
||||
|
||||
public Statement getStatement() {
|
||||
return this.statement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_CreateTable("tablename", new String[] { "Player" });
|
||||
*/
|
||||
public void SQL_CreateTable(String table, String[] list) {
|
||||
if (!isConnection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
if (i + 1 < list.length) {
|
||||
stringBuilder.append("`").append(checkString(list[i])).append("` varchar(255), ");
|
||||
} else {
|
||||
stringBuilder.append("`").append(checkString(list[i])).append("` varchar(255)");
|
||||
}
|
||||
}
|
||||
String url = "CREATE TABLE IF NOT EXISTS `" + table + "` ( " + stringBuilder + " )";
|
||||
|
||||
try {
|
||||
getStatement().execute(url);
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_SetValues("tablename", new String[] { "Player" }, new String[] { "BlackSKY" });
|
||||
*/
|
||||
public void SQL_SetValues(String table, String[] list, String[] values) {
|
||||
if (!isConnection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder listbuilder = new StringBuilder();
|
||||
StringBuilder valuebuilder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
if (i + 1 < list.length) {
|
||||
listbuilder.append("`").append(checkString(list[i])).append("`, ");
|
||||
valuebuilder.append("'").append(checkString(values[i])).append("', ");
|
||||
} else {
|
||||
listbuilder.append("`").append(checkString(list[i])).append("`");
|
||||
valuebuilder.append("'").append(checkString(values[i])).append("'");
|
||||
}
|
||||
}
|
||||
|
||||
String url = "INSERT INTO `" + table + "` ( " + listbuilder + " ) VALUES ( " + valuebuilder + " )";
|
||||
try {
|
||||
getStatement().execute(url);
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
for (int i = 0; i < e.getStackTrace().length && i < 5; i++) {
|
||||
String name = e.getStackTrace()[i].getClassName();
|
||||
|
||||
System("(" + i + ")位置: " + name.substring(0, name.lastIndexOf(".")));
|
||||
System(" 类名: " + e.getStackTrace()[i].getFileName().replaceAll(".java", ""));
|
||||
System(" 行数: " + e.getStackTrace()[i].getLineNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_GetValue("tablename", "Player", "BlackSKY", "Value");
|
||||
*/
|
||||
public String SQL_GetValue(String table, String line, String linevalue, String row) {
|
||||
if (!isConnection()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String url = "SELECT * FROM " + checkString(table) + " WHERE `" + checkString(line) + "` = '" + checkString(linevalue) + "'";
|
||||
try {
|
||||
ResultSet resultSet = getStatement().executeQuery(url);
|
||||
while (resultSet.next()) {
|
||||
return resultSet.getString(row);
|
||||
}
|
||||
resultSet.close();
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_GetValues("tablename", "Player");
|
||||
*/
|
||||
public List<String> SQL_GetValues(String table, String row) {
|
||||
if (!isConnection()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
|
||||
String url = "SELECT * FROM " + checkString(table);
|
||||
try {
|
||||
ResultSet resultSet = getStatement().executeQuery(url);
|
||||
while (resultSet.next()) {
|
||||
if (resultSet.getString(row) == null) {
|
||||
continue;
|
||||
}
|
||||
list.add(resultSet.getString(row));
|
||||
}
|
||||
resultSet.close();
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_isExists("tablename", "Player", "BlackSKY");
|
||||
*/
|
||||
public boolean SQL_isExists(String table, String row, String value) {
|
||||
if (!isConnection()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String url = "SELECT * FROM " + checkString(table) + " WHERE `" + checkString(row) + "` = '" + checkString(value) + "'";
|
||||
try {
|
||||
ResultSet resultSet = getStatement().executeQuery(url);
|
||||
while (resultSet.next()) {
|
||||
return true;
|
||||
}
|
||||
resultSet.close();
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_UpdateValue("tablename", "Player", "BlackSKY", "Value", "10")
|
||||
*/
|
||||
public void SQL_UpdateValue(String table, String line, String linevalue, String row, String value) {
|
||||
if (!isConnection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String url = "UPDATE `" + checkString(table) + "` SET `" + checkString(row) + "` = '" + checkString(value) + "' WHERE `" + checkString(line) + "` = '" + checkString(linevalue) + "'";
|
||||
try {
|
||||
getStatement().execute(url);
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_DeleteValue("tablename", "BlackSKY");
|
||||
*/
|
||||
public void SQL_DeleteValue(String table, String line, String linevalue) {
|
||||
if (!isConnection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String url = "DELETE FROM `" + checkString(table) + "` WHERE `" + checkString(line) + "` = '" + checkString(linevalue) + "'";
|
||||
try {
|
||||
getStatement().execute(url);
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: SQL_ClearTable("tablename");
|
||||
* @deprecated 即将过期
|
||||
*/
|
||||
@Deprecated
|
||||
public void SQL_ClearTable(String table) {
|
||||
if (!isConnection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String url = "TRUNCATE TABLE `" + checkString(table) + "`";
|
||||
try {
|
||||
getStatement().execute(url);
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
}
|
||||
|
||||
public void SQL_execute(String url) {
|
||||
if (!isConnection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
getStatement().execute(url);
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet SQL_executeQuery(String url) {
|
||||
if (!isConnection()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return getStatement().executeQuery(url);
|
||||
} catch (SQLException e) {
|
||||
System("执行 MYSQL 任务出错 详细信息: " + e.getLocalizedMessage());
|
||||
System("任务: " + url);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void SQL_clearTable(String table) {
|
||||
SQL_execute("DELETE FROM " + checkString(table) + ";");
|
||||
}
|
||||
|
||||
public void SQL_deleteTable(String table) {
|
||||
SQL_execute("DROP TABLE " + checkString(table) + ";");
|
||||
}
|
||||
|
||||
private void System(String string) {
|
||||
System.out.println("[TabooLib - MYSQL] " + string);
|
||||
}
|
||||
|
||||
private String checkString(String string) {
|
||||
return string.replace("`", "").replace("'", "").replace("\"", "");
|
||||
}
|
||||
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package me.skymc.taboolib.mysql;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.mysql.protect.MySQLConnection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class MysqlUtils {
|
||||
|
||||
public final static CopyOnWriteArrayList<MySQLConnection> CONNECTIONS = new CopyOnWriteArrayList<>();
|
||||
|
||||
@Deprecated
|
||||
public static MysqlConnection getMysqlConnectionFromConfiguration(FileConfiguration conf, String key) {
|
||||
return new MysqlConnection(conf.getString(key + ".host"), conf.getString(key + ".port"), conf.getString(key + ".database"), conf.getString(key + ".user"), conf.getString(key + ".pass"));
|
||||
}
|
||||
|
||||
public static MySQLConnection getMySQLConnectionFromConfiguration(FileConfiguration conf, String key) {
|
||||
return getMySQLConnectionFromConfiguration(conf, key, 60, Main.getInst());
|
||||
}
|
||||
|
||||
public static MySQLConnection getMySQLConnectionFromConfiguration(FileConfiguration conf, String key, int recheck, Plugin plugin) {
|
||||
MySQLConnection connection = getMySQLConnectionExists(conf, key);
|
||||
if (connection == null) {
|
||||
connection = new MySQLConnection(conf.getString(key + ".url"), conf.getString(key + ".user"), conf.getString(key + ".port"), conf.getString(key + ".password"), conf.getString(key + ".database"), recheck, plugin);
|
||||
if (connection.isConnection()) {
|
||||
CONNECTIONS.add(connection);
|
||||
TLocale.Logger.info("MYSQL-CONNECTION.SUCCESS-REGISTERED", plugin.getName());
|
||||
}
|
||||
} else {
|
||||
TLocale.Logger.info("MYSQL-CONNECTION.SUCCESS-REGISTERED-EXISTS", plugin.getName(), connection.getPlugin().getName());
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
private static MySQLConnection getMySQLConnectionExists(FileConfiguration conf, String key) {
|
||||
return CONNECTIONS.stream().filter(connection -> isSameConnection(conf, key, connection)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private static boolean isSameConnection(FileConfiguration conf, String key, MySQLConnection connection) {
|
||||
return conversionHost(connection.getUrl()).equals(conversionHost(conf.getString(key + ".url", "localhost"))) && connection.getDatabase().equals(conf.getString(key + ".database"));
|
||||
}
|
||||
|
||||
private static String conversionHost(String host) {
|
||||
return "localhost".equals(host) ? "127.0.0.1" : host;
|
||||
}
|
||||
}
|
@ -1,773 +0,0 @@
|
||||
package me.skymc.taboolib.mysql.protect;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
@Deprecated
|
||||
public class MySQLConnection {
|
||||
|
||||
private String url;
|
||||
private String user;
|
||||
private String port;
|
||||
private String password;
|
||||
private String database;
|
||||
private String connectionUrl;
|
||||
private Connection connection;
|
||||
private Plugin plugin;
|
||||
private boolean fallReconnection = true;
|
||||
private int recheck = 10;
|
||||
private Thread recheckThread;
|
||||
|
||||
public MySQLConnection(String url, String user, String port, String password, String database) {
|
||||
this(url, user, port, password, database, 10, Main.getInst());
|
||||
}
|
||||
|
||||
public MySQLConnection(String url, String user, String port, String password, String database, int recheck, Plugin plugin) {
|
||||
// 检查驱动
|
||||
if (!loadDriverMySQL()) {
|
||||
TLocale.Logger.error("MYSQL-CONNECTION.FAIL-NOTFOUND-DRIVE");
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置信息
|
||||
this.plugin = plugin;
|
||||
this.recheck = recheck;
|
||||
|
||||
// 设置数据
|
||||
this.url = url == null ? "localhost" : url;
|
||||
this.user = user == null ? "root" : user;
|
||||
this.port = port == null ? "3306" : port;
|
||||
this.password = password == null ? "" : password;
|
||||
this.database = database == null ? "test" : database;
|
||||
this.connectionUrl = Strings.replaceWithOrder("jdbc:mysql://{0}:{1}/{2}?characterEncoding=utf-8&useSSL=false", this.url, this.port, this.database);
|
||||
|
||||
// 连接数据库
|
||||
connect();
|
||||
|
||||
// 断线检测
|
||||
recheckThread = new Thread(() -> {
|
||||
while (!Main.isDisable()) {
|
||||
try {
|
||||
Thread.sleep(getReCheckSeconds() * 1000);
|
||||
|
||||
if (connection == null) {
|
||||
TLocale.Logger.error("MYSQL-CONNECTION.FAIL-NOTFOUND-CONNECTION", plugin.getName());
|
||||
} else {
|
||||
isExists("taboolib");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.error("MYSQL-CONNECTION.FAIL-COMMAND-NORMAL", e.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 启动检测
|
||||
if (isConnection()) {
|
||||
recheckThread.start();
|
||||
TLocale.Logger.info("MYSQL-CONNECTION.SUCCESS-REGISTERED-LISTENER");
|
||||
}
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public String getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public String getConnectionUrl() {
|
||||
return connectionUrl;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public void setPlugin(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public boolean isFallReconnection() {
|
||||
return fallReconnection;
|
||||
}
|
||||
|
||||
public void setFallReconnection(boolean fallReconnection) {
|
||||
this.fallReconnection = fallReconnection;
|
||||
}
|
||||
|
||||
public int getRecheck() {
|
||||
return recheck;
|
||||
}
|
||||
|
||||
public Thread getRecheckThread() {
|
||||
return recheckThread;
|
||||
}
|
||||
|
||||
public int getReCheckSeconds() {
|
||||
return recheck;
|
||||
}
|
||||
|
||||
public void setReCheckSeconds(int s) {
|
||||
this.recheck = s;
|
||||
}
|
||||
|
||||
public boolean isConnection() {
|
||||
try {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void closeConnection() {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
recheckThread.stop();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteTable(String name) {
|
||||
return execute("drop table if exists " + name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 2018年1月17日 新增, TabooLib 版本 3.25
|
||||
*/
|
||||
public boolean truncateTable(String name) {
|
||||
return execute("truncate table " + name);
|
||||
}
|
||||
|
||||
public boolean clearTable(String name) {
|
||||
return execute("delete from " + name);
|
||||
}
|
||||
|
||||
public boolean renameTable(String name, String newName) {
|
||||
return execute(Strings.replaceWithOrder("rename table `{0}` to `{1}`", name, newName));
|
||||
}
|
||||
|
||||
public boolean deleteColumn(String name, String column) {
|
||||
return execute(Strings.replaceWithOrder("alter table `{0}` drop `{1}`", name, column));
|
||||
}
|
||||
|
||||
public void addColumn(String name, Column... columns) {
|
||||
Arrays.stream(columns).map(column -> Strings.replaceWithOrder("alter table {0} add {1}", name, column.toString())).forEach(this::execute);
|
||||
}
|
||||
|
||||
public boolean addColumn(String name, String column) {
|
||||
if (!column.contains("/")) {
|
||||
return execute(Strings.replaceWithOrder("alter table {0} add `{1}` text", name, column));
|
||||
}
|
||||
return execute(Strings.replaceWithOrder("alter table {0} add `{1}` {2}", name, column.split("/")[0], column.split("/")[1]));
|
||||
}
|
||||
|
||||
public boolean editColumn(String name, String oldColumn, Column newColumn) {
|
||||
return execute(Strings.replaceWithOrder("alter table {0} change `{1}` {2}", name, oldColumn, newColumn.toString()));
|
||||
}
|
||||
|
||||
public boolean editColumn(String name, String oldColumn, String newColumn) {
|
||||
if (!newColumn.contains("/")) {
|
||||
return execute(Strings.replaceWithOrder("alter table {0} change `{1}` `{2}` text", name, oldColumn, newColumn));
|
||||
}
|
||||
return execute(Strings.replaceWithOrder("alter table {0} change `{1}` `{2}` {3}", name, oldColumn, newColumn.split("/")[0], newColumn.split("/")[1]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param columnValue 参考值
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean deleteValue(String name, String column, Object columnValue) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("delete from `{0}` where `{1}` = ?", name, column));
|
||||
preparedStatement.setObject(1, columnValue);
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(null, preparedStatement);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入数据
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param columnValue 参考值
|
||||
* @param valueColumn 数据列
|
||||
* @param value 数据值
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean setValue(String name, String column, Object columnValue, String valueColumn, Object value) {
|
||||
return setValue(name, column, columnValue, valueColumn, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入数据
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param columnValue 参考值
|
||||
* @param valueColumn 数据列
|
||||
* @param value 数据值
|
||||
* @param append 是否追加(数据列类型必须为数字)
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean setValue(String name, String column, Object columnValue, String valueColumn, Object value, boolean append) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
if (append) {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("update `{0}` set `{1}` = `{2}` + ? where `{3}` = ?", name, valueColumn, valueColumn, column));
|
||||
} else {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("update `{0}` set `{1}` = ? where `{2}` = ?", name, valueColumn, column));
|
||||
}
|
||||
preparedStatement.setObject(1, value);
|
||||
preparedStatement.setObject(2, columnValue);
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(null, preparedStatement);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入数据
|
||||
*
|
||||
* @param name 名称
|
||||
* @param values 值
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean intoValue(String name, Object... values) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Arrays.stream(values).map(value -> "?, ").forEach(sb::append);
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("insert into `{0}` values(null, {1})", name, sb.substring(0, sb.length() - 2)));
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
preparedStatement.setObject(i + 1, values[i]);
|
||||
}
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(null, preparedStatement);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数据表
|
||||
*
|
||||
* @param name 名称
|
||||
* @param columns 列表
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean createTable(String name, Column... columns) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Arrays.stream(columns).forEach(column -> sb.append(column.toString()).append(", "));
|
||||
return execute(Strings.replaceWithOrder("create table if not exists {0} (id int(1) not null primary key auto_increment, {1})", name, sb.substring(0, sb.length() - 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数据表
|
||||
*
|
||||
* @param name 名称
|
||||
* @param columns 列表
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean createTable(String name, String... columns) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String column : columns) {
|
||||
if (!column.contains("/")) {
|
||||
sb.append("`").append(column).append("` text, ");
|
||||
} else {
|
||||
sb.append("`").append(column.split("/")[0]).append("` ").append(column.split("/")[1]).append(", ");
|
||||
}
|
||||
}
|
||||
return execute(Strings.replaceWithOrder("create table if not exists {0} (id int(1) not null primary key auto_increment, {1})", name, sb.substring(0, sb.length() - 2)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数据表是否存在
|
||||
*
|
||||
* @param name 名称
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExists(String name) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement("select table_name FROM information_schema.TABLES where table_name = ?");
|
||||
preparedStatement.setString(1, name);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数据是否存在
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 列表名
|
||||
* @param columnValue 列表值
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExists(String name, String column, Object columnValue) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` where `{1}` = ?", name, column));
|
||||
preparedStatement.setObject(1, columnValue);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有列表名称(不含主键)
|
||||
*
|
||||
* @param name 名称
|
||||
* @return {@link List}
|
||||
*/
|
||||
public List<String> getColumns(String name) {
|
||||
return getColumns(name, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有列表名称
|
||||
*
|
||||
* @param name 名称
|
||||
* @param primary 是否获取主键
|
||||
* @return {@link List}
|
||||
*/
|
||||
public List<String> getColumns(String name, boolean primary) {
|
||||
List<String> list = new ArrayList<>();
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement("select column_name from information_schema.COLUMNS where table_name = ?");
|
||||
preparedStatement.setString(1, name);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
list.add(resultSet.getString(1));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
// 是否获取主键
|
||||
if (!primary) {
|
||||
list.remove("id");
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单项数据
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param columnValue 参考值
|
||||
* @param valueColumn 数据列
|
||||
* @return Object
|
||||
*/
|
||||
public Object getValue(String name, String column, Object columnValue, String valueColumn) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` where `{1}` = ? limit 1", name, column));
|
||||
preparedStatement.setObject(1, columnValue);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
return resultSet.getObject(valueColumn);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单项数据(根据主键倒叙排列后的最后一项)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param columnValue 参考值
|
||||
* @param valueColumn 数据列
|
||||
* @return Object
|
||||
*/
|
||||
public Object getValueLast(String name, String column, Object columnValue, String valueColumn) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` where `{1}` = ? order by id desc limit 1", name, column));
|
||||
preparedStatement.setObject(1, columnValue);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
return resultSet.getObject(valueColumn);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多项数据(根据主键倒叙排列后的最后一项)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param columnValue 参考值
|
||||
* @param valueColumn 数据列
|
||||
* @return {@link HashMap}
|
||||
*/
|
||||
public HashMap<String, Object> getValueLast(String name, String column, Object columnValue, String... valueColumn) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` where `{1}` = ? order by id desc limit 1", name, column));
|
||||
preparedStatement.setObject(1, columnValue);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
for (String _column : valueColumn) {
|
||||
map.put(_column, resultSet.getObject(_column));
|
||||
}
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多项数据(单项多列)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param columnValue 参考值
|
||||
* @param valueColumn 数据列
|
||||
* @return {@link HashMap}
|
||||
*/
|
||||
public HashMap<String, Object> getValue(String name, String column, Object columnValue, String... valueColumn) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` where `{1}` = ? limit 1", name, column));
|
||||
preparedStatement.setObject(1, columnValue);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
for (String _column : valueColumn) {
|
||||
map.put(_column, resultSet.getObject(_column));
|
||||
}
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多项数据(单列多列)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param size 获取数量(-1 为无限制)
|
||||
* @return {@link List}
|
||||
*/
|
||||
public List<Object> getValues(String name, String column, int size) {
|
||||
return getValues(name, column, size, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多项数据(单列多列)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param column 参考列
|
||||
* @param size 获取数量(-1 位无限制)
|
||||
* @param desc 是否倒序
|
||||
* @return {@link List}
|
||||
*/
|
||||
public List<Object> getValues(String name, String column, int size, boolean desc) {
|
||||
List<Object> list = new LinkedList<>();
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
if (desc) {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` order by `{1}` desc {2}", name, column, size < 0 ? "" : " limit " + size));
|
||||
} else {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` order by `{1}` {2}", name, column, size < 0 ? "" : " limit " + size));
|
||||
}
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
list.add(resultSet.getObject(column));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多线数据(多项多列)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param sortColumn 参考列(该列类型必须为数字)
|
||||
* @param size 获取数量(-1 为无限制)
|
||||
* @param valueColumn 获取数据列
|
||||
* @return {@link LinkedList}
|
||||
*/
|
||||
public LinkedList<HashMap<String, Object>> getValues(String name, String sortColumn, int size, String... valueColumn) {
|
||||
return getValues(name, sortColumn, size, false, valueColumn);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多项数据(多项多列)
|
||||
*
|
||||
* @param name 名称
|
||||
* @param sortColumn 参考列(该列类型必须为数字)
|
||||
* @param size 获取数量(-1 为无限制)
|
||||
* @param desc 是否倒序
|
||||
* @param valueColumn 获取数据列
|
||||
* @return {@link LinkedList}
|
||||
*/
|
||||
public LinkedList<HashMap<String, Object>> getValues(String name, String sortColumn, int size, boolean desc, String... valueColumn) {
|
||||
LinkedList<HashMap<String, Object>> list = new LinkedList<>();
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
if (desc) {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` order by `{1}` desc{2}", name, sortColumn, size < 0 ? "" : " limit " + size));
|
||||
} else {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder("select * from `{0}` order by `{1}`{2}", name, sortColumn, size < 0 ? "" : " limit " + size));
|
||||
}
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
for (String _column : valueColumn) {
|
||||
map.put(_column, resultSet.getObject(_column));
|
||||
}
|
||||
list.add(map);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
freeResult(resultSet, preparedStatement);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public boolean execute(String sql) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.execute();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
printExceptionDetail(e);
|
||||
return false;
|
||||
} finally {
|
||||
freeResult(null, preparedStatement);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean connect() {
|
||||
TLocale.Logger.info("MYSQL-CONNECTION.NOTIFY-CONNECTING", connectionUrl);
|
||||
try {
|
||||
long time = System.currentTimeMillis();
|
||||
connection = DriverManager.getConnection(connectionUrl, this.user, this.password);
|
||||
TLocale.Logger.info("MYSQL-CONNECTION.NOTIFY-CONNECTED", String.valueOf(System.currentTimeMillis() - time));
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
printExceptionDetail(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void print(String message) {
|
||||
System.out.println("[TabooLib - MySQL] " + message);
|
||||
}
|
||||
|
||||
private void printException(Exception e) {
|
||||
TLocale.Logger.error("MYSQL-CONNECTION.FAIL-COMMAND-NORMAL", e.toString());
|
||||
reconnection(e);
|
||||
}
|
||||
|
||||
private void printExceptionDetail(SQLException e) {
|
||||
TLocale.Logger.error("MYSQL-CONNECTION.FAIL-COMMAND-DETAIL", String.valueOf(e.getErrorCode()), e.toString());
|
||||
reconnection(e);
|
||||
}
|
||||
|
||||
private void reconnection(Exception e) {
|
||||
if (fallReconnection && e.getMessage().contains("closed")) {
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean loadDriverMySQL() {
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
return true;
|
||||
} catch (ClassNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void freeResult(ResultSet resultSet, PreparedStatement preparedStatement) {
|
||||
try {
|
||||
if (resultSet != null) {
|
||||
resultSet.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
if (preparedStatement != null) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
public enum ColumnInteger {
|
||||
|
||||
TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT
|
||||
}
|
||||
|
||||
public enum ColumnFloat {
|
||||
|
||||
FLOAT, DOUBLE
|
||||
}
|
||||
|
||||
public enum ColumnChar {
|
||||
|
||||
CHAR, VARCHAR
|
||||
}
|
||||
|
||||
public enum ColumnString {
|
||||
|
||||
TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT
|
||||
}
|
||||
|
||||
public static class Column {
|
||||
|
||||
private String name;
|
||||
private Object type;
|
||||
private int a;
|
||||
private int b;
|
||||
|
||||
public Column(String name) {
|
||||
this.name = name;
|
||||
this.type = ColumnString.TEXT;
|
||||
}
|
||||
|
||||
public Column(String name, ColumnInteger type) {
|
||||
this(name);
|
||||
this.type = type;
|
||||
this.a = 12;
|
||||
}
|
||||
|
||||
public Column(String name, ColumnInteger type, int m) {
|
||||
this(name);
|
||||
this.type = type;
|
||||
this.a = m;
|
||||
}
|
||||
|
||||
public Column(String name, ColumnFloat type, int m, int d) {
|
||||
this(name);
|
||||
this.type = type;
|
||||
this.a = m;
|
||||
this.b = d;
|
||||
}
|
||||
|
||||
public Column(String name, ColumnChar type, int n) {
|
||||
this(name);
|
||||
this.type = type;
|
||||
this.a = n;
|
||||
}
|
||||
|
||||
public Column(String name, ColumnString type) {
|
||||
this(name);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (type instanceof ColumnInteger || type instanceof ColumnChar) {
|
||||
return Strings.replaceWithOrder("`{0}` {1}({2})", name, type.toString().toLowerCase(), a);
|
||||
} else if (type instanceof ColumnFloat) {
|
||||
return Strings.replaceWithOrder("`{0}` {1}({2},{3})", name, type.toString().toLowerCase(), a, b);
|
||||
} else {
|
||||
return Strings.replaceWithOrder("`{0}` {1}", name, type.toString().toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,379 +0,0 @@
|
||||
package me.skymc.taboolib.nms;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* @author Unknown
|
||||
*/
|
||||
public class NMSUtils {
|
||||
|
||||
public static Class<?> c = getOBCClass("block.CraftBlock");
|
||||
public static Method m = getMethodSilent(c, "getNMSBlock");
|
||||
|
||||
public static String getVersion() {
|
||||
String name = Bukkit.getServer().getClass().getPackage().getName();
|
||||
return name.substring(name.lastIndexOf('.') + 1) + ".";
|
||||
}
|
||||
|
||||
public static <T extends Enum<T>> T getEnumSilent(Class<T> enumType, String str) {
|
||||
try {
|
||||
return Enum.valueOf(enumType, str);
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> getClassWithException(String name) throws Exception {
|
||||
return Class.forName(name);
|
||||
}
|
||||
|
||||
public static Class<?> getClass(String name) {
|
||||
try {
|
||||
return getClassWithException(name);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class<?> getClass(String... strings) {
|
||||
for (String s : strings) {
|
||||
try {
|
||||
return getClassWithException(s);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class<?> getClassSilent(String name) {
|
||||
try {
|
||||
return getClassWithException(name);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClassWithException(String className) throws Exception {
|
||||
return Class.forName("net.minecraft.server." + getVersion() + className);
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClass(String className) {
|
||||
try {
|
||||
return getNMSClassWithException(className);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClassSilent(String className) {
|
||||
try {
|
||||
return getNMSClassWithException(className);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClass(String className, String embedded) {
|
||||
try {
|
||||
return getNMSClassWithException(className);
|
||||
} catch (Exception e) {
|
||||
return getInnerClassSilent(getNMSClassSilent(embedded), className);
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClassSilent(String className, String embedded) {
|
||||
try {
|
||||
return getNMSClassWithException(className);
|
||||
} catch (Exception e) {
|
||||
return getInnerClassSilent(getNMSClassSilent(embedded), className);
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> getOBCClassWithException(String className) throws Exception {
|
||||
return Class.forName("org.bukkit.craftbukkit." + getVersion() + className);
|
||||
}
|
||||
|
||||
public static Class<?> getOBCClass(String className) {
|
||||
try {
|
||||
return getOBCClassWithException(className);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class<?> getOBCClassSilent(String className) {
|
||||
try {
|
||||
return getOBCClassWithException(className);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object getHandle(Object obj) {
|
||||
try {
|
||||
return getMethodWithException(obj.getClass(), "getHandle").invoke(obj);
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getHandleSilent(Object obj) {
|
||||
try {
|
||||
return getMethodWithException(obj.getClass(), "getHandle").invoke(obj);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getBlockHandleWithException(Object obj) throws Exception {
|
||||
return m.invoke(obj);
|
||||
}
|
||||
|
||||
public static Object getBlockHandle(Object obj) {
|
||||
try {
|
||||
return m.invoke(obj);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getBlockHandleSilent(Object obj) {
|
||||
try {
|
||||
return m.invoke(obj);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Field getFieldWithException(Class<?> clazz, String name) throws Exception {
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (field.getName().equals(name)) {
|
||||
field.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
int modifiers = modifiersField.getInt(field);
|
||||
modifiers &= ~Modifier.FINAL;
|
||||
modifiersField.setInt(field, modifiers);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
for (Field field : clazz.getFields()) {
|
||||
if (field.getName().equals(name)) {
|
||||
field.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
int modifiers = modifiersField.getInt(field);
|
||||
modifiers &= ~Modifier.FINAL;
|
||||
modifiersField.setInt(field, modifiers);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
throw new Exception("Field Not Found");
|
||||
}
|
||||
|
||||
public static Field getFieldSilent(Class<?> clazz, String... names) {
|
||||
for (String name : names) {
|
||||
try {
|
||||
return getFieldWithException(clazz, name);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Field getFieldSilent(Class<?> clazz, String name) {
|
||||
try {
|
||||
return getFieldWithException(clazz, name);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Field getField(Class<?> clazz, String... names) {
|
||||
for (String name : names) {
|
||||
try {
|
||||
return getFieldWithException(clazz, name);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Field getFieldOfTypeWithException(Class<?> clazz, Class<?> type, String name) throws Exception {
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (field.getName().equals(name) && field.getType().equals(type)) {
|
||||
field.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
int modifiers = modifiersField.getInt(field);
|
||||
modifiers &= ~Modifier.FINAL;
|
||||
modifiersField.setInt(field, modifiers);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
for (Field field : clazz.getFields()) {
|
||||
if (field.getName().equals(name) && field.getType().equals(type)) {
|
||||
field.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
int modifiers = modifiersField.getInt(field);
|
||||
modifiers &= ~Modifier.FINAL;
|
||||
modifiersField.setInt(field, modifiers);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
throw new Exception("Field Not Found");
|
||||
}
|
||||
|
||||
public static Field getFieldOfType(Class<?> clazz, Class<?> type, String name) {
|
||||
try {
|
||||
return getFieldOfTypeWithException(clazz, type, name);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Field getFirstFieldOfTypeWithException(Class<?> clazz, Class<?> type) throws Exception {
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (field.getType().equals(type)) {
|
||||
field.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
int modifiers = modifiersField.getInt(field);
|
||||
modifiers &= ~Modifier.FINAL;
|
||||
modifiersField.setInt(field, modifiers);
|
||||
return field;
|
||||
}
|
||||
}
|
||||
throw new Exception("Field Not Found");
|
||||
}
|
||||
|
||||
public static Field getFirstFieldOfType(Class<?> clazz, Class<?> type) {
|
||||
try {
|
||||
return getFirstFieldOfTypeWithException(clazz, type);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Field getLastFieldOfTypeWithException(Class<?> clazz, Class<?> type) throws Exception {
|
||||
Field field = null;
|
||||
for (Field f : clazz.getDeclaredFields()) {
|
||||
if (f.getType().equals(type)) {
|
||||
field = f;
|
||||
}
|
||||
}
|
||||
if (field == null) {
|
||||
throw new Exception("Field Not Found");
|
||||
}
|
||||
field.setAccessible(true);
|
||||
Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
int modifiers = modifiersField.getInt(field);
|
||||
modifiers &= ~Modifier.FINAL;
|
||||
modifiersField.setInt(field, modifiers);
|
||||
return field;
|
||||
}
|
||||
|
||||
public static Field getLastFieldOfType(Class<?> clazz, Class<?> type) {
|
||||
try {
|
||||
return getLastFieldOfTypeWithException(clazz, type);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Method getMethodWithException(Class<?> clazz, String name, Class<?>... args) throws Exception {
|
||||
for (Method m : clazz.getDeclaredMethods()) {
|
||||
if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0 || classListEqual(args, m.getParameterTypes()))) {
|
||||
m.setAccessible(true);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
for (Method m : clazz.getMethods()) {
|
||||
if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0 || classListEqual(args, m.getParameterTypes()))) {
|
||||
m.setAccessible(true);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
throw new Exception("Method Not Found");
|
||||
}
|
||||
|
||||
public static Method getMethodSilent(Class<?> clazz, String name, Class<?>... args) {
|
||||
try {
|
||||
return getMethodWithException(clazz, name, args);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean classListEqual(Class<?>[] l1, Class<?>[] l2) {
|
||||
return l1.length == l2.length && IntStream.range(0, l1.length).noneMatch(i -> l1[i] != l2[i]);
|
||||
}
|
||||
|
||||
public static Class<?> getInnerClassWithException(Class<?> c, String className) throws Exception {
|
||||
for (Class<?> cl : c.getDeclaredClasses()) {
|
||||
if (cl.getSimpleName().equals(className)) {
|
||||
return cl;
|
||||
}
|
||||
}
|
||||
throw new Exception("Inner Class Not Found");
|
||||
}
|
||||
|
||||
public static Class<?> getInnerClass(Class<?> c, String className) {
|
||||
try {
|
||||
return getInnerClassWithException(c, className);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class<?> getInnerClassSilent(Class<?> c, String className) {
|
||||
try {
|
||||
return getInnerClassWithException(c, className);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... args) throws Exception {
|
||||
for (Constructor<?> c : clazz.getDeclaredConstructors()) {
|
||||
if (args.length == 0 && c.getParameterTypes().length == 0 || classListEqual(args, c.getParameterTypes())) {
|
||||
c.setAccessible(true);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
for (Constructor<?> c : clazz.getConstructors()) {
|
||||
if (args.length == 0 && c.getParameterTypes().length == 0 || classListEqual(args, c.getParameterTypes())) {
|
||||
c.setAccessible(true);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
throw new Exception("Constructor Not Found");
|
||||
}
|
||||
|
||||
public static Constructor<?> getConstructorSilent(Class<?> clazz, Class<?>... args) {
|
||||
try {
|
||||
return getConstructor(clazz, args);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static Enum<?> getEnum(final String value, final Class enumClass) {
|
||||
return Enum.valueOf(enumClass, value);
|
||||
}
|
||||
}
|
@ -1,453 +0,0 @@
|
||||
package me.skymc.taboolib.nms.item;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.json.JSONArray;
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import me.skymc.taboolib.nms.item.impl._164ItemUtils;
|
||||
import me.skymc.taboolib.nms.item.impl._1710ItemUtils;
|
||||
import me.skymc.taboolib.nms.item.impl._194ItemUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DabItemUtils {
|
||||
|
||||
private static IDabItemUtils inst = load();
|
||||
|
||||
private static IDabItemUtils load(){
|
||||
ItemStack is = new ItemStack(Material.DIAMOND_SWORD);
|
||||
ItemMeta im = is.getItemMeta();
|
||||
im.addEnchant(Enchantment.KNOCKBACK, 20, true);
|
||||
is.setItemMeta(im);
|
||||
try{
|
||||
IDabItemUtils inst = new _194ItemUtils();
|
||||
inst.convertItemStackToJSON(is);
|
||||
MsgUtils.send("载入 1.9.4 Spigot 物品工具成功!");
|
||||
return inst;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try{
|
||||
_1710ItemUtils inst = new _1710ItemUtils();
|
||||
inst.convertItemStackToJSON(is);
|
||||
MsgUtils.send("载入 1.7.10 Cauldron 物品工具成功!");
|
||||
return inst;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try{
|
||||
IDabItemUtils inst = new _164ItemUtils();
|
||||
inst.convertItemStackToJSON(is);
|
||||
MsgUtils.send("载入 1.6.4 Cauldron 物品工具成功!");
|
||||
return inst;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
MsgUtils.send("&4物品工具载入失败, 插件已关闭!");
|
||||
Bukkit.getPluginManager().disablePlugin(Main.getInst());
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IDabItemUtils getInstance(){
|
||||
return inst;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean hasBanner(){
|
||||
return inst.hasBanner();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getNMSCopy(ItemStack is) throws Exception{
|
||||
return inst.getNMSCopy(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean hasTag(Object is) throws Exception{
|
||||
return inst.hasTag(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static ItemStack asCraftMirror(Object nis) throws Exception{
|
||||
return inst.asCraftMirror(nis);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static ItemStack asBukkitCopy(Object nmis) throws Exception{
|
||||
return inst.asBukkitCopy(nmis);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String getName(ItemStack is){
|
||||
return inst.getName(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getItem(Object nis) throws Exception{
|
||||
return inst.getItem(nis);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Method getA(){
|
||||
return inst.getA();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String getRawName(ItemStack is){
|
||||
return inst.getRawName(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String getItemName(ItemStack is){
|
||||
return inst.getItemName(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getRegistry(){
|
||||
return inst.getRegistry();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String getMinecraftName(ItemStack is){
|
||||
return inst.getMinecraftName(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getTag(Object is) throws Exception{
|
||||
return inst.getTag(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void setTag(Object is, Object tag1) throws Exception{
|
||||
inst.setTag(is, tag1);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean isEmpty(Object tag) throws Exception{
|
||||
return inst.isEmpty(tag);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getMap(Object tag) throws Exception{
|
||||
return inst.getMap(tag);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void set(Object tag, String key, Object value) throws Exception{
|
||||
inst.set(tag, key, value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void setString(Object tag, String key, String value) throws Exception{
|
||||
inst.setString(tag, key, value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void setShort(Object tag, String key, short value) throws Exception{
|
||||
inst.setShort(tag, key, value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void setInt(Object tag, String key, int i) throws Exception{
|
||||
inst.setInt(tag, key, i);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void setDouble(Object tag, String key, double d) throws Exception{
|
||||
inst.setDouble(tag, key, d);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void setLong(Object tag, String key, long l) throws Exception{
|
||||
inst.setLong(tag, key, l);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean hasKey(Object tag, String key) throws Exception{
|
||||
return inst.hasKey(tag, key);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object get(Object tag, String key) throws Exception{
|
||||
return inst.get(tag, key);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static String getString(Object tag, String key) throws Exception{
|
||||
return inst.getString(tag, key);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static int getInt(Object tag, String key) throws Exception{
|
||||
return inst.getInt(tag, key);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static double getDouble(Object tag, String key) throws Exception{
|
||||
return inst.getDouble(tag, key);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static long getLong(Object tag, String key) throws Exception{
|
||||
return inst.getLong(tag, key);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static short getShort(Object tag, String key) throws Exception{
|
||||
return inst.getShort(tag, key);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getNewNBTTagCompound() throws Exception{
|
||||
return inst.getNewNBTTagCompound();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean hasAttributeModifiersKey(Object tag) throws Exception{
|
||||
return inst.hasAttributeModifiersKey(tag);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getList(Object tag) throws Exception{
|
||||
return inst.getList(tag);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getList(Object tag, String name, int id) throws Exception{
|
||||
return inst.getList(tag, name, id);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean getUnbreakable(Object tag) throws Exception{
|
||||
return inst.getUnbreakable(tag);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void setUnbreakable(Object tag, boolean value) throws Exception{
|
||||
inst.setUnbreakable(tag, value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getNewNBTTagList() throws Exception{
|
||||
return inst.getNewNBTTagList();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void addToList(Object taglist, Object nbt) throws Exception{
|
||||
inst.addToList(taglist, nbt);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static int getSize(Object list) throws Exception{
|
||||
return inst.getSize(list);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object get(Object tlist, int i) throws Exception{
|
||||
return inst.get(tlist, i);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getNewNBTTagByte(byte value) throws Exception{
|
||||
return inst.getNewNBTTagByte(value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getNewNBTTagByteArray(byte[] value) throws Exception{
|
||||
return inst.getNewNBTTagByteArray(value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getData(Object nbt) throws Exception{
|
||||
return inst.getData(nbt);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object createData(Object value) throws Exception{
|
||||
return inst.createData(value);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Map<String, Object> convertCompoundTagToValueMap(Object nbt) throws Exception{
|
||||
return inst.convertCompoundTagToValueMap(nbt);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static List<Object> convertListTagToValueList(Object nbttl) throws Exception{
|
||||
return inst.convertListTagToValueList(nbttl);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object convertValueMapToCompoundTag(Map<String, Object> map) throws Exception{
|
||||
return inst.convertValueMapToCompoundTag(map);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object convertValueListToListTag(List<Object> list) throws Exception{
|
||||
return inst.convertValueListToListTag(list);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void convertListTagToJSON(Object nbttl, JSONArray ja, JSONArray helper) throws Exception{
|
||||
inst.convertListTagToJSON(nbttl, ja, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void convertListTagToJSON(Object nbttl, JSONArray ja) throws Exception{
|
||||
inst.convertListTagToJSON(nbttl, ja);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void convertCompoundTagToJSON(Object nbt, JSONObject jo, JSONObject helper) throws Exception{
|
||||
inst.convertCompoundTagToJSON(nbt, jo, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void convertCompoundTagToJSON(Object nbt, JSONObject jo) throws Exception{
|
||||
inst.convertCompoundTagToJSON(nbt, jo);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object convertJSONToCompoundTag(JSONObject jo, JSONObject helper) throws Exception{
|
||||
return inst.convertJSONToCompoundTag(jo, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object convertJSONToCompoundTag(JSONObject jo) throws Exception{
|
||||
return inst.convertJSONToCompoundTag(jo);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object convertJSONToListTag(JSONArray ja, JSONArray helper) throws Exception{
|
||||
return inst.convertJSONToListTag(ja, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object convertJSONToListTag(JSONArray ja) throws Exception{
|
||||
return inst.convertJSONToListTag(ja);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getDataJSON(String key, Object nbt, JSONObject jo, JSONObject helper) throws Exception{
|
||||
return inst.getDataJSON(key, nbt, jo, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static JSONArray getDataJSON(Object nbt) throws Exception{
|
||||
return inst.getDataJSON(nbt);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object getDataJSON(Object nbt, JSONArray ja, JSONArray helper) throws Exception{
|
||||
return inst.getDataJSON(nbt, ja, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object createDataJSON(String key, JSONObject jo, JSONObject helper) throws Exception{
|
||||
return inst.createDataJSON(key, jo, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object createDataJSON(String key, JSONObject jo) throws Exception{
|
||||
return inst.createDataJSON(key, jo);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static byte getByte(Object o){
|
||||
return inst.getByte(o);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static short getShort(Object o){
|
||||
return inst.getShort(o);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static int getInt(Object o){
|
||||
return inst.getInt(o);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static double getDouble(Object o){
|
||||
return inst.getDouble(o);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static float getFloat(Object o){
|
||||
return inst.getFloat(o);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static long getLong(Object o){
|
||||
return inst.getLong(o);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object createDataJSON(int key, JSONArray jo, JSONArray helper) throws Exception{
|
||||
return inst.createDataJSON(key, jo, helper);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Object createDataJSON(int key, JSONArray jo) throws Exception{
|
||||
return inst.createDataJSON(key, jo);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean compareBaseTag(Object tag, Object tag1) throws Exception{
|
||||
return inst.compareBaseTag(tag, tag1);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean compareCompoundTag(Object tag, Object tag1) throws Exception{
|
||||
return inst.compareCompoundTag(tag, tag1);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean compareListTag(Object tag, Object tag1) throws Exception{
|
||||
return inst.compareListTag(tag, tag1);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean compare(ItemStack is1, ItemStack is2){
|
||||
return inst.compare(is1, is2);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean canMerge(ItemStack add, ItemStack to){
|
||||
return compare(add, to);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean isModified(ItemStack is){
|
||||
return inst.isModified(is);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void sortByMaterial(List<ItemStack> items){
|
||||
inst.sortByMaterial(items);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void sortByName(List<ItemStack> items){
|
||||
inst.sortByName(items);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void sortByAmount(List<ItemStack> items){
|
||||
inst.sortByAmount(items);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static ItemStack convertJSONToItemStack(JSONObject jo) throws Exception{
|
||||
return inst.convertJSONToItemStack(jo);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static JSONObject convertItemStackToJSON(ItemStack is) throws Exception{
|
||||
return inst.convertItemStackToJSON(is);
|
||||
}
|
||||
}
|
@ -1,171 +0,0 @@
|
||||
package me.skymc.taboolib.nms.item;
|
||||
|
||||
import me.skymc.taboolib.json.JSONArray;
|
||||
import me.skymc.taboolib.json.JSONObject;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface IDabItemUtils{
|
||||
|
||||
//@formatter:off
|
||||
boolean hasBanner();
|
||||
|
||||
Object getNMSCopy(ItemStack is) throws Exception;
|
||||
|
||||
boolean hasTag(Object is) throws Exception;
|
||||
|
||||
ItemStack asCraftMirror(Object nis) throws Exception;
|
||||
|
||||
ItemStack asBukkitCopy(Object nmis) throws Exception;
|
||||
|
||||
String getName(ItemStack is);
|
||||
|
||||
Object getItem(Object nis) throws Exception;
|
||||
|
||||
Method getA();
|
||||
|
||||
String getRawName(ItemStack is);
|
||||
|
||||
String getItemName(ItemStack is);
|
||||
|
||||
Object getRegistry();
|
||||
|
||||
String getMinecraftName(ItemStack is);
|
||||
|
||||
Object getTag(Object is) throws Exception;
|
||||
|
||||
void setTag(Object is, Object tag1) throws Exception;
|
||||
|
||||
boolean isEmpty(Object tag) throws Exception;
|
||||
|
||||
Object getMap(Object tag) throws Exception;
|
||||
|
||||
void set(Object tag, String key, Object value) throws Exception;
|
||||
|
||||
void setString(Object tag, String key, String value) throws Exception;
|
||||
|
||||
void setShort(Object tag, String key, short value) throws Exception;
|
||||
|
||||
void setInt(Object tag, String key, int i) throws Exception;
|
||||
|
||||
void setDouble(Object tag, String key, double d) throws Exception;
|
||||
|
||||
void setLong(Object tag, String key, long l) throws Exception;
|
||||
|
||||
boolean hasKey(Object tag, String key) throws Exception;
|
||||
|
||||
Object get(Object tag, String key) throws Exception;
|
||||
|
||||
String getString(Object tag, String key) throws Exception;
|
||||
|
||||
int getInt(Object tag, String key) throws Exception;
|
||||
|
||||
double getDouble(Object tag, String key) throws Exception;
|
||||
|
||||
long getLong(Object tag, String key) throws Exception;
|
||||
|
||||
short getShort(Object tag, String key) throws Exception;
|
||||
|
||||
Object getNewNBTTagCompound() throws Exception;
|
||||
|
||||
boolean hasAttributeModifiersKey(Object tag) throws Exception;
|
||||
|
||||
Object getList(Object tag) throws Exception;
|
||||
|
||||
Object getList(Object tag, String name, int id) throws Exception;
|
||||
|
||||
boolean getUnbreakable(Object tag) throws Exception;
|
||||
|
||||
void setUnbreakable(Object tag, boolean value) throws Exception;
|
||||
|
||||
Object getNewNBTTagList() throws Exception;
|
||||
|
||||
void addToList(Object taglist, Object nbt) throws Exception;
|
||||
|
||||
int getSize(Object list) throws Exception;
|
||||
|
||||
Object get(Object tlist, int i) throws Exception;
|
||||
|
||||
Object getNewNBTTagByte(byte value) throws Exception;
|
||||
|
||||
Object getNewNBTTagByteArray(byte[] value) throws Exception;
|
||||
|
||||
Object getData(Object nbt) throws Exception;
|
||||
|
||||
Object createData(Object value) throws Exception;
|
||||
|
||||
Map<String, Object> convertCompoundTagToValueMap(Object nbt) throws Exception;
|
||||
|
||||
List<Object> convertListTagToValueList(Object nbttl) throws Exception;
|
||||
|
||||
Object convertValueMapToCompoundTag(Map<String, Object> map) throws Exception;
|
||||
|
||||
Object convertValueListToListTag(List<Object> list) throws Exception;
|
||||
@Deprecated
|
||||
void convertListTagToJSON(Object nbttl, JSONArray ja, JSONArray helper) throws Exception;
|
||||
|
||||
void convertListTagToJSON(Object nbttl, JSONArray ja) throws Exception;
|
||||
@Deprecated
|
||||
void convertCompoundTagToJSON(Object nbt, JSONObject jo, JSONObject helper) throws Exception;
|
||||
|
||||
void convertCompoundTagToJSON(Object nbt, JSONObject jo) throws Exception;
|
||||
@Deprecated
|
||||
Object convertJSONToCompoundTag(JSONObject jo, JSONObject helper) throws Exception;
|
||||
|
||||
Object convertJSONToCompoundTag(JSONObject jo) throws Exception;
|
||||
@Deprecated
|
||||
Object convertJSONToListTag(JSONArray ja, JSONArray helper) throws Exception;
|
||||
|
||||
Object convertJSONToListTag(JSONArray ja) throws Exception;
|
||||
@Deprecated
|
||||
Object getDataJSON(String key, Object nbt, JSONObject jo, JSONObject helper) throws Exception;
|
||||
|
||||
JSONArray getDataJSON(Object nbt) throws Exception;
|
||||
@Deprecated
|
||||
Object getDataJSON(Object nbt, JSONArray ja, JSONArray helper) throws Exception;
|
||||
@Deprecated
|
||||
Object createDataJSON(String key, JSONObject jo, JSONObject helper) throws Exception;
|
||||
|
||||
Object createDataJSON(String key, JSONObject jo) throws Exception;
|
||||
|
||||
byte getByte(Object o);
|
||||
|
||||
short getShort(Object o);
|
||||
|
||||
int getInt(Object o);
|
||||
|
||||
double getDouble(Object o);
|
||||
|
||||
float getFloat(Object o);
|
||||
|
||||
long getLong(Object o);
|
||||
@Deprecated
|
||||
Object createDataJSON(int key, JSONArray jo, JSONArray helper) throws Exception;
|
||||
|
||||
Object createDataJSON(int key, JSONArray jo) throws Exception;
|
||||
|
||||
boolean compareBaseTag(Object tag, Object tag1) throws Exception;
|
||||
|
||||
boolean compareCompoundTag(Object tag, Object tag1) throws Exception;
|
||||
|
||||
boolean compareListTag(Object tag, Object tag1) throws Exception;
|
||||
|
||||
boolean compare(ItemStack is1, ItemStack is2);
|
||||
|
||||
boolean canMerge(ItemStack add, ItemStack to);
|
||||
|
||||
boolean isModified(ItemStack is);
|
||||
|
||||
void sortByMaterial(List<ItemStack> items);
|
||||
|
||||
void sortByName(List<ItemStack> items);
|
||||
|
||||
void sortByAmount(List<ItemStack> items);
|
||||
|
||||
ItemStack convertJSONToItemStack(JSONObject jo) throws Exception;
|
||||
|
||||
JSONObject convertItemStackToJSON(ItemStack is) throws Exception;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,25 +0,0 @@
|
||||
package me.skymc.taboolib.nms.nbt;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
@Deprecated
|
||||
public final class NBTConstants {
|
||||
|
||||
public static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
public static final int TYPE_END = 0;
|
||||
public static final int TYPE_BYTE = 1;
|
||||
public static final int TYPE_SHORT = 2;
|
||||
public static final int TYPE_INT = 3;
|
||||
public static final int TYPE_LONG = 4;
|
||||
public static final int TYPE_FLOAT = 5;
|
||||
public static final int TYPE_DOUBLE = 6;
|
||||
public static final int TYPE_BYTE_ARRAY = 7;
|
||||
public static final int TYPE_STRING = 8;
|
||||
public static final int TYPE_LIST = 9;
|
||||
public static final int TYPE_COMPOUND = 10;
|
||||
public static final int TYPE_INT_ARRAY = 11;
|
||||
|
||||
private NBTConstants() {
|
||||
throw new AssertionError("Not instantiable");
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package me.skymc.taboolib.object;
|
||||
|
||||
@Deprecated
|
||||
public class WeightCategory {
|
||||
|
||||
private String category;
|
||||
private Integer weight;
|
||||
|
||||
public WeightCategory() {
|
||||
super();
|
||||
}
|
||||
|
||||
public WeightCategory(String category, Integer weight) {
|
||||
super();
|
||||
this.setCategory(category);
|
||||
this.setWeight(weight);
|
||||
}
|
||||
|
||||
public Integer getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(Integer weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
package me.skymc.taboolib.other;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
/**
|
||||
* 最后一次更新:2018年1月16日21:07:27
|
||||
*
|
||||
* @author sky
|
||||
*/
|
||||
|
||||
public static SimpleDateFormat CH_ALL = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
|
||||
public static SimpleDateFormat EN_ALL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
public static SimpleDateFormat YEAR = new SimpleDateFormat("yyyy");
|
||||
|
||||
public static SimpleDateFormat MONTH = new SimpleDateFormat("MM");
|
||||
|
||||
public static SimpleDateFormat DAY_OF_MONTH = new SimpleDateFormat("dd");
|
||||
public static SimpleDateFormat DAY_OF_YEAR = new SimpleDateFormat("DD");
|
||||
|
||||
public static SimpleDateFormat HOUR_OF_DAY = new SimpleDateFormat("HH");
|
||||
public static SimpleDateFormat HOUR = new SimpleDateFormat("hh");
|
||||
|
||||
public static SimpleDateFormat MINUTE = new SimpleDateFormat("mm");
|
||||
public static SimpleDateFormat SECONDS = new SimpleDateFormat("ss");
|
||||
|
||||
public static SimpleDateFormat MILLISECOND = new SimpleDateFormat("SSS");
|
||||
|
||||
public static SimpleDateFormat WEEK = new SimpleDateFormat("E");
|
||||
|
||||
private static SimpleDateFormat Hour = new SimpleDateFormat("HH:mm");
|
||||
|
||||
public static Calendar getCalendar() {
|
||||
return Calendar.getInstance();
|
||||
}
|
||||
|
||||
public static Date getTime() {
|
||||
return Calendar.getInstance().getTime();
|
||||
}
|
||||
|
||||
public static Integer getTime(SimpleDateFormat date) {
|
||||
return Integer.valueOf(date.format(getTime()));
|
||||
}
|
||||
|
||||
public static boolean timeInDifference(String m1, String m2) {
|
||||
try {
|
||||
Date now = Hour.parse(Hour.format(System.currentTimeMillis()));
|
||||
Date min = Hour.parse(m1);
|
||||
Date max = Hour.parse(m2);
|
||||
return (now.after(min) && now.before(max)) || now.equals(min) || now.equals(max);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static long formatDate(String time) {
|
||||
long date = 0;
|
||||
try {
|
||||
for (String value : time.toLowerCase().split(";")) {
|
||||
Integer num = Integer.valueOf(value.substring(0, value.length() - 1));
|
||||
if (value.endsWith("d")) {
|
||||
date += num * 1000L * 60 * 60 * 24;
|
||||
} else if (value.endsWith("h")) {
|
||||
date += num * 1000L * 60 * 60;
|
||||
} else if (value.endsWith("m")) {
|
||||
date += num * 1000L * 60;
|
||||
} else if (value.endsWith("s")) {
|
||||
date += num * 1000L;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
return date;
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package me.skymc.taboolib.other;
|
||||
|
||||
import me.skymc.taboolib.particle.EffLib;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class MathUtils {
|
||||
|
||||
public static double tronc(double paramDouble, int paramInt) {
|
||||
double d = Math.pow(10.0D, paramInt);
|
||||
return Math.floor(paramDouble * d) / d;
|
||||
}
|
||||
|
||||
public static void drawVec(Vector paramVector, Location paramLocation, Color paramColor) {
|
||||
for (double d = 0.0D; d < 1.0D; d += 0.1D) {
|
||||
EffLib.REDSTONE.display(new EffLib.OrdinaryColor(paramColor), paramLocation.clone().add(paramVector.clone().multiply(d)), 100.0D);
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendCoords(Player paramPlayer, Vector paramVector, String paramString) {
|
||||
paramPlayer.sendMessage(paramString + tronc(paramVector.getX(), 2) + " " + tronc(paramVector.getY(), 2) + " " + tronc(paramVector.getZ(), 2));
|
||||
}
|
||||
|
||||
public static void sendCoords(Player paramPlayer, Location paramLocation, String paramString) {
|
||||
paramPlayer.sendMessage(paramString + tronc(paramLocation.getX(), 2) + " " + tronc(paramLocation.getY(), 2) + " " + tronc(paramLocation.getZ(), 2));
|
||||
}
|
||||
|
||||
public static Vector rotAxisX(Vector paramVector, double paramDouble) {
|
||||
double d1 = paramVector.getY() * Math.cos(paramDouble) - paramVector.getZ() * Math.sin(paramDouble);
|
||||
double d2 = paramVector.getY() * Math.sin(paramDouble) + paramVector.getZ() * Math.cos(paramDouble);
|
||||
return paramVector.setY(d1).setZ(d2);
|
||||
}
|
||||
|
||||
public static Vector rotAxisY(Vector paramVector, double paramDouble) {
|
||||
double d1 = paramVector.getX() * Math.cos(paramDouble) + paramVector.getZ() * Math.sin(paramDouble);
|
||||
double d2 = paramVector.getX() * -Math.sin(paramDouble) + paramVector.getZ() * Math.cos(paramDouble);
|
||||
return paramVector.setX(d1).setZ(d2);
|
||||
}
|
||||
|
||||
public static Vector rotAxisZ(Vector paramVector, double paramDouble) {
|
||||
double d1 = paramVector.getX() * Math.cos(paramDouble) - paramVector.getY() * Math.sin(paramDouble);
|
||||
double d2 = paramVector.getX() * Math.sin(paramDouble) + paramVector.getY() * Math.cos(paramDouble);
|
||||
return paramVector.setX(d1).setY(d2);
|
||||
}
|
||||
|
||||
public static Vector rotateFunc(Vector paramVector, Location paramLocation) {
|
||||
double d1 = paramLocation.getYaw() / 180.0F * 3.141592653589793D;
|
||||
double d2 = paramLocation.getPitch() / 180.0F * 3.141592653589793D;
|
||||
paramVector = rotAxisX(paramVector, d2);
|
||||
paramVector = rotAxisY(paramVector, -d1);
|
||||
return paramVector;
|
||||
}
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
package me.skymc.taboolib.other;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class NumberUtils {
|
||||
|
||||
private static Random random = new Random();
|
||||
private static DecimalFormat doubleFormat = new DecimalFormat("#.##");
|
||||
|
||||
public static Random getRandom() {
|
||||
return random;
|
||||
}
|
||||
|
||||
public static Double format(Double num) {
|
||||
return Double.valueOf(doubleFormat.format(num));
|
||||
}
|
||||
|
||||
public static int getRandomInteger(Number num1, Number num2) {
|
||||
int min = Math.min(num1.intValue(), num2.intValue());
|
||||
int max = Math.max(num1.intValue(), num2.intValue());
|
||||
return (int) (random.nextDouble() * (max - min) + min);
|
||||
}
|
||||
|
||||
public static double getRandomDouble(Number num1, Number num2) {
|
||||
double min = Math.min(num1.doubleValue(), num2.doubleValue());
|
||||
double max = Math.max(num1.doubleValue(), num2.doubleValue());
|
||||
return random.nextDouble() * (max - min) + min;
|
||||
}
|
||||
|
||||
public static int getInteger(String s) {
|
||||
try {
|
||||
return Integer.valueOf(s);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static double getDouble(String s) {
|
||||
try {
|
||||
return Double.valueOf(s);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean getBoolean(String s) {
|
||||
try {
|
||||
return Boolean.valueOf(s);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean getBooleanAbbreviation(String str) {
|
||||
if (str == null || str.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (str.length() < 4) {
|
||||
char var = str.charAt(0);
|
||||
if (var == 'y' || var == 'Y' || var == 't' || var == 'T' || var == '1') {
|
||||
return true;
|
||||
}
|
||||
if (var == 'n' || var == 'N' || var == 'f' || var == 'F' || var == '0') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return getBoolean(str);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static Random getRand() {
|
||||
return random;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean getChance(int a) {
|
||||
return getRandom().nextInt(100) <= a;
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package me.skymc.taboolib.other;
|
||||
|
||||
import me.skymc.taboolib.object.WeightCategory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Deprecated
|
||||
public class WeightUtils {
|
||||
|
||||
public static String getStringByWeight(List<WeightCategory> categorys) {
|
||||
|
||||
int weightSum = 0;
|
||||
for (WeightCategory wc : categorys) {
|
||||
weightSum += wc.getWeight();
|
||||
}
|
||||
|
||||
if (weightSum <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Integer n = NumberUtils.getRandom().nextInt(weightSum);
|
||||
Integer m = 0;
|
||||
|
||||
for (WeightCategory wc : categorys) {
|
||||
if (m <= n && n < m + wc.getWeight()) {
|
||||
return wc.getCategory();
|
||||
}
|
||||
m += wc.getWeight();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package me.skymc.taboolib.packet;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.ProtocolManager;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class PacketUtils {
|
||||
|
||||
public static void checkProtocolLib() {
|
||||
Preconditions.checkArgument(isProtocolLibEnabled(), "Plugin \"ProtocolLib\" cannot found!");
|
||||
}
|
||||
|
||||
public static boolean isProtocolLibEnabled() {
|
||||
return Bukkit.getPluginManager().getPlugin("ProtocolLib") != null && Bukkit.getPluginManager().getPlugin("ProtocolLib").isEnabled();
|
||||
}
|
||||
|
||||
public static ProtocolManager getManager() {
|
||||
return ProtocolLibrary.getProtocolManager();
|
||||
}
|
||||
|
||||
public static void sendPacketEntityStatus(Entity entity, EntityStatus status, Player... players) {
|
||||
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);
|
||||
switch (status) {
|
||||
case FIRE:
|
||||
watcher.setObject(0, serializer, (byte) 0x01);
|
||||
break;
|
||||
case CROUCHED:
|
||||
watcher.setObject(0, serializer, (byte) 0x02);
|
||||
break;
|
||||
case UNUSED1:
|
||||
watcher.setObject(0, serializer, (byte) 0x04);
|
||||
break;
|
||||
case SPRINTING:
|
||||
watcher.setObject(0, serializer, (byte) 0x08);
|
||||
break;
|
||||
case UNUSED2:
|
||||
watcher.setObject(0, serializer, (byte) 0x10);
|
||||
break;
|
||||
case INVISIBLE:
|
||||
watcher.setObject(0, serializer, (byte) 0x20);
|
||||
break;
|
||||
case GLOWING:
|
||||
watcher.setObject(0, serializer, (byte) 0x40);
|
||||
break;
|
||||
case ELYTRA:
|
||||
watcher.setObject(0, serializer, (byte) 0x80);
|
||||
break;
|
||||
default:
|
||||
watcher.setObject(0, serializer, (byte) 0x00);
|
||||
break;
|
||||
}
|
||||
packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
|
||||
try {
|
||||
for (Player player : players) {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendPacketEntityCustomName(Entity entity, String value, Player... players) {
|
||||
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(String.class);
|
||||
watcher.setObject(2, serializer, value == null ? "" : value);
|
||||
packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
|
||||
try {
|
||||
for (Player player : players) {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public enum EntityStatus {
|
||||
FIRE, CROUCHED, UNUSED1, UNUSED2, SPRINTING, INVISIBLE, GLOWING, ELYTRA
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package me.skymc.taboolib.particle;
|
||||
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class ParticlePack {
|
||||
|
||||
public EffLib particle = EffLib.VILLAGER_HAPPY;
|
||||
|
||||
public float x = 0F;
|
||||
public float y = 0F;
|
||||
public float z = 0F;
|
||||
public int a = 0;
|
||||
|
||||
/**
|
||||
* VILLAGER_HAPPY-10-1-1-1
|
||||
* 粒子-数量-X-Y-Z
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public ParticlePack(String value) {
|
||||
try {
|
||||
particle = EffLib.valueOf(value.split("-")[0]);
|
||||
a = Integer.valueOf(value.split("-")[1]);
|
||||
x = Float.valueOf(value.split("-")[2]);
|
||||
y = Float.valueOf(value.split("-")[3]);
|
||||
z = Float.valueOf(value.split("-")[4]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放粒子
|
||||
*
|
||||
* @param loc
|
||||
*/
|
||||
public void play(Location loc) {
|
||||
if (TabooLib.getVerint() > 10800) {
|
||||
particle.display(x, y, z, 0f, a, loc, 50);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package me.skymc.taboolib.particle;
|
||||
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Effect;
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class ParticleUtils {
|
||||
|
||||
public static void sendColor(Effect particle, Location l, int data, Color color) {
|
||||
l.getWorld().spigot().playEffect(l, particle, data, 0, (float)getColor(color.getRed()), (float)getColor(color.getGreen()), (float)getColor(color.getBlue()), 1, 0, 35);
|
||||
}
|
||||
|
||||
public static void sendColor(Effect particle, Location l, int data, java.awt.Color color) {
|
||||
l.getWorld().spigot().playEffect(l, particle, data, 0, (float)getColor(color.getRed()), (float)getColor(color.getGreen()), (float)getColor(color.getBlue()), 1, 0, 35);
|
||||
}
|
||||
|
||||
public static void sendEffect(Effect particle, Location l, float offsetX, float offsetY, float offsetZ, float speed, int amount) {
|
||||
l.getWorld().spigot().playEffect(l, particle, 0, 0, offsetX, offsetY, offsetZ, speed, amount, 35);
|
||||
}
|
||||
|
||||
private static double getColor(double value) {
|
||||
if (value <= 0) {
|
||||
value = -1;
|
||||
}
|
||||
return value / 255;
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package me.skymc.taboolib.permission;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PermissionUtils {
|
||||
|
||||
private static Permission perms;
|
||||
|
||||
public static void loadRegisteredServiceProvider() {
|
||||
RegisteredServiceProvider<Permission> rsp = Main.getInst().getServer().getServicesManager().getRegistration(Permission.class);
|
||||
perms = rsp.getProvider();
|
||||
}
|
||||
|
||||
public static Permission getPermission() {
|
||||
return perms;
|
||||
}
|
||||
|
||||
public static void addPermission(Player player, String perm) {
|
||||
perms.playerAdd(player, perm);
|
||||
}
|
||||
|
||||
public static void removePermission(Player player, String perm) {
|
||||
perms.playerRemove(player, perm);
|
||||
}
|
||||
|
||||
public static boolean hasPermission(Player player, String perm) {
|
||||
return perms.playerHas(player, perm) || Arrays.stream(perms.getPlayerGroups(player)).anyMatch(group -> perms.groupHas(player.getWorld(), group, perm));
|
||||
}
|
||||
}
|
@ -1,139 +0,0 @@
|
||||
package me.skymc.taboolib.player;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class PlayerUtils {
|
||||
|
||||
private static boolean setup;
|
||||
private static boolean useReflection;
|
||||
private static Method oldGetOnlinePlayersMethod;
|
||||
|
||||
public static Collection<? extends Player> getOnlinePlayers() {
|
||||
try {
|
||||
if (!setup) {
|
||||
oldGetOnlinePlayersMethod = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
if (oldGetOnlinePlayersMethod.getReturnType() == Player[].class) {
|
||||
useReflection = true;
|
||||
}
|
||||
setup = true;
|
||||
}
|
||||
if (!useReflection) {
|
||||
return Bukkit.getOnlinePlayers();
|
||||
} else {
|
||||
Player[] playersArray = (Player[]) oldGetOnlinePlayersMethod.invoke(null);
|
||||
return ImmutableList.copyOf(playersArray);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取目标方块
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param max 最大视野
|
||||
* @return
|
||||
*/
|
||||
public static Block getTargetBlock(Player player, int max) {
|
||||
HashSet<Byte> bytes = new HashSet<>();
|
||||
bytes.add((byte) 0);
|
||||
return player.getTargetBlock(bytes, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写数据
|
||||
*
|
||||
* @param player 玩家
|
||||
* @param scoreboard 是否清理计分板
|
||||
*/
|
||||
public static void resetData(Player player, boolean scoreboard) {
|
||||
if (player.isDead()) {
|
||||
player.spigot().respawn();
|
||||
}
|
||||
player.closeInventory();
|
||||
player.setGameMode(GameMode.SURVIVAL);
|
||||
player.getInventory().setArmorContents(new ItemStack[4]);
|
||||
player.getInventory().setContents(new ItemStack[0]);
|
||||
player.setAllowFlight(false);
|
||||
player.setFlying(false);
|
||||
player.setExp(0.0F);
|
||||
player.setLevel(0);
|
||||
player.setSneaking(false);
|
||||
player.setSprinting(false);
|
||||
player.setFoodLevel(20);
|
||||
player.setSaturation(10.0F);
|
||||
player.setExhaustion(0.0F);
|
||||
player.setMaxHealth(20.0D);
|
||||
player.setHealth(20.0D);
|
||||
player.setFireTicks(0);
|
||||
player.setItemOnCursor(null);
|
||||
player.getActivePotionEffects().clear();
|
||||
player.getEnderChest().clear();
|
||||
player.updateInventory();
|
||||
if (scoreboard) {
|
||||
player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家的鱼钩
|
||||
*
|
||||
* @param player 玩家
|
||||
* @return net.minecraft.server.{version}.EntityFishingHook
|
||||
*/
|
||||
public static Object getPlayerHookedFish(HumanEntity player) {
|
||||
try {
|
||||
Object entityHuman = player.getClass().getMethod("getHandle").invoke(player);
|
||||
return entityHuman.getClass().getField("hookedFish").get(entityHuman);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取鱼钩的钓鱼时间
|
||||
*
|
||||
* @param fishHook 鱼钩
|
||||
* @return int
|
||||
*/
|
||||
public static int getFishingTicks(Object fishHook) {
|
||||
try {
|
||||
Field fishingTicks = fishHook.getClass().getDeclaredField("h");
|
||||
fishingTicks.setAccessible(true);
|
||||
return (int) fishingTicks.get(fishHook);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置鱼钩的钓鱼时间
|
||||
*
|
||||
* @param fishHook 鱼钩
|
||||
* @param ticks 时间
|
||||
*/
|
||||
public static void setFishingTicks(Object fishHook, int ticks) {
|
||||
try {
|
||||
Field fishingTicks = fishHook.getClass().getDeclaredField("h");
|
||||
fishingTicks.setAccessible(true);
|
||||
fishingTicks.set(fishHook, ticks);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package me.skymc.taboolib.player;
|
||||
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.util.BlockIterator;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
|
||||
@Deprecated
|
||||
public class TargetUtils {
|
||||
|
||||
public static LinkedList<Block> getTarget(LivingEntity p, int max) {
|
||||
BlockIterator itor = new BlockIterator(p);
|
||||
LinkedList<Block> block = new LinkedList<>();
|
||||
while (itor.hasNext()) {
|
||||
max++;
|
||||
block.add(itor.next());
|
||||
if (max >= 100) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return block;
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user