Skip to content

Commit 366a540

Browse files
committed
basic folia support
1 parent fe96235 commit 366a540

File tree

7 files changed

+143
-71
lines changed

7 files changed

+143
-71
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
<pattern>lol.hyper.githubreleaseapi</pattern>
7676
<shadedPattern>lol.hyper.toolstats.updater</shadedPattern>
7777
</relocation>
78+
<relocation>
79+
<pattern>space.arim.morepaperlib</pattern>
80+
<shadedPattern>lol.hyper.toolstats.morepaperlib</shadedPattern>
81+
</relocation>
7882
</relocations>
7983
</configuration>
8084
<executions>
@@ -103,6 +107,10 @@
103107
<id>spigotmc-repo</id>
104108
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
105109
</repository>
110+
<repository>
111+
<id>arim-mvn-lgpl3</id>
112+
<url>https://mvn-repo.arim.space/lesser-gpl3/</url>
113+
</repository>
106114
</repositories>
107115

108116
<dependencies>
@@ -136,5 +144,11 @@
136144
<version>4.3.0</version>
137145
<scope>compile</scope>
138146
</dependency>
147+
<dependency>
148+
<groupId>space.arim.morepaperlib</groupId>
149+
<artifactId>morepaperlib</artifactId>
150+
<version>0.4.3</version>
151+
<scope>compile</scope>
152+
</dependency>
139153
</dependencies>
140154
</project>

src/main/java/lol/hyper/toolstats/ToolStats.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
import lol.hyper.toolstats.tools.NumberFormat;
2626
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2727
import org.bstats.bukkit.Metrics;
28-
import org.bukkit.Bukkit;
29-
import org.bukkit.ChatColor;
30-
import org.bukkit.NamespacedKey;
28+
import org.bukkit.*;
3129
import org.bukkit.configuration.file.FileConfiguration;
3230
import org.bukkit.configuration.file.YamlConfiguration;
31+
import org.bukkit.entity.Entity;
3332
import org.bukkit.inventory.ItemStack;
3433
import org.bukkit.plugin.java.JavaPlugin;
34+
import org.bukkit.scheduler.BukkitRunnable;
35+
import space.arim.morepaperlib.MorePaperLib;
3536

