Add folia support

This commit is contained in:
ceze88 2023-05-17 12:57:17 +02:00
parent 549a1ce9f4
commit 06041b8780
12 changed files with 120 additions and 97 deletions

31
pom.xml
View File

@ -112,17 +112,17 @@
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.songoda</groupId>
<artifactId>SongodaCore</artifactId>
<version>2.6.21</version>
<version>2.6.22</version>
<scope>compile</scope>
</dependency>
@ -140,18 +140,25 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.minebuilders</groupId>
<artifactId>Clearlag</artifactId>
<version>3.0.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.lumine</groupId>
<artifactId>Mythic-Dist</artifactId>
<version>5.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -32,7 +32,6 @@ import com.songoda.ultimatestacker.hook.StackerHook;
import com.songoda.ultimatestacker.hook.hooks.JobsHook;
import com.songoda.ultimatestacker.listeners.BlockListeners;
import com.songoda.ultimatestacker.listeners.BreedListeners;
import com.songoda.ultimatestacker.listeners.ClearLagListeners;
import com.songoda.ultimatestacker.listeners.DeathListeners;
import com.songoda.ultimatestacker.listeners.InteractListeners;
import com.songoda.ultimatestacker.listeners.ShearListeners;
@ -57,7 +56,7 @@ import com.songoda.ultimatestacker.stackable.spawner.SpawnerStackManager;
import com.songoda.ultimatestacker.tasks.StackingTask;
import com.songoda.ultimatestacker.utils.Async;
import com.songoda.ultimatestacker.utils.Methods;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@ -210,8 +209,8 @@ public class UltimateStacker extends SongodaPlugin {
pluginManager.registerEvents(new SpawnerListeners(this), this);
pluginManager.registerEvents(new SheepDyeListeners(this), this);
if (Settings.CLEAR_LAG.getBoolean() && pluginManager.isPluginEnabled("ClearLag"))
pluginManager.registerEvents(new ClearLagListeners(this), this);
// if (Settings.CLEAR_LAG.getBoolean() && pluginManager.isPluginEnabled("ClearLag"))
// pluginManager.registerEvents(new ClearLagListeners(this), this);
// Register Hooks
if (pluginManager.isPluginEnabled("Jobs"))

View File

@ -12,7 +12,7 @@ import com.songoda.ultimatestacker.stackable.block.BlockStack;
import com.songoda.ultimatestacker.stackable.block.BlockStackManager;
import com.songoda.ultimatestacker.stackable.spawner.SpawnerStack;
import com.songoda.ultimatestacker.utils.Methods;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;

View File

@ -1,5 +1,6 @@
package com.songoda.ultimatestacker.listeners;
import com.songoda.SchedulerUtils;
import com.songoda.ultimatestacker.UltimateStacker;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
@ -17,7 +18,7 @@ public class BreedListeners implements Listener {
@EventHandler
public void onBread(EntityBreedEvent event) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
SchedulerUtils.runEntityTask(plugin, event.getEntity(), () -> {
event.getFather().removeMetadata("breedCooldown", plugin);
event.getMother().removeMetadata("breedCooldown", plugin);
}, 5 * 20 * 60);

View File

@ -1,27 +0,0 @@
package com.songoda.ultimatestacker.listeners;
import com.songoda.ultimatestacker.UltimateStacker;
import me.minebuilders.clearlag.events.EntityRemoveEvent;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class ClearLagListeners implements Listener {
private final UltimateStacker plugin;
public ClearLagListeners(UltimateStacker plugin) {
this.plugin = plugin;
}
@EventHandler
public void onClearLaggTask(EntityRemoveEvent event) {
for (Entity entity : event.getWorld().getEntities()) {
if (entity instanceof LivingEntity && plugin.getEntityStackManager().isStackedEntity(entity)) {
plugin.getEntityStackManager().getStack((LivingEntity) entity).destroy();
event.addEntity(entity);
}
}
}
}

View File

@ -1,5 +1,6 @@
package com.songoda.ultimatestacker.listeners;
import com.songoda.SchedulerUtils;
import com.songoda.core.compatibility.ServerProject;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.lootables.loot.Drop;
@ -108,7 +109,7 @@ public class DeathListeners implements Listener {
commands.add(command);
}
});
Bukkit.getScheduler().runTask(plugin, () -> {
SchedulerUtils.runTask(plugin, () -> {
for (String command : commands) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}

View File

@ -1,5 +1,6 @@
package com.songoda.ultimatestacker.listeners;
import com.songoda.SchedulerUtils;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.ultimatestacker.UltimateStacker;
@ -66,7 +67,7 @@ public class InteractListeners implements Listener {
}
entity.setMetadata("inLove", new FixedMetadataValue(plugin, true));
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
SchedulerUtils.runEntityTask(plugin, entity, () -> {
if (entity.isDead()) return;
entity.removeMetadata("inLove", plugin);
}, 20 * 20);

View File

@ -1,5 +1,6 @@
package com.songoda.ultimatestacker.listeners.entity;
import com.songoda.SchedulerUtils;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.settings.Settings;
@ -89,7 +90,7 @@ public class EntityListeners implements Listener {
if (plugin.getEntityStackManager().isStackedEntity(entity)
&& Settings.DISABLE_KNOCKBACK.getBoolean()
&& ((Player) event.getDamager()).getItemInHand().getEnchantmentLevel(Enchantment.KNOCKBACK) == 0) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
SchedulerUtils.runEntityTask(plugin, entity, () -> {
event.getEntity().setVelocity(new Vector());
}, 0L);
}

View File

@ -4,7 +4,7 @@ import com.songoda.core.nms.NmsManager;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.settings.Settings;
import com.songoda.ultimatestacker.utils.Methods;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Item;

View File

@ -1,5 +1,6 @@
package com.songoda.ultimatestacker.stackable.entity;
import com.songoda.SchedulerUtils;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.lootables.loot.Drop;
import com.songoda.core.lootables.loot.DropUtils;
@ -62,15 +63,26 @@ public class EntityStack extends StackedEntity {
event.setDroppedExp(droppedExp * getAmount());
}
if (plugin.getCustomEntityManager().getCustomEntity(killed) == null) {
Async.run(() -> {
drops.removeIf(it -> it.getItemStack() != null
&& it.getItemStack().isSimilar(killed.getEquipment().getItemInHand()));
for (ItemStack item : killed.getEquipment().getArmorContents()) {
drops.removeIf(it -> it.getItemStack() != null && it.getItemStack().isSimilar(item));
if (ServerVersion.isFolia()) {
killed.getScheduler().run(plugin, scheduledTask -> {
drops.removeIf(it -> it.getItemStack() != null
&& it.getItemStack().isSimilar(killed.getEquipment().getItemInHand()));
for (ItemStack item : killed.getEquipment().getArmorContents()) {
drops.removeIf(it -> it.getItemStack() != null && it.getItemStack().isSimilar(item));
}
}, () -> DropUtils.processStackedDrop(killed, plugin.getLootablesManager().getDrops(killed, getAmount()), event));
} else {
drops.removeIf(it -> it.getItemStack() != null
&& it.getItemStack().isSimilar(killed.getEquipment().getItemInHand()));
for (ItemStack item : killed.getEquipment().getArmorContents()) {
drops.removeIf(it -> it.getItemStack() != null && it.getItemStack().isSimilar(item));
}
DropUtils.processStackedDrop(killed, plugin.getLootablesManager().getDrops(killed, getAmount()), event);
}
DropUtils.processStackedDrop(killed, plugin.getLootablesManager().getDrops(killed, getAmount()), event);
});
}
@ -155,7 +167,7 @@ public class EntityStack extends StackedEntity {
public synchronized void destroy() {
if (hostEntity == null) return;
Bukkit.getScheduler().runTask(plugin, hostEntity::remove);
SchedulerUtils.runEntityTask(plugin, hostEntity, hostEntity::remove);
hostEntity = null;
}

View File

@ -1,5 +1,6 @@
package com.songoda.ultimatestacker.tasks;
import com.songoda.SchedulerUtils;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.hooks.WorldGuardHook;
@ -11,6 +12,9 @@ import com.songoda.ultimatestacker.stackable.entity.EntityStack;
import com.songoda.ultimatestacker.stackable.entity.EntityStackManager;
import com.songoda.ultimatestacker.stackable.entity.custom.CustomEntity;
import com.songoda.ultimatestacker.utils.CachedChunk;
import io.papermc.paper.threadedregions.RegionizedServerInitEvent;
import io.papermc.paper.threadedregions.scheduler.EntityScheduler;
import io.papermc.paper.threadedregions.scheduler.RegionScheduler;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@ -63,29 +67,29 @@ import java.util.concurrent.TimeUnit;
public class StackingTask extends TimerTask {
private final UltimateStacker plugin;
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
protected final UltimateStacker plugin;
protected final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
private final EntityStackManager stackManager;
protected final EntityStackManager stackManager;
private final ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile();
private final List<UUID> processed = new ArrayList<>();
protected final ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile();
protected final List<UUID> processed = new ArrayList<>();
private final Map<EntityType, Integer> entityStackSizes = new HashMap<>();
private final int maxEntityStackSize = Settings.MAX_STACK_ENTITIES.getInt(),
protected final Map<EntityType, Integer> entityStackSizes = new HashMap<>();
protected final int maxEntityStackSize = Settings.MAX_STACK_ENTITIES.getInt(),
minEntityStackSize = Settings.MIN_STACK_ENTITIES.getInt(),
searchRadius = Settings.SEARCH_RADIUS.getInt(),
maxPerTypeStacksPerChunk = Settings.MAX_PER_TYPE_STACKS_PER_CHUNK.getInt();
private final List<String> disabledWorlds = Settings.DISABLED_WORLDS.getStringList(),
protected final List<String> disabledWorlds = Settings.DISABLED_WORLDS.getStringList(),
stackReasons = Settings.STACK_REASONS.getStringList();
private final List<Check> checks = Check.getChecks(Settings.STACK_CHECKS.getStringList());
private final boolean stackFlyingDown = Settings.ONLY_STACK_FLYING_DOWN.getBoolean(),
protected final List<Check> checks = Check.getChecks(Settings.STACK_CHECKS.getStringList());
protected final boolean stackFlyingDown = Settings.ONLY_STACK_FLYING_DOWN.getBoolean(),
stackWholeChunk = Settings.STACK_WHOLE_CHUNK.getBoolean(),
weaponsArentEquipment = Settings.WEAPONS_ARENT_EQUIPMENT.getBoolean(),
onlyStackFromSpawners = Settings.ONLY_STACK_FROM_SPAWNERS.getBoolean(),
onlyStackOnSurface = Settings.ONLY_STACK_ON_SURFACE.getBoolean();
Set<SWorld> loadedWorlds = new HashSet<>();
protected Set<SWorld> loadedWorlds = new HashSet<>();
public StackingTask(UltimateStacker plugin) {
this.plugin = plugin;
@ -125,49 +129,72 @@ public class StackingTask extends TimerTask {
}
Collections.reverse(entities);
Bukkit.getScheduler().runTask(plugin, () -> {
// Loop through the entities.
for (LivingEntity entity : entities) {
// Make sure our entity has not already been processed.
// Skip it if it has been.
if (this.processed.contains(entity.getUniqueId())) continue;
if (ServerVersion.isFolia()) {
SchedulerUtils.runTask(plugin, () -> {
Map<EntityScheduler, List<LivingEntity>> entitySchedulerMap = sWorld.getRegionizedEntities();
for (EntityScheduler scheduler : entitySchedulerMap.keySet()) {
List<LivingEntity> livingEntities = entitySchedulerMap.get(scheduler);
if (livingEntities == null) continue;
scheduler.run(plugin, scheduledTask -> {
// Loop through the entities.
for (LivingEntity entity : livingEntities) {
// Make sure our entity has not already been processed.
// Skip it if it has been.
if (this.processed.contains(entity.getUniqueId())) continue;
// Check to see if entity is not stackable.
if (!isEntityStackable(entity)) {
continue;
// Check to see if entity is not stackable.
if (!isEntityStackable(entity)) {
continue;
}
// Get entity location to pass around as its faster this way.
Location location = entity.getLocation();
// Process the entity.
this.processEntity(entity, sWorld, location);
}
}, null);
}
});
} else {
SchedulerUtils.runTask(plugin, () -> {
// Loop through the entities.
for (LivingEntity entity : entities) {
// Make sure our entity has not already been processed.
// Skip it if it has been.
if (this.processed.contains(entity.getUniqueId())) continue;
// Get entity location to pass around as its faster this way.
Location location = entity.getLocation();
// Check to see if entity is not stackable.
if (!isEntityStackable(entity)) {
continue;
}
// Process the entity.
this.processEntity(entity, sWorld, location);
}
});
// Get entity location to pass around as its faster this way.
Location location = entity.getLocation();
// Process the entity.
this.processEntity(entity, sWorld, location);
}
});
}
}
// Clear caches in preparation for the next run.
this.processed.clear();
} catch (Exception ignored) {}
}
private Future<List<LivingEntity>> getLivingEntitiesSync(SWorld sWorld) {
protected Future<List<LivingEntity>> getLivingEntitiesSync(SWorld sWorld) {
CompletableFuture<List<LivingEntity>> future = new CompletableFuture<>();
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> future.complete(sWorld.getLivingEntities()));
SchedulerUtils.runTask(this.plugin, () -> future.complete(sWorld.getLivingEntities()));
return future;
}
private Future<Entity[]> getEntitiesInChunkSync(CachedChunk cachedChunk) {
CompletableFuture<Entity[]> future = new CompletableFuture<>();
Bukkit.getScheduler().runTask(this.plugin, () -> future.complete(cachedChunk.getEntities()));
return future;
}
public boolean isWorldDisabled(World world) {
return disabledWorlds.stream().anyMatch(worldStr -> world.getName().equalsIgnoreCase(worldStr));
}
private boolean isEntityStackable(Entity entity) {
protected boolean isEntityStackable(Entity entity) {
// Make sure we have the correct entity type and that it is valid.
if (!entity.isValid()
|| entity instanceof HumanEntity
@ -210,7 +237,7 @@ public class StackingTask extends TimerTask {
}
private void processEntity(LivingEntity baseEntity, SWorld sWorld, Location location) {
protected void processEntity(LivingEntity baseEntity, SWorld sWorld, Location location) {
// Check our WorldGuard flag.
Boolean flag = WorldGuardHook.isEnabled() ? WorldGuardHook.getBooleanFlag(baseEntity.getLocation(), "mob-stacking") : null;
@ -278,7 +305,7 @@ public class StackingTask extends TimerTask {
stackManager.createStack(friendlyEntity, amount + amountToStack);
processed.add(baseEntity.getUniqueId());
Bukkit.getScheduler().runTask(plugin, () -> {
SchedulerUtils.runEntityTask(plugin, baseEntity, () -> {
if (baseEntity.isLeashed()) {
baseEntity.getWorld().dropItemNaturally(baseEntity.getLocation(), CompatibleMaterial.LEAD.getItem());
}
@ -297,7 +324,7 @@ public class StackingTask extends TimerTask {
baseStack.setAmount(maxEntityStackAmount);
Bukkit.getScheduler().runTask(plugin, () -> {
SchedulerUtils.runLocationTask(plugin, baseStack.getHostEntity().getLocation(), () -> {
int finalStackSize = stackSize - maxEntityStackAmount;
do {
// Create a new stack, summon entity and add to stack.
@ -314,7 +341,7 @@ public class StackingTask extends TimerTask {
return true;
}
private Set<CachedChunk> getNearbyChunks(SWorld sWorld, Location location, double radius, boolean singleChunk) {
protected Set<CachedChunk> getNearbyChunks(SWorld sWorld, Location location, double radius, boolean singleChunk) {
//get current chunk
if (radius == -1) {
return new HashSet<>(Collections.singletonList(new CachedChunk(sWorld, location.getChunk().getX(), location.getChunk().getZ())));
@ -349,7 +376,7 @@ public class StackingTask extends TimerTask {
* @param singleChunk Whether to only get entities in the same chunk as the entity.
* @return A list of similar entities around the entity.
*/
private List<LivingEntity> getFriendlyStacksNearby(LivingEntity entity, double radius, boolean singleChunk) {
protected List<LivingEntity> getFriendlyStacksNearby(LivingEntity entity, double radius, boolean singleChunk) {
List<LivingEntity> entities = new ArrayList<>();
try {
Set<CachedChunk> chunks = getNearbyChunks(new SWorld(entity.getWorld()), entity.getLocation(), radius, singleChunk);
@ -639,7 +666,7 @@ public class StackingTask extends TimerTask {
|| (equipment.getBoots() != null && equipment.getBoots().getType() != Material.AIR));
}
private int getEntityStackSize(LivingEntity initialEntity) {
protected int getEntityStackSize(LivingEntity initialEntity) {
Integer max = entityStackSizes.get(initialEntity.getType());
if (max == null) {
max = configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size");

View File

@ -6,6 +6,7 @@ loadbefore: [WorldGuard]
main: com.songoda.ultimatestacker.UltimateStacker
author: songoda
api-version: 1.13
folia-supported: true
commands:
us:
description: Reloads the configuration file