Skip to content

Commit

Permalink
Merge branch 'master' into v1.3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
byteful authored Jun 17, 2024
2 parents 5ee43d1 + 6ed8356 commit 1fb07ab
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 94 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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,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 {
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;
}
}
}
Loading

0 comments on commit 1fb07ab

Please sign in to comment.