From e31cb5bb9e8fcbb34641a3c315320f68938ef76a Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 3 Nov 2019 10:54:43 +0100 Subject: [PATCH] Fix some CodeClimate issues - Mostly missing Javadoc on methods & line length violations --- .../fr/xephi/authme/data/auth/PlayerAuth.java | 1 + .../fr/xephi/authme/datasource/MySQL.java | 1 + .../fr/xephi/authme/datasource/SQLite.java | 1 + .../mysqlextensions/WordpressExtension.java | 102 ++++++++---------- .../protocollib/InventoryPacketAdapter.java | 10 ++ .../service/bungeecord/BungeeReceiver.java | 16 ++- .../translations/TranslationsGatherer.java | 6 +- .../HelpTranslationVerifier.java | 6 ++ .../tools/messages/VerifyMessagesTask.java | 11 +- 9 files changed, 83 insertions(+), 71 deletions(-) diff --git a/src/main/java/fr/xephi/authme/data/auth/PlayerAuth.java b/src/main/java/fr/xephi/authme/data/auth/PlayerAuth.java index 4aebd88db..23aa8116d 100644 --- a/src/main/java/fr/xephi/authme/data/auth/PlayerAuth.java +++ b/src/main/java/fr/xephi/authme/data/auth/PlayerAuth.java @@ -13,6 +13,7 @@ import static com.google.common.base.Preconditions.checkNotNull; /** * AuthMe player data. */ +@SuppressWarnings("checkstyle:FinalClass") // Justification: class is mocked in multiple tests public class PlayerAuth { /** Default email used in the database if the email column is defined to be NOT NULL. */ diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 768af7320..501758181 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -170,6 +170,7 @@ public class MySQL extends AbstractSqlDataSource { /** * Creates the table or any of its required columns if they don't exist. */ + @SuppressWarnings({"checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS"}) private void checkTablesAndColumns() throws SQLException { try (Connection con = getConnection(); Statement st = con.createStatement()) { // Create table with ID column if it doesn't exist diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index 29b6a87c1..3ca138d59 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -98,6 +98,7 @@ public class SQLite extends AbstractSqlDataSource { * @throws SQLException when an SQL error occurs while initializing the database */ @VisibleForTesting + @SuppressWarnings("checkstyle:CyclomaticComplexity") protected void setup() throws SQLException { try (Statement st = con.createStatement()) { // Note: cannot add unique fields later on in SQLite, so we add it on initialization diff --git a/src/main/java/fr/xephi/authme/datasource/mysqlextensions/WordpressExtension.java b/src/main/java/fr/xephi/authme/datasource/mysqlextensions/WordpressExtension.java index 22ef97f3c..8105d2e55 100644 --- a/src/main/java/fr/xephi/authme/datasource/mysqlextensions/WordpressExtension.java +++ b/src/main/java/fr/xephi/authme/datasource/mysqlextensions/WordpressExtension.java @@ -30,73 +30,55 @@ class WordpressExtension extends MySqlExtension { } } + /** + * Saves the required data to Wordpress tables. + * + * @param auth the player data + * @param id the player id + * @param con the sql connection + * @throws SQLException . + */ private void saveSpecifics(PlayerAuth auth, int id, Connection con) throws SQLException { String sql = "INSERT INTO " + wordpressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?)"; try (PreparedStatement pst = con.prepareStatement(sql)) { - // First Name - pst.setInt(1, id); - pst.setString(2, "first_name"); - pst.setString(3, ""); - pst.addBatch(); - // Last Name - pst.setInt(1, id); - pst.setString(2, "last_name"); - pst.setString(3, ""); - pst.addBatch(); - // Nick Name - pst.setInt(1, id); - pst.setString(2, "nickname"); - pst.setString(3, auth.getNickname()); - pst.addBatch(); - // Description - pst.setInt(1, id); - pst.setString(2, "description"); - pst.setString(3, ""); - pst.addBatch(); - // Rich_Editing - pst.setInt(1, id); - pst.setString(2, "rich_editing"); - pst.setString(3, "true"); - pst.addBatch(); - // Comments_Shortcuts - pst.setInt(1, id); - pst.setString(2, "comment_shortcuts"); - pst.setString(3, "false"); - pst.addBatch(); - // admin_color - pst.setInt(1, id); - pst.setString(2, "admin_color"); - pst.setString(3, "fresh"); - pst.addBatch(); - // use_ssl - pst.setInt(1, id); - pst.setString(2, "use_ssl"); - pst.setString(3, "0"); - pst.addBatch(); - // show_admin_bar_front - pst.setInt(1, id); - pst.setString(2, "show_admin_bar_front"); - pst.setString(3, "true"); - pst.addBatch(); - // wp_capabilities - pst.setInt(1, id); - pst.setString(2, wordpressPrefix + "capabilities"); - pst.setString(3, "a:1:{s:10:\"subscriber\";b:1;}"); - pst.addBatch(); - // wp_user_level - pst.setInt(1, id); - pst.setString(2, wordpressPrefix + "user_level"); - pst.setString(3, "0"); - pst.addBatch(); - // default_password_nag - pst.setInt(1, id); - pst.setString(2, "default_password_nag"); - pst.setString(3, ""); - pst.addBatch(); + + new UserMetaBatchAdder(pst, id) + .addMetaRow("first_name", "") + .addMetaRow("last_name", "") + .addMetaRow("nickname", auth.getNickname()) + .addMetaRow("description", "") + .addMetaRow("rich_editing", "true") + .addMetaRow("comment_shortcuts", "false") + .addMetaRow("admin_color", "fresh") + .addMetaRow("use_ssl", "0") + .addMetaRow("show_admin_bar_front", "true") + .addMetaRow(wordpressPrefix + "capabilities", "a:1:{s:10:\"subscriber\";b:1;}") + .addMetaRow(wordpressPrefix + "user_level", "0") + .addMetaRow("default_password_nag", ""); // Execute queries pst.executeBatch(); pst.clearBatch(); } } + + /** Helper to add batch entries to the wrapped prepared statement. */ + private static final class UserMetaBatchAdder { + + private final PreparedStatement pst; + private final int userId; + + UserMetaBatchAdder(PreparedStatement pst, int userId) { + this.pst = pst; + this.userId = userId; + } + + UserMetaBatchAdder addMetaRow(String metaKey, String metaValue) throws SQLException { + pst.setInt(1, userId); + pst.setString(2, metaKey); + pst.setString(3, metaValue); + pst.addBatch(); + return this; + } + } } diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java index 5ebaadb46..bceeaca7a 100644 --- a/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java @@ -69,6 +69,11 @@ class InventoryPacketAdapter extends PacketAdapter { } } + /** + * Registers itself to ProtocolLib and blanks out the inventory packet to any applicable players. + * + * @param bukkitService the bukkit service (for retrieval of online players) + */ public void register(BukkitService bukkitService) { ProtocolLibrary.getProtocolManager().addPacketListener(this); @@ -85,6 +90,11 @@ class InventoryPacketAdapter extends PacketAdapter { ProtocolLibrary.getProtocolManager().removePacketListener(this); } + /** + * Sends a blanked out packet to the given player in order to hide the inventory. + * + * @param player the player to send the blank inventory packet to + */ public void sendBlankInventoryPacket(Player player) { ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS); diff --git a/src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java b/src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java index fd38d50b3..ae1b79cd9 100644 --- a/src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java +++ b/src/main/java/fr/xephi/authme/service/bungeecord/BungeeReceiver.java @@ -30,8 +30,8 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent private boolean isEnabled; @Inject - BungeeReceiver(final AuthMe plugin, final BukkitService bukkitService, final Management management, - final DataSource dataSource, final Settings settings) { + BungeeReceiver(AuthMe plugin, BukkitService bukkitService, Management management, + DataSource dataSource, Settings settings) { this.plugin = plugin; this.bukkitService = bukkitService; this.management = management; @@ -51,6 +51,11 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent } } + /** + * Processes the given data input and attempts to translate it to a message for the "AuthMe.v2.Broadcast" channel. + * + * @param in the input to handle + */ private void handleBroadcast(final ByteArrayDataInput in) { // Read data byte array final short dataLength = in.readShort(); @@ -91,7 +96,12 @@ public class BungeeReceiver implements PluginMessageListener, SettingsDependent } } - private void handle(final ByteArrayDataInput in) { + /** + * Processes the given data input and attempts to translate it to a message for the "AuthMe.v2" channel. + * + * @param in the input to handle + */ + private void handle(ByteArrayDataInput in) { // Parse type final String typeId = in.readUTF(); final Optional type = MessageType.fromId(typeId); diff --git a/src/test/java/tools/docs/translations/TranslationsGatherer.java b/src/test/java/tools/docs/translations/TranslationsGatherer.java index 9a08bd50d..ebe72f7a8 100644 --- a/src/test/java/tools/docs/translations/TranslationsGatherer.java +++ b/src/test/java/tools/docs/translations/TranslationsGatherer.java @@ -2,9 +2,8 @@ package tools.docs.translations; import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.YamlFileReader; -import fr.xephi.authme.message.MessagePathHelper; import fr.xephi.authme.message.MessageKey; -import tools.utils.ToolsConstants; +import fr.xephi.authme.message.MessagePathHelper; import java.io.File; import java.util.ArrayList; @@ -12,13 +11,14 @@ import java.util.Comparator; import java.util.List; import static tools.utils.FileIoUtils.listFilesOrThrow; +import static tools.utils.ToolsConstants.MAIN_RESOURCES_ROOT; /** * Gathers all available translations of AuthMe. */ public class TranslationsGatherer { - private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER; + private static final String MESSAGES_FOLDER = MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER; private List translationInfo = new ArrayList<>(); diff --git a/src/test/java/tools/helptranslation/HelpTranslationVerifier.java b/src/test/java/tools/helptranslation/HelpTranslationVerifier.java index f40af5175..08a446ded 100644 --- a/src/test/java/tools/helptranslation/HelpTranslationVerifier.java +++ b/src/test/java/tools/helptranslation/HelpTranslationVerifier.java @@ -109,6 +109,12 @@ public class HelpTranslationVerifier { return commandPaths; } + /** + * Creates all paths of the properties that are used to define the help translation of the given command definition. + * + * @param command the command to create the paths for + * @return all yaml paths that can be used to translate the command + */ private List getYamlPaths(CommandDescription command) { // e.g. commands.authme.register String commandPath = "commands." + CommandUtils.constructParentList(command).stream() diff --git a/src/test/java/tools/messages/VerifyMessagesTask.java b/src/test/java/tools/messages/VerifyMessagesTask.java index c6e82cd80..5b8407c7e 100644 --- a/src/test/java/tools/messages/VerifyMessagesTask.java +++ b/src/test/java/tools/messages/VerifyMessagesTask.java @@ -1,13 +1,12 @@ package tools.messages; import com.google.common.collect.Multimap; -import fr.xephi.authme.message.MessagePathHelper; import fr.xephi.authme.message.MessageKey; +import fr.xephi.authme.message.MessagePathHelper; import fr.xephi.authme.util.StringUtils; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import tools.utils.ToolTask; -import tools.utils.ToolsConstants; import java.io.File; import java.util.ArrayList; @@ -19,6 +18,7 @@ import java.util.Set; import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_MESSAGES_FILE; import static tools.utils.FileIoUtils.listFilesOrThrow; +import static tools.utils.ToolsConstants.MAIN_RESOURCES_ROOT; /** * Task to verify the keys in the messages files. @@ -26,7 +26,7 @@ import static tools.utils.FileIoUtils.listFilesOrThrow; public final class VerifyMessagesTask implements ToolTask { /** The folder containing the message files. */ - private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER; + private static final String MESSAGES_FOLDER = MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER; @Override public String getTaskName() { @@ -47,13 +47,14 @@ public final class VerifyMessagesTask implements ToolTask { if (StringUtils.isEmpty(inputFile)) { messageFiles = getMessagesFiles(); } else { - File customFile = new File(ToolsConstants.MAIN_RESOURCES_ROOT, MessagePathHelper.createMessageFilePath(inputFile)); + File customFile = new File(MAIN_RESOURCES_ROOT, MessagePathHelper.createMessageFilePath(inputFile)); messageFiles = Collections.singletonList(customFile); } FileConfiguration defaultFileConfiguration = null; if (addMissingKeys) { - defaultFileConfiguration = YamlConfiguration.loadConfiguration(new File(ToolsConstants.MAIN_RESOURCES_ROOT, DEFAULT_MESSAGES_FILE)); + defaultFileConfiguration = YamlConfiguration.loadConfiguration( + new File(MAIN_RESOURCES_ROOT, DEFAULT_MESSAGES_FILE)); } // Verify the given files