Added support for Folia

This commit is contained in:
Intelli 2023-03-23 19:45:44 -06:00
parent 29345e2fb1
commit 7170e29452
20 changed files with 1095 additions and 942 deletions

View File

@ -82,7 +82,7 @@
</repository>
<repository>
<id>paper-repo</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>codemc-repo</id>
@ -100,9 +100,9 @@
bukkit: org.bukkit
-->
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -21,6 +21,7 @@ import net.coreprotect.listener.player.PlayerQuitListener;
import net.coreprotect.paper.PaperAdapter;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.NetworkHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.Util;
@ -94,7 +95,7 @@ public final class CoreProtect extends JavaPlugin {
Chat.console(Phrase.build(Phrase.LINK_DISCORD, "www.coreprotect.net/discord/"));
Chat.console("--------------------");
getServer().getScheduler().scheduleSyncDelayedTask(this, () -> {
Scheduler.scheduleSyncDelayedTask(this, () -> {
try {
Thread networkHandler = new Thread(new NetworkHandler(true, true));
networkHandler.start();

View File

@ -20,6 +20,7 @@ import org.bukkit.World;
import net.coreprotect.CoreProtect;
import net.coreprotect.language.Language;
import net.coreprotect.thread.Scheduler;
public class Config extends Language {
@ -383,7 +384,7 @@ public class Config extends Language {
// for now this solution is good enough to ensure we only modify on the main thread
final CompletableFuture<Void> complete = new CompletableFuture<>();
Bukkit.getScheduler().runTask(CoreProtect.getInstance(), () -> {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
try {
parseConfig(data);
}

View File

@ -55,6 +55,7 @@ public class ConfigHandler extends Queue {
public static HikariDataSource hikariDataSource = null;
public static final boolean isSpigot = Util.isSpigot();
public static final boolean isPaper = Util.isPaper();
public static final boolean isFolia = Util.isFolia();
public static volatile boolean serverRunning = false;
public static volatile boolean converterRunning = false;
public static volatile boolean purgeRunning = false;

View File

@ -24,6 +24,7 @@ import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.process.Process;
import net.coreprotect.listener.block.BlockUtil;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Util;
public class Queue {
@ -85,7 +86,7 @@ public class Queue {
}
protected static void queueBlockBreakValidate(final String user, final Block block, final BlockState blockState, final Material type, final String blockData, final int extraData, int ticks) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
try {
if (!block.getType().equals(type)) {
queueBlockBreak(user, blockState, type, blockData, null, extraData, 0);
@ -94,7 +95,7 @@ public class Queue {
catch (Exception e) {
e.printStackTrace();
}
}, ticks);
}, block.getLocation(), ticks);
}
protected static void queueBlockBreak(String user, BlockState block, Material type, String blockData, Material breakType, int extraData, int blockNumber) {
@ -178,18 +179,18 @@ public class Queue {
}
protected static void queueBlockPlaceDelayed(final String user, final Location placed, final Material type, final String blockData, final BlockState replaced, int ticks) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
try {
queueBlockPlace(user, placed.getBlock().getState(), type, replaced, null, -1, 0, blockData);
}
catch (Exception e) {
e.printStackTrace();
}
}, ticks);
}, placed, ticks);
}
protected static void queueBlockPlaceValidate(final String user, final BlockState blockLocation, final Block block, final BlockState blockReplaced, final Material forceT, final int forceD, final int forceData, final String blockData, int ticks) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
try {
Material blockType = block.getType();
if (blockType.equals(forceT)) {
@ -204,11 +205,11 @@ public class Queue {
catch (Exception e) {
e.printStackTrace();
}
}, ticks);
}, blockLocation.getLocation(), ticks);
}
protected static void queueBlockGravityValidate(final String user, final Location location, final Block block, final Material blockType, int ticks) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
try {
Block placementBlock = BlockUtil.gravityScan(location, blockType, user);
if (!block.equals(placementBlock)) {
@ -218,7 +219,7 @@ public class Queue {
catch (Exception e) {
e.printStackTrace();
}
}, ticks);
}, location, ticks);
}
protected static void queueContainerBreak(String user, Location location, Material type, ItemStack[] oldInventory) {

View File

@ -6,7 +6,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -24,6 +23,7 @@ import net.coreprotect.consumer.Queue;
import net.coreprotect.consumer.process.Process;
import net.coreprotect.language.Phrase;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Util;
@ -46,9 +46,9 @@ public class ContainerRollback extends Queue {
Queue.queueRollbackUpdate(userString, location, lookupList, Process.CONTAINER_ROLLBACK_UPDATE, rollbackType); // Perform update transaction in consumer
final String finalUserString = userString;
ConfigHandler.rollbackHash.put(userString, new int[] { 0, 0, 0, 0 });
ConfigHandler.rollbackHash.put(userString, new int[] { 0, 0, 0, 0, 0 });
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), new Runnable() {
Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), new Runnable() {
@Override
public void run() {
try {
@ -140,13 +140,13 @@ public class ContainerRollback extends Queue {
}
matchingFrames.clear();
ConfigHandler.rollbackHash.put(finalUserString, new int[] { itemCount, modifyCount, entityCount, 1 });
ConfigHandler.rollbackHash.put(finalUserString, new int[] { itemCount, modifyCount, entityCount, 1, 1 });
}
catch (Exception e) {
e.printStackTrace();
}
}
}, 0);
}, location, 0);
int[] rollbackHashData = ConfigHandler.rollbackHash.get(finalUserString);
int next = rollbackHashData[3];

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,6 @@ package net.coreprotect.listener.entity;
import java.util.Locale;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
@ -35,6 +34,7 @@ import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.Database;
import net.coreprotect.listener.player.PlayerInteractEntityListener;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Util;
public final class EntityDamageByEntityListener extends Queue implements Listener {
@ -125,13 +125,13 @@ public final class EntityDamageByEntityListener extends Queue implements Listene
if (Config.getConfig(entityLocation.getWorld()).ITEM_TRANSACTIONS) {
String killer = user;
ItemStack[] contents = Util.getContainerContents(Material.ARMOR_STAND, entity, block.getLocation());
Bukkit.getScheduler().runTask(CoreProtect.getInstance(), () -> {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
if (entity != null && entity.isDead()) {
entityLocation.setY(entityLocation.getY() + 0.99);
Database.containerBreakCheck(killer, Material.ARMOR_STAND, entity, contents, block.getLocation());
Queue.queueBlockBreak(killer, block.getState(), Material.ARMOR_STAND, null, (int) entityLocation.getYaw());
}
});
}, entity);
}
}
}

