| Conditions | 17 |
| Total Lines | 96 |
| Code Lines | 52 |
| Lines | 44 |
| Ratio | 45.83 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like net.labymod.serverapi.bukkit.LabyModPlugin.onEnable() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package net.labymod.serverapi.bukkit; |
||
| 49 | @Override |
||
| 50 | public void onEnable() { |
||
| 51 | instance = this; |
||
|
|
|||
| 52 | |||
| 53 | // Initializing packet utils |
||
| 54 | this.packetUtils = new PacketUtils(); |
||
| 55 | |||
| 56 | // Creating the data folder |
||
| 57 | if ( !getDataFolder().exists() ) |
||
| 58 | getDataFolder().mkdir(); |
||
| 59 | |||
| 60 | // Initializing the config |
||
| 61 | this.labyModConfig = new BukkitLabyModConfig( new File( getDataFolder(), "config.yml" ) ); |
||
| 62 | |||
| 63 | // Registering the listeners |
||
| 64 | Bukkit.getPluginManager().registerEvents( new PlayerJoinListener(), this ); |
||
| 65 | |||
| 66 | // The LABYMOD plugin channel is higly deprecated and shouldn't be used - we just listen to it to retrieve old labymod clients. |
||
| 67 | // Registering the incoming plugin messages listeners |
||
| 68 | getServer().getMessenger().registerIncomingPluginChannel( this, "LABYMOD", new PluginMessageListener() { |
||
| 69 | @Override |
||
| 70 | public void onPluginMessageReceived( String channel, final Player player, byte[] bytes ) { |
||
| 71 | // Converting the byte array into a byte buffer |
||
| 72 | ByteBuf buf = Unpooled.wrappedBuffer( bytes ); |
||
| 73 | |||
| 74 | try { |
||
| 75 | // Reading the version from the buffer |
||
| 76 | final String version = api.readString( buf, Short.MAX_VALUE ); |
||
| 77 | |||
| 78 | // Calling the event synchronously |
||
| 79 | Bukkit.getScheduler().runTask( LabyModPlugin.this, new Runnable() { |
||
| 80 | @Override |
||
| 81 | public void run() { |
||
| 82 | // Checking whether the player is still online |
||
| 83 | if ( !player.isOnline() ) |
||
| 84 | return; |
||
| 85 | |||
| 86 | // Calling the LabyModPlayerJoinEvent |
||
| 87 | Bukkit.getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, false, 0, new ArrayList<Addon>() ) ); |
||
| 88 | } |
||
| 89 | } ); |
||
| 90 | } catch ( RuntimeException ex ) { |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } ); |
||
| 94 | |||
| 95 | getServer().getMessenger().registerIncomingPluginChannel( this, "LMC", new PluginMessageListener() { |
||
| 96 | @Override |
||
| 97 | public void onPluginMessageReceived( String channel, final Player player, byte[] bytes ) { |
||
| 98 | // Converting the byte array into a byte buffer |
||
| 99 | ByteBuf buf = Unpooled.wrappedBuffer( bytes ); |
||
| 100 | |||
| 101 | View Code Duplication | try { |
|
| 102 | // Reading the message key |
||
| 103 | final String messageKey = api.readString( buf, Short.MAX_VALUE ); |
||
| 104 | final String messageContents = api.readString( buf, Short.MAX_VALUE ); |
||
| 105 | final JsonElement jsonMessage = JSON_PARSER.parse( messageContents ); |
||
| 106 | |||
| 107 | // Calling the event synchronously |
||
| 108 | Bukkit.getScheduler().runTask( LabyModPlugin.this, new Runnable() { |
||
| 109 | @Override |
||
| 110 | public void run() { |
||
| 111 | // Checking whether the player is still online |
||
| 112 | if ( !player.isOnline() ) |
||
| 113 | return; |
||
| 114 | |||
| 115 | // Listening to the INFO (join) message |
||
| 116 | if ( messageKey.equals( "INFO" ) && jsonMessage.isJsonObject() ) { |
||
| 117 | JsonObject jsonObject = jsonMessage.getAsJsonObject(); |
||
| 118 | String version = jsonObject.has( "version" ) |
||
| 119 | && jsonObject.get( "version" ).isJsonPrimitive() |
||
| 120 | && jsonObject.get( "version" ).getAsJsonPrimitive().isString() ? jsonObject.get( "version" ).getAsString() : "Unknown"; |
||
| 121 | |||
| 122 | boolean chunkCachingEnabled = false; |
||
| 123 | int chunkCachingVersion = 0; |
||
| 124 | |||
| 125 | if ( jsonObject.has( "ccp" ) && jsonObject.get( "ccp" ).isJsonObject() ) { |
||
| 126 | JsonObject chunkCachingObject = jsonObject.get( "ccp" ).getAsJsonObject(); |
||
| 127 | |||
| 128 | if ( chunkCachingObject.has( "enabled" ) ) |
||
| 129 | chunkCachingEnabled = chunkCachingObject.get( "enabled" ).getAsBoolean(); |
||
| 130 | |||
| 131 | if ( chunkCachingObject.has( "version" ) ) |
||
| 132 | chunkCachingVersion = chunkCachingObject.get( "version" ).getAsInt(); |
||
| 133 | } |
||
| 134 | |||
| 135 | Bukkit.getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, |
||
| 136 | chunkCachingEnabled, chunkCachingVersion, Addon.getAddons( jsonObject ) ) ); |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | // Calling the MessageReceiveEvent |
||
| 141 | Bukkit.getPluginManager().callEvent( new MessageReceiveEvent( player, messageKey, jsonMessage ) ); |
||
| 142 | } |
||
| 143 | } ); |
||
| 144 | } catch ( RuntimeException ignored ) { |
||
| 145 | } |
||
| 209 |
If you really need to set this static field, consider writing a thread-safe setter and atomic getter.