Skip to content

Commit

Permalink
Add Folia support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelien30000 committed Aug 12, 2023
1 parent 2334926 commit 4c97f97
Show file tree
Hide file tree
Showing 18 changed files with 411 additions and 24 deletions.
115 changes: 113 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,113 @@
# Project exclude paths
/target/
# User-specific stuff
.idea/

*.iml
*.ipr
*.iws

# IntelliJ
out/

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

target/

pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next

release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.flattened-pom.xml

# Common working directory
run/
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,31 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.20-R0.1-SNAPSHOT</version>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.20-R0.1-SNAPSHOT</version>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.9-SNAPSHOT</version>
<version>7.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.2.15-SNAPSHOT</version>
<version>7.2.16-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/uk/antiperson/stackmob/StackMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import uk.antiperson.stackmob.hook.HookManager;
import uk.antiperson.stackmob.listeners.*;
import uk.antiperson.stackmob.packets.PlayerManager;
import uk.antiperson.stackmob.tasks.MergeTask;
import uk.antiperson.stackmob.tasks.TagCheckTask;
import uk.antiperson.stackmob.tasks.TagMoveTask;
import uk.antiperson.stackmob.scheduler.BukkitScheduler;
import uk.antiperson.stackmob.scheduler.FoliaScheduler;
import uk.antiperson.stackmob.scheduler.Scheduler;
import uk.antiperson.stackmob.tasks.*;
import uk.antiperson.stackmob.utils.ItemTools;
import uk.antiperson.stackmob.utils.Updater;
import uk.antiperson.stackmob.utils.Utilities;
Expand All @@ -30,6 +31,18 @@

