Skip to content

Commit

Permalink
v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
byteful committed Jun 3, 2024
1 parent 6d5de38 commit 3e942b4
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 91 deletions.
13 changes: 11 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -40,14 +46,16 @@ 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.5'
compileOnly 'me.clip:placeholderapi:2.11.6'
}

group = 'me.byteful.plugin'
version = '1.3.7'
version = '1.4.0'
description = 'LevelTools'
java.sourceCompatibility = JavaVersion.VERSION_1_8

Expand All @@ -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
Expand Down
39 changes: 5 additions & 34 deletions src/main/java/me/byteful/plugin/leveltools/LevelToolsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -99,11 +79,6 @@ public void onEnable() {

@Override
public void onDisable() {
if (blockDataManager != null) {
blockDataManager.saveAndClose();
blockDataManager = null;
}

if (metrics != null) {
metrics.shutdown();
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
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;
import org.bukkit.event.EventPriority;
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<Vector> 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();
Expand All @@ -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<Vector> 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;
}
}
}

Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
74 changes: 38 additions & 36 deletions src/main/java/me/byteful/plugin/leveltools/util/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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());
}
}
}
Loading

0 comments on commit 3e942b4

Please sign in to comment.