diff --git a/src/main/scala/io/izzel/taboolib/client/TabooLibClient.java b/src/main/scala/io/izzel/taboolib/client/TabooLibClient.java index 8e657fc..5c5ff13 100644 --- a/src/main/scala/io/izzel/taboolib/client/TabooLibClient.java +++ b/src/main/scala/io/izzel/taboolib/client/TabooLibClient.java @@ -2,13 +2,12 @@ package io.izzel.taboolib.client; import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.client.packet.Packet; +import io.izzel.taboolib.client.packet.PacketSerializer; import io.izzel.taboolib.client.packet.impl.PacketCommand; import io.izzel.taboolib.client.packet.impl.PacketMessage; -import io.izzel.taboolib.module.locale.TLocale; import io.izzel.taboolib.module.command.lite.CommandBuilder; -import io.izzel.taboolib.client.packet.PacketSerializer; +import io.izzel.taboolib.module.locale.TLocale; import io.izzel.taboolib.util.ArrayUtil; -import org.bukkit.Bukkit; import org.bukkit.util.NumberConversions; import java.io.*; @@ -34,7 +33,7 @@ public class TabooLibClient { public static void init() { if (TabooLibSettings.load()) { connect(); - Bukkit.getScheduler().runTaskTimerAsynchronously(TabooLib.getPlugin(), TabooLibClient::reconnect, 0, 100); + TabooLib.getPlugin().runTaskAsync(TabooLibClient::reconnect, 0, 100); } else { TLocale.sendToConsole("COMMUNICATION.FAILED-LOAD-SETTINGS", TabooLibSettings.getThrowable().toString()); } diff --git a/src/main/scala/io/izzel/taboolib/common/listener/ListenerCommand.java b/src/main/scala/io/izzel/taboolib/common/listener/ListenerCommand.java index 0106de4..3cbb7a2 100644 --- a/src/main/scala/io/izzel/taboolib/common/listener/ListenerCommand.java +++ b/src/main/scala/io/izzel/taboolib/common/listener/ListenerCommand.java @@ -134,11 +134,11 @@ public class ListenerCommand implements Listener { public void run(Player player) { player.sendMessage("§8[§fTabooLib§8] §7Lighting. §a(+)"); TLight.create(player.getLocation().getBlock(), Type.BLOCK, 15); - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> { + TabooLib.getPlugin().runTask(() -> { TLight.create(player.getLocation().getBlock(), Type.BLOCK, 5); player.sendMessage("§8[§fTabooLib§8] §7Lighting. §c(-)"); }, 20); - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> { + TabooLib.getPlugin().runTask(() -> { TLight.delete(player.getLocation().getBlock(), Type.BLOCK); player.sendMessage("§8[§fTabooLib§8] §7Lighting. §8(-)"); }, 40); diff --git a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPlugin.java b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPlugin.java index ee3c7fe..9bcaf79 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPlugin.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPlugin.java @@ -137,4 +137,64 @@ public class InternalPlugin implements Plugin { public List onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { return null; } + + public void runTask(Runnable runnable) { + Bukkit.getScheduler().runTask(this, () -> { + try { + runnable.run(); + } catch (Throwable t) { + t.printStackTrace(); + } + }); + } + + public void runTaskAsync(Runnable runnable) { + Bukkit.getScheduler().runTaskAsynchronously(this, () -> { + try { + runnable.run(); + } catch (Throwable t) { + t.printStackTrace(); + } + }); + } + + public void runTask(Runnable runnable, long delay) { + Bukkit.getScheduler().runTaskLater(this, () -> { + try { + runnable.run(); + } catch (Throwable t) { + t.printStackTrace(); + } + }, delay); + } + + public void runTaskAsync(Runnable runnable, long delay) { + Bukkit.getScheduler().runTaskLaterAsynchronously(this, () -> { + try { + runnable.run(); + } catch (Throwable t) { + t.printStackTrace(); + } + }, delay); + } + + public void runTask(Runnable runnable, long delay, long period) { + Bukkit.getScheduler().runTaskTimer(this, () -> { + try { + runnable.run(); + } catch (Throwable t) { + t.printStackTrace(); + } + }, delay, period); + } + + public void runTaskAsync(Runnable runnable, long delay, long period) { + Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> { + try { + runnable.run(); + } catch (Throwable t) { + t.printStackTrace(); + } + }, delay, period); + } } diff --git a/src/main/scala/io/izzel/taboolib/module/hologram/Hologram.java b/src/main/scala/io/izzel/taboolib/module/hologram/Hologram.java index fb7fb58..0d76afe 100644 --- a/src/main/scala/io/izzel/taboolib/module/hologram/Hologram.java +++ b/src/main/scala/io/izzel/taboolib/module/hologram/Hologram.java @@ -107,7 +107,7 @@ public class Hologram { public Hologram flash(List text, int period) { for (int i = 0; i < text.size(); i++) { String line = text.get(i); - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> flash(line), period * i); + TabooLib.getPlugin().runTask(() -> flash(line), period * i); } return this; } @@ -129,7 +129,7 @@ public class Hologram { public Hologram flash(Vector vector, int period, int times) { for (int i = 0; i < times; i++) { - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> flash(location.add(vector)), period * i); + TabooLib.getPlugin().runTask(() -> flash(location.add(vector)), period * i); } return this; } @@ -156,7 +156,7 @@ public class Hologram { } public Hologram deleteOn(int delay) { - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), this::delete, delay); + TabooLib.getPlugin().runTask(this::delete, delay); return this; } diff --git a/src/main/scala/io/izzel/taboolib/module/hologram/THologramHandler.java b/src/main/scala/io/izzel/taboolib/module/hologram/THologramHandler.java index 3055a4f..607d6b4 100644 --- a/src/main/scala/io/izzel/taboolib/module/hologram/THologramHandler.java +++ b/src/main/scala/io/izzel/taboolib/module/hologram/THologramHandler.java @@ -45,7 +45,7 @@ class THologramHandler implements Listener { static boolean d(Player player, Packet packet) { if (packet.is("PacketPlayInPosition") && !learned) { learned = true; - Bukkit.getScheduler().runTask(TabooLib.getPlugin(), () -> learn(player)); + TabooLib.getPlugin().runTask(() -> learn(player)); } if (packet.is("PacketPlayInUseEntity")) { int id = packet.read("a", Integer.TYPE); diff --git a/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickListener.java b/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickListener.java index 093821f..61f2f1e 100644 --- a/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickListener.java +++ b/src/main/scala/io/izzel/taboolib/util/item/inventory/ClickListener.java @@ -35,14 +35,14 @@ class ClickListener implements Listener { public void e(InventoryOpenEvent e) { MenuBuilder builder = MenuHolder.get(e.getInventory()); if (builder != null) { - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> { + TabooLib.getPlugin().runTask(() -> { try { builder.getBuildTask().run(e.getInventory()); } catch (Throwable t) { t.printStackTrace(); } }, 1); - Bukkit.getScheduler().runTaskLaterAsynchronously(TabooLib.getPlugin(), () -> { + TabooLib.getPlugin().runTaskAsync(() -> { try { builder.getBuildTaskAsync().run(e.getInventory()); } catch (Throwable t) { diff --git a/src/main/scala/io/izzel/taboolib/util/lite/Catchers.java b/src/main/scala/io/izzel/taboolib/util/lite/Catchers.java index 74094a8..b8e6493 100644 --- a/src/main/scala/io/izzel/taboolib/util/lite/Catchers.java +++ b/src/main/scala/io/izzel/taboolib/util/lite/Catchers.java @@ -3,7 +3,6 @@ package io.izzel.taboolib.util.lite; import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.module.inject.PlayerContainer; import io.izzel.taboolib.module.inject.TListener; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -38,7 +37,7 @@ public class Catchers implements Listener { if (playerdata.containsKey(e.getPlayer().getName()) && contains(e.getPlayer())) { e.setCancelled(true); // 1.14 supported. - Bukkit.getScheduler().runTask(TabooLib.getPlugin(), () -> { + TabooLib.getPlugin().runTask(() -> { Catcher catcher = playerdata.get(e.getPlayer().getName()).getFirst(); // 退出 if (e.getMessage().split(" ")[0].matches(catcher.quit())) { diff --git a/src/main/scala/io/izzel/taboolib/util/lite/Signs.java b/src/main/scala/io/izzel/taboolib/util/lite/Signs.java index e0a09fb..01b0108 100644 --- a/src/main/scala/io/izzel/taboolib/util/lite/Signs.java +++ b/src/main/scala/io/izzel/taboolib/util/lite/Signs.java @@ -8,7 +8,6 @@ import io.izzel.taboolib.module.nms.impl.Position; import io.izzel.taboolib.module.packet.Packet; import io.izzel.taboolib.module.packet.TPacket; import org.apache.commons.lang3.Validate; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.block.Sign; @@ -80,12 +79,8 @@ public class Signs { if (data == null) { return true; } - Bukkit.getScheduler().runTask(TabooLib.getPlugin(), () -> { - try { - data.catcher.accept(packet.read("b", new String[0])); - } catch (Throwable t) { - t.printStackTrace(); - } + TabooLib.getPlugin().runTask(() -> { + data.catcher.accept(packet.read("b", new String[0])); }); signs.remove(data); } catch (Throwable t) { diff --git a/src/main/scala/io/izzel/taboolib/util/lite/SoundPack.java b/src/main/scala/io/izzel/taboolib/util/lite/SoundPack.java index eaa646d..b737b87 100644 --- a/src/main/scala/io/izzel/taboolib/util/lite/SoundPack.java +++ b/src/main/scala/io/izzel/taboolib/util/lite/SoundPack.java @@ -1,7 +1,6 @@ package io.izzel.taboolib.util.lite; import io.izzel.taboolib.TabooLib; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Sound; import org.bukkit.entity.Player; @@ -37,11 +36,11 @@ public class SoundPack { } public void play(Player p) { - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> p.playSound(p.getLocation(), this.sound, this.a, this.b), delay); + TabooLib.getPlugin().runTask(() -> p.playSound(p.getLocation(), this.sound, this.a, this.b), delay); } public void play(Location l) { - Bukkit.getScheduler().runTaskLater(TabooLib.getPlugin(), () -> l.getWorld().playSound(l, this.sound, this.a, this.b), delay); + TabooLib.getPlugin().runTask(() -> l.getWorld().playSound(l, this.sound, this.a, this.b), delay); } public void parse(String s) {