diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 0669a74..aa959a4 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -20,11 +20,11 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Set up JDK 8 + - name: Set up JDK 17 uses: actions/setup-java@v2 with: - java-version: '8' - distribution: 'temurin' + java-version: '17' + distribution: 'adopt' - name: Change wrapper permissions run: chmod +x ./gradlew - name: Build with Gradle diff --git a/build.gradle b/build.gradle index 88a1e7f..35403eb 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,12 @@ repositories { maven { url = uri('https://repo.extendedclip.com/content/repositories/placeholderapi/') } + maven { + url = uri('https://repo.papermc.io/repository/maven-public/') + } + maven { + url = uri('https://hub.jeff-media.com/nexus/repository/jeff-media-public/') + } } dependencies { @@ -40,16 +46,18 @@ dependencies { implementation 'com.github.Revxrsal.Lamp:bukkit:3.2.1' implementation 'com.github.Sven65:Item-Names:1.0.2' implementation 'org.bstats:bstats-bukkit:3.0.2' + implementation 'com.jeff_media:MorePersistentDataTypes:2.4.0' + compileOnly 'dev.folia:folia-api:1.20.4-R0.1-SNAPSHOT' compileOnly 'org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT' compileOnly 'org.jetbrains:annotations:24.1.0' compileOnly 'me.clip:placeholderapi:2.11.6' } group = 'me.byteful.plugin' -version = '1.3.8' +version = '1.4.0' description = 'LevelTools' -java.sourceCompatibility = JavaVersion.VERSION_1_8 +java.sourceCompatibility = JavaVersion.VERSION_17 publishing { publications { @@ -69,6 +77,7 @@ shadowJar { relocate "redempt.redlib", "me.byteful.plugin.leveltools.libs.redlib" relocate 'revxrsal.commands', 'me.byteful.plugin.leveltools.libs.lamp' relocate 'org.bstats', 'me.byteful.plugin.leveltools.libs.bstats' + relocate 'com.jeff_media.morepersistentdatatypes', 'me.byteful.plugin.leveltools.libs.morepersistentdatatypes' } def targetJavaVersion = 8 diff --git a/src/main/java/me/byteful/plugin/leveltools/LevelToolsPlugin.java b/src/main/java/me/byteful/plugin/leveltools/LevelToolsPlugin.java index 78c7498..efbc95e 100644 --- a/src/main/java/me/byteful/plugin/leveltools/LevelToolsPlugin.java +++ b/src/main/java/me/byteful/plugin/leveltools/LevelToolsPlugin.java @@ -2,15 +2,14 @@ import static me.byteful.plugin.leveltools.util.Text.colorize; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Objects; import java.util.concurrent.TimeUnit; import me.byteful.plugin.leveltools.api.AnvilCombineMode; +import me.byteful.plugin.leveltools.api.scheduler.Scheduler; import me.byteful.plugin.leveltools.listeners.AnvilListener; import me.byteful.plugin.leveltools.listeners.BlockEventListener; import me.byteful.plugin.leveltools.listeners.EntityEventListener; +import me.byteful.plugin.leveltools.util.LevelToolsUtil; import me.byteful.plugin.leveltools.util.UpdateChecker; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -19,14 +18,11 @@ import redempt.crunch.CompiledExpression; import redempt.crunch.Crunch; import redempt.redlib.RedLib; -import redempt.redlib.blockdata.BlockDataManager; -import redempt.redlib.misc.Task; import revxrsal.commands.bukkit.BukkitCommandHandler; public final class LevelToolsPlugin extends JavaPlugin { private static LevelToolsPlugin instance; - private BlockDataManager blockDataManager; private BukkitCommandHandler commandManager; private AnvilCombineMode anvilCombineMode; private UpdateChecker updateChecker; @@ -41,25 +37,9 @@ public static LevelToolsPlugin getInstance() { public void onEnable() { sendStartupBanner(); instance = this; - updateChecker = new UpdateChecker(this); - if (!getDataFolder().exists()) { - getDataFolder().mkdirs(); - } - - final Path blocksFile = getDataFolder().toPath().resolve("player_placed_blocks.db"); - - if (!Files.exists(blocksFile)) { - try { - blocksFile.toFile().createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - blockDataManager = BlockDataManager.createSQLite(this, blocksFile, true, true); - blockDataManager.migrate(); - getLogger().info("Loaded BlockDataManager..."); + final Scheduler scheduler = LevelToolsUtil.createScheduler(this); + updateChecker = new UpdateChecker(this, scheduler); saveDefaultConfig(); getConfig().options().copyDefaults(true); @@ -73,7 +53,7 @@ public void onEnable() { if (getConfig().getBoolean("update.periodically")) { final long delay = 20L * TimeUnit.DAYS.toSeconds(1); - Task.syncRepeating(() -> updateChecker.check(), delay, delay); + scheduler.syncTimer(() -> updateChecker.check(), delay, delay); } registerListeners(); @@ -99,11 +79,6 @@ public void onEnable() { @Override public void onDisable() { - if (blockDataManager != null) { - blockDataManager.saveAndClose(); - blockDataManager = null; - } - if (metrics != null) { metrics.shutdown(); } @@ -147,10 +122,6 @@ public void setLevelXpFormula() { getConfig().getString("level_xp_formula").replace("{current_level}", "$1")); } - public BlockDataManager getBlockDataManager() { - return blockDataManager; - } - public AnvilCombineMode getAnvilCombineMode() { return anvilCombineMode; } diff --git a/src/main/java/me/byteful/plugin/leveltools/api/scheduler/Scheduler.java b/src/main/java/me/byteful/plugin/leveltools/api/scheduler/Scheduler.java new file mode 100644 index 0000000..9ec4d79 --- /dev/null +++ b/src/main/java/me/byteful/plugin/leveltools/api/scheduler/Scheduler.java @@ -0,0 +1,7 @@ +package me.byteful.plugin.leveltools.api.scheduler; + +public interface Scheduler { + void asyncDelayed(Runnable runnable, long ticksDelay); + + void syncTimer(Runnable runnable, long ticksDelay, long ticksPeriod); +} diff --git a/src/main/java/me/byteful/plugin/leveltools/api/scheduler/impl/BukkitScheduler.java b/src/main/java/me/byteful/plugin/leveltools/api/scheduler/impl/BukkitScheduler.java new file mode 100644 index 0000000..71ad966 --- /dev/null +++ b/src/main/java/me/byteful/plugin/leveltools/api/scheduler/impl/BukkitScheduler.java @@ -0,0 +1,23 @@ +package me.byteful.plugin.leveltools.api.scheduler.impl; + +import me.byteful.plugin.leveltools.LevelToolsPlugin; +import me.byteful.plugin.leveltools.api.scheduler.Scheduler; +import org.bukkit.Bukkit; + +public class BukkitScheduler implements Scheduler { + private final LevelToolsPlugin plugin; + + public BukkitScheduler(LevelToolsPlugin plugin) { + this.plugin = plugin; + } + + @Override + public void asyncDelayed(Runnable runnable, long ticksDelay) { + Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, ticksDelay); + } + + @Override + public void syncTimer(Runnable runnable, long ticksDelay, long ticksPeriod) { + Bukkit.getScheduler().runTaskTimer(plugin, runnable, ticksDelay, ticksPeriod); + } +} diff --git a/src/main/java/me/byteful/plugin/leveltools/api/scheduler/impl/FoliaScheduler.java b/src/main/java/me/byteful/plugin/leveltools/api/scheduler/impl/FoliaScheduler.java new file mode 100644 index 0000000..2bc5f9a --- /dev/null +++ b/src/main/java/me/byteful/plugin/leveltools/api/scheduler/impl/FoliaScheduler.java @@ -0,0 +1,25 @@ +package me.byteful.plugin.leveltools.api.scheduler.impl; + +import me.byteful.plugin.leveltools.LevelToolsPlugin; +import me.byteful.plugin.leveltools.api.scheduler.Scheduler; +import org.bukkit.Bukkit; + +import java.util.concurrent.TimeUnit; + +public class FoliaScheduler implements Scheduler { + private final LevelToolsPlugin plugin; + + public FoliaScheduler(LevelToolsPlugin plugin) { + this.plugin = plugin; + } + + @Override + public void asyncDelayed(Runnable runnable, long ticksDelay) { + Bukkit.getAsyncScheduler().runDelayed(plugin, x -> runnable.run(), ticksDelay * 50, TimeUnit.MILLISECONDS); + } + + @Override + public void syncTimer(Runnable runnable, long ticksDelay, long ticksPeriod) { + Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, x -> runnable.run(), ticksDelay, ticksPeriod); + } +} diff --git a/src/main/java/me/byteful/plugin/leveltools/listeners/BlockEventListener.java b/src/main/java/me/byteful/plugin/leveltools/listeners/BlockEventListener.java index cd44ced..f39bd72 100644 --- a/src/main/java/me/byteful/plugin/leveltools/listeners/BlockEventListener.java +++ b/src/main/java/me/byteful/plugin/leveltools/listeners/BlockEventListener.java @@ -1,11 +1,12 @@ package me.byteful.plugin.leveltools.listeners; -import java.util.Objects; -import java.util.Set; +import com.jeff_media.morepersistentdatatypes.DataType; +import java.util.*; import java.util.stream.Collectors; import me.byteful.plugin.leveltools.LevelToolsPlugin; import me.byteful.plugin.leveltools.util.LevelToolsUtil; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -13,9 +14,30 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; -import redempt.redlib.blockdata.DataBlock; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.util.Vector; public class BlockEventListener extends XPListener { + private final NamespacedKey trackedBlocks = new NamespacedKey(LevelToolsPlugin.getInstance(), "tracked_blocks"); + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onBlockPlaceTracker(BlockPlaceEvent event) { + if (!LevelToolsPlugin.getInstance().getConfig().getBoolean("playerPlacedBlocks")) { + final PersistentDataContainer pdc = event.getBlock().getChunk().getPersistentDataContainer(); + if (!pdc.has(trackedBlocks)) { + pdc.set(trackedBlocks, DataType.asList(DataType.VECTOR), new ArrayList<>()); + } + + List blocks = pdc.get(trackedBlocks, DataType.asList(DataType.VECTOR)); + if (blocks == null) { + blocks = new ArrayList<>(); + } + + blocks.add(event.getBlock().getLocation().toVector()); + pdc.set(trackedBlocks, DataType.asList(DataType.VECTOR), blocks); + } + } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent e) { final Player player = e.getPlayer(); @@ -28,11 +50,13 @@ public void onBlockBreak(BlockBreakEvent e) { final ItemStack hand = LevelToolsUtil.getHand(player); if (!LevelToolsPlugin.getInstance().getConfig().getBoolean("playerPlacedBlocks")) { - final DataBlock db = - LevelToolsPlugin.getInstance().getBlockDataManager().getDataBlock(block, false); - - if (db != null && db.contains("level_tools") && db.getBoolean("level_tools")) { - return; + final PersistentDataContainer pdc = block.getChunk().getPersistentDataContainer(); + if (pdc.has(trackedBlocks)) { + final List blocks = pdc.get(trackedBlocks, DataType.asList(DataType.VECTOR)); + if (blocks != null && blocks.remove(block.getLocation().toVector())) { + pdc.set(trackedBlocks, DataType.asList(DataType.VECTOR), blocks); + return; + } } } @@ -63,14 +87,4 @@ public void onBlockBreak(BlockBreakEvent e) { player, LevelToolsUtil.getBlockModifier(block.getType())); } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onBlockPlace(BlockPlaceEvent e) { - if (!LevelToolsPlugin.getInstance().getConfig().getBoolean("playerPlacedBlocks")) { - LevelToolsPlugin.getInstance() - .getBlockDataManager() - .getDataBlockAsync(e.getBlockPlaced(), true) - .thenAccept(db -> db.set("level_tools", true)); - } - } } diff --git a/src/main/java/me/byteful/plugin/leveltools/util/LevelToolsUtil.java b/src/main/java/me/byteful/plugin/leveltools/util/LevelToolsUtil.java index 7c78890..4a772bf 100644 --- a/src/main/java/me/byteful/plugin/leveltools/util/LevelToolsUtil.java +++ b/src/main/java/me/byteful/plugin/leveltools/util/LevelToolsUtil.java @@ -21,6 +21,9 @@ import me.byteful.plugin.leveltools.api.item.LevelToolsItem; import me.byteful.plugin.leveltools.api.item.impl.NBTLevelToolsItem; import me.byteful.plugin.leveltools.api.item.impl.PDCLevelToolsItem; +import me.byteful.plugin.leveltools.api.scheduler.Scheduler; +import me.byteful.plugin.leveltools.api.scheduler.impl.BukkitScheduler; +import me.byteful.plugin.leveltools.api.scheduler.impl.FoliaScheduler; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.apache.commons.lang.math.NumberUtils; @@ -353,4 +356,21 @@ public static void sendActionBar(Player player, String msg) { ActionBar.sendActionBar(player, msg); } } + + public static Scheduler createScheduler(LevelToolsPlugin plugin) { + if (isFolia()) { + return new FoliaScheduler(plugin); + } + + return new BukkitScheduler(plugin); + } + + private static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } } diff --git a/src/main/java/me/byteful/plugin/leveltools/util/UpdateChecker.java b/src/main/java/me/byteful/plugin/leveltools/util/UpdateChecker.java index 0f3e5d4..41882c6 100644 --- a/src/main/java/me/byteful/plugin/leveltools/util/UpdateChecker.java +++ b/src/main/java/me/byteful/plugin/leveltools/util/UpdateChecker.java @@ -5,14 +5,16 @@ import java.net.URL; import java.util.Scanner; import me.byteful.plugin.leveltools.LevelToolsPlugin; +import me.byteful.plugin.leveltools.api.scheduler.Scheduler; import org.jetbrains.annotations.NotNull; -import redempt.redlib.misc.Task; public class UpdateChecker { @NotNull private final LevelToolsPlugin plugin; + private final Scheduler scheduler; - public UpdateChecker(@NotNull LevelToolsPlugin plugin) { + public UpdateChecker(@NotNull LevelToolsPlugin plugin, Scheduler scheduler) { this.plugin = plugin; + this.scheduler = scheduler; } public void check() { @@ -24,41 +26,41 @@ public void check() { return; } - Task.asyncDelayed( - plugin, - () -> { - try (final InputStream inputStream = - new URL("https://api.byteful.me/leveltools").openStream(); - final Scanner scanner = new Scanner(inputStream)) { - if (!scanner.hasNext()) { - return; - } + scheduler.asyncDelayed(() -> check0(currentVersion), 1L); + } + + private void check0(String currentVersion) { + try (final InputStream inputStream = + new URL("https://api.byteful.me/leveltools").openStream(); + final Scanner scanner = new Scanner(inputStream)) { + if (!scanner.hasNext()) { + return; + } - final String latestVersion = scanner.next(); + final String latestVersion = scanner.next(); - if (currentVersion.equals(latestVersion)) { - plugin.getLogger().info("No new updates found."); - } else { - plugin - .getLogger() - .info( - "A new update was found. You are on " - + currentVersion - + " while the latest version is " - + latestVersion - + "."); - plugin - .getLogger() - .info( - "Please install this update from: https://github.com/byteful/LevelTools/releases/download/v" - + latestVersion - + "/LevelTools-" - + latestVersion - + ".jar"); - } - } catch (IOException e) { - plugin.getLogger().info("Unable to check for updates: " + e.getMessage()); - } - }); + if (currentVersion.equals(latestVersion)) { + plugin.getLogger().info("No new updates found."); + } else { + plugin + .getLogger() + .info( + "A new update was found. You are on " + + currentVersion + + " while the latest version is " + + latestVersion + + "."); + plugin + .getLogger() + .info( + "Please install this update from: https://github.com/byteful/LevelTools/releases/download/v" + + latestVersion + + "/LevelTools-" + + latestVersion + + ".jar"); + } + } catch (IOException e) { + plugin.getLogger().info("Unable to check for updates: " + e.getMessage()); + } } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index eb43089..26f14f6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,6 +5,7 @@ author: byteful description: "A plugin that adds a leveling system to tools, swords, and bows." website: "https://github.com/byteful/LevelTools" api-version: "1.13" +folia-supported: true softdepend: - PlaceholderAPI permissions: @@ -13,4 +14,4 @@ permissions: default: op leveltools.enabled: description: "Permission to let a player level up a tool." - default: true \ No newline at end of file + default: true