3637
import java.io.File;
3738
import java.io.IOException;
@@ -112,11 +113,13 @@ public final class ToolStats extends JavaPlugin {
112113
public final int CONFIG_VERSION = 5;
113114

114115
private BukkitAudiences adventure;
116+
public MorePaperLib morePaperLib;
115117

116118

117119
@Override
118120
public void onEnable() {
119121
this.adventure = BukkitAudiences.create(this);
122+
morePaperLib = new MorePaperLib(this);
120123
if (!configFile.exists()) {
121124
this.saveResource("config.yml", true);
122125
logger.info("Copying default config!");
@@ -154,7 +157,7 @@ public void onEnable() {
154157

155158
new Metrics(this, 14110);
156159

157-
Bukkit.getScheduler().runTaskAsynchronously(this, this::checkForUpdates);
160+
morePaperLib.scheduling().asyncScheduler().run(this::checkForUpdates);
158161
}
159162

160163
public void loadConfig() {
@@ -305,4 +308,27 @@ public BukkitAudiences getAdventure() {
305308
}
306309
return this.adventure;
307310
}
311+
312+
public void scheduleEntity(BukkitRunnable runnable, Entity entity, int delay) {
313+
if (Bukkit.getServer().getVersion().contains("Folia")) {
314+
morePaperLib.scheduling().entitySpecificScheduler(entity).runDelayed(runnable, null, delay);
315+
} else {
316+
runnable.runTaskLater(this, delay);
317+
}
318+
}
319+
320+
public void scheduleGlobal(BukkitRunnable runnable, int delay) {
321+
if (Bukkit.getServer().getVersion().contains("Folia")) {
322+
morePaperLib.scheduling().globalRegionalScheduler().runDelayed(runnable, delay);
323+
} else {
324+
runnable.runTaskLater(this, delay);
325+
}
326+
}
327+
public void scheduleRegion(BukkitRunnable runnable, World world, Chunk chunk, int delay) {
328+
if (Bukkit.getServer().getVersion().contains("Folia")) {
329+
morePaperLib.scheduling().regionSpecificScheduler(world, chunk.getX(), chunk.getZ()).runDelayed(runnable, delay);
330+
} else {
331+
runnable.runTaskLater(this, delay);
332+
}
333+
}
308334
}

src/main/java/lol/hyper/toolstats/events/ChunkPopulate.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.bukkit.inventory.meta.ItemMeta;
3333
import org.bukkit.persistence.PersistentDataContainer;
3434
import org.bukkit.persistence.PersistentDataType;
35+
import org.bukkit.scheduler.BukkitRunnable;
3536

3637
public class ChunkPopulate implements Listener {
3738

@@ -51,28 +52,32 @@ public void onPopulate(ChunkPopulateEvent event) {
5152
}
5253
// this is delayed because entities are not loaded instantly
5354
// we just check 1 second later
54-
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
55-
Chunk chunk = event.getChunk();
56-
for (Entity entity : chunk.getEntities()) {
57-
// if there is a new item frame
58-
if (!(entity instanceof ItemFrame)) {
59-
continue;
60-
}
61-
ItemFrame itemFrame = (ItemFrame) entity;
62-
// if the item frame has an elytra
63-
if (itemFrame.getItem().getType() == Material.ELYTRA) {
64-
ItemStack elytraCopy = itemFrame.getItem();
65-
ItemMeta meta = elytraCopy.getItemMeta();
66-
if (meta == null) {
55+
Chunk chunk = event.getChunk();
56+
BukkitRunnable runnable = new BukkitRunnable() {
57+
@Override
58+
public void run() {
59+
for (Entity entity : chunk.getEntities()) {
60+
// if there is a new item frame
61+
if (!(entity instanceof ItemFrame)) {
6762
continue;
6863
}
69-
// add the new tag so we know it's new
70-
PersistentDataContainer container = meta.getPersistentDataContainer();
71-
container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1);
72-
elytraCopy.setItemMeta(meta);
73-
itemFrame.setItem(elytraCopy);
64+
ItemFrame itemFrame = (ItemFrame) entity;
65+
// if the item frame has an elytra
66+
if (itemFrame.getItem().getType() == Material.ELYTRA) {
67+
ItemStack elytraCopy = itemFrame.getItem();
68+
ItemMeta meta = elytraCopy.getItemMeta();
69+
if (meta == null) {
70+
continue;
71+
}
72+
// add the new tag so we know it's new
73+
PersistentDataContainer container = meta.getPersistentDataContainer();
74+
container.set(toolStats.newElytra, PersistentDataType.INTEGER, 1);
75+
elytraCopy.setItemMeta(meta);
76+
itemFrame.setItem(elytraCopy);
77+
}
7478
}
7579
}
76-
}, 20);
80+
};
81+
toolStats.scheduleRegion(runnable, chunk.getWorld(), chunk, 20);
7782
}
7883
}

src/main/java/lol/hyper/toolstats/events/GenerateLoot.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.bukkit.inventory.meta.ItemMeta;
3838
import org.bukkit.persistence.PersistentDataContainer;
3939
import org.bukkit.persistence.PersistentDataType;
40+
import org.bukkit.scheduler.BukkitRunnable;
4041

4142
import java.util.Date;
4243
import java.util.List;
@@ -77,26 +78,29 @@ public void onGenerateLoot(LootGenerateEvent event) {
7778
return;
7879
}
7980

80-
// run task later since if it runs on the same tick it breaks idk
81+
// run task later since if it runs on the same tick it breaks
8182
Block finalOpenedChest = openedChest;
82-
Bukkit.getScheduler().runTaskLater(toolStats, () -> {
83-
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
84-
// do a classic for loop, so we keep track of chest index of item
85-
for (int i = 0; i < chestInv.getContents().length; i++) {
86-
ItemStack itemStack = chestInv.getItem(i);
87-
// ignore air
88-
if (itemStack == null || itemStack.getType() == Material.AIR) {
89-
continue;
90-
}
91-
if (ItemChecker.isValidItem(itemStack.getType())) {
92-
ItemStack newItem = addLore(itemStack, player);
93-
if (newItem != null) {
94-
chestInv.setItem(i, newItem);
83+
BukkitRunnable runnable = new BukkitRunnable() {
84+
@Override
85+
public void run() {
86+
Player player = toolStats.playerInteract.openedChests.get(finalOpenedChest);
87+
// keep track of chest index of item
88+
for (int i = 0; i < chestInv.getContents().length; i++) {
89+
ItemStack itemStack = chestInv.getItem(i);
90+
// ignore air
91+
if (itemStack == null || itemStack.getType() == Material.AIR) {
92+
continue;
93+
}
94+
if (ItemChecker.isValidItem(itemStack.getType())) {
95+
ItemStack newItem = addLore(itemStack, player);
96+
if (newItem != null) {
97+
chestInv.setItem(i, newItem);
98+
}
9599
}
96100
}
97101
}
98-
99-
}, 1);
102+
};
103+
toolStats.scheduleRegion(runnable, lootLocation.getWorld(), lootLocation.getChunk(), 1);
100104
}
101105
if (inventoryHolder instanceof StorageMinecart) {
102106
StorageMinecart mineCart = (StorageMinecart) inventoryHolder;

src/main/java/lol/hyper/toolstats/events/InventoryOpen.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import lol.hyper.toolstats.tools.ItemChecker;
2222
import org.bukkit.Bukkit;
2323
import org.bukkit.Material;
24+
import org.bukkit.entity.Player;
2425
import org.bukkit.event.EventHandler;
2526
import org.bukkit.event.Listener;
2627
import org.bukkit.event.inventory.InventoryOpenEvent;
2728
import org.bukkit.inventory.Inventory;
2829
import org.bukkit.inventory.ItemStack;
30+
import org.bukkit.inventory.PlayerInventory;
2931
import org.bukkit.inventory.meta.ItemMeta;
3032
import org.bukkit.persistence.PersistentDataContainer;
3133
import org.bukkit.persistence.PersistentDataType;
34+
import org.bukkit.scheduler.BukkitRunnable;
3235

3336
import java.util.List;
3437

@@ -46,33 +49,39 @@ public void onOpen(InventoryOpenEvent event) {
4649
return;
4750
}
4851

49-
Bukkit.getScheduler().runTaskLater(toolStats, ()-> {
50-
Inventory inventory = event.getInventory();
51-
for (ItemStack itemStack : inventory) {
52-
if (itemStack == null) {
53-
continue;
54-
}
55-
ItemMeta itemMeta = itemStack.getItemMeta();
56-
if (itemMeta == null) {
57-
continue;
58-
}
59-
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
60-
// ignore any items that already have the origin tag
61-
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
62-
continue;
63-
}
64-
// ignore items that are not the right type
65-
if (!ItemChecker.isValidItem(itemStack.getType())) {
66-
continue;
67-
}
52+
Player player = (Player) event.getPlayer();
6853

69-
ItemMeta newMeta = getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
70-
if (newMeta == null) {
71-
continue;
72-
}
73-
itemStack.setItemMeta(newMeta);
54+
Inventory inventory = event.getInventory();
55+
for (ItemStack itemStack : inventory) {
56+
if (itemStack == null) {
57+
continue;
58+
}
59+
ItemMeta itemMeta = itemStack.getItemMeta();
60+
if (itemMeta == null) {
61+
continue;
7462
}
75-
},1);
63+
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
64+
// ignore any items that already have the origin tag
65+
if (container.has(toolStats.originType, PersistentDataType.INTEGER)) {
66+
continue;
67+
}
68+
// ignore items that are not the right type
69+
if (!ItemChecker.isValidItem(itemStack.getType())) {
70+
continue;
71+
}
72+
73+
ItemMeta newMeta = getOrigin(itemMeta, itemStack.getType() == Material.ELYTRA);
74+
if (newMeta == null) {
75+
continue;
76+
}
77+
BukkitRunnable runnable = new BukkitRunnable() {
78+
@Override
79+
public void run() {
80+
itemStack.setItemMeta(newMeta);
81+
}
82+
};
83+
toolStats.scheduleEntity(runnable, player, 1);
84+
}
7685
}
7786

7887
/**
@@ -93,11 +102,11 @@ private ItemMeta getOrigin(ItemMeta itemMeta, boolean elytra) {
93102
for (String line : lore) {
94103
// this is the worst code I have ever written
95104
String createdBy = toolStats.getLoreFromConfig("created.created-by", false);
96-
String createdOn = toolStats.getLoreFromConfig("created.created-by", false);
97-
String caughtBy = toolStats.getLoreFromConfig("created.created-by", false);
98-
String lootedBy = toolStats.getLoreFromConfig("created.created-by", false);
99-
String foundBy = toolStats.getLoreFromConfig("created.created-by", false);
100-
String tradedBy = toolStats.getLoreFromConfig("created.created-by", false);
105+
String createdOn = toolStats.getLoreFromConfig("created.created-on", false);
106+
String caughtBy = toolStats.getLoreFromConfig("fished.caught-by", false);
107+
String lootedBy = toolStats.getLoreFromConfig("looted.looted-by", false);
108+
String foundBy = toolStats.getLoreFromConfig("looted.found-by", false);
109+
String tradedBy = toolStats.getLoreFromConfig("traded.traded-by", false);
101110

102111
if (createdBy != null && line.contains(createdBy)) {
103112
origin = 0;

src/main/java/lol/hyper/toolstats/events/PlayerInteract.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.bukkit.event.block.Action;
3232
import org.bukkit.event.player.PlayerInteractEntityEvent;
3333
import org.bukkit.event.player.PlayerInteractEvent;
34+
import org.bukkit.scheduler.BukkitRunnable;
3435

3536
import java.util.HashMap;
3637

@@ -63,7 +64,13 @@ public void onInteract(PlayerInteractEvent event) {
6364
// store when a player opens a chest
6465
if (block.getType() != Material.AIR && block.getType() == Material.CHEST) {
6566
openedChests.put(block, player);
66-
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedChests.remove(block), 20);
67+
BukkitRunnable runnable = new BukkitRunnable() {
68+
@Override
69+
public void run() {
70+
openedChests.remove(block);
71+
}
72+
};
73+
toolStats.scheduleGlobal(runnable, 20);
6774
}
6875
}
6976

@@ -78,7 +85,13 @@ public void onInteract(PlayerInteractEntityEvent event) {
7885
if (clicked.getType() == EntityType.MINECART_CHEST) {
7986
StorageMinecart storageMinecart = (StorageMinecart) clicked;
8087
openedMineCarts.put(storageMinecart, player);
81-
Bukkit.getScheduler().runTaskLater(toolStats, () -> openedMineCarts.remove(storageMinecart), 20);
88+
BukkitRunnable runnable = new BukkitRunnable() {
89+
@Override
90+
public void run() {
91+
openedMineCarts.remove(storageMinecart);
92+
}
93+
};
94+
toolStats.scheduleGlobal(runnable, 20);
8295
}
8396
}
8497
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ main: lol.hyper.toolstats.ToolStats
44
api-version: 1.15
55
author: hyperdefined
66
description: Track various tool stats!
7+
folia-supported: true
78
commands:
89
toolstats:
910
usage: /toolstats

0 commit comments

Comments
 (0)