diff --git a/pom.xml b/pom.xml index 4702a10..b550d5b 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,10 @@ lol.hyper.githubreleaseapi lol.hyper.toolstats.updater + + space.arim.morepaperlib + lol.hyper.toolstats.morepaperlib + @@ -103,6 +107,10 @@ spigotmc-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + arim-mvn-lgpl3 + https://mvn-repo.arim.space/lesser-gpl3/ + @@ -136,5 +144,11 @@ 4.3.0 compile + + space.arim.morepaperlib + morepaperlib + 0.4.3 + compile + diff --git a/src/main/java/lol/hyper/toolstats/ToolStats.java b/src/main/java/lol/hyper/toolstats/ToolStats.java index 010cd0c..3ad5c91 100644 --- a/src/main/java/lol/hyper/toolstats/ToolStats.java +++ b/src/main/java/lol/hyper/toolstats/ToolStats.java @@ -25,13 +25,14 @@ import lol.hyper.toolstats.tools.ItemLore; import lol.hyper.toolstats.tools.NumberFormat; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bstats.bukkit.Metrics; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.NamespacedKey; +import org.bukkit.*; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; +import space.arim.morepaperlib.MorePaperLib; import java.io.File; import java.io.IOException; @@ -112,11 +113,13 @@ public final class ToolStats extends JavaPlugin { public final int CONFIG_VERSION = 5; private BukkitAudiences adventure; + public MorePaperLib morePaperLib; @Override public void onEnable() { this.adventure = BukkitAudiences.create(this); + morePaperLib = new MorePaperLib(this); if (!configFile.exists()) { this.saveResource("config.yml", true); logger.info("Copying default config!"); @@ -154,7 +157,7 @@ public final class ToolStats extends JavaPlugin { new Metrics(this, 14110); - Bukkit.getScheduler().runTaskAsynchronously(this, this::checkForUpdates); + morePaperLib.scheduling().asyncScheduler().run(this::checkForUpdates); } public void loadConfig() { @@ -305,4 +308,27 @@ public final class ToolStats extends JavaPlugin { } return this.adventure; } + + public void scheduleEntity(BukkitRunnable runnable, Entity entity, int delay) { + if (Bukkit.getServer().getVersion().contains("Folia")) { + morePaperLib.scheduling().entitySpecificScheduler(entity).runDelayed(runnable, null, delay); + } else { + runnable.runTaskLater(this, delay); + } + } + + public void scheduleGlobal(BukkitRunnable runnable, int delay) { + if (Bukkit.getServer().getVersion().contains("Folia")) { + morePaperLib.scheduling().globalRegionalScheduler().runDelayed(runnable, delay); + } else { + runnable.runTaskLater(this, delay); + } + } + public void scheduleRegion(BukkitRunnable runnable, World world, Chunk chunk, int delay) { + if (Bukkit.getServer().getVersion().contains("Folia")) { + morePaperLib.scheduling().regionSpecificScheduler(world, chunk.getX(), chunk.getZ()).runDelayed(runnable, delay); + } else { + runnable.runTaskLater(this, delay); + } + } } diff --git a/src/main/java/lol/hyper/toolstats/events/ChunkPopulate.java b/src/main/java/lol/hyper/toolstats/events/ChunkPopulate.java index 36899c4..28a2d57 100644 --- a/src/main/java/lol/hyper/toolstats/events/ChunkPopulate.java +++ b/src/main/java/lol/hyper/toolstats/events/ChunkPopulate.java @@ -32,6 +32,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import org.bukkit.scheduler.BukkitRunnable; public class ChunkPopulate implements Listener { @@ -51,28 +52,32 @@ public class ChunkPopulate implements Listener { } // this is delayed because entities are not loaded instantly // we just check 1 second later - Bukkit.getScheduler().runTaskLater(toolStats, () -> { - Chunk chunk = event.getChunk(); - for (Entity entity : chunk.getEntities()) { - // if there is a new item frame - if (!(entity instanceof ItemFrame)) { - continue; - } - ItemFrame itemFrame = (ItemFrame) entity; - // if the item frame has an elytra - if (itemFrame.getItem().getType() == Material.ELYTRA) { - ItemStack elytraCopy = itemFrame.getItem(); - ItemMeta meta = elytraCopy.getItemMeta(); - if (meta == null) { + Chunk chunk = event.getChunk(); + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + for (Entity entity : chunk.getEntities()) { + // if there is a new item frame + if (!(entity instanceof ItemFrame)) { continue; } - // add the new tag so we know it's new - PersistentDataContainer container = meta.getPersistentDataContainer(); - container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1); - elytraCopy.setItemMeta(meta); - itemFrame.setItem(elytraCopy); + ItemFrame itemFrame = (ItemFrame) entity; + // if the item frame has an elytra + if (itemFrame.getItem().getType() == Material.ELYTRA) { + ItemStack elytraCopy = itemFrame.getItem(); + ItemMeta meta = elytraCopy.getItemMeta(); + if (meta == null) { + continue; + } + // add the new tag so we know it's new + PersistentDataContainer container = meta.getPersistentDataContainer(); + container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1); + elytraCopy.setItemMeta(meta); + itemFrame.setItem(elytraCopy); + } } } - }, 20); + }; + toolStats.scheduleRegion(runnable, chunk.getWorld(), chunk, 20); } } diff --git a/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java b/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java index 89f6bb8..ccaa5f0 100644 --- a/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java +++ b/src/main/java/lol/hyper/toolstats/events/GenerateLoot.java @@ -37,6 +37,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import org.bukkit.scheduler.BukkitRunnable; import java.util.Date; import java.util.List; @@ -77,26 +78,29 @@ public class GenerateLoot implements Listener { return; } - // run task later since if it runs on the same tick it breaks idk + // run task later since if it runs on the same tick it breaks Block finalOpenedChest = openedChest; - Bukkit.getScheduler().runTaskLater(toolStats, () -> { - Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest); - // do a classic for loop, so we keep track of chest index of item - for (int i = 0; i < chestInv.getContents().length; i++) { - ItemStack itemStack = chestInv.getItem(i); - // ignore air - if (itemStack == null || itemStack.getType() == Material.AIR) { - continue; - } - if (ItemChecker.isValidItem(itemStack.getType())) { - ItemStack newItem = addLore(itemStack, player); - if (newItem != null) { - chestInv.setItem(i, newItem); + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest); + // keep track of chest index of item + for (int i = 0; i < chestInv.getContents().length; i++) { + ItemStack itemStack = chestInv.getItem(i); + // ignore air + if (itemStack == null || itemStack.getType() == Material.AIR) { + continue; + } + if (ItemChecker.isValidItem(itemStack.getType())) { + ItemStack newItem = addLore(itemStack, player); + if (newItem != null) { + chestInv.setItem(i, newItem); + } } } } - - }, 1); + }; + toolStats.scheduleRegion(runnable, lootLocation.getWorld(), lootLocation.getChunk(), 1); } if (inventoryHolder instanceof StorageMinecart) { StorageMinecart mineCart = (StorageMinecart) inventoryHolder; diff --git a/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java b/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java index c03f9e2..bb5b031 100644 --- a/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java +++ b/src/main/java/lol/hyper/toolstats/events/InventoryOpen.java @@ -21,14 +21,17 @@ import lol.hyper.toolstats.ToolStats; import lol.hyper.toolstats.tools.ItemChecker; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import org.bukkit.scheduler.BukkitRunnable; import java.util.List; @@ -46,33 +49,39 @@ public class InventoryOpen implements Listener { return; } - Bukkit.getScheduler().runTaskLater(toolStats, ()-> { - Inventory inventory = event.getInventory(); - for (ItemStack itemStack : inventory) { - if (itemStack == null) { - continue; - } - ItemMeta itemMeta = itemStack.getItemMeta(); - if (itemMeta == null) { - continue; - } - PersistentDataContainer container = itemMeta.getPersistentDataContainer(); - // ignore any items that already have the origin tag - if (container.has(toolStats.originType, PersistentDataType.INTEGER)) { - continue; - } - // ignore items that are not the right type - if (!ItemChecker.isValidItem(itemStack.getType())) { - continue; - } + Player player = (Player) event.getPlayer(); - ItemMeta newMeta = getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA); - if (newMeta == null) { - continue; - } - itemStack.setItemMeta(newMeta); + Inventory inventory = event.getInventory(); + for (ItemStack itemStack : inventory) { + if (itemStack == null) { + continue; } - },1); + ItemMeta itemMeta = itemStack.getItemMeta(); + if (itemMeta == null) { + continue; + } + PersistentDataContainer container = itemMeta.getPersistentDataContainer(); + // ignore any items that already have the origin tag + if (container.has(toolStats.originType, PersistentDataType.INTEGER)) { + continue; + } + // ignore items that are not the right type + if (!ItemChecker.isValidItem(itemStack.getType())) { + continue; + } + + ItemMeta newMeta = getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA); + if (newMeta == null) { + continue; + } + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + itemStack.setItemMeta(newMeta); + } + }; + toolStats.scheduleEntity(runnable, player, 1); + } } /** @@ -93,11 +102,11 @@ public class InventoryOpen implements Listener { for (String line : lore) { // this is the worst code I have ever written String createdBy = toolStats.getLoreFromConfig("created.created-by", false); - String createdOn = toolStats.getLoreFromConfig("created.created-by", false); - String caughtBy = toolStats.getLoreFromConfig("created.created-by", false); - String lootedBy = toolStats.getLoreFromConfig("created.created-by", false); - String foundBy = toolStats.getLoreFromConfig("created.created-by", false); - String tradedBy = toolStats.getLoreFromConfig("created.created-by", false); + String createdOn = toolStats.getLoreFromConfig("created.created-on", false); + String caughtBy = toolStats.getLoreFromConfig("fished.caught-by", false); + String lootedBy = toolStats.getLoreFromConfig("looted.looted-by", false); + String foundBy = toolStats.getLoreFromConfig("looted.found-by", false); + String tradedBy = toolStats.getLoreFromConfig("traded.traded-by", false); if (createdBy != null && line.contains(createdBy)) { origin = 0; diff --git a/src/main/java/lol/hyper/toolstats/events/PlayerInteract.java b/src/main/java/lol/hyper/toolstats/events/PlayerInteract.java index 428197b..59fd5ba 100644 --- a/src/main/java/lol/hyper/toolstats/events/PlayerInteract.java +++ b/src/main/java/lol/hyper/toolstats/events/PlayerInteract.java @@ -31,6 +31,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.scheduler.BukkitRunnable; import java.util.HashMap; @@ -63,7 +64,13 @@ public class PlayerInteract implements Listener { // store when a player opens a chest if (block.getType() != Material.AIR && block.getType() == Material.CHEST) { openedChests.put(block, player); - Bukkit.getScheduler().runTaskLater(toolStats, () -> openedChests.remove(block), 20); + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + openedChests.remove(block); + } + }; + toolStats.scheduleGlobal(runnable, 20); } } @@ -78,7 +85,13 @@ public class PlayerInteract implements Listener { if (clicked.getType() == EntityType.MINECART_CHEST) { StorageMinecart storageMinecart = (StorageMinecart) clicked; openedMineCarts.put(storageMinecart, player); - Bukkit.getScheduler().runTaskLater(toolStats, () -> openedMineCarts.remove(storageMinecart), 20); + BukkitRunnable runnable = new BukkitRunnable() { + @Override + public void run() { + openedMineCarts.remove(storageMinecart); + } + }; + toolStats.scheduleGlobal(runnable, 20); } } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c735881..d344bf4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,6 +4,7 @@ main: lol.hyper.toolstats.ToolStats api-version: 1.15 author: hyperdefined description: Track various tool stats! +folia-supported: true commands: toolstats: usage: /toolstats