| Conditions | 15 |
| Total Lines | 74 |
| Code Lines | 45 |
| Lines | 41 |
| Ratio | 55.41 % |
| 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.bungee.listener.PluginMessageListener.onPluginMessage(PluginMessageEvent) 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.bungee.listener; |
||
| 30 | @EventHandler |
||
| 31 | public void onPluginMessage( PluginMessageEvent event ) { |
||
| 32 | if ( !(event.getSender() instanceof ProxiedPlayer) ) |
||
| 33 | return; |
||
| 34 | |||
| 35 | final ProxiedPlayer player = ( ProxiedPlayer ) event.getSender(); |
||
| 36 | |||
| 37 | // The LABYMOD plugin channel is higly deprecated and shouldn't be used - we just listen to it to retrieve old labymod clients. |
||
| 38 | if ( event.getTag().equals( "LABYMOD" ) ) { |
||
| 39 | // Converting the byte array into a byte buffer |
||
| 40 | ByteBuf buf = Unpooled.wrappedBuffer( event.getData() ); |
||
| 41 | |||
| 42 | try { |
||
| 43 | // Reading the version from the buffer |
||
| 44 | final String version = LabyModPlugin.getInstance().getApi().readString( buf, Short.MAX_VALUE ); |
||
| 45 | |||
| 46 | // Calling the event synchronously |
||
| 47 | ProxyServer.getInstance().getScheduler().schedule( LabyModPlugin.getInstance(), new Runnable() { |
||
| 48 | @Override |
||
| 49 | public void run() { |
||
| 50 | // Calling the LabyModPlayerJoinEvent |
||
| 51 | ProxyServer.getInstance().getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, false, 0, new ArrayList<Addon>() ) ); |
||
| 52 | } |
||
| 53 | }, 0L, TimeUnit.SECONDS ); |
||
| 54 | } catch ( RuntimeException ex ) { |
||
| 55 | ex.printStackTrace(); |
||
|
|
|||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | if ( event.getTag().equals( "LMC" ) ) { |
||
| 60 | // Converting the byte array into a byte buffer |
||
| 61 | ByteBuf buf = Unpooled.wrappedBuffer( event.getData() ); |
||
| 62 | |||
| 63 | View Code Duplication | try { |
|
| 64 | // Reading the message key |
||
| 65 | final String messageKey = LabyModPlugin.getInstance().getApi().readString( buf, Short.MAX_VALUE ); |
||
| 66 | final String messageContents = LabyModPlugin.getInstance().getApi().readString( buf, Short.MAX_VALUE ); |
||
| 67 | final JsonElement jsonMessage = jsonParser.parse( messageContents ); |
||
| 68 | |||
| 69 | // Calling the event synchronously |
||
| 70 | ProxyServer.getInstance().getScheduler().schedule( LabyModPlugin.getInstance(), new Runnable() { |
||
| 71 | @Override |
||
| 72 | public void run() { |
||
| 73 | // Listening to the INFO (join) message |
||
| 74 | if ( messageKey.equals( "INFO" ) && jsonMessage.isJsonObject() ) { |
||
| 75 | JsonObject jsonObject = jsonMessage.getAsJsonObject(); |
||
| 76 | String version = jsonObject.has( "version" ) |
||
| 77 | && jsonObject.get( "version" ).isJsonPrimitive() |
||
| 78 | && jsonObject.get( "version" ).getAsJsonPrimitive().isString() ? jsonObject.get( "version" ).getAsString() : "Unknown"; |
||
| 79 | |||
| 80 | boolean chunkCachingEnabled = false; |
||
| 81 | int chunkCachingVersion = 0; |
||
| 82 | |||
| 83 | if ( jsonObject.has( "ccp" ) && jsonObject.get( "ccp" ).isJsonObject() ) { |
||
| 84 | JsonObject chunkCachingObject = jsonObject.get( "ccp" ).getAsJsonObject(); |
||
| 85 | |||
| 86 | if ( chunkCachingObject.has( "enabled" ) ) |
||
| 87 | chunkCachingEnabled = chunkCachingObject.get( "enabled" ).getAsBoolean(); |
||
| 88 | |||
| 89 | if ( chunkCachingObject.has( "version" ) ) |
||
| 90 | chunkCachingVersion = chunkCachingObject.get( "version" ).getAsInt(); |
||
| 91 | } |
||
| 92 | |||
| 93 | ProxyServer.getInstance().getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, |
||
| 94 | chunkCachingEnabled, chunkCachingVersion, Addon.getAddons( jsonObject ) ) ); |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | // Calling the LabyModPlayerJoinEvent |
||
| 99 | ProxyServer.getInstance().getPluginManager().callEvent( new MessageReceiveEvent( player, messageKey, jsonMessage ) ); |
||
| 100 | } |
||
| 101 | }, 0L, TimeUnit.SECONDS ); |
||
| 102 | } catch ( RuntimeException ex ) { |
||
| 103 | ex.printStackTrace(); |
||
| 104 | } |
||
| 109 |