This commit is contained in:
sky 2020-05-06 01:39:21 +08:00
parent 133eb4e31e
commit c0b983ca86
15 changed files with 360 additions and 113 deletions

View File

@ -5,8 +5,8 @@ plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4' id 'com.github.johnrengelman.shadow' version '4.0.4'
} }
group = 'me.skymc' group = 'io.izzel'
version = '5.27' version = '5.28'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
targetCompatibility = 1.8 targetCompatibility = 1.8

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import io.izzel.taboolib.common.loader.StartupLoader; import io.izzel.taboolib.common.loader.StartupLoader;
import io.izzel.taboolib.common.plugin.InternalPlugin;
import io.izzel.taboolib.module.command.TCommandHandler; import io.izzel.taboolib.module.command.TCommandHandler;
import io.izzel.taboolib.module.config.TConfig; import io.izzel.taboolib.module.config.TConfig;
import io.izzel.taboolib.module.config.TConfigWatcher; import io.izzel.taboolib.module.config.TConfigWatcher;
@ -32,6 +33,7 @@ public abstract class PluginLoader {
private static final Set<String> plugins = Sets.newHashSet(); private static final Set<String> plugins = Sets.newHashSet();
private static boolean firstLoading = false; private static boolean firstLoading = false;
private static boolean firstStarting = false; private static boolean firstStarting = false;
private static Plugin firstLoaded = null;
static { static {
registerLoader.add(new PluginLoader() { registerLoader.add(new PluginLoader() {
@ -47,7 +49,8 @@ public abstract class PluginLoader {
// 加载插件类 // 加载插件类
TabooLibLoader.preLoadClass(plugin, TabooLibLoader.getPluginClassSafely(plugin)); TabooLibLoader.preLoadClass(plugin, TabooLibLoader.getPluginClassSafely(plugin));
// 首次运行 // 首次运行
if (!firstLoading) { if (!firstLoading && !(plugin instanceof InternalPlugin)) {
firstLoaded = plugin;
firstLoading = true; firstLoading = true;
StartupLoader.onLoading(); StartupLoader.onLoading();
} }
@ -62,7 +65,7 @@ public abstract class PluginLoader {
// 注册插件命令 // 注册插件命令
TCommandHandler.registerCommand(plugin); TCommandHandler.registerCommand(plugin);
// 首次运行 // 首次运行
if (!firstStarting) { if (!firstStarting && !(plugin instanceof InternalPlugin)) {
firstStarting = true; firstStarting = true;
StartupLoader.onStarting(); StartupLoader.onStarting();
} }
@ -163,4 +166,8 @@ public abstract class PluginLoader {
public static Object get(Plugin plugin) { public static Object get(Plugin plugin) {
return redefine.getOrDefault(plugin.getName(), plugin); return redefine.getOrDefault(plugin.getName(), plugin);
} }
public static Plugin getFirstLoaded() {
return firstLoaded;
}
} }

View File

@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.common.loader.Startup; import io.izzel.taboolib.common.loader.Startup;
import io.izzel.taboolib.common.loader.StartupLoader;
import io.izzel.taboolib.module.ai.SimpleAiSelector; import io.izzel.taboolib.module.ai.SimpleAiSelector;
import io.izzel.taboolib.module.command.lite.CommandBuilder; import io.izzel.taboolib.module.command.lite.CommandBuilder;
import io.izzel.taboolib.module.db.local.Local; import io.izzel.taboolib.module.db.local.Local;
@ -42,10 +41,6 @@ import java.util.stream.Collectors;
@TListener @TListener
public class ListenerCommand implements Listener { public class ListenerCommand implements Listener {
static {
StartupLoader.register(ListenerCommand.class);
}
abstract static class Module { abstract static class Module {
abstract public String[] name(); abstract public String[] name();
@ -170,6 +165,17 @@ public class ListenerCommand implements Listener {
}); });
}, 20); }, 20);
} }
},
new Module() {
@Override
public String[] name() {
return new String[] {"local"};
}
@Override
public void run(Player player) {
TellrawJson.create().append("§8[§fTabooLib§8] §7LocalPlayer: ").append("§c[...]").hoverText(LocalPlayer.get(player).saveToString()).send(player);
}
}); });

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.common.loader;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.common.listener.ListenerCommand;
import io.izzel.taboolib.module.inject.TInjectHelper; import io.izzel.taboolib.module.inject.TInjectHelper;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@ -17,6 +18,10 @@ public class StartupLoader {
static List<Class<?>> classList = Lists.newArrayList(); static List<Class<?>> classList = Lists.newArrayList();
static {
StartupLoader.register(ListenerCommand.class);
}
public static void register(Class<?> clazz) { public static void register(Class<?> clazz) {
classList.add(clazz); classList.add(clazz);
} }

View File

@ -7,6 +7,7 @@ import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.instrument.ClassFileTransformer;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -77,4 +78,10 @@ public abstract class InternalPluginBridge {
abstract public Class getClass(String name) throws ClassNotFoundException; abstract public Class getClass(String name) throws ClassNotFoundException;
abstract public ClassLoader getClassLoader();
abstract public void attach(ClassFileTransformer transformer, List<String> c);
abstract public void test();
} }

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.common.plugin.bridge; package io.izzel.taboolib.common.plugin.bridge;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.ilummc.tlib.dependency.TDependency;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
@ -24,6 +25,7 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import protocolsupport.api.ProtocolSupportAPI; import protocolsupport.api.ProtocolSupportAPI;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import java.lang.instrument.ClassFileTransformer;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -225,6 +227,37 @@ public class BridgeImpl extends InternalPluginBridge {
return Class.forName(name); return Class.forName(name);
} }
@Override
public ClassLoader getClassLoader() {
return this.getClass().getClassLoader();
}
@Override
public void attach(ClassFileTransformer transformer, List<String> c) {
// Class<?>[] classes = new Class[c.size()];
// for (int i = 0; i < c.size(); i++) {
// try {
// classes[i] = Class.forName(c.get(i));
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// }
// Instrumentation instrumentation = Attacher.instrumentation();
// try {
// instrumentation.addTransformer(transformer, true);
// instrumentation.retransformClasses(classes);
// } catch (UnmodifiableClassException e) {
// e.printStackTrace();
// } finally {
// instrumentation.removeTransformer(transformer);
// }
}
@Override
public void test() {
TDependency.requestPlugin("");
}
private RegionManager worldguardRegionManager(World world) { private RegionManager worldguardRegionManager(World world) {
if (WorldGuardPlugin.inst().getDescription().getVersion().startsWith("7")) { if (WorldGuardPlugin.inst().getDescription().getVersion().startsWith("7")) {
return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world));

View File

@ -0,0 +1,96 @@
package io.izzel.taboolib.compat.plugin;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.inject.TFunction;
import io.izzel.taboolib.util.Files;
import io.izzel.taboolib.util.IO;
import io.izzel.taboolib.util.KV;
import io.izzel.taboolib.util.Strings;
import io.izzel.taboolib.util.plugin.PluginUtils;
import org.bukkit.Bukkit;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipFile;
/**
* @Author sky
* @Since 2020-05-04 23:55
*/
public class PluginPatch {
private static final Map<String, List<KV<String, byte[]>>> patchMap = Maps.newHashMap();
static {
patch("TabooLib", "me/skymc/taboolib/database/PlayerDataManager");
patch("TabooLib", "com/ilummc/tlib/dependency/TDependency");
}
public static void patch(String plugin, String path) {
try {
patchMap.computeIfAbsent(plugin, i -> Lists.newArrayList()).add(new KV<>(path, IO.readFully(Files.getResourceChecked(TabooLib.getPlugin(), "patch/" + path.substring(path.lastIndexOf("/") + 1) + ".class"))));
} catch (Throwable t) {
t.printStackTrace();
}
}
@TFunction.Init
static void init() {
List<String> p = Lists.newArrayList();
for (Map.Entry<String, List<KV<String, byte[]>>> entry : patchMap.entrySet()) {
File file;
try {
file = PluginUtils.getPluginFile(entry.getKey());
if (file == null) {
continue;
}
} catch (Throwable ignored) {
continue;
}
boolean i = false;
try (ZipFile zipFile = new ZipFile(file)) {
for (KV<String, byte[]> pair : entry.getValue()) {
String hash1 = Strings.hashKeyForDisk(IO.readFully(zipFile.getInputStream(zipFile.getEntry(pair.getKey() + ".class"))), "sha1");
String hash2 = Strings.hashKeyForDisk(pair.getValue(), "sha1");
if (!hash1.equals(hash2)) {
i = true;
}
}
} catch (Throwable t) {
t.printStackTrace();
}
if (i) {
File input = Files.folder(TabooLib.getPlugin().getDataFolder(), "patch/" + entry.getKey());
File output = Files.file(TabooLib.getPlugin().getDataFolder(), "patch-output/" + file.getName());
Files.fromZip(file, input);
for (KV<String, byte[]> pair : entry.getValue()) {
Files.toFile(pair.getValue(), Files.file(TabooLib.getPlugin().getDataFolder(), "patch/" + entry.getKey() + "/" + pair.getKey() + ".class"));
}
Files.toZipSkipDirectory(input, output);
Files.copy(output, file);
p.add(file.getName());
}
}
if (!p.isEmpty()) {
Files.deepDelete(Files.folder(TabooLib.getPlugin().getDataFolder(), "patch"));
Files.deepDelete(Files.folder(TabooLib.getPlugin().getDataFolder(), "patch-output"));
for (String name : p) {
System.out.println("[TabooLib] The \"" + name + "\" has patched.");
}
if (Bukkit.getOnlinePlayers().isEmpty()) {
System.out.println("[TabooLib] The Server will be restart now.");
try {
Thread.sleep(3000L);
} catch (Throwable ignored) {
}
Bukkit.shutdown();
} else {
System.out.println("[TabooLib] The Server required restart now.");
}
}
}
}

View File

@ -2,15 +2,23 @@ package io.izzel.taboolib.module.db.local;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.common.loader.Startup;
import io.izzel.taboolib.common.loader.StartupLoader;
import io.izzel.taboolib.module.db.IHost;
import io.izzel.taboolib.module.db.source.DBSource;
import io.izzel.taboolib.module.db.sql.SQLTable;
import io.izzel.taboolib.module.db.sql.query.Where;
import io.izzel.taboolib.module.db.sqlite.SQLiteHost;
import io.izzel.taboolib.module.inject.TSchedule; import io.izzel.taboolib.module.inject.TSchedule;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import javax.sql.DataSource;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -20,35 +28,64 @@ import java.util.UUID;
*/ */
public class LocalPlayer { public class LocalPlayer {
static {
StartupLoader.register(LocalPlayer.class);
}
private static final Map<String, FileConfiguration> files = Maps.newConcurrentMap(); private static final Map<String, FileConfiguration> files = Maps.newConcurrentMap();
private static IHost host;
private static SQLTable table;
private static DataSource dataSource;
@Startup.Starting
public static void init() {
host = new SQLiteHost(new File(LocalPlayer.getFolder(), "v2/data.db"), TabooLib.getPlugin());
table = new SQLTable("player_data");
try {
dataSource = DBSource.create(host);
table.executeUpdate("create table if not exists player_data (id integer not null primary key autoincrement, name text not null primary key, data text)").dataSource(dataSource).run();
} catch (Throwable t) {
t.printStackTrace();
}
}
public static synchronized boolean find0(OfflinePlayer player) {
return table.select(Where.equals("name", LocalPlayer.toName(player))).find(dataSource);
}
public static synchronized FileConfiguration get0(OfflinePlayer player) {
File file = toFile(toName(player));
if (file.exists()) {
try {
return SecuredFile.loadConfiguration(file);
} finally {
Files.copy(file, Files.file(getFolder(), toName(player) + ".bak"));
Files.deepDelete(file);
}
}
return table.select(Where.equals("name", LocalPlayer.toName(player))).to(dataSource).resultNext(r -> SecuredFile.loadConfiguration(new String(Base64.getDecoder().decode(r.getString("data")), StandardCharsets.UTF_8))).run(new SecuredFile(), SecuredFile.class);
}
public static synchronized void set0(OfflinePlayer player, FileConfiguration data) {
if (find0(player)) {
table.update(Where.equals("name", LocalPlayer.toName(player))).set("data", Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource);
} else {
table.insert(null, LocalPlayer.toName(player), Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource);
}
}
public static FileConfiguration get(OfflinePlayer player) { public static FileConfiguration get(OfflinePlayer player) {
return TabooLibAPI.isOriginLoaded() ? TabooLibAPI.getPluginBridge().taboolibGetPlayerData(toName(player)) : files.computeIfAbsent(toName(player), n -> Files.load(toFile(n))); return files.computeIfAbsent(toName(player), n -> get0(player));
} }
@TSchedule(delay = 20 * 30, period = 20 * 30, async = true) @TSchedule(delay = 1200, period = 1200, async = true)
public static void saveFiles() { public static void saveFiles() {
files.forEach((name, file) -> { files.forEach((name, file) -> {
try { OfflinePlayer player = toPlayer(name);
file.save(toFile(name)); if (!player.isOnline()) {
} catch (NullPointerException ignored) { files.remove(name);
} catch (Throwable t) {
t.printStackTrace();
}
});
}
@TSchedule(delay = 20 * 30, period = 20 * 30, async = true)
public static void checkFile() {
files.forEach((name, file) -> {
if (toPlayer(name) == null) {
try {
files.remove(name).save(toFile(name));
} catch (NullPointerException ignored) {
} catch (Throwable t) {
t.printStackTrace();
}
} }
set0(player, file);
}); });
} }
@ -57,7 +94,7 @@ public class LocalPlayer {
} }
public static File toFile(String name) { public static File toFile(String name) {
return Files.file(getFolder(), name + ".yml"); return new File(getFolder(), name + ".yml");
} }
public static String toName(OfflinePlayer player) { public static String toName(OfflinePlayer player) {
@ -68,7 +105,7 @@ public class LocalPlayer {
return TabooLib.getConfig().getBoolean("LOCAL-PLAYER-UUID"); return TabooLib.getConfig().getBoolean("LOCAL-PLAYER-UUID");
} }
public static Player toPlayer(String name) { public static OfflinePlayer toPlayer(String name) {
return isUniqueIdMode() ? Bukkit.getPlayer(UUID.fromString(name)) : Bukkit.getPlayerExact(name); return isUniqueIdMode() ? Bukkit.getOfflinePlayer(UUID.fromString(name)) : Bukkit.getOfflinePlayer(name);
} }
} }

View File

@ -1,64 +0,0 @@
package io.izzel.taboolib.module.db.local.player;
import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.db.IHost;
import io.izzel.taboolib.module.db.local.LocalPlayer;
import io.izzel.taboolib.module.db.local.SecuredFile;
import io.izzel.taboolib.module.db.source.DBSource;
import io.izzel.taboolib.module.db.sql.SQLTable;
import io.izzel.taboolib.module.db.sql.query.Where;
import io.izzel.taboolib.module.db.sqlite.SQLiteHost;
import io.izzel.taboolib.module.inject.TFunction;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import javax.sql.DataSource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
/**
* @Author sky
* @Since 2020-04-29 21:46
*/
public class StoredPlayer {
private static IHost host;
private static SQLTable table;
private static DataSource dataSource;
private static final Map<String, FileConfiguration> caches = Maps.newConcurrentMap();
@TFunction.Init
public static void init() {
if (Bukkit.getPluginManager().getPlugin("TabooLib") != null) {
host = new SQLiteHost(new File(LocalPlayer.getFolder(), "data.db"), TabooLib.getPlugin());
table = new SQLTable("player_data");
try {
dataSource = DBSource.create(host);
table.executeUpdate("create table player_data (id integer not null primary key autoincrement, name text, data text)").dataSource(dataSource).run();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public static synchronized boolean find(OfflinePlayer player) {
return table.select(Where.equals("name", LocalPlayer.toName(player))).find(dataSource);
}
public static synchronized FileConfiguration get(OfflinePlayer player) {
return table.select(Where.equals("name", LocalPlayer.toName(player))).to(dataSource).resultNext(r -> SecuredFile.loadConfiguration(new String(Base64.getDecoder().decode(r.getString("data")), StandardCharsets.UTF_8))).run(new SecuredFile(), SecuredFile.class);
}
public static synchronized void set(OfflinePlayer player, FileConfiguration data) {
if (find(player)) {
table.update(Where.equals("name", LocalPlayer.toName(player))).set("data", Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource);
} else {
table.insert(LocalPlayer.toName(player), Base64.getEncoder().encodeToString(data.saveToString().getBytes(StandardCharsets.UTF_8))).run(dataSource);
}
}
}

View File

@ -65,8 +65,8 @@ import java.util.function.Consumer;
public class NMSImpl extends NMS { public class NMSImpl extends NMS {
private Field entityTypesField; private Field entityTypesField;
private boolean is11400 = Version.isAfter(Version.v1_14); private final boolean is11400 = Version.isAfter(Version.v1_14);
private boolean is11500 = Version.isAfter(Version.v1_15); private final boolean is11500 = Version.isAfter(Version.v1_15);
static { static {
SimpleReflection.saveField(NBTTagString.class); SimpleReflection.saveField(NBTTagString.class);

View File

@ -21,9 +21,9 @@ import java.util.stream.Collectors;
public class TellrawJson { public class TellrawJson {
private List<BaseComponent> components = new ArrayList<>(); private List<BaseComponent> components = new ArrayList<>();
private List<BaseComponent> componentsLatest = new ArrayList<>(); private final List<BaseComponent> componentsLatest = new ArrayList<>();
private Map<String, String[]> itemTag = new HashMap<>(); private final Map<String, String[]> itemTag = new HashMap<>();
private List<String> nbtWhitelist = ArrayUtil.asList( private final List<String> nbtWhitelist = ArrayUtil.asList(
// 附魔 // 附魔
"ench", "ench",
// 附魔 1.14 // 附魔 1.14
@ -141,6 +141,11 @@ public class TellrawJson {
return this; return this;
} }
public TellrawJson clickInsertion(String command) {
getLatestComponent().forEach(component -> component.setInsertion(command));
return this;
}
public TellrawJson clickCommand(String command) { public TellrawJson clickCommand(String command) {
getLatestComponent().forEach(component -> component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command))); getLatestComponent().forEach(component -> component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)));
return this; return this;

View File

@ -25,6 +25,7 @@ import java.util.jar.JarFile;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/** /**
* @author sky * @author sky
@ -113,6 +114,16 @@ public class Files {
} }
} }
public static File toFile(byte[] in, File file) {
try (FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) {
bufferedOutputStream.write(in);
bufferedOutputStream.flush();
} catch (Throwable t) {
t.printStackTrace();
}
return file;
}
public static File toFile(String in, File file) { public static File toFile(String in, File file) {
try (FileWriter fileWriter = new FileWriter(file); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { try (FileWriter fileWriter = new FileWriter(file); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
bufferedWriter.write(in); bufferedWriter.write(in);
@ -189,10 +200,7 @@ public class Files {
} }
public static void copy(File file1, File file2) { public static void copy(File file1, File file2) {
try (FileInputStream fileIn = new FileInputStream(file1); try (FileInputStream fileIn = new FileInputStream(file1); FileOutputStream fileOut = new FileOutputStream(file2); FileChannel channelIn = fileIn.getChannel(); FileChannel channelOut = fileOut.getChannel()) {
FileOutputStream fileOut = new FileOutputStream(file2);
FileChannel channelIn = fileIn.getChannel();
FileChannel channelOut = fileOut.getChannel()) {
channelIn.transferTo(0, channelIn.size(), channelOut); channelIn.transferTo(0, channelIn.size(), channelOut);
} catch (IOException t) { } catch (IOException t) {
t.printStackTrace(); t.printStackTrace();
@ -210,13 +218,13 @@ public class Files {
} }
} }
if (originFile.isDirectory()) { if (originFile.isDirectory()) {
for (File file : Objects.requireNonNull(originFile.listFiles())) { Arrays.stream(originFile.listFiles()).parallel().forEach(file -> {
if (file.isDirectory()) { if (file.isDirectory()) {
deepCopy(file.getAbsolutePath(), targetFileName + "/" + file.getName()); deepCopy(file.getAbsolutePath(), targetFileName + "/" + file.getName());
} else { } else {
copy(file, new File(targetFileName + "/" + file.getName())); copy(file, new File(targetFileName + "/" + file.getName()));
} }
} });
} else { } else {
copy(originFile, targetFile); copy(originFile, targetFile);
} }
@ -230,9 +238,7 @@ public class Files {
file.delete(); file.delete();
return; return;
} }
for (File file1 : Objects.requireNonNull(file.listFiles())) { Arrays.stream(file.listFiles()).parallel().forEach(Files::deepDelete);
deepDelete(file1);
}
file.delete(); file.delete();
} }
@ -398,6 +404,58 @@ public class Files {
return null; return null;
} }
public static void toZip(File source, File target) {
try (FileOutputStream fileOutputStream = new FileOutputStream(target); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
toZip(zipOutputStream, source, "");
} catch (Throwable t) {
t.printStackTrace();
}
}
public static void toZipSkipDirectory(File source, File target) {
try (FileOutputStream fileOutputStream = new FileOutputStream(target); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
if (source.isDirectory()) {
Arrays.stream(source.listFiles()).forEach(f -> toZip(zipOutputStream, f, ""));
} else {
toZip(zipOutputStream, source, "");
}
} catch (Throwable t) {
t.printStackTrace();
}
}
public static void toZip(ZipOutputStream zipOutputStream, File file, String path) {
if (file.isDirectory()) {
Arrays.stream(file.listFiles()).forEach(f -> toZip(zipOutputStream, f, path + file.getName() + "/"));
} else {
try (FileInputStream fileInputStream = new FileInputStream(file)) {
zipOutputStream.putNextEntry(new ZipEntry(path + file.getName()));
zipOutputStream.write(IO.readFully(fileInputStream));
zipOutputStream.flush();
zipOutputStream.closeEntry();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public static void fromZip(File source, File target) {
try (ZipFile zipFile = new ZipFile(source)) {
zipFile.stream().parallel().forEach(e -> {
if (e.isDirectory()) {
return;
}
try {
Files.toFile(zipFile.getInputStream(e), Files.file(target, e.getName()));
} catch (Throwable t) {
t.printStackTrace();
}
});
} catch (Throwable t) {
t.printStackTrace();
}
}
private static Class getCaller(Class<?> obj) { private static Class getCaller(Class<?> obj) {
try { try {
return Class.forName(Thread.currentThread().getStackTrace()[3].getClassName(), false, obj.getClassLoader()); return Class.forName(Thread.currentThread().getStackTrace()[3].getClassName(), false, obj.getClassLoader());

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -75,6 +76,18 @@ public class Strings {
return cacheKey; return cacheKey;
} }
public static String hashKeyForDisk(byte[] key, String type) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance(type);
mDigest.update(key);
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(Arrays.hashCode(key));
}
return cacheKey;
}
public static double similarDegree(String strA, String strB){ public static double similarDegree(String strA, String strB){
String newStrA = removeSign(max(strA, strB)); String newStrA = removeSign(max(strA, strB));
String newStrB = removeSign(min(strA, strB)); String newStrB = removeSign(min(strA, strB));

View File

@ -36,22 +36,26 @@ public final class ClickEvent {
* {@link ClickEvent#value} * {@link ClickEvent#value}
*/ */
OPEN_URL, OPEN_URL,
/** /**
* Open a file at the path given by * Open a file at the path given by
* {@link ClickEvent#value} * {@link ClickEvent#value}
*/ */
OPEN_FILE, OPEN_FILE,
/** /**
* Run the command given by * Run the command given by
* {@link ClickEvent#value} * {@link ClickEvent#value}
*/ */
RUN_COMMAND, RUN_COMMAND,
/** /**
* Inserts the string given by * Inserts the string given by
* {@link ClickEvent#value} into the players * {@link ClickEvent#value} into the players
* text box * text box
*/ */
SUGGEST_COMMAND, SUGGEST_COMMAND,
/** /**
* Change to the page number given by * Change to the page number given by
* {@link ClickEvent#value} in a book * {@link ClickEvent#value} in a book

View File

@ -1,9 +1,12 @@
package io.izzel.taboolib.util.lite; package io.izzel.taboolib.util.lite;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.izzel.taboolib.util.ArrayUtil; import io.izzel.taboolib.util.ArrayUtil;
import io.izzel.taboolib.util.Reflection;
import io.izzel.taboolib.util.TMap; import io.izzel.taboolib.util.TMap;
import io.izzel.taboolib.util.item.Items; import io.izzel.taboolib.util.item.Items;
import org.bukkit.Color;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -68,6 +71,8 @@ public class Effects {
effects.data(new ItemStack(Items.asMaterial(data[0]), 1, data.length > 1 ? NumberConversions.toShort(data[1]) : 0)); effects.data(new ItemStack(Items.asMaterial(data[0]), 1, data.length > 1 ? NumberConversions.toShort(data[1]) : 0));
} else if (effects.particle.getDataType().equals(MaterialData.class)) { } else if (effects.particle.getDataType().equals(MaterialData.class)) {
effects.data(new MaterialData(Items.asMaterial(data[0]), data.length > 1 ? NumberConversions.toByte(data[1]) : 0)); effects.data(new MaterialData(Items.asMaterial(data[0]), data.length > 1 ? NumberConversions.toByte(data[1]) : 0));
} else if (effects.particle == Particle.REDSTONE) {
effects.data(new ColorData(Color.fromRGB(NumberConversions.toInt(data[0])), NumberConversions.toInt(data[1])));
} }
break; break;
} }
@ -114,6 +119,9 @@ public class Effects {
} }
public void play() { public void play() {
if (data instanceof ColorData) {
data = ((ColorData) data).instance();
}
if (player.size() > 0) { if (player.size() > 0) {
player.forEach(p -> p.spawnParticle(particle, Optional.ofNullable(center).orElse(p.getLocation()), count, offset[0], offset[1], offset[2], speed, data)); player.forEach(p -> p.spawnParticle(particle, Optional.ofNullable(center).orElse(p.getLocation()), count, offset[0], offset[1], offset[2], speed, data));
} }
@ -172,4 +180,36 @@ public class Effects {
return this; return this;
} }
public Effects data(ColorData data) {
this.data = data;
return this;
}
public static class ColorData {
private final Color color;
private final float size;
public ColorData(Color color, float size) {
Preconditions.checkArgument(color != null, "color");
this.color = color;
this.size = size;
}
public Color getColor() {
return this.color;
}
public float getSize() {
return this.size;
}
public Object instance() {
try {
return Reflection.instantiateObject(Class.forName("org.bukkit.Particle$DustOptions"), color, size);
} catch (Throwable ignored) {
}
return null;
}
}
} }