Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add Folia Support #349

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Minepacks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
<artifactId>PluginLib</artifactId>
<version>${pcgfPluginLibVersion}</version>
</dependency>
<!-- FoliaLib -->
<dependency>
<groupId>com.github.technicallycoded</groupId>
<artifactId>FoliaLib</artifactId>
<version>0.4.3</version>
<scope>compile</scope>
</dependency>
<!-- Tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -143,13 +150,18 @@
<include>at.pcgamingfreaks:Minepacks-API</include>
<include>at.pcgamingfreaks:Minepacks-MagicValues</include>
<include>at.pcgamingfreaks:PluginLib</include>
<include>com.github.technicallycoded:FoliaLib</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>at.pcgf.libs</pattern>
<shadedPattern>at.pcgamingfreaks.MinepacksStandalone.libs</shadedPattern>
</relocation>
<relocation>
<pattern>com.tcoded.folialib</pattern>
<shadedPattern>at.pcgamingfreaks.MinepacksStandalone.libs.folialib</shadedPattern>
</relocation>
<relocation>
<pattern>at.pcgamingfreaks.Minepacks</pattern>
<shadedPattern>at.pcgamingfreaks.MinepacksStandalone</shadedPattern>
Expand Down
1 change: 1 addition & 0 deletions Minepacks/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version: "${pluginVersion}"
api-version: "1.13"
depend: [${dependencies}]
softdepend: [MVdWPlaceholderAPI, PlaceholderAPI ${soft-dependencies}]
folia-supported: true