View File

@ -67,6 +67,7 @@ import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.consumer.Queue;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.serialize.ItemMetaHandler;
public final class EntityDeathListener extends Queue implements Listener {
@ -96,13 +97,13 @@ public final class EntityDeathListener extends Queue implements Listener {
}
}
Bukkit.getScheduler().runTask(CoreProtect.getInstance(), () -> {
for (LivingEntity entity : entityList) {
for (LivingEntity entity : entityList) {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
if (entity != null && entity.isDead()) {
logEntityDeath(entity, "#command");
}
}
});
}, entity);
}
}
protected static void logEntityDeath(LivingEntity entity, String e) {

View File

@ -1,6 +1,5 @@
package net.coreprotect.listener.player;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -15,6 +14,7 @@ import org.bukkit.event.entity.FoodLevelChangeEvent;
import net.coreprotect.CoreProtect;
import net.coreprotect.consumer.Queue;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Util;
public final class FoodLevelChangeListener extends Queue implements Listener {
@ -50,7 +50,7 @@ public final class FoodLevelChangeListener extends Queue implements Listener {
}
final Material oldBlockType = oldType;
Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
try {
Block newBlock = oldBlockState.getBlock();
BlockState newBlockState = newBlock.getState();
@ -66,7 +66,7 @@ public final class FoodLevelChangeListener extends Queue implements Listener {
catch (Exception e) {
e.printStackTrace();
}
});
}, oldBlockState.getLocation());
}
CacheHandler.interactCache.remove(coordinates);

View File

