mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-28 05:54:16 +02:00
(1.7.1) Fix Folia inventory ticking error (#11)
This commit is contained in:
@@ -22,6 +22,7 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
@@ -73,13 +74,13 @@ public final class SModerationPaper extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getPluginManager().registerEvents(new PunishmentListener(), this);
|
||||
getPluginManager().registerEvents(new CustomInventoryListener(), this);
|
||||
getPluginManager().registerEvents(new InvSeeListener(), this);
|
||||
getPluginManager().registerEvents(new EnderchestSeeListener(), this);
|
||||
getPluginManager().registerEvents(new VanishListener(), this);
|
||||
getPluginManager().registerEvents(new ChatInputListener(), this);
|
||||
getPluginManager().registerEvents(new SocialSpyListener(), this);
|
||||
listen(new PunishmentListener());
|
||||
listen(new CustomInventoryListener());
|
||||
listen(new InvSeeListener());
|
||||
listen(new EnderchestSeeListener());
|
||||
listen(new VanishListener());
|
||||
listen(new ChatInputListener());
|
||||
listen(new SocialSpyListener());
|
||||
|
||||
getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> {
|
||||
Commands commands = event.registrar();
|
||||
@@ -98,7 +99,13 @@ public final class SModerationPaper extends JavaPlugin {
|
||||
registerCommand(commands, new OfflineTPCommand());
|
||||
});
|
||||
|
||||
SchedulerUtil.scheduleGlobalRepeating(PLUGIN, CustomInventoryListener::onTick, 1, 1);
|
||||
if (SchedulerUtil.isFolia){
|
||||
// Normal ticking logic can cause issues on Folia
|
||||
listen(new FoliaInventoryUpdatingListener());
|
||||
} else {
|
||||
SchedulerUtil.scheduleGlobalRepeating(PLUGIN, CustomInventoryListener::tickAllPaper, 1, 1);
|
||||
}
|
||||
|
||||
SchedulerUtil.scheduleGlobalRepeating(PLUGIN, ChatInput::tickAll, 1, 1);
|
||||
|
||||
container.load(SAVE_FILE);
|
||||
@@ -106,6 +113,10 @@ public final class SModerationPaper extends JavaPlugin {
|
||||
LOGGER.info("Folia: {}", SchedulerUtil.isFolia ? "yes" : "no");
|
||||
}
|
||||
|
||||
private void listen(Listener listener) {
|
||||
getPluginManager().registerEvents(listener, this);
|
||||
}
|
||||
|
||||
private void registerCommand(Commands commands, CommandProvider provider){
|
||||
commands.register(
|
||||
provider.getCommandNode(),
|
||||
|
||||
@@ -25,11 +25,15 @@ public class CustomInventoryListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
public static void onTick(){
|
||||
public static void tickAllPaper(){
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (onlinePlayer.getOpenInventory().getTopInventory().getHolder() instanceof AutoUpdatingCustomInventory ci) {
|
||||
ci.refresh();
|
||||
}
|
||||
tickForPlayer(onlinePlayer);
|
||||
}
|
||||
}
|
||||
|
||||
public static void tickForPlayer(Player onlinePlayer) {
|
||||
if (onlinePlayer.getOpenInventory().getTopInventory().getHolder() instanceof AutoUpdatingCustomInventory ci) {
|
||||
ci.refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package de.shiewk.smoderation.paper.listener;
|
||||
|
||||
import de.shiewk.smoderation.paper.SModerationPaper;
|
||||
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.metadata.FixedMetadataValue;
|
||||
import org.bukkit.metadata.MetadataValue;
|
||||
|
||||
public class FoliaInventoryUpdatingListener implements Listener {
|
||||
|
||||
public static final String METADATA_KEY = "smod_invtick";
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event){
|
||||
Player player = event.getPlayer();
|
||||
ScheduledTask task = player.getScheduler().runAtFixedRate(
|
||||
SModerationPaper.PLUGIN,
|
||||
t -> CustomInventoryListener.tickForPlayer(player),
|
||||
null,
|
||||
1,
|
||||
1
|
||||
);
|
||||
player.setMetadata(METADATA_KEY, new FixedMetadataValue(
|
||||
SModerationPaper.PLUGIN,
|
||||
task
|
||||
));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event){
|
||||
for (MetadataValue meta : event.getPlayer().getMetadata("smod_invtick")) {
|
||||
if (meta.getOwningPlugin() == SModerationPaper.PLUGIN) {
|
||||
if (meta.value() instanceof ScheduledTask task) {
|
||||
task.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user