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; |
||
52 | @Override |
||
53 | public void onEnable() { |
||
54 | instance = this; |
||
|
|||
55 | |||
56 | // Initializing packet utils |
||
57 | this.packetUtils = new PacketUtils(); |
||
58 | |||
59 | // Creating the data folder |
||
60 | if ( !getDataFolder().exists() ) |
||
61 | getDataFolder().mkdir(); |
||
62 | |||
63 | // Initializing the config |
||
64 | this.labyModConfig = new BukkitLabyModConfig( new File( getDataFolder(), "config.yml" ) ); |
||
65 | |||
66 | // Registering the listeners |
||
67 | Bukkit.getPluginManager().registerEvents( new PlayerJoinListener(), this ); |
||
68 | |||
69 | // The LABYMOD plugin channel is higly deprecated and shouldn't be used - we just listen to it to retrieve old labymod clients. |
||
70 | // Registering the incoming plugin messages listeners |
||
71 | getServer().getMessenger().registerIncomingPluginChannel( this, "LABYMOD", new PluginMessageListener() { |
||
72 | @Override |
||
73 | public void onPluginMessageReceived( String channel, final Player player, byte[] bytes ) { |
||
74 | // Converting the byte array into a byte buffer |
||
75 | ByteBuf buf = Unpooled.wrappedBuffer( bytes ); |
||
76 | |||
77 | try { |
||
78 | // Reading the version from the buffer |
||
79 | final String version = api.readString( buf, Short.MAX_VALUE ); |
||
80 | |||
81 | // Calling the event synchronously |
||
82 | Bukkit.getScheduler().runTask( LabyModPlugin.this, new Runnable() { |
||
83 | @Override |
||
84 | public void run() { |
||
85 | // Checking whether the player is still online |
||
86 | if ( !player.isOnline() ) |
||
87 | return; |
||
88 | |||
89 | // Calling the LabyModPlayerJoinEvent |
||
90 | Bukkit.getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, false, 0, new ArrayList<Addon>() ) ); |
||
91 | } |
||
92 | } ); |
||
93 | } catch ( RuntimeException ex ) { |
||
94 | } |
||
95 | } |
||
96 | } ); |
||
97 | |||
98 | getServer().getMessenger().registerIncomingPluginChannel( this, "LMC", new PluginMessageListener() { |
||
99 | @Override |
||
100 | public void onPluginMessageReceived( String channel, final Player player, byte[] bytes ) { |
||
101 | // Converting the byte array into a byte buffer |
||
102 | ByteBuf buf = Unpooled.wrappedBuffer( bytes ); |
||
103 | |||
104 | View Code Duplication | try { |
|
105 | // Reading the message key |
||
106 | final String messageKey = api.readString( buf, Short.MAX_VALUE ); |
||
107 | final String messageContents = api.readString( buf, Short.MAX_VALUE ); |
||
108 | final JsonElement jsonMessage = JSON_PARSER.parse( messageContents ); |
||
109 | |||
110 | // Calling the event synchronously |
||
111 | Bukkit.getScheduler().runTask( LabyModPlugin.this, new Runnable() { |
||
112 | @Override |
||
113 | public void run() { |
||
114 | // Checking whether the player is still online |
||
115 | if ( !player.isOnline() ) |
||
116 | return; |
||
117 | |||
118 | // Listening to the INFO (join) message |
||
119 | if ( messageKey.equals( "INFO" ) && jsonMessage.isJsonObject() ) { |
||
120 | JsonObject jsonObject = jsonMessage.getAsJsonObject(); |
||
121 | String version = jsonObject.has( "version" ) |
||
122 | && jsonObject.get( "version" ).isJsonPrimitive() |
||
123 | && jsonObject.get( "version" ).getAsJsonPrimitive().isString() ? jsonObject.get( "version" ).getAsString() : "Unknown"; |
||
124 | |||
125 | boolean chunkCachingEnabled = false; |
||
126 | int chunkCachingVersion = 0; |
||
127 | |||
128 | if ( jsonObject.has( "ccp" ) && jsonObject.get( "ccp" ).isJsonObject() ) { |
||
129 | JsonObject chunkCachingObject = jsonObject.get( "ccp" ).getAsJsonObject(); |
||
130 | |||
131 | if ( chunkCachingObject.has( "enabled" ) ) |
||
132 | chunkCachingEnabled = chunkCachingObject.get( "enabled" ).getAsBoolean(); |
||
133 | |||
134 | if ( chunkCachingObject.has( "version" ) ) |
||
135 | chunkCachingVersion = chunkCachingObject.get( "version" ).getAsInt(); |
||
136 | } |
||
137 | |||
138 | Bukkit.getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, |
||
139 | chunkCachingEnabled, chunkCachingVersion, Addon.getAddons( jsonObject ) ) ); |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | // Calling the MessageReceiveEvent |
||
144 | Bukkit.getPluginManager().callEvent( new MessageReceiveEvent( player, messageKey, jsonMessage ) ); |
||
145 | } |
||
146 | } ); |
||
147 | } catch ( RuntimeException ignored ) { |
||
148 | } |
||
222 |
If you really need to set this static field, consider writing a thread-safe setter and atomic getter.