@ -5,7 +5,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.FurnaceInventory;
@ -16,6 +15,7 @@ import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Util;
public final class HopperPullListener {
@ -38,7 +38,7 @@ public final class HopperPullListener {
ItemStack movedItem = item.clone();
final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
Bukkit.getServer().getScheduler().runTaskAsynchronously(CoreProtect.getInstance(), () -> {
Scheduler.runTaskAsynchronously(CoreProtect.getInstance(), () -> {
try {
if (sourceHolder == null || destinationHolder == null) {
return;

View File

@ -5,7 +5,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.FurnaceInventory;
@ -16,6 +15,7 @@ import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Util;
public final class HopperPushListener {
@ -38,7 +38,7 @@ public final class HopperPushListener {
ItemStack movedItem = item.clone();
final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
Bukkit.getServer().getScheduler().runTaskAsynchronously(CoreProtect.getInstance(), () -> {
Scheduler.runTaskAsynchronously(CoreProtect.getInstance(), () -> {
try {
if (sourceHolder == null || destinationHolder == null) {
return;

View File

@ -6,7 +6,6 @@ import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicLong;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -32,6 +31,7 @@ import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.paper.PaperAdapter;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Util;
import net.coreprotect.utility.Validate;
@ -227,7 +227,7 @@ public final class InventoryChangeListener extends Queue implements Listener {
ItemStack[] containerState = Util.getContainerState(inventory.getContents());
final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
Bukkit.getServer().getScheduler().runTaskAsynchronously(CoreProtect.getInstance(), () -> {
Scheduler.runTaskAsynchronously(CoreProtect.getInstance(), () -> {
try {
Material containerType = (enderChest != true ? null : Material.ENDER_CHEST);
InventoryChangeListener.checkTasks(taskStarted);

View File

@ -8,7 +8,6 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -55,6 +54,7 @@ import net.coreprotect.database.lookup.SignMessageLookup;
import net.coreprotect.language.Phrase;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Chat;
import net.coreprotect.utility.Color;
import net.coreprotect.utility.Util;
@ -735,7 +735,7 @@ public final class PlayerInteractListener extends Queue implements Listener {
if (!exists) {
final Player playerFinal = player;
final Location locationFinal = crystalLocation;
Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
try {
boolean blockExists = false;
int showingBottom = 0;
@ -755,7 +755,7 @@ public final class PlayerInteractListener extends Queue implements Listener {
catch (Exception e) {
e.printStackTrace();
}
});
}, locationFinal);
}
}
}

View File

@ -0,0 +1,91 @@
package net.coreprotect.thread;
import java.util.concurrent.TimeUnit;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
public class Scheduler {
private Scheduler() {
throw new IllegalStateException("Scheduler class");
}
public static void scheduleSyncDelayedTask(CoreProtect plugin, Runnable task, Object regionData, int delay) {
if (ConfigHandler.isFolia) {
if (regionData instanceof Location) { // REGION
Location location = (Location) regionData;
if (delay == 0) {
Bukkit.getServer().getRegionScheduler().run(plugin, location, value -> task.run());
}
else {
Bukkit.getServer().getRegionScheduler().runDelayed(plugin, location, value -> task.run(), delay);
}
}
else if (regionData instanceof Entity) { // ENTITY
Entity entity = (Entity) regionData;
if (delay == 0) {
entity.getScheduler().run(plugin, value -> task.run(), null);
}
else {
entity.getScheduler().runDelayed(plugin, value -> task.run(), null, delay);
}
}
else { // GLOBAL
if (delay == 0) {
Bukkit.getServer().getGlobalRegionScheduler().run(plugin, value -> task.run());
}
else {
Bukkit.getServer().getGlobalRegionScheduler().runDelayed(plugin, value -> task.run(), delay);
}
}
}
else { // BUKKIT
if (delay == 0) {
Bukkit.getServer().getScheduler().runTask(plugin, task);
}
else {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, task, delay);
}
}
}
public static void scheduleAsyncDelayedTask(CoreProtect plugin, Runnable task, int delay) {
if (ConfigHandler.isFolia) {
if (delay == 0) {
Bukkit.getServer().getAsyncScheduler().runNow(plugin, value -> task.run());
}
else {
Bukkit.getServer().getAsyncScheduler().runDelayed(plugin, value -> task.run(), (delay * 50L), TimeUnit.MILLISECONDS);
}
}
else { // BUKKIT
if (delay == 0) {
Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, task);
}
else {
Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(plugin, task, delay);
}
}
}
public static void scheduleSyncDelayedTask(CoreProtect plugin, Runnable task, int delay) {
scheduleSyncDelayedTask(plugin, task, null, delay);
}
public static void runTask(CoreProtect plugin, Runnable task) {
scheduleSyncDelayedTask(plugin, task, null, 0);
}
public static void runTask(CoreProtect plugin, Runnable task, Object regionData) {
scheduleSyncDelayedTask(plugin, task, regionData, 0);
}
public static void runTaskAsynchronously(CoreProtect plugin, Runnable task) {
scheduleAsyncDelayedTask(plugin, task, 0);
}
}

View File

@ -1,6 +1,5 @@
package net.coreprotect.utility;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
@ -9,6 +8,7 @@ import org.bukkit.block.data.type.Chest;
import org.bukkit.block.data.type.Chest.Type;
import net.coreprotect.CoreProtect;
import net.coreprotect.thread.Scheduler;
public class ChestTool {
@ -75,7 +75,7 @@ public class ChestTool {
}
private static void validateContainer(BlockData blockData, Type newType, Block block, Block relativeBlock) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
try {
BlockData relativeBlockData = relativeBlock.getBlockData();
if (!blockData.getAsString().equals(block.getBlockData().getAsString()) || !(relativeBlockData instanceof Chest) || ((Chest) relativeBlockData).getType() == newType) {
@ -89,7 +89,7 @@ public class ChestTool {
catch (Exception e) {
e.printStackTrace();
}
}, 2);
}, relativeBlock.getLocation(), 2);
}
}

View File

@ -59,6 +59,7 @@ import net.coreprotect.database.Rollback;
import net.coreprotect.language.Phrase;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.serialize.ItemMetaHandler;
import net.coreprotect.worldedit.CoreProtectEditSessionEvent;
@ -1281,6 +1282,17 @@ public class Util extends Queue {
return true;
}
public static boolean isFolia() {
try {
Class.forName("io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler");
}
catch (Exception e) {
return false;
}
return true;
}
public static String getBranch() {
String branch = "";
try {
@ -1521,7 +1533,7 @@ public class Util extends Queue {
}
public static void updateBlock(final BlockState block) {
Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
try {
if (block.getBlockData() instanceof Waterlogged) {
Block currentBlock = block.getBlock();
@ -1534,7 +1546,7 @@ public class Util extends Queue {
catch (Exception e) {
e.printStackTrace();
}
});
}, block.getLocation());
}
public static void updateInventory(Player player) {

View File

@ -59,6 +59,7 @@ import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.database.Rollback;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Util;
public class EntityUtil {
@ -71,7 +72,7 @@ public class EntityUtil {
if (type == null) {
return;
}
Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
try {
Location location = block.getLocation();
location.setX(location.getX() + 0.50);
@ -557,7 +558,7 @@ public class EntityUtil {
catch (Exception e) {
e.printStackTrace();
}
});
}, block.getLocation());
}
}

View File

@ -14,6 +14,7 @@ import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.language.Phrase;
import net.coreprotect.language.Selector;
import net.coreprotect.thread.Scheduler;
import net.coreprotect.utility.Chat;
public class CoreProtectEditSessionEvent {
@ -47,7 +48,7 @@ public class CoreProtectEditSessionEvent {
// Failed to initialize WorldEdit logging
}
Bukkit.getServer().getScheduler().runTask(CoreProtect.getInstance(), () -> {
Scheduler.runTask(CoreProtect.getInstance(), () -> {
try {
if (isInitialized()) {
Chat.console(Phrase.build(Phrase.INTEGRATION_SUCCESS, "WorldEdit", Selector.FIRST));

View File

@ -3,6 +3,7 @@ main: net.coreprotect.CoreProtect
version: ${project.version}
branch: ${project.branch}
api-version: 1.13
folia-supported: true
website: http://coreprotect.net
author: Intelli
softdepend: [WorldEdit]