From c52fff5a7dcc6ee9c04c67aeadbf1b582ca1777a Mon Sep 17 00:00:00 2001 From: Jsinco Date: Tue, 17 Feb 2026 20:23:46 -0500 Subject: [PATCH] add folia support --- build.gradle.kts | 8 +- gradle/libs.versions.toml | 4 + .../fr/aerwyn81/headblocks/HeadBlocks.java | 38 +- .../headblocks/commands/list/Debug.java | 54 +- .../headblocks/commands/list/Export.java | 2 +- .../headblocks/data/reward/Reward.java | 2 +- .../holograms/types/AdvancedHologram.java | 6 +- .../hooks/HeadHidingPacketListener.java | 20 +- .../headblocks/runnables/GlobalTask.java | 17 +- .../runnables/HeadBlocksRunnable.java | 143 +++ .../headblocks/services/HeadService.java | 8 +- .../headblocks/services/HologramService.java | 9 +- .../headblocks/services/RewardService.java | 23 +- .../utils/bukkit/ParticlesUtils.java | 8 +- .../headblocks/utils/internal/Metrics.java | 902 ------------------ .../utils/runnables/BukkitFutureResult.java | 3 +- 16 files changed, 258 insertions(+), 989 deletions(-) create mode 100644 src/main/java/fr/aerwyn81/headblocks/runnables/HeadBlocksRunnable.java delete mode 100644 src/main/java/fr/aerwyn81/headblocks/utils/internal/Metrics.java diff --git a/build.gradle.kts b/build.gradle.kts index 3f1f320d..e65624f7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,6 +17,7 @@ repositories { maven("https://repo.codemc.org/repository/maven-public") maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") maven("https://jitpack.io") + maven("https://repo.tcoded.com/releases") } dependencies { @@ -31,6 +32,8 @@ dependencies { implementation(libs.commons.lang3) implementation(libs.nbt.api) implementation(libs.holoeasy) + implementation(libs.folialib) + implementation(libs.bstats) } tasks { @@ -60,6 +63,8 @@ tasks { relocate("org.holoeasy", "fr.aerwyn81.libs.holoEasy") relocate("org.json", "fr.aerwyn81.libs.json") relocate("org.slf4j", "fr.aerwyn81.libs.slf4j") + relocate("com.tcoded.folialib", "fr.aerwyn81.libs.foliaLib") + relocate("org.bstats", "fr.aerwyn81.libs.bstats") if (project.hasProperty("cd")) archiveFileName.set("HeadBlocks.jar") @@ -68,7 +73,7 @@ tasks { destinationDirectory.set(file(System.getenv("outputDir") ?: "$rootDir/build/")) - minimize() + //minimize() } } @@ -81,6 +86,7 @@ bukkit { softDepend = listOf("PlaceholderAPI", "HeadDatabase", "packetevents") version = project.version.toString() website = "https://just2craft.fr" + foliaSupported = true commands { register("headblocks") { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 64552304..4fd4fcc9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,6 +12,8 @@ gson = "2.11.0" commons-lang3 = "3.12.0" nbt-api = "2.15.3" holoeasy = "master-SNAPSHOT" +folialib = "0.5.1" +bstats = "3.1.0" # Build Plugins shadow = "8.3.5" @@ -32,6 +34,8 @@ gson = { module = "com.google.code.gson:gson", version.ref = "gson" } commons-lang3 = { module = "org.apache.commons:commons-lang3", version.ref = "commons-lang3" } nbt-api = { module = "de.tr7zw:item-nbt-api", version.ref = "nbt-api" } holoeasy = { module = "com.github.AerWyn81.holoeasy:holoeasy-core", version.ref = "holoeasy" } +folialib = { module = "com.tcoded:FoliaLib", version.ref = "folialib" } +bstats = { module = "org.bstats:bstats-bukkit", version.ref = "bstats" } [plugins] shadow = { id = "com.gradleup.shadow", version.ref = "shadow" } diff --git a/src/main/java/fr/aerwyn81/headblocks/HeadBlocks.java b/src/main/java/fr/aerwyn81/headblocks/HeadBlocks.java index 833a77e0..f8fb9831 100644 --- a/src/main/java/fr/aerwyn81/headblocks/HeadBlocks.java +++ b/src/main/java/fr/aerwyn81/headblocks/HeadBlocks.java @@ -1,5 +1,8 @@ package fr.aerwyn81.headblocks; +import com.tcoded.folialib.FoliaLib; +import com.tcoded.folialib.impl.PlatformScheduler; +import com.tcoded.folialib.util.FoliaLibOptions; import de.tr7zw.changeme.nbtapi.NBT; import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; import fr.aerwyn81.headblocks.commands.HBCommandExecutor; @@ -14,7 +17,9 @@ import fr.aerwyn81.headblocks.utils.bukkit.VersionUtils; import fr.aerwyn81.headblocks.utils.config.ConfigUpdater; import fr.aerwyn81.headblocks.utils.internal.LogUtil; -import fr.aerwyn81.headblocks.utils.internal.Metrics; +import org.bstats.bukkit.Metrics; +import org.bstats.charts.SimplePie; +import org.bstats.charts.SingleLineChart; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.holoeasy.HoloEasy; @@ -27,6 +32,7 @@ public final class HeadBlocks extends JavaPlugin { private static HeadBlocks instance; + private static FoliaLib foliaLib; public static boolean isPlaceholderApiActive; public static boolean isReloadInProgress; public static boolean isHeadDatabaseActive; @@ -71,7 +77,9 @@ public void onLoad() { @Override public void onEnable() { instance = this; - + FoliaLibOptions options = new FoliaLibOptions(); + options.disableNotifications(); + foliaLib = new FoliaLib(this, options); initializeExternals(); LogUtil.info("HeadBlocks initializing..."); @@ -122,30 +130,30 @@ public void onEnable() { if (ConfigService.isMetricsEnabled()) { var m = new Metrics(this, 15495); - m.addCustomChart(new Metrics.SimplePie("database_type", StorageService::selectedStorageType)); - m.addCustomChart(new Metrics.SingleLineChart("heads", () -> HeadService.getChargedHeadLocations().size())); - m.addCustomChart(new Metrics.SimplePie("lang", LanguageService::getLanguage)); - m.addCustomChart(new Metrics.SimplePie("feat_order", () -> { + m.addCustomChart(new SimplePie("database_type", StorageService::selectedStorageType)); + m.addCustomChart(new SingleLineChart("heads", () -> HeadService.getChargedHeadLocations().size())); + m.addCustomChart(new SimplePie("lang", LanguageService::getLanguage)); + m.addCustomChart(new SimplePie("feat_order", () -> { var anyOrder = HeadService.getChargedHeadLocations().stream().anyMatch(h -> h.getOrderIndex() != -1); return anyOrder ? "True" : "False"; })); - m.addCustomChart(new Metrics.SimplePie("feat_hit", () -> { + m.addCustomChart(new SimplePie("feat_hit", () -> { var anyHit = HeadService.getChargedHeadLocations().stream().anyMatch(h -> h.getHitCount() != -1); return anyHit ? "True" : "False"; })); - m.addCustomChart(new Metrics.SimplePie("feat_hint_sound", () -> { + m.addCustomChart(new SimplePie("feat_hint_sound", () -> { var anyHit = HeadService.getChargedHeadLocations().stream().anyMatch(HeadLocation::isHintSoundEnabled); return anyHit ? "True" : "False"; })); - m.addCustomChart(new Metrics.SimplePie("feat_hint_actionBarMessage", () -> { + m.addCustomChart(new SimplePie("feat_hint_actionBarMessage", () -> { var anyHit = HeadService.getChargedHeadLocations().stream().anyMatch(HeadLocation::isHintActionBarEnabled); return anyHit ? "True" : "False"; })); - m.addCustomChart(new Metrics.SimplePie("feat_hint_rewards", () -> { + m.addCustomChart(new SimplePie("feat_hint_rewards", () -> { var anyHit = HeadService.getChargedHeadLocations().stream().anyMatch(h -> !h.getRewards().isEmpty()); return anyHit ? "True" : "False"; })); - m.addCustomChart(new Metrics.SimplePie("feat_hide_heads", () -> ConfigService.hideFoundHeads() ? "True" : "False")); + m.addCustomChart(new SimplePie("feat_hide_heads", () -> ConfigService.hideFoundHeads() ? "True" : "False")); } LogUtil.info("HeadBlocks successfully loaded!"); @@ -170,7 +178,7 @@ public void onDisable() { HeadService.clearHeadMoves(); GuiService.clearCache(); - Bukkit.getScheduler().cancelTasks(this); + foliaLib.getScheduler().cancelAllTasks(); packetEventsHook.unload(); @@ -198,13 +206,17 @@ public void startInternalTaskTimer() { return; } - globalTask.runTaskTimer(this, 0, ConfigService.getDelayGlobalTask()); + globalTask.runAsyncRepeating(0, ConfigService.getDelayGlobalTask()); } public static HeadBlocks getInstance() { return instance; } + public static PlatformScheduler getScheduler() { + return foliaLib.getScheduler(); + } + public HeadDatabaseHook getHeadDatabaseHook() { return headDatabaseHook; } diff --git a/src/main/java/fr/aerwyn81/headblocks/commands/list/Debug.java b/src/main/java/fr/aerwyn81/headblocks/commands/list/Debug.java index 7d712608..ba1dd707 100644 --- a/src/main/java/fr/aerwyn81/headblocks/commands/list/Debug.java +++ b/src/main/java/fr/aerwyn81/headblocks/commands/list/Debug.java @@ -18,6 +18,7 @@ import java.util.*; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -371,25 +372,25 @@ private void handleResyncLocations(CommandSender sender) { .replaceAll("%count%", String.valueOf(headLocations.size()))); // Run on main thread since we're modifying blocks - Bukkit.getScheduler().runTask(HeadBlocks.getInstance(), () -> { - int restored = 0; - int textureApplied = 0; - int skipped = 0; - int failed = 0; - - for (var headLocation : headLocations) { - var location = headLocation.getLocation(); + AtomicInteger restored = new AtomicInteger(); + AtomicInteger textureApplied = new AtomicInteger(); + AtomicInteger skipped = new AtomicInteger(); + AtomicInteger failed = new AtomicInteger(); + + for (var headLocation : headLocations) { + var location = headLocation.getLocation(); + HeadBlocks.getScheduler().runAtLocation(location, (task) -> { if (location == null || location.getWorld() == null) { - failed++; - continue; + failed.getAndIncrement(); + return; } try { var texture = StorageService.getHeadTexture(headLocation.getUuid()); if (texture == null || texture.isEmpty()) { LogUtil.warning("Resync locations: No texture found for head {0}", headLocation.getUuid()); - failed++; - continue; + failed.getAndIncrement(); + return; } var block = location.getBlock(); @@ -398,36 +399,37 @@ private void handleResyncLocations(CommandSender sender) { // Block is already a head, check if texture matches var currentTexture = HeadUtils.getHeadTexture(block); if (texture.equals(currentTexture)) { - skipped++; - continue; + skipped.getAndIncrement(); + return; } if (HeadUtils.applyTextureToBlock(block, texture)) { - textureApplied++; + textureApplied.getAndIncrement(); } else { - failed++; + failed.getAndIncrement(); } } else { // Block is not a head, create it block.setType(Material.PLAYER_HEAD); if (HeadUtils.applyTextureToBlock(block, texture)) { - restored++; + restored.getAndIncrement(); } else { - failed++; + failed.getAndIncrement(); } } } catch (InternalException e) { LogUtil.error("Resync locations: Error processing head {0}: {1}", headLocation.getUuid(), e.getMessage()); - failed++; + failed.getAndIncrement(); } - } - sender.sendMessage(LanguageService.getMessage("Messages.ResyncLocationsSuccess") - .replaceAll("%restored%", String.valueOf(restored)) - .replaceAll("%textureApplied%", String.valueOf(textureApplied)) - .replaceAll("%skipped%", String.valueOf(skipped)) - .replaceAll("%failed%", String.valueOf(failed))); - }); + sender.sendMessage(LanguageService.getMessage("Messages.ResyncLocationsSuccess") + .replaceAll("%restored%", String.valueOf(restored.get())) + .replaceAll("%textureApplied%", String.valueOf(textureApplied.get())) + .replaceAll("%skipped%", String.valueOf(skipped.get())) + .replaceAll("%failed%", String.valueOf(failed.get()))); + }); + } + } public static List pickRandomUUIDs(List uuidList, int numberOfElements) { diff --git a/src/main/java/fr/aerwyn81/headblocks/commands/list/Export.java b/src/main/java/fr/aerwyn81/headblocks/commands/list/Export.java index f4d992d1..8acf9dfe 100644 --- a/src/main/java/fr/aerwyn81/headblocks/commands/list/Export.java +++ b/src/main/java/fr/aerwyn81/headblocks/commands/list/Export.java @@ -38,7 +38,7 @@ public boolean perform(CommandSender sender, String[] args) { sender.sendMessage(MessageUtils.colorize(LanguageService.getMessage("Messages.ExportInProgress"))); - Bukkit.getScheduler().runTaskAsynchronously(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAsync((task) -> { try { ExportSQLHelper.generateFile(typeDatabase, fileName); Thread.sleep(10000); diff --git a/src/main/java/fr/aerwyn81/headblocks/data/reward/Reward.java b/src/main/java/fr/aerwyn81/headblocks/data/reward/Reward.java index de708cfe..6178b6fd 100644 --- a/src/main/java/fr/aerwyn81/headblocks/data/reward/Reward.java +++ b/src/main/java/fr/aerwyn81/headblocks/data/reward/Reward.java @@ -72,7 +72,7 @@ public void execute(Player player, HeadLocation headLocation) { var value = parsedValue; switch (type) { case MESSAGE -> player.sendMessage(parsedValue); - case COMMAND -> Bukkit.getScheduler().runTaskLater(plugin, () -> + case COMMAND -> HeadBlocks.getScheduler().runLater(() -> plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), value), 1L); case BROADCAST -> plugin.getServer().broadcastMessage(parsedValue); } diff --git a/src/main/java/fr/aerwyn81/headblocks/holograms/types/AdvancedHologram.java b/src/main/java/fr/aerwyn81/headblocks/holograms/types/AdvancedHologram.java index cb725f6c..d99ff992 100644 --- a/src/main/java/fr/aerwyn81/headblocks/holograms/types/AdvancedHologram.java +++ b/src/main/java/fr/aerwyn81/headblocks/holograms/types/AdvancedHologram.java @@ -30,7 +30,7 @@ public void hide(Player player) { @Override public void delete() { - Bukkit.getScheduler().runTaskAsynchronously(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAsync((task) -> { var pool = getPool(); if (pool != null) { hologram.hide(getPool()); @@ -42,7 +42,7 @@ public void delete() { public IHologram create(String name, Location location, List lines) { hologram = new Hologram(HeadBlocks.getInstance().getHoloEasyLib(), location); - Bukkit.getScheduler().runTaskAsynchronously(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAsync((task) -> { ConfigService.getHologramsAdvancedLines().forEach(l -> hologram.getLines().add( new DisplayTextLine(hologram, player -> { @@ -81,7 +81,7 @@ public boolean isVisible(Player player) { } public void refresh(Player player) { - Bukkit.getScheduler().runTaskAsynchronously(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAsync((task) -> { for (Line line : hologram.getLines()) { line.update(player); } diff --git a/src/main/java/fr/aerwyn81/headblocks/hooks/HeadHidingPacketListener.java b/src/main/java/fr/aerwyn81/headblocks/hooks/HeadHidingPacketListener.java index 6e0937ca..00f9ad6f 100644 --- a/src/main/java/fr/aerwyn81/headblocks/hooks/HeadHidingPacketListener.java +++ b/src/main/java/fr/aerwyn81/headblocks/hooks/HeadHidingPacketListener.java @@ -105,7 +105,7 @@ private void handleChunkData(PacketSendEvent event, Player player) { } // Schedule a task to send block changes after the chunk is loaded on the client side - Bukkit.getScheduler().runTaskLater(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAtEntityLater(player, () -> { for (var headUuid : headsInChunk) { var headLocation = HeadService.getHeadByUUID(headUuid); if (headLocation != null && player.getWorld().equals(headLocation.getLocation().getWorld())) { @@ -143,7 +143,7 @@ public void onPlayerJoin(Player player) { } playerChunkHeadsCache.put(player.getUniqueId(), chunkHeadsMap); - Bukkit.getScheduler().runTaskLater(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAtEntityLater(player, (task) -> { for (var headUuid : foundHeads) { var headLocation = HeadService.getHeadByUUID(headUuid); if (headLocation != null && player.getWorld().equals(headLocation.getLocation().getWorld())) { @@ -211,7 +211,7 @@ public void removeFoundHead(Player player, UUID headUuid) { } if (player.getWorld().equals(loc.getWorld())) { - Bukkit.getScheduler().runTaskLater(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAtLocationLater(loc, () -> { player.sendBlockChange(loc, loc.getBlock().getBlockData()); var world = loc.getWorld(); if (world != null) { @@ -234,20 +234,20 @@ public void showAllPreviousHeads(Player player) { playerChunkHeadsCache.remove(player.getUniqueId()); if (previouslyHiddenHeads != null && !previouslyHiddenHeads.isEmpty()) { - Bukkit.getScheduler().runTaskLater(HeadBlocks.getInstance(), () -> { - for (var headUuid : previouslyHiddenHeads) { - var headLocation = HeadService.getHeadByUUID(headUuid); - if (headLocation != null && player.getWorld().equals(headLocation.getLocation().getWorld())) { - var location = headLocation.getLocation(); + for (var headUuid : previouslyHiddenHeads) { + var headLocation = HeadService.getHeadByUUID(headUuid); + if (headLocation != null && player.getWorld().equals(headLocation.getLocation().getWorld())) { + var location = headLocation.getLocation(); + HeadBlocks.getScheduler().runAtLocationLater(location, () -> { player.sendBlockChange(location, location.getBlock().getBlockData()); var world = location.getWorld(); if (world != null) { var blockState = location.getBlock().getState(); blockState.update(true, false); } - } + }, 1L); } - }, 1L); + } } } diff --git a/src/main/java/fr/aerwyn81/headblocks/runnables/GlobalTask.java b/src/main/java/fr/aerwyn81/headblocks/runnables/GlobalTask.java index c1717106..82f376dc 100644 --- a/src/main/java/fr/aerwyn81/headblocks/runnables/GlobalTask.java +++ b/src/main/java/fr/aerwyn81/headblocks/runnables/GlobalTask.java @@ -12,12 +12,11 @@ import org.bukkit.Location; import org.bukkit.Particle; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; import java.util.Collections; import java.util.Random; -public class GlobalTask extends BukkitRunnable { +public class GlobalTask extends HeadBlocksRunnable { private static final int CHUNK_SIZE = 16; private static int VIEW_RADIUS_CHUNKS = 1; @@ -35,14 +34,16 @@ public void run() { HeadService.getChargedHeadLocations().forEach(headLocation -> { var location = headLocation.getLocation(); - if (location.getWorld() == null || !location.getWorld().isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4)) - return; + HeadBlocks.getScheduler().runAtLocation(location, (task) -> { + if (location.getWorld() == null || !location.getWorld().isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4)) + return; - if (ConfigService.isSpinEnabled() && ConfigService.isSpinLinked()) { - HeadService.rotateHead(headLocation); - } + if (ConfigService.isSpinEnabled() && ConfigService.isSpinLinked()) { + HeadService.rotateHead(headLocation); + } - handleHologramAndParticles(headLocation); + handleHologramAndParticles(headLocation); + }); }); } diff --git a/src/main/java/fr/aerwyn81/headblocks/runnables/HeadBlocksRunnable.java b/src/main/java/fr/aerwyn81/headblocks/runnables/HeadBlocksRunnable.java new file mode 100644 index 00000000..55276b1a --- /dev/null +++ b/src/main/java/fr/aerwyn81/headblocks/runnables/HeadBlocksRunnable.java @@ -0,0 +1,143 @@ +package fr.aerwyn81.headblocks.runnables; + +import com.tcoded.folialib.wrapper.task.WrappedTask; +import fr.aerwyn81.headblocks.HeadBlocks; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; + +import java.util.concurrent.CompletableFuture; + +public abstract class HeadBlocksRunnable { + + @MonotonicNonNull + private WrappedTask task = null; + + + protected abstract void run(); + + + public WrappedTask runAsync() { + this.task = new CompletablableWrappedTask<>(HeadBlocks.getScheduler().runAsync((task) -> this.run())); + return this.task; + } + + public WrappedTask runSync(Location location) { + this.task = new CompletablableWrappedTask<>(HeadBlocks.getScheduler().runAtLocation(location, (task) -> this.run())); + return this.task; + } + + public WrappedTask runSync(Entity entity) { + this.task = new CompletablableWrappedTask<>(HeadBlocks.getScheduler().runAtEntity(entity, (task) -> this.run())); + return this.task; + } + + public WrappedTask runAsyncDelayed(long delay) { + this.task = HeadBlocks.getScheduler().runLaterAsync(this::run, delay); + return this.task; + } + + public WrappedTask runSyncDelayed(Location location, long delay) { + this.task = HeadBlocks.getScheduler().runAtLocationLater(location, this::run, delay); + return this.task; + } + + public WrappedTask runSyncDelayed(Entity entity, long delay) { + this.task = HeadBlocks.getScheduler().runAtEntityLater(entity, this::run, delay); + return this.task; + } + + + public WrappedTask runAsyncRepeating(long delay, long period) { + this.task = HeadBlocks.getScheduler().runTimerAsync(this::run, delay, period); + return this.task; + } + + public WrappedTask runSyncRepeating(Location location, long delay, long period) { + this.task = HeadBlocks.getScheduler().runAtLocationTimer(location, this::run, delay, period); + return this.task; + } + + public WrappedTask runSyncRepeating(Entity entity, long delay, long period) { + this.task = HeadBlocks.getScheduler().runAtEntityTimer(entity, this::run, delay, period); + return this.task; + } + + public WrappedTask runGlobal() { + this.task = new CompletablableWrappedTask<>(HeadBlocks.getScheduler().runNextTick((task) -> this.run())); + return this.task; + } + + public WrappedTask runGlobalDelayed(long delay) { + this.task = HeadBlocks.getScheduler().runLater(this::run, delay); + return this.task; + } + + + public WrappedTask runGlobalRepeating(long delay, long period) { + this.task = HeadBlocks.getScheduler().runTimer(this::run, delay, period); + return this.task; + } + + + public CompletableFuture cancel() { + return CompletableFuture.supplyAsync(() -> { + long start = System.currentTimeMillis(); + while (task == null) { + if (System.currentTimeMillis() - start > 500) return true; // 0.5s timeout + Thread.onSpinWait(); + } + task.cancel(); + return true; + }); + } + + public CompletableFuture isCancelled() { + return CompletableFuture.supplyAsync(() -> { + long start = System.currentTimeMillis(); + while (task == null) { + if (System.currentTimeMillis() - start > 500) return true; // 0.5s timeout, consider cancelled + Thread.onSpinWait(); + } + return task.isCancelled(); + }); + } + + + public static class CompletablableWrappedTask implements WrappedTask { + + private final CompletableFuture future; + private final boolean async; + + public CompletablableWrappedTask(CompletableFuture future) { + this(future, true); + } + + public CompletablableWrappedTask(CompletableFuture future, boolean async) { + this.future = future; + this.async = async; + } + + @Override + public void cancel() { + future.cancel(true); + } + + @Override + public boolean isCancelled() { + return future.isCancelled(); + } + + @Override + public Plugin getOwningPlugin() { + return HeadBlocks.getInstance(); + } + + @Override + public boolean isAsync() { + return async; + } + } +} diff --git a/src/main/java/fr/aerwyn81/headblocks/services/HeadService.java b/src/main/java/fr/aerwyn81/headblocks/services/HeadService.java index c93436f6..a11f496d 100644 --- a/src/main/java/fr/aerwyn81/headblocks/services/HeadService.java +++ b/src/main/java/fr/aerwyn81/headblocks/services/HeadService.java @@ -1,5 +1,6 @@ package fr.aerwyn81.headblocks.services; +import com.tcoded.folialib.wrapper.task.WrappedTask; import de.tr7zw.changeme.nbtapi.NBT; import de.tr7zw.changeme.nbtapi.NBTTileEntity; import fr.aerwyn81.headblocks.HeadBlocks; @@ -25,7 +26,6 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.persistence.PersistentDataType; -import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -44,7 +44,7 @@ public class HeadService { private static HashMap headMoves; private static ArrayList headLocations; - private static HashMap tasksHeadSpin; + private static HashMap tasksHeadSpin; public static String HB_KEY = "HB_HEAD"; @@ -65,7 +65,7 @@ public static void load() { heads.clear(); headLocations.clear(); headMoves.clear(); - tasksHeadSpin.values().forEach(BukkitTask::cancel); + tasksHeadSpin.values().forEach(WrappedTask::cancel); loadHeads(); loadLocations(); @@ -162,7 +162,7 @@ private static void addHeadToSpin(HeadLocation headLoc, int offset) { return; } - var task = Bukkit.getScheduler().runTaskTimer(HeadBlocks.getInstance(), + var task = HeadBlocks.getScheduler().runAtLocationTimer(headLoc.getLocation(), () -> rotateHead(headLoc), 5L * offset, ConfigService.getSpinSpeed()); tasksHeadSpin.put(headLoc.getUuid(), task); } diff --git a/src/main/java/fr/aerwyn81/headblocks/services/HologramService.java b/src/main/java/fr/aerwyn81/headblocks/services/HologramService.java index 30d751f1..78996f0e 100644 --- a/src/main/java/fr/aerwyn81/headblocks/services/HologramService.java +++ b/src/main/java/fr/aerwyn81/headblocks/services/HologramService.java @@ -62,9 +62,12 @@ public static void load() { } for (HeadLocation loc : HeadService.getHeadLocations()) { - if (loc.isCharged()) { - createHolograms(loc.getLocation()); - } + Location location = loc.getLocation(); + HeadBlocks.getScheduler().runAtLocation(location, (task) -> { + if (loc.isCharged()) { + createHolograms(location); + } + }); } } diff --git a/src/main/java/fr/aerwyn81/headblocks/services/RewardService.java b/src/main/java/fr/aerwyn81/headblocks/services/RewardService.java index beb5ca80..647a3ed3 100644 --- a/src/main/java/fr/aerwyn81/headblocks/services/RewardService.java +++ b/src/main/java/fr/aerwyn81/headblocks/services/RewardService.java @@ -4,7 +4,6 @@ import fr.aerwyn81.headblocks.data.HeadLocation; import fr.aerwyn81.headblocks.data.TieredReward; import fr.aerwyn81.headblocks.utils.bukkit.PlayerUtils; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.util.List; @@ -28,24 +27,26 @@ public static void giveReward(Player p, List playerHeads, HeadLocation hea p.sendMessage(PlaceholdersService.parse(p, headLocation, messages)); } - Bukkit.getScheduler().runTaskLater(plugin, () -> { + HeadBlocks.getScheduler().runAtEntityLater(p, () -> { List tieredCommands = tieredReward.getCommands(); if (!tieredCommands.isEmpty()) { if (tieredReward.isRandom()) { String randomCommand = tieredCommands.get(new Random().nextInt(tieredCommands.size())); - Bukkit.getScheduler().runTaskLater(plugin, () -> { + HeadBlocks.getScheduler().runLater(() -> { String parsedCommand = PlaceholdersService.parse(p.getName(), p.getUniqueId(), headLocation, randomCommand); if (!parsedCommand.isBlank()) { plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), parsedCommand); } }, 1L); } else { - tieredCommands.forEach(command -> { - String parsedCommand = PlaceholdersService.parse(p.getName(), p.getUniqueId(), headLocation, command); - if (!parsedCommand.isBlank()) { - plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), parsedCommand); - } - }); + HeadBlocks.getScheduler().runLater(() -> { + tieredCommands.forEach(command -> { + String parsedCommand = PlaceholdersService.parse(p.getName(), p.getUniqueId(), headLocation, command); + if (!parsedCommand.isBlank()) { + plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), parsedCommand); + } + }); + }, 1L); } } @@ -83,14 +84,14 @@ public static void giveReward(Player p, List playerHeads, HeadLocation hea if (isRandomCommand) { String randomCommand = headClickCommands.get(new Random().nextInt(headClickCommands.size())); - Bukkit.getScheduler().runTaskLater(plugin, () -> { + HeadBlocks.getScheduler().runLater(() -> { String parsedCommand = PlaceholdersService.parse(p.getName(), p.getUniqueId(), headLocation, randomCommand); if (!parsedCommand.isBlank()) { plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), parsedCommand); } }, 1L); } else { - Bukkit.getScheduler().runTaskLater(plugin, () -> headClickCommands.forEach(reward -> { + HeadBlocks.getScheduler().runLater(() -> headClickCommands.forEach(reward -> { String parsedCommand = PlaceholdersService.parse(p.getName(), p.getUniqueId(), headLocation, reward); if (!parsedCommand.isBlank()) { plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), parsedCommand); diff --git a/src/main/java/fr/aerwyn81/headblocks/utils/bukkit/ParticlesUtils.java b/src/main/java/fr/aerwyn81/headblocks/utils/bukkit/ParticlesUtils.java index 5ec1f6a8..788a15ff 100644 --- a/src/main/java/fr/aerwyn81/headblocks/utils/bukkit/ParticlesUtils.java +++ b/src/main/java/fr/aerwyn81/headblocks/utils/bukkit/ParticlesUtils.java @@ -33,12 +33,10 @@ public static void spawn(Location loc, Particle particle, int amount, ArrayList< } } - Bukkit.getScheduler().runTaskAsynchronously(HeadBlocks.getInstance(), () -> { + HeadBlocks.getScheduler().runAsync((task) -> { if (!dustOptions.isEmpty()) { - Bukkit.getScheduler().runTaskAsynchronously(HeadBlocks.getInstance(), () -> { - dustOptions.forEach(dustOpt -> - player.spawnParticle(particle, location, amount, size, size, size, dustOpt)); - }); + dustOptions.forEach(dustOpt -> + player.spawnParticle(particle, location, amount, size, size, size, dustOpt)); return; } diff --git a/src/main/java/fr/aerwyn81/headblocks/utils/internal/Metrics.java b/src/main/java/fr/aerwyn81/headblocks/utils/internal/Metrics.java deleted file mode 100644 index 517db125..00000000 --- a/src/main/java/fr/aerwyn81/headblocks/utils/internal/Metrics.java +++ /dev/null @@ -1,902 +0,0 @@ -/* - * This Metrics class was auto-generated and can be copied into your project if you are - * not using a build tool like Gradle or Maven for dependency management. - * - * IMPORTANT: You are not allowed to modify this class, except changing the package. - * - * Disallowed modifications include but are not limited to: - * - Remove the option for users to opt-out - * - Change the frequency for data submission - * - Obfuscate the code (every obfuscator should allow you to make an exception for specific files) - * - Reformat the code (if you use a linter, add an exception) - * - * Violations will result in a ban of your plugin and account from bStats. - */ - -package fr.aerwyn81.headblocks.utils.internal; - -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -import javax.net.ssl.HttpsURLConnection; -import java.io.*; -import java.lang.reflect.Method; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.concurrent.Callable; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Supplier; -import java.util.logging.Level; -import java.util.stream.Collectors; -import java.util.zip.GZIPOutputStream; - -public class Metrics { - - private final Plugin plugin; - - private final MetricsBase metricsBase; - - /** - * Creates a new Metrics instance. - * - * @param plugin Your plugin instance. - * @param serviceId The id of the service. It can be found at What is my plugin id? - */ - public Metrics(Plugin plugin, int serviceId) { - this.plugin = plugin; - // Get the config file - File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); - File configFile = new File(bStatsFolder, "config.yml"); - YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); - if (!config.isSet("serverUuid")) { - config.addDefault("enabled", true); - config.addDefault("serverUuid", UUID.randomUUID().toString()); - config.addDefault("logFailedRequests", false); - config.addDefault("logSentData", false); - config.addDefault("logResponseStatusText", false); - // Inform the server owners about bStats - config - .options() - .header( - "bStats (https://bStats.org) collects some basic information for plugin authors, like how\n" - + "many people use their plugin and their total player count. It's recommended to keep bStats\n" - + "enabled, but if you're not comfortable with this, you can turn this setting off. There is no\n" - + "performance penalty associated with having metrics enabled, and data sent to bStats is fully\n" - + "anonymous.") - .copyDefaults(true); - try { - config.save(configFile); - } catch (IOException ignored) { - } - } - // Load the data - boolean enabled = config.getBoolean("enabled", true); - String serverUUID = config.getString("serverUuid"); - boolean logErrors = config.getBoolean("logFailedRequests", false); - boolean logSentData = config.getBoolean("logSentData", false); - boolean logResponseStatusText = config.getBoolean("logResponseStatusText", false); - boolean isFolia = false; - try { - isFolia = Class.forName("io.papermc.paper.threadedregions.RegionizedServer") != null; - } catch (Exception e) { - } - metricsBase = - new // See https://github.com/Bastian/bstats-metrics/pull/126 - // See https://github.com/Bastian/bstats-metrics/pull/126 - // See https://github.com/Bastian/bstats-metrics/pull/126 - // See https://github.com/Bastian/bstats-metrics/pull/126 - // See https://github.com/Bastian/bstats-metrics/pull/126 - // See https://github.com/Bastian/bstats-metrics/pull/126 - // See https://github.com/Bastian/bstats-metrics/pull/126 - MetricsBase( - "bukkit", - serverUUID, - serviceId, - enabled, - this::appendPlatformData, - this::appendServiceData, - isFolia - ? null - : submitDataTask -> Bukkit.getScheduler().runTask(plugin, submitDataTask), - plugin::isEnabled, - (message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error), - (message) -> this.plugin.getLogger().log(Level.INFO, message), - logErrors, - logSentData, - logResponseStatusText, - false); - } - - /** - * Shuts down the underlying scheduler service. - */ - public void shutdown() { - metricsBase.shutdown(); - } - - /** - * Adds a custom chart. - * - * @param chart The chart to add. - */ - public void addCustomChart(CustomChart chart) { - metricsBase.addCustomChart(chart); - } - - private void appendPlatformData(JsonObjectBuilder builder) { - builder.appendField("playerAmount", getPlayerAmount()); - builder.appendField("onlineMode", Bukkit.getOnlineMode() ? 1 : 0); - builder.appendField("bukkitVersion", Bukkit.getVersion()); - builder.appendField("bukkitName", Bukkit.getName()); - builder.appendField("javaVersion", System.getProperty("java.version")); - builder.appendField("osName", System.getProperty("os.name")); - builder.appendField("osArch", System.getProperty("os.arch")); - builder.appendField("osVersion", System.getProperty("os.version")); - builder.appendField("coreCount", Runtime.getRuntime().availableProcessors()); - } - - private void appendServiceData(JsonObjectBuilder builder) { - builder.appendField("pluginVersion", plugin.getDescription().getVersion()); - } - - private int getPlayerAmount() { - try { - // Around MC 1.8 the return type was changed from an array to a collection, - // This fixes java.lang.NoSuchMethodError: - // org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection; - Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers"); - return onlinePlayersMethod.getReturnType().equals(Collection.class) - ? ((Collection) onlinePlayersMethod.invoke(Bukkit.getServer())).size() - : ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length; - } catch (Exception e) { - // Just use the new method if the reflection failed - return Bukkit.getOnlinePlayers().size(); - } - } - - public static class MetricsBase { - - /** - * The version of the Metrics class. - */ - public static final String METRICS_VERSION = "3.1.0"; - - private static final String REPORT_URL = "https://bStats.org/api/v2/data/%s"; - - private final ScheduledExecutorService scheduler; - - private final String platform; - - private final String serverUuid; - - private final int serviceId; - - private final Consumer appendPlatformDataConsumer; - - private final Consumer appendServiceDataConsumer; - - private final Consumer submitTaskConsumer; - - private final Supplier checkServiceEnabledSupplier; - - private final BiConsumer errorLogger; - - private final Consumer infoLogger; - - private final boolean logErrors; - - private final boolean logSentData; - - private final boolean logResponseStatusText; - - private final Set customCharts = new HashSet<>(); - - private final boolean enabled; - - /** - * Creates a new MetricsBase class instance. - * - * @param platform The platform of the service. - * @param serviceId The id of the service. - * @param serverUuid The server uuid. - * @param enabled Whether or not data sending is enabled. - * @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and - * appends all platform-specific data. - * @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and - * appends all service-specific data. - * @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be - * used to delegate the data collection to a another thread to prevent errors caused by - * concurrency. Can be {@code null}. - * @param checkServiceEnabledSupplier A supplier to check if the service is still enabled. - * @param errorLogger A consumer that accepts log message and an error. - * @param infoLogger A consumer that accepts info log messages. - * @param logErrors Whether or not errors should be logged. - * @param logSentData Whether or not the sent data should be logged. - * @param logResponseStatusText Whether or not the response status text should be logged. - * @param skipRelocateCheck Whether or not the relocate check should be skipped. - */ - public MetricsBase( - String platform, - String serverUuid, - int serviceId, - boolean enabled, - Consumer appendPlatformDataConsumer, - Consumer appendServiceDataConsumer, - Consumer submitTaskConsumer, - Supplier checkServiceEnabledSupplier, - BiConsumer errorLogger, - Consumer infoLogger, - boolean logErrors, - boolean logSentData, - boolean logResponseStatusText, - boolean skipRelocateCheck) { - ScheduledThreadPoolExecutor scheduler = - new ScheduledThreadPoolExecutor( - 1, - task -> { - Thread thread = new Thread(task, "bStats-Metrics"); - thread.setDaemon(true); - return thread; - }); - // We want delayed tasks (non-periodic) that will execute in the future to be - // cancelled when the scheduler is shutdown. - // Otherwise, we risk preventing the server from shutting down even when - // MetricsBase#shutdown() is called - scheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); - this.scheduler = scheduler; - this.platform = platform; - this.serverUuid = serverUuid; - this.serviceId = serviceId; - this.enabled = enabled; - this.appendPlatformDataConsumer = appendPlatformDataConsumer; - this.appendServiceDataConsumer = appendServiceDataConsumer; - this.submitTaskConsumer = submitTaskConsumer; - this.checkServiceEnabledSupplier = checkServiceEnabledSupplier; - this.errorLogger = errorLogger; - this.infoLogger = infoLogger; - this.logErrors = logErrors; - this.logSentData = logSentData; - this.logResponseStatusText = logResponseStatusText; - if (!skipRelocateCheck) { - checkRelocation(); - } - if (enabled) { - // WARNING: Removing the option to opt-out will get your plugin banned from - // bStats - startSubmitting(); - } - } - - public void addCustomChart(CustomChart chart) { - this.customCharts.add(chart); - } - - public void shutdown() { - scheduler.shutdown(); - } - - private void startSubmitting() { - final Runnable submitTask = - () -> { - if (!enabled || !checkServiceEnabledSupplier.get()) { - // Submitting data or service is disabled - scheduler.shutdown(); - return; - } - if (submitTaskConsumer != null) { - submitTaskConsumer.accept(this::submitData); - } else { - this.submitData(); - } - }; - // Many servers tend to restart at a fixed time at xx:00 which causes an uneven - // distribution of requests on the - // bStats backend. To circumvent this problem, we introduce some randomness into - // the initial and second delay. - // WARNING: You must not modify and part of this Metrics class, including the - // submit delay or frequency! - // WARNING: Modifying this code will get your plugin banned on bStats. Just - // don't do it! - long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3)); - long secondDelay = (long) (1000 * 60 * (Math.random() * 30)); - scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS); - scheduler.scheduleAtFixedRate( - submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS); - } - - private void submitData() { - final JsonObjectBuilder baseJsonBuilder = new JsonObjectBuilder(); - appendPlatformDataConsumer.accept(baseJsonBuilder); - final JsonObjectBuilder serviceJsonBuilder = new JsonObjectBuilder(); - appendServiceDataConsumer.accept(serviceJsonBuilder); - JsonObjectBuilder.JsonObject[] chartData = - customCharts.stream() - .map(customChart -> customChart.getRequestJsonObject(errorLogger, logErrors)) - .filter(Objects::nonNull) - .toArray(JsonObjectBuilder.JsonObject[]::new); - serviceJsonBuilder.appendField("id", serviceId); - serviceJsonBuilder.appendField("customCharts", chartData); - baseJsonBuilder.appendField("service", serviceJsonBuilder.build()); - baseJsonBuilder.appendField("serverUUID", serverUuid); - baseJsonBuilder.appendField("metricsVersion", METRICS_VERSION); - JsonObjectBuilder.JsonObject data = baseJsonBuilder.build(); - scheduler.execute( - () -> { - try { - // Send the data - sendData(data); - } catch (Exception e) { - // Something went wrong! :( - if (logErrors) { - errorLogger.accept("Could not submit bStats metrics data", e); - } - } - }); - } - - private void sendData(JsonObjectBuilder.JsonObject data) throws Exception { - if (logSentData) { - infoLogger.accept("Sent bStats metrics data: " + data.toString()); - } - String url = String.format(REPORT_URL, platform); - HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); - // Compress the data to save bandwidth - byte[] compressedData = compress(data.toString()); - connection.setRequestMethod("POST"); - connection.addRequestProperty("Accept", "application/json"); - connection.addRequestProperty("Connection", "close"); - connection.addRequestProperty("Content-Encoding", "gzip"); - connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length)); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setRequestProperty("User-Agent", "Metrics-Service/1"); - connection.setDoOutput(true); - try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) { - outputStream.write(compressedData); - } - StringBuilder builder = new StringBuilder(); - try (BufferedReader bufferedReader = - new BufferedReader(new InputStreamReader(connection.getInputStream()))) { - String line; - while ((line = bufferedReader.readLine()) != null) { - builder.append(line); - } - } - if (logResponseStatusText) { - infoLogger.accept("Sent data to bStats and received response: " + builder); - } - } - - /** - * Checks that the class was properly relocated. - */ - private void checkRelocation() { - // You can use the property to disable the check in your test environment - if (System.getProperty("bstats.relocatecheck") == null - || !System.getProperty("bstats.relocatecheck").equals("false")) { - // Maven's Relocate is clever and changes strings, too. So we have to use this - // little "trick" ... :D - final String defaultPackage = - new String(new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's'}); - final String examplePackage = - new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'}); - // We want to make sure no one just copy & pastes the example and uses the wrong - // package names - if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage) - || MetricsBase.class.getPackage().getName().startsWith(examplePackage)) { - throw new IllegalStateException("bStats Metrics class has not been relocated correctly!"); - } - } - } - - /** - * Gzips the given string. - * - * @param str The string to gzip. - * @return The gzipped string. - */ - private static byte[] compress(final String str) throws IOException { - if (str == null) { - return null; - } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) { - gzip.write(str.getBytes(StandardCharsets.UTF_8)); - } - return outputStream.toByteArray(); - } - } - - public static class AdvancedBarChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public AdvancedBarChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue().length == 0) { - // Skip this invalid - continue; - } - allSkipped = false; - valuesBuilder.appendField(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public static class SimplePie extends CustomChart { - - private final Callable callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SimplePie(String chartId, Callable callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - String value = callable.call(); - if (value == null || value.isEmpty()) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("value", value).build(); - } - } - - public static class DrilldownPie extends CustomChart { - - private final Callable>> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public DrilldownPie(String chartId, Callable>> callable) { - super(chartId); - this.callable = callable; - } - - @Override - public JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map> map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean reallyAllSkipped = true; - for (Map.Entry> entryValues : map.entrySet()) { - JsonObjectBuilder valueBuilder = new JsonObjectBuilder(); - boolean allSkipped = true; - for (Map.Entry valueEntry : map.get(entryValues.getKey()).entrySet()) { - valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue()); - allSkipped = false; - } - if (!allSkipped) { - reallyAllSkipped = false; - valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build()); - } - } - if (reallyAllSkipped) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public static class SingleLineChart extends CustomChart { - - private final Callable callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SingleLineChart(String chartId, Callable callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - int value = callable.call(); - if (value == 0) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("value", value).build(); - } - } - - public static class MultiLineChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public MultiLineChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() == 0) { - // Skip this invalid - continue; - } - allSkipped = false; - valuesBuilder.appendField(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public static class AdvancedPie extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public AdvancedPie(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() == 0) { - // Skip this invalid - continue; - } - allSkipped = false; - valuesBuilder.appendField(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public abstract static class CustomChart { - - private final String chartId; - - protected CustomChart(String chartId) { - if (chartId == null) { - throw new IllegalArgumentException("chartId must not be null"); - } - this.chartId = chartId; - } - - public JsonObjectBuilder.JsonObject getRequestJsonObject( - BiConsumer errorLogger, boolean logErrors) { - JsonObjectBuilder builder = new JsonObjectBuilder(); - builder.appendField("chartId", chartId); - try { - JsonObjectBuilder.JsonObject data = getChartData(); - if (data == null) { - // If the data is null we don't send the chart. - return null; - } - builder.appendField("data", data); - } catch (Throwable t) { - if (logErrors) { - errorLogger.accept("Failed to get data for custom chart with id " + chartId, t); - } - return null; - } - return builder.build(); - } - - protected abstract JsonObjectBuilder.JsonObject getChartData() throws Exception; - } - - public static class SimpleBarChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SimpleBarChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - for (Map.Entry entry : map.entrySet()) { - valuesBuilder.appendField(entry.getKey(), new int[]{entry.getValue()}); - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - /** - * An extremely simple JSON builder. - * - *

While this class is neither feature-rich nor the most performant one, it's sufficient enough - * for its use-case. - */ - public static class JsonObjectBuilder { - - private StringBuilder builder = new StringBuilder(); - - private boolean hasAtLeastOneField = false; - - public JsonObjectBuilder() { - builder.append("{"); - } - - /** - * Appends a null field to the JSON. - * - * @param key The key of the field. - * @return A reference to this object. - */ - public JsonObjectBuilder appendNull(String key) { - appendFieldUnescaped(key, "null"); - return this; - } - - /** - * Appends a string field to the JSON. - * - * @param key The key of the field. - * @param value The value of the field. - * @return A reference to this object. - */ - public JsonObjectBuilder appendField(String key, String value) { - if (value == null) { - throw new IllegalArgumentException("JSON value must not be null"); - } - appendFieldUnescaped(key, "\"" + escape(value) + "\""); - return this; - } - - /** - * Appends an integer field to the JSON. - * - * @param key The key of the field. - * @param value The value of the field. - * @return A reference to this object. - */ - public JsonObjectBuilder appendField(String key, int value) { - appendFieldUnescaped(key, String.valueOf(value)); - return this; - } - - /** - * Appends an object to the JSON. - * - * @param key The key of the field. - * @param object The object. - * @return A reference to this object. - */ - public JsonObjectBuilder appendField(String key, JsonObject object) { - if (object == null) { - throw new IllegalArgumentException("JSON object must not be null"); - } - appendFieldUnescaped(key, object.toString()); - return this; - } - - /** - * Appends a string array to the JSON. - * - * @param key The key of the field. - * @param values The string array. - * @return A reference to this object. - */ - public JsonObjectBuilder appendField(String key, String[] values) { - if (values == null) { - throw new IllegalArgumentException("JSON values must not be null"); - } - String escapedValues = - Arrays.stream(values) - .map(value -> "\"" + escape(value) + "\"") - .collect(Collectors.joining(",")); - appendFieldUnescaped(key, "[" + escapedValues + "]"); - return this; - } - - /** - * Appends an integer array to the JSON. - * - * @param key The key of the field. - * @param values The integer array. - * @return A reference to this object. - */ - public JsonObjectBuilder appendField(String key, int[] values) { - if (values == null) { - throw new IllegalArgumentException("JSON values must not be null"); - } - String escapedValues = - Arrays.stream(values).mapToObj(String::valueOf).collect(Collectors.joining(",")); - appendFieldUnescaped(key, "[" + escapedValues + "]"); - return this; - } - - /** - * Appends an object array to the JSON. - * - * @param key The key of the field. - * @param values The integer array. - * @return A reference to this object. - */ - public JsonObjectBuilder appendField(String key, JsonObject[] values) { - if (values == null) { - throw new IllegalArgumentException("JSON values must not be null"); - } - String escapedValues = - Arrays.stream(values).map(JsonObject::toString).collect(Collectors.joining(",")); - appendFieldUnescaped(key, "[" + escapedValues + "]"); - return this; - } - - /** - * Appends a field to the object. - * - * @param key The key of the field. - * @param escapedValue The escaped value of the field. - */ - private void appendFieldUnescaped(String key, String escapedValue) { - if (builder == null) { - throw new IllegalStateException("JSON has already been built"); - } - if (key == null) { - throw new IllegalArgumentException("JSON key must not be null"); - } - if (hasAtLeastOneField) { - builder.append(","); - } - builder.append("\"").append(escape(key)).append("\":").append(escapedValue); - hasAtLeastOneField = true; - } - - /** - * Builds the JSON string and invalidates this builder. - * - * @return The built JSON string. - */ - public JsonObject build() { - if (builder == null) { - throw new IllegalStateException("JSON has already been built"); - } - JsonObject object = new JsonObject(builder.append("}").toString()); - builder = null; - return object; - } - - /** - * Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt. - * - *

This method escapes only the necessary characters '"', '\'. and '\u0000' - '\u001F'. - * Compact escapes are not used (e.g., '\n' is escaped as "\u000a" and not as "\n"). - * - * @param value The value to escape. - * @return The escaped value. - */ - private static String escape(String value) { - final StringBuilder builder = new StringBuilder(); - for (int i = 0; i < value.length(); i++) { - char c = value.charAt(i); - if (c == '"') { - builder.append("\\\""); - } else if (c == '\\') { - builder.append("\\\\"); - } else if (c <= '\u000F') { - builder.append("\\u000").append(Integer.toHexString(c)); - } else if (c <= '\u001F') { - builder.append("\\u00").append(Integer.toHexString(c)); - } else { - builder.append(c); - } - } - return builder.toString(); - } - - /** - * A super simple representation of a JSON object. - * - *

This class only exists to make methods of the {@link JsonObjectBuilder} type-safe and not - * allow a raw string inputs for methods like {@link JsonObjectBuilder#appendField(String, - * JsonObject)}. - */ - public static class JsonObject { - - private final String value; - - private JsonObject(String value) { - this.value = value; - } - - @Override - public String toString() { - return value; - } - } - } -} \ No newline at end of file diff --git a/src/main/java/fr/aerwyn81/headblocks/utils/runnables/BukkitFutureResult.java b/src/main/java/fr/aerwyn81/headblocks/utils/runnables/BukkitFutureResult.java index 8126513c..0410fd4d 100644 --- a/src/main/java/fr/aerwyn81/headblocks/utils/runnables/BukkitFutureResult.java +++ b/src/main/java/fr/aerwyn81/headblocks/utils/runnables/BukkitFutureResult.java @@ -1,5 +1,6 @@ package fr.aerwyn81.headblocks.utils.runnables; +import fr.aerwyn81.headblocks.HeadBlocks; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,7 +33,7 @@ public void whenComplete(@NotNull Consumer callback, Consumer callback, Consumer throwableConsumer) { - var executor = (Executor) r -> plugin.getServer().getScheduler().runTask(plugin, r); + var executor = (Executor) r -> HeadBlocks.getScheduler().runNextTick(t -> r.run()); this.future.thenAcceptAsync(callback, executor).exceptionally(throwable -> { throwableConsumer.accept(throwable); return null;