permissions:
backpack.*:
Expand Down
10 changes: 6 additions & 4 deletions Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Backpack.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ public Backpack(final OfflinePlayer owner, ItemStack[] backpack, final int ID)
Minepacks.getInstance().getLogger().warning(owner.getName() + "'s backpack has to many items.");
if(owner.isOnline())
{
Bukkit.getScheduler().runTask(Minepacks.getInstance(), () -> {
Minepacks.getScheduler().runNextTick(task -> {
if(owner.isOnline())
{
Player player = owner.getPlayer();
assert player != null;
Map<Integer, ItemStack> left = player.getInventory().addItem(toMuch.toArray(new ItemStack[0]));
left.forEach((id, stack) -> player.getWorld().dropItemNaturally(player.getLocation(), stack));
this.setChanged();
Minepacks.getScheduler().runAtEntity(player, task1 -> {
Map<Integer, ItemStack> left = player.getInventory().addItem(toMuch.toArray(new ItemStack[0]));
left.forEach((id, stack) -> player.getWorld().dropItemNaturally(player.getLocation(), stack));
this.setChanged();
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package at.pcgamingfreaks.Minepacks.Bukkit;

import com.tcoded.folialib.impl.PlatformScheduler;
import com.tcoded.folialib.wrapper.task.WrappedTask;

public abstract class CancellableRunnable {
protected WrappedTask task = null;

public abstract void run();
public abstract void schedule();

public void cancel() {
if (task != null) {
task.cancel();
}
}

protected PlatformScheduler getScheduler() {
return Minepacks.getScheduler();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,10 @@ private void debugSystem(final @NotNull CommandSender commandSender)
ItemStack slot = sender.getInventory().getItem(0);
if(slot == null || slot.getAmount() == 0 || slot.getType() == Material.AIR) sender.getInventory().setItem(0, new ItemStack(Material.ACACIA_BOAT));

Bukkit.getServer().getScheduler().runTaskLater(plugin, () -> {
sender.performCommand("backpack");
}, 5*20L);
Bukkit.getServer().getScheduler().runTaskLater(plugin, () -> {
Bukkit.getPluginManager().callEvent(new ClickEvent(sender.getOpenInventory(), InventoryType.SlotType.QUICKBAR, InventoryUtils.getPlayerTopInventory(sender).getSize() + 27, ClickType.LEFT, InventoryAction.PICKUP_ALL));
}, 10*20L);
Bukkit.getServer().getScheduler().runTaskLater(plugin, sender::closeInventory, 20*20L);
Bukkit.getServer().getScheduler().runTaskLater(plugin, () -> {
Minepacks.getScheduler().runAtEntityLater(sender, () -> sender.performCommand("backpack"), 5*20L);
Minepacks.getScheduler().runAtEntityLater(sender, () -> Bukkit.getPluginManager().callEvent(new ClickEvent(sender.getOpenInventory(), InventoryType.SlotType.QUICKBAR, InventoryUtils.getPlayerTopInventory(sender).getSize() + 27, ClickType.LEFT, InventoryAction.PICKUP_ALL)), 10*20L);
Minepacks.getScheduler().runAtEntityLater(sender, sender::closeInventory, 20*20L);
Minepacks.getScheduler().runLater(() -> {
try
{
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CooldownManager extends BukkitRunnable implements Listener
public class CooldownManager extends CancellableRunnable implements Listener
{
private final Minepacks plugin;
private final Map<UUID, Long> cooldowns = new HashMap<>();
Expand All @@ -47,8 +46,7 @@ public CooldownManager(Minepacks plugin)
addOnJoin = plugin.getConfiguration().isCommandCooldownAddOnJoinEnabled();
clearOnLeave = plugin.getConfiguration().isCommandCooldownClearOnLeaveEnabled();
plugin.getServer().getPluginManager().registerEvents(this, plugin);

runTaskTimer(plugin, plugin.getConfiguration().getCommandCooldownCleanupInterval(), plugin.getConfiguration().getCommandCooldownCleanupInterval());
schedule();
}

public void close()
Expand Down Expand Up @@ -109,4 +107,9 @@ public void run()
{
cooldowns.entrySet().removeIf(entry -> entry.getValue() < System.currentTimeMillis());
}
}

@Override
public void schedule() {
task = getScheduler().runTimer(this::run, plugin.getConfiguration().getCommandCooldownCleanupInterval(), plugin.getConfiguration().getCommandCooldownCleanupInterval());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void migrateDB(final String targetDatabaseType, final MigrationCallback c
}
//endregion
//region Migrate data
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
Minepacks.getScheduler().runAsync(task -> {
MigrationResult migrationResult = null;
try
{
Expand All @@ -80,7 +80,7 @@ public void migrateDB(final String targetDatabaseType, final MigrationCallback c

//region Start the plugin again
final MigrationResult migrationResultFinal = migrationResult;
plugin.getServer().getScheduler().runTask(plugin, () -> {
Minepacks.getScheduler().runNextTick(task1 -> {
db.close();
// No need to reload the config
try
Expand Down
19 changes: 9 additions & 10 deletions Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Utils;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -172,7 +171,7 @@ protected String replacePlaceholders(@Language("SQL") String query)

protected void runStatementAsync(final String query, final Object... args)
{
Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, () -> runStatement(query, args));
Minepacks.getScheduler().runAsync(task -> runStatement(query, args));
}

protected void runStatement(final String query, final Object... args)
Expand Down Expand Up @@ -219,7 +218,7 @@ public void saveBackpack(final Backpack backpack)
{
final int newID = rs.getInt(fieldPlayerID);
DBTools.runStatement(connection, queryInsertBp, newID, data, usedSerializer);
plugin.getServer().getScheduler().runTask(plugin, () -> backpack.setOwnerDatabaseId(newID));
Minepacks.getScheduler().runNextTick(task -> backpack.setOwnerDatabaseId(newID));
}
else
{
Expand All @@ -240,13 +239,13 @@ public void saveBackpack(final Backpack backpack)
writeBackup(name, nameOrUUID, usedSerializer, data);
}
};
if(asyncSave) Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable); else runnable.run();
if(asyncSave) Minepacks.getScheduler().runAsync(task -> runnable.run()); else runnable.run();
}

@Override
protected void loadBackpack(final OfflinePlayer player, final Callback<Backpack> callback)
{
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
Minepacks.getScheduler().runAsync(task -> {
try(Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(queryGetBP))
{
final String playerUUID = getPlayerFormattedUUID(player);
Expand Down Expand Up @@ -275,7 +274,7 @@ protected void loadBackpack(final OfflinePlayer player, final Callback<Backpack>
writeBackup(player.getName(), playerUUID, version, data);
}
final Backpack backpack = (its != null) ? new Backpack(player, its, bpID) : null;
plugin.getServer().getScheduler().runTask(plugin, () -> {
Minepacks.getScheduler().runNextTick(task1 -> {
if(backpack != null)
{
callback.onResult(backpack);
Expand All @@ -289,7 +288,7 @@ protected void loadBackpack(final OfflinePlayer player, final Callback<Backpack>
catch(SQLException e)
{
plugin.getLogger().log(Level.SEVERE, "Failed to load backpack! Error: {0}", e.getMessage());
plugin.getServer().getScheduler().runTask(plugin, callback::onFail);
Minepacks.getScheduler().runNextTick(task1 -> callback.onFail());
}
});
}
Expand All @@ -304,20 +303,20 @@ public void syncCooldown(Player player, long cooldownTime)
@Override
public void getCooldown(final Player player, final Callback<Long> callback)
{
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
Minepacks.getScheduler().runAsync(asyncTask -> {
try(Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(queryGetCooldown))
{
ps.setString(1, getPlayerFormattedUUID(player));
try(ResultSet rs = ps.executeQuery())
{
final long time = (rs.next()) ? rs.getTimestamp(fieldCdTime).getTime() : 0;
plugin.getServer().getScheduler().runTask(plugin, () -> callback.onResult(time));
Minepacks.getScheduler().runNextTick(task -> callback.onResult(time));
}
}
catch(SQLException e)
{
plugin.getLogger().log(Level.SEVERE, "Failed to load cooldown! Error: {0}", e.getMessage());
plugin.getServer().getScheduler().runTask(plugin, () -> callback.onResult(0L));
Minepacks.getScheduler().runNextTick(task -> callback.onResult(0L));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import at.pcgamingfreaks.Database.DBTools;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Version;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -124,7 +123,7 @@ protected void checkDB()
@Override
public void updatePlayer(final Player player)
{
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
Minepacks.getScheduler().runAsync(task -> {
runStatement(queryUpdatePlayerAdd, player.getName(), getPlayerFormattedUUID(player));
runStatement("UPDATE `" + tablePlayers + "` SET `" + fieldPlayerName + "`=? WHERE `" + fieldPlayerUUID + "`=?;", player.getName(), getPlayerFormattedUUID(player));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Backpack;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Database;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import org.bukkit.Bukkit;
import com.tcoded.folialib.wrapper.task.WrappedTask;

public class Interval extends UnCacheStrategy implements Runnable
{
private final int taskID;
private final WrappedTask task;

public Interval(Database cache)
{
super(cache);
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Minepacks.getInstance(), this, Minepacks.getInstance().getConfiguration().getUnCacheDelay(), Minepacks.getInstance().getConfiguration().getUnCacheInterval());
task = Minepacks.getScheduler().runTimer(this, Minepacks.getInstance().getConfiguration().getUnCacheDelay(), Minepacks.getInstance().getConfiguration().getUnCacheInterval());
}

@Override
Expand All @@ -47,7 +47,7 @@ public void run()
@Override
public void close()
{
Bukkit.getScheduler().cancelTask(taskID);
Minepacks.getScheduler().cancelTask(task);
super.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Database;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;

import org.bukkit.Bukkit;
import com.tcoded.folialib.wrapper.task.WrappedTask;
import org.bukkit.OfflinePlayer;

public class IntervalChecked extends UnCacheStrategy implements Runnable
{
private final long delay;
private final int taskID;
private final WrappedTask task;

public IntervalChecked(Database cache)
{
super(cache);
long delayTicks = Minepacks.getInstance().getConfiguration().getUnCacheDelay();
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Minepacks.getInstance(), this, delayTicks, Minepacks.getInstance().getConfiguration().getUnCacheInterval());
task = Minepacks.getScheduler().runTimer(this, delayTicks, Minepacks.getInstance().getConfiguration().getUnCacheInterval());
this.delay = delayTicks * 50L;
}

Expand All @@ -54,7 +54,7 @@ public void run()
@Override
public void close()
{
Bukkit.getScheduler().cancelTask(taskID);
Minepacks.getScheduler().cancelTask(task);
super.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

public class OnDisconnectDelayed extends UnCacheStrategy implements Listener
{
Expand All @@ -45,21 +44,20 @@ public void playerLeaveEvent(PlayerQuitEvent event)
final Backpack backpack = cache.getBackpack(event.getPlayer());
if(backpack != null) // We only uncache unmarried player.
{
new BukkitRunnable()
{
@Override
public void run()
Minepacks.getScheduler().runLater(() -> {
if (!backpack.isOpen())
{
if(!backpack.isOpen())
cache.unloadBackpack(backpack);
} else {
Minepacks.getScheduler().runLater(() ->
{
cache.unloadBackpack(backpack);
}
else
{
this.runTaskLater(Minepacks.getInstance(), delay);
}
if (!backpack.isOpen())
{
cache.unloadBackpack(backpack);
}
}, delay);
}
}.runTaskLater(Minepacks.getInstance(), delay);
}, delay);
}
}

Expand Down
Loading