Update
This commit is contained in:
parent
133eb4e31e
commit
c0b983ca86
@ -5,8 +5,8 @@ plugins {
|
||||
id 'com.github.johnrengelman.shadow' version '4.0.4'
|
||||
}
|
||||
|
||||
group = 'me.skymc'
|
||||
version = '5.27'
|
||||
group = 'io.izzel'
|
||||
version = '5.28'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
|
@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
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.config.TConfig;
|
||||
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 boolean firstLoading = false;
|
||||
private static boolean firstStarting = false;
|
||||
private static Plugin firstLoaded = null;
|
||||
|
||||
static {
|
||||
registerLoader.add(new PluginLoader() {
|
||||
@ -47,7 +49,8 @@ public abstract class PluginLoader {
|
||||
// 加载插件类
|
||||
TabooLibLoader.preLoadClass(plugin, TabooLibLoader.getPluginClassSafely(plugin));
|
||||
// 首次运行
|
||||
if (!firstLoading) {
|
||||
if (!firstLoading && !(plugin instanceof InternalPlugin)) {
|
||||
firstLoaded = plugin;
|
||||
firstLoading = true;
|
||||
StartupLoader.onLoading();
|
||||
}
|
||||
@ -62,7 +65,7 @@ public abstract class PluginLoader {
|
||||
// 注册插件命令
|
||||
TCommandHandler.registerCommand(plugin);
|
||||
// 首次运行
|
||||
if (!firstStarting) {
|
||||
if (!firstStarting && !(plugin instanceof InternalPlugin)) {
|
||||
firstStarting = true;
|
||||
StartupLoader.onStarting();
|
||||
}
|
||||
@ -163,4 +166,8 @@ public abstract class PluginLoader {
|
||||
public static Object get(Plugin plugin) {
|
||||
return redefine.getOrDefault(plugin.getName(), plugin);
|
||||
}
|
||||
|
||||
public static Plugin getFirstLoaded() {
|
||||
return firstLoaded;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
|
||||
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.ai.SimpleAiSelector;
|
||||
import io.izzel.taboolib.module.command.lite.CommandBuilder;
|
||||
import io.izzel.taboolib.module.db.local.Local;
|
||||
@ -42,10 +41,6 @@ import java.util.stream.Collectors;
|
||||
@TListener
|
||||
public class ListenerCommand implements Listener {
|
||||
|
||||
static {
|
||||
StartupLoader.register(ListenerCommand.class);
|
||||
}
|
||||
|
||||
abstract static class Module {
|
||||
|
||||
abstract public String[] name();
|
||||
@ -170,6 +165,17 @@ public class ListenerCommand implements Listener {
|
||||
});
|
||||
}, 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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
@ -2,6 +2,7 @@ package io.izzel.taboolib.common.loader;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.common.listener.ListenerCommand;
|
||||
import io.izzel.taboolib.module.inject.TInjectHelper;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
@ -17,6 +18,10 @@ public class StartupLoader {
|
||||
|
||||
static List<Class<?>> classList = Lists.newArrayList();
|
||||
|
||||
static {
|
||||
StartupLoader.register(ListenerCommand.class);
|
||||
}
|
||||
|
||||
public static void register(Class<?> clazz) {
|
||||
classList.add(clazz);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -77,4 +78,10 @@ public abstract class InternalPluginBridge {
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package io.izzel.taboolib.common.plugin.bridge;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.dependency.TDependency;
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
@ -24,6 +25,7 @@ import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import protocolsupport.api.ProtocolSupportAPI;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@ -225,6 +227,37 @@ public class BridgeImpl extends InternalPluginBridge {
|
||||
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) {
|
||||
if (WorldGuardPlugin.inst().getDescription().getVersion().startsWith("7")) {
|
||||
return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world));
|
||||
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -2,15 +2,23 @@ package io.izzel.taboolib.module.db.local;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
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.util.Files;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -20,35 +28,64 @@ import java.util.UUID;
|
||||
*/
|
||||
public class LocalPlayer {
|
||||
|
||||
static {
|
||||
StartupLoader.register(LocalPlayer.class);
|
||||
}
|
||||
|
||||
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) {
|
||||
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() {
|
||||
files.forEach((name, file) -> {
|
||||
try {
|
||||
file.save(toFile(name));
|
||||
} catch (NullPointerException ignored) {
|
||||
} 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();
|
||||
}
|
||||
OfflinePlayer player = toPlayer(name);
|
||||
if (!player.isOnline()) {
|
||||
files.remove(name);
|
||||
}
|
||||
set0(player, file);
|
||||
});
|
||||
}
|
||||
|
||||
@ -57,7 +94,7 @@ public class LocalPlayer {
|
||||
}
|
||||
|
||||
public static File toFile(String name) {
|
||||
return Files.file(getFolder(), name + ".yml");
|
||||
return new File(getFolder(), name + ".yml");
|
||||
}
|
||||
|
||||
public static String toName(OfflinePlayer player) {
|
||||
@ -68,7 +105,7 @@ public class LocalPlayer {
|
||||
return TabooLib.getConfig().getBoolean("LOCAL-PLAYER-UUID");
|
||||
}
|
||||
|
||||
public static Player toPlayer(String name) {
|
||||
return isUniqueIdMode() ? Bukkit.getPlayer(UUID.fromString(name)) : Bukkit.getPlayerExact(name);
|
||||
public static OfflinePlayer toPlayer(String name) {
|
||||
return isUniqueIdMode() ? Bukkit.getOfflinePlayer(UUID.fromString(name)) : Bukkit.getOfflinePlayer(name);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -65,8 +65,8 @@ import java.util.function.Consumer;
|
||||
public class NMSImpl extends NMS {
|
||||
|
||||
private Field entityTypesField;
|
||||
private boolean is11400 = Version.isAfter(Version.v1_14);
|
||||
private boolean is11500 = Version.isAfter(Version.v1_15);
|
||||
private final boolean is11400 = Version.isAfter(Version.v1_14);
|
||||
private final boolean is11500 = Version.isAfter(Version.v1_15);
|
||||
|
||||
static {
|
||||
SimpleReflection.saveField(NBTTagString.class);
|
||||
|
@ -21,9 +21,9 @@ import java.util.stream.Collectors;
|
||||
public class TellrawJson {
|
||||
|
||||
private List<BaseComponent> components = new ArrayList<>();
|
||||
private List<BaseComponent> componentsLatest = new ArrayList<>();
|
||||
private Map<String, String[]> itemTag = new HashMap<>();
|
||||
private List<String> nbtWhitelist = ArrayUtil.asList(
|
||||
private final List<BaseComponent> componentsLatest = new ArrayList<>();
|
||||
private final Map<String, String[]> itemTag = new HashMap<>();
|
||||
private final List<String> nbtWhitelist = ArrayUtil.asList(
|
||||
// 附魔
|
||||
"ench",
|
||||
// 附魔 1.14
|
||||
@ -141,6 +141,11 @@ public class TellrawJson {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TellrawJson clickInsertion(String command) {
|
||||
getLatestComponent().forEach(component -> component.setInsertion(command));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TellrawJson clickCommand(String command) {
|
||||
getLatestComponent().forEach(component -> component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command)));
|
||||
return this;
|
||||
|
@ -25,6 +25,7 @@ import java.util.jar.JarFile;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
try (FileWriter fileWriter = new FileWriter(file); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
|
||||
bufferedWriter.write(in);
|
||||
@ -189,10 +200,7 @@ public class Files {
|
||||
}
|
||||
|
||||
public static void copy(File file1, File file2) {
|
||||
try (FileInputStream fileIn = new FileInputStream(file1);
|
||||
FileOutputStream fileOut = new FileOutputStream(file2);
|
||||
FileChannel channelIn = fileIn.getChannel();
|
||||
FileChannel channelOut = fileOut.getChannel()) {
|
||||
try (FileInputStream fileIn = new FileInputStream(file1); FileOutputStream fileOut = new FileOutputStream(file2); FileChannel channelIn = fileIn.getChannel(); FileChannel channelOut = fileOut.getChannel()) {
|
||||
channelIn.transferTo(0, channelIn.size(), channelOut);
|
||||
} catch (IOException t) {
|
||||
t.printStackTrace();
|
||||
@ -210,13 +218,13 @@ public class Files {
|
||||
}
|
||||
}
|
||||
if (originFile.isDirectory()) {
|
||||
for (File file : Objects.requireNonNull(originFile.listFiles())) {
|
||||
Arrays.stream(originFile.listFiles()).parallel().forEach(file -> {
|
||||
if (file.isDirectory()) {
|
||||
deepCopy(file.getAbsolutePath(), targetFileName + "/" + file.getName());
|
||||
} else {
|
||||
copy(file, new File(targetFileName + "/" + file.getName()));
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
copy(originFile, targetFile);
|
||||
}
|
||||
@ -230,9 +238,7 @@ public class Files {
|
||||
file.delete();
|
||||
return;
|
||||
}
|
||||
for (File file1 : Objects.requireNonNull(file.listFiles())) {
|
||||
deepDelete(file1);
|
||||
}
|
||||
Arrays.stream(file.listFiles()).parallel().forEach(Files::deepDelete);
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@ -398,6 +404,58 @@ public class Files {
|
||||
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) {
|
||||
try {
|
||||
return Class.forName(Thread.currentThread().getStackTrace()[3].getClassName(), false, obj.getClassLoader());
|
||||
|
@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@ -75,6 +76,18 @@ public class Strings {
|
||||
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){
|
||||
String newStrA = removeSign(max(strA, strB));
|
||||
String newStrB = removeSign(min(strA, strB));
|
||||
|
@ -36,22 +36,26 @@ public final class ClickEvent {
|
||||
* {@link ClickEvent#value}
|
||||
*/
|
||||
OPEN_URL,
|
||||
|
||||
/**
|
||||
* Open a file at the path given by
|
||||
* {@link ClickEvent#value}
|
||||
*/
|
||||
OPEN_FILE,
|
||||
|
||||
/**
|
||||
* Run the command given by
|
||||
* {@link ClickEvent#value}
|
||||
*/
|
||||
RUN_COMMAND,
|
||||
|
||||
/**
|
||||
* Inserts the string given by
|
||||
* {@link ClickEvent#value} into the players
|
||||
* text box
|
||||
*/
|
||||
SUGGEST_COMMAND,
|
||||
|
||||
/**
|
||||
* Change to the page number given by
|
||||
* {@link ClickEvent#value} in a book
|
||||
|
@ -1,9 +1,12 @@
|
||||
package io.izzel.taboolib.util.lite;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.Lists;
|
||||
import io.izzel.taboolib.util.ArrayUtil;
|
||||
import io.izzel.taboolib.util.Reflection;
|
||||
import io.izzel.taboolib.util.TMap;
|
||||
import io.izzel.taboolib.util.item.Items;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Particle;
|
||||
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));
|
||||
} else if (effects.particle.getDataType().equals(MaterialData.class)) {
|
||||
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;
|
||||
}
|
||||
@ -114,6 +119,9 @@ public class Effects {
|
||||
}
|
||||
|
||||
public void play() {
|
||||
if (data instanceof ColorData) {
|
||||
data = ((ColorData) data).instance();
|
||||
}
|
||||
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));
|
||||
}
|
||||
@ -172,4 +180,36 @@ public class Effects {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user