public class StackMob extends JavaPlugin {

public static final boolean IS_FOLIA;

static {
boolean f = false;
try {
Class.forName("io.papermc.paper.threadedregions.scheduler.RegionScheduler");
f = true;
} catch (ClassNotFoundException ignored) {
}
IS_FOLIA = f;
}

private final NamespacedKey stackKey = new NamespacedKey(this, "stack-size");
private final NamespacedKey toolKey = new NamespacedKey(this, "stack-tool");

Expand All @@ -43,6 +56,7 @@ public class StackMob extends JavaPlugin {
private Updater updater;
private ItemTools itemTools;
private PlayerManager playerManager;
private Scheduler scheduler;

@Override
public void onLoad() {
Expand All @@ -55,6 +69,7 @@ public void onLoad() {
getLogger().log(Level.SEVERE, "There was a problem registering hooks. Features won't work.");
e.printStackTrace();
}
scheduler = IS_FOLIA ? new FoliaScheduler() : new BukkitScheduler();
}

@Override
Expand Down Expand Up @@ -98,11 +113,11 @@ public void onEnable() {
command.setTabCompleter(commands);
commands.registerSubCommands();
final int stackInterval = getMainConfig().getStackInterval();
new MergeTask(this).runTaskTimer(this, 20, stackInterval);
scheduler.runGlobalTaskTimer(this, IS_FOLIA ? new FoliaMergeTask(this) : new MergeTask(this), 20, stackInterval);
final int tagInterval = getMainConfig().getTagNearbyInterval();
new TagCheckTask(this).runTaskTimer(this, 30, tagInterval);
scheduler.runGlobalTaskTimer(this, IS_FOLIA ? new FoliaTagCheckTask(this) : new TagCheckTask(this), 30, tagInterval);
if (getMainConfig().isTagNearbyArmorStandEnabled()) {
new TagMoveTask(this).runTaskTimer(this, 10, 1);
scheduler.runGlobalTaskTimer(this, IS_FOLIA ? new FoliaTagMoveTask(this) : new TagMoveTask(this), 10, 1);
}
getLogger().info("Detected CraftBukkit NMS version " + Utilities.getMinecraftVersion() +
(Utilities.getMinecraftVersion() != Utilities.NMS_VERSION ? ", native version is " + Utilities.NMS_VERSION : ""));
Expand Down Expand Up @@ -244,4 +259,8 @@ public ItemTools getItemTools() {
return itemTools;
}

public Scheduler getScheduler() {
return scheduler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onSpawn(StackEntity spawned) {
final double maxHealth = Math.min(attribute.getValue(), attribute.getDefaultValue());
final StackableMobHook smh = sm.getHookManager().getApplicableHook(spawned);
if (smh instanceof MythicMobsStackHook) {
sm.getServer().getScheduler().runTaskLater(sm, bukkitTask -> {
sm.getScheduler().runTaskLater(sm, spawned.getEntity(), () -> {
if (!spawned.getEntity().isDead()) {
spawned.getEntity().setHealth(maxHealth - leftOverDamage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void onSpawn(LivingEntity spawned) {

@EventHandler
public void onJobsPrePayment(JobsPrePaymentEvent event) {
if (event.getLivingEntity() == null) {
if (event.getLivingEntity() == null || StackMob.IS_FOLIA) { // TODO: Make this work with Folia.
return;
}
stackEntity = sm.getEntityManager().getStackEntity(event.getLivingEntity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onStackDeath(EntityDeathEvent event) {
stackEntity.incrementSize(-deathStep);
deathMethod.onSpawn(stackEntity);
} else {
sm.getServer().getScheduler().runTask(sm, () -> {
sm.getScheduler().runTask(sm, event.getEntity(), () -> {
final StackEntity spawned = stackEntity.duplicate(stackEntity.getSize() - deathStep);
deathMethod.onSpawn(spawned);
stackEntity.removeStackData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onShearSheep(BlockShearEntityEvent event) {
if (is == null || (maxDurability - durability) == 1) {
return;
}
sm.getServer().getScheduler().runTask(sm, () -> {
sm.getScheduler().runTask(sm, event.getBlock().getLocation(), () -> {
Dispenser dispenser = (Dispenser) event.getBlock().getState();
dispenser.getInventory().setItem(dispenser.getInventory().first(event.getTool()), is);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ public SpawnListener(StackMob sm) {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSpawn(CreatureSpawnEvent event) {
sm.getServer().getScheduler().runTask(sm, () -> {
sm.getScheduler().runTask(sm, event.getEntity(), () -> {
final LivingEntity eventEntity = event.getEntity();
if (!eventEntity.isValid()) {
return;
}
if (eventEntity instanceof Bee
&& event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BEEHIVE) {
if (eventEntity instanceof Bee && event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BEEHIVE) {
return;
}
if (sm.getEntityManager().isStackedEntity(eventEntity)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.antiperson.stackmob.scheduler;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;

public class BukkitScheduler implements Scheduler {

@Override
public void runGlobalTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) {
plugin.getServer().getScheduler().runTaskTimer(plugin, runnable, delay, period);
}

@Override
public void runTask(Plugin plugin, Location location, Runnable runnable) {
plugin.getServer().getScheduler().runTask(plugin, runnable);
}

@Override
public void runTask(Plugin plugin, Entity entity, Runnable runnable) {
plugin.getServer().getScheduler().runTask(plugin, runnable);
}

@Override
public void runTaskAsynchronously(Plugin plugin, Runnable runnable) {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, runnable);
}

@Override
public void runTaskLater(Plugin plugin, Entity entity, Runnable runnable, long delay) {
plugin.getServer().getScheduler().runTaskLater(plugin, runnable, delay);
}

}
36 changes: 36 additions & 0 deletions src/main/java/uk/antiperson/stackmob/scheduler/FoliaScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package uk.antiperson.stackmob.scheduler;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;

public class FoliaScheduler implements Scheduler {

@Override
public void runGlobalTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) {
plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, scheduledTask -> runnable.run(), (int) delay, (int) period);
}

@Override
public void runTask(Plugin plugin, Location location, Runnable runnable) {
plugin.getServer().getRegionScheduler().run(plugin, location, scheduledTask -> runnable.run());
}

@Override
public void runTask(Plugin plugin, Entity entity, Runnable runnable) {
entity.getScheduler().run(plugin, scheduledTask -> runnable.run(), () -> {
});
}

@Override
public void runTaskAsynchronously(Plugin plugin, Runnable runnable) {
plugin.getServer().getAsyncScheduler().runNow(plugin, scheduledTask -> runnable.run());
}

@Override
public void runTaskLater(Plugin plugin, Entity entity, Runnable runnable, long delay) {
entity.getScheduler().runDelayed(plugin, scheduledTask -> runnable.run(), () -> {
}, (int) delay);
}

}
19 changes: 19 additions & 0 deletions src/main/java/uk/antiperson/stackmob/scheduler/Scheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.antiperson.stackmob.scheduler;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;

public interface Scheduler {

void runGlobalTaskTimer(Plugin plugin, Runnable runnable, long delay, long period);

void runTask(Plugin plugin, Location location, Runnable runnable);

void runTask(Plugin plugin, Entity entity, Runnable runnable);

void runTaskAsynchronously(Plugin plugin, Runnable runnable);

void runTaskLater(Plugin plugin, Entity entity, Runnable runnable, long delay);

}
Loading

0 comments on commit 4c97f97

Please sign in to comment.