From 10b6ab95065124dd9ce51170a8470bcbedd9b17d Mon Sep 17 00:00:00 2001 From: okx-code Date: Sat, 23 Mar 2024 01:09:07 +0000 Subject: [PATCH] add logging --- .../mc/civmodcore/CivModCorePlugin.java | 2 ++ .../civmodcore/commands/ChunkMetaCommand.java | 33 +++++++++++++++++++ .../chunkmeta/WorldChunkMetaManager.java | 16 +++++++-- .../block/table/TableBasedBlockChunkMeta.java | 5 +++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/commands/ChunkMetaCommand.java diff --git a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/CivModCorePlugin.java b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/CivModCorePlugin.java index 0aab7bd5f..ab877bbaf 100644 --- a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/CivModCorePlugin.java +++ b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/CivModCorePlugin.java @@ -9,6 +9,7 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPluginLoader; import org.ipvp.canvas.MenuFunctionListener; import vg.civcraft.mc.civmodcore.chat.dialog.DialogManager; +import vg.civcraft.mc.civmodcore.commands.ChunkMetaCommand; import vg.civcraft.mc.civmodcore.commands.CommandManager; import vg.civcraft.mc.civmodcore.commands.StatCommand; import vg.civcraft.mc.civmodcore.dao.DatabaseCredentials; @@ -95,6 +96,7 @@ public class CivModCorePlugin extends ACivMod { this.commands.init(); this.commands.registerCommand(new ConfigCommand()); this.commands.registerCommand(new StatCommand()); + this.commands.registerCommand(new ChunkMetaCommand()); // Load APIs EnchantUtils.loadEnchantAbbreviations(this); MoreTags.init(); diff --git a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/commands/ChunkMetaCommand.java b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/commands/ChunkMetaCommand.java new file mode 100644 index 000000000..f57ba8a3a --- /dev/null +++ b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/commands/ChunkMetaCommand.java @@ -0,0 +1,33 @@ +package vg.civcraft.mc.civmodcore.commands; + +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Subcommand; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.CommandSender; + +@CommandAlias("cmc") +public class ChunkMetaCommand extends BaseCommand { + + private static boolean ENABLE_CHUNK_META_LOGS = false; + + public static boolean chunkMetaLogsEnabled() { + return ENABLE_CHUNK_META_LOGS; + } + + @Subcommand("chunkmeta") + @Description("Toggle showing chunk meta logs") + @CommandPermission("cmc.debug") + public void chunkmeta(CommandSender sender) { + if (ENABLE_CHUNK_META_LOGS) { + ENABLE_CHUNK_META_LOGS = false; + sender.sendMessage(Component.text("Chunk meta logs disabled.", NamedTextColor.RED)); + } else { + ENABLE_CHUNK_META_LOGS = true; + sender.sendMessage(Component.text("Chunk meta logs enabled.", NamedTextColor.GREEN)); + } + } +} diff --git a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java index 20bb1baf0..549016066 100644 --- a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java +++ b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/WorldChunkMetaManager.java @@ -11,6 +11,7 @@ import java.util.function.Supplier; import java.util.logging.Logger; import org.bukkit.World; +import vg.civcraft.mc.civmodcore.commands.ChunkMetaCommand; /** * Stores Chunk metadata for all plugins for one specific world. Metadata is @@ -20,7 +21,6 @@ import org.bukkit.World; * */ public class WorldChunkMetaManager { - /** * How long should chunk data be kept in memory after the chunk is unloaded? 5 * minutes @@ -228,16 +228,22 @@ public class WorldChunkMetaManager { } if (System.currentTimeMillis() - coord.getLastUnloadedTime() > UNLOAD_DELAY) { + if (ChunkMetaCommand.chunkMetaLogsEnabled()) { + logger.info("[Chunkmeta] Unloading chunk " + coord + " - unloaded: " + coord.getLastUnloadedTime()); + } unloadChunkCoord(coord); } else { if (readdList == null) { readdList = new HashSet<>(); } readdList.add(coord); + } } - if (readdList != null) { + if (ChunkMetaCommand.chunkMetaLogsEnabled()) { + logger.info("[Chunkmeta] Unloaded chunks remaining unsaved: " + readdList); + } unloadingQueue.addAll(readdList); } }, UNLOAD_CHECK_INTERVAL, UNLOAD_CHECK_INTERVAL, TimeUnit.MILLISECONDS); @@ -267,6 +273,9 @@ public class WorldChunkMetaManager { // written to the db synchronized (metas) { if (coord.isUnloaded()) { + if (ChunkMetaCommand.chunkMetaLogsEnabled()) { + logger.info("[Chunkmeta] Chunk no longer being tracked: " + coord); + } metas.remove(coord); coord.clearUnloaded(); } @@ -313,6 +322,9 @@ public class WorldChunkMetaManager { */ void unloadChunk(int x, int z) { ChunkCoord chunkCoord = getChunkCoord(x, z, false, false); + if (ChunkMetaCommand.chunkMetaLogsEnabled()) { + logger.info("[Chunkmeta] Add to unloading queue: " + chunkCoord); + } // chunkCoord can never be null here, otherwise our data structure would be // broken, in which case we'd want to know chunkCoord.minecraftChunkUnloaded(); diff --git a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/block/table/TableBasedBlockChunkMeta.java b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/block/table/TableBasedBlockChunkMeta.java index d4a969987..644ba85de 100644 --- a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/block/table/TableBasedBlockChunkMeta.java +++ b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/world/locations/chunkmeta/block/table/TableBasedBlockChunkMeta.java @@ -3,6 +3,8 @@ package vg.civcraft.mc.civmodcore.world.locations.chunkmeta.block.table; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; +import vg.civcraft.mc.civmodcore.CivModCorePlugin; +import vg.civcraft.mc.civmodcore.commands.ChunkMetaCommand; import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.CacheState; import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.block.BlockBasedChunkMeta; @@ -52,6 +54,9 @@ public abstract class TableBasedBlockChunkMeta @Override public void insert() { + if (ChunkMetaCommand.chunkMetaLogsEnabled()) { + CivModCorePlugin.getInstance().getLogger().info("Inserting at " + chunkCoord); + } for (D data : modifiedEntries) { switch (data.getCacheState()) { case NORMAL: