-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Optimize GuildCommand database calls #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |||||
| import org.bukkit.plugin.java.JavaPlugin; | ||||||
| import org.bukkit.plugin.RegisteredServiceProvider; | ||||||
|
|
||||||
| public final class GuildManager extends JavaPlugin { | ||||||
| public class GuildManager extends JavaPlugin { | ||||||
|
||||||
| public class GuildManager extends JavaPlugin { | |
| public final class GuildManager extends JavaPlugin { |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mcatk.guildmanager.GuildManager; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mcatk.guildmanager.Msg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mcatk.guildmanager.gui.GuildsGUI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mcatk.guildmanager.core.service.GuildService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mcatk.guildmanager.models.ApplicantsList; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mcatk.guildmanager.models.Guild; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mcatk.guildmanager.models.Member; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -15,12 +16,16 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class GuildCommand implements CommandExecutor { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private CommandSender sender; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private String[] args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Guild guild; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final GuildManager plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final GuildService guildService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public GuildCommand(GuildManager plugin, GuildService guildService) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.plugin = plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.guildService = guildService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // usage: /gmg gui|apply|tp|create|t|quit|offer|msg|memgui|msggui | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void printHelp() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void printHelp(CommandSender sender) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sender.sendMessage("§e------------公会帮助------------"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sender.sendMessage("§a/gmg gui §2公会列表"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sender.sendMessage("§a/gmg apply <ID> §2申请加入公会"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -31,61 +36,67 @@ private void printHelp() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.sender = sender; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.args = args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (args.length == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printHelp(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.guild = GuildManager.getPlugin().getGuildService().getPlayerGuild(sender.getName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onCommandWithoutGuild(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //以下要求发送者在一个公会之中 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (guild != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onCommandWithGuild(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (ParaLengthException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sender.sendMessage("参数错误"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printHelp(sender); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Optimization: Run DB lookup asynchronously | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guild guild = guildService.getPlayerGuild(sender.getName()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The async task now calls Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Switch back to sync thread for command processing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bukkit.getScheduler().runTask(plugin, () -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onCommandWithoutGuild(sender, args, guild); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //以下要求发送者在一个公会之中 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (guild != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onCommandWithGuild(sender, args, guild); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (ParaLengthException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sender.sendMessage("参数错误"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+59
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Guild guild = guildService.getPlayerGuild(sender.getName()); | |
| // Switch back to sync thread for command processing | |
| Bukkit.getScheduler().runTask(plugin, () -> { | |
| try { | |
| onCommandWithoutGuild(sender, args, guild); | |
| //以下要求发送者在一个公会之中 | |
| if (guild != null) { | |
| onCommandWithGuild(sender, args, guild); | |
| } | |
| } catch (ParaLengthException e) { | |
| sender.sendMessage("参数错误"); | |
| } | |
| }); | |
| try { | |
| Guild guild = guildService.getPlayerGuild(sender.getName()); | |
| // Switch back to sync thread for command processing | |
| Bukkit.getScheduler().runTask(plugin, () -> { | |
| try { | |
| onCommandWithoutGuild(sender, args, guild); | |
| //以下要求发送者在一个公会之中 | |
| if (guild != null) { | |
| onCommandWithGuild(sender, args, guild); | |
| } | |
| } catch (ParaLengthException e) { | |
| sender.sendMessage("参数错误"); | |
| } | |
| }); | |
| } catch (Exception e) { | |
| // Handle unexpected errors in async task and notify the sender on the main thread | |
| Bukkit.getScheduler().runTask(plugin, () -> { | |
| sender.sendMessage("§c执行公会命令时发生错误,请稍后重试。"); | |
| }); | |
| e.printStackTrace(); | |
| } |
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code casts CommandSender to Player without checking if the sender is actually a Player. If a console sender executes this command with the "gui" argument, this will throw a ClassCastException. Add a check before casting: if (!(sender instanceof Player)) { sender.sendMessage("This command can only be used by players"); return; }
| case "gui": | |
| case "gui": | |
| if (!(sender instanceof Player)) { | |
| sender.sendMessage("This command can only be used by players"); | |
| break; | |
| } |
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines make blocking database calls (guildService.createGuild and guildService.addMember) on the main thread. According to the GuildService implementation, both methods are synchronized and call repository methods that perform database I/O, then call refresh() which also does database I/O. These operations should be moved to an async task to avoid blocking the main thread.
| guildService.createGuild(guildID, sender.getName()); | |
| guildService.addMember(sender.getName(), guildID); | |
| Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { | |
| guildService.createGuild(guildID, sender.getName()); | |
| guildService.addMember(sender.getName(), guildID); | |
| }); |
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Bukkit.getLogger() instead of plugin.getLogger() changes the logger context from the plugin-specific logger to the global Minecraft logger. This makes it harder to identify which plugin generated the log message and may affect log filtering. Consider using plugin.getLogger() to maintain proper logging attribution, especially since the plugin instance is now available via the constructor-injected field.
| Bukkit.getLogger().info("玩家" + sender.getName() + "创建了公会" + args[1]); | |
| plugin.getLogger().info("玩家" + sender.getName() + "创建了公会" + args[1]); |
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line makes a blocking database call (guildService.getMember) on the main thread after the async operation completes. The getMember method in GuildService is synchronized and calls repository.getMember, which performs database I/O. This defeats the purpose of the async optimization. Move this database call to the async section along with other DB operations.
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Bukkit.getLogger() instead of plugin.getLogger() changes the logger context from the plugin-specific logger to the global Minecraft logger. This makes it harder to identify which plugin generated the log message and may affect log filtering. Consider using plugin.getLogger() to maintain proper logging attribution, especially since the plugin instance is now available via the constructor-injected field.
| Bukkit.getLogger().info(p + "捐献了" + n + "给" + guild.getGuildName()); | |
| plugin.getLogger().info(p + "捐献了" + n + "给" + guild.getGuildName()); |
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines make blocking database calls (guildService.saveMember and guildService.saveGuild) on the main thread. Both methods are synchronized and perform database I/O operations followed by a refresh() call that also does database I/O. These operations should be moved to an async task to avoid blocking the main thread.
| guildService.saveMember(member); | |
| guildService.saveGuild(guild); | |
| Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { | |
| guildService.saveMember(member); | |
| guildService.saveGuild(guild); | |
| }); |
Copilot
AI
Feb 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line makes a blocking database call (guildService.removeMember) on the main thread. The removeMember method is synchronized and performs database I/O operations followed by a refresh() call that also does database I/O. This operation should be moved to an async task to avoid blocking the main thread.
| guildService.removeMember(sender.getName(), guild.getId()); | |
| Bukkit.dispatchCommand(Bukkit.getConsoleSender(), String.format("res pset main.gh %s move remove", sender.getName())); | |
| sender.sendMessage(Msg.INFO + "退出公会" + guild.getGuildName()); | |
| String playerName = sender.getName(); | |
| String guildId = guild.getId(); | |
| String guildName = guild.getGuildName(); | |
| Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { | |
| guildService.removeMember(playerName, guildId); | |
| Bukkit.getScheduler().runTask(plugin, () -> { | |
| Bukkit.dispatchCommand( | |
| Bukkit.getConsoleSender(), | |
| String.format("res pset main.gh %s move remove", playerName) | |
| ); | |
| sender.sendMessage(Msg.INFO + "退出公会" + guildName); | |
| }); | |
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package com.mcatk.guildmanager.command; | ||
|
|
||
| import com.mcatk.guildmanager.GuildManager; | ||
|
||
| import com.mcatk.guildmanager.GuildItem; | ||
| import com.mcatk.guildmanager.Msg; | ||
| import com.mcatk.guildmanager.exceptions.ParaLengthException; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Mockito version 3.12.4 is from 2021 and is outdated. Consider upgrading to a more recent version like 4.11.0 or 5.x for better compatibility and features. Similarly, JUnit 4.13.2 is the last version of JUnit 4, but JUnit 5 (Jupiter) is the current standard and provides better features. Consider upgrading to JUnit 5 unless there are specific compatibility requirements.