Passed
Pull Request — master (#3)
by
unknown
02:41
created

net.labymod.serverapi.bungee.listener.PluginMessageListener   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 40.74 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 33
c 1
b 0
f 0
dl 22
loc 54
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C onPluginMessage(PluginMessageEvent) 22 50 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
package net.labymod.serverapi.bungee.listener;
2
3
import com.google.gson.JsonElement;
4
import com.google.gson.JsonObject;
5
import com.google.gson.JsonParser;
6
import io.netty.buffer.ByteBuf;
7
import io.netty.buffer.Unpooled;
8
import net.labymod.serverapi.Addon;
9
import net.labymod.serverapi.bungee.LabyModPlugin;
10
import net.labymod.serverapi.bungee.event.LabyModPlayerJoinEvent;
11
import net.labymod.serverapi.bungee.event.MessageReceiveEvent;
12
import net.md_5.bungee.api.ProxyServer;
13
import net.md_5.bungee.api.connection.ProxiedPlayer;
14
import net.md_5.bungee.api.event.PluginMessageEvent;
15
import net.md_5.bungee.api.plugin.Listener;
16
import net.md_5.bungee.event.EventHandler;
17
18
import java.util.ArrayList;
19
20
/**
21
 * Class created by qlow | Jan
22
 */
23
public class PluginMessageListener implements Listener {
24
25
    private static final JsonParser JSON_PARSER = new JsonParser();
26
27
    @EventHandler
28
    public void onPluginMessage( PluginMessageEvent event ) {
29
        if ( !( event.getSender() instanceof ProxiedPlayer ) )
30
            return;
31
32
        ProxiedPlayer player = (ProxiedPlayer) event.getSender();
33
34
        // The LABYMOD plugin channel is higly deprecated and shouldn't be used - we just listen to it to retrieve old labymod clients.
35
        if ( event.getTag().equals( "LABYMOD" ) ) {
36
            // Converting the byte array into a byte buffer
37
            ByteBuf buf = Unpooled.wrappedBuffer( event.getData() );
38
            String version = LabyModPlugin.getInstance().getApi().readString( buf, Short.MAX_VALUE );
39
            ProxyServer.getInstance().getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, false, 0, new ArrayList<Addon>() ) );
40
        }
41
42
        if ( event.getTag().equals( "LMC" ) ) {
43
            // Converting the byte array into a byte buffer
44
            ByteBuf buf = Unpooled.wrappedBuffer( event.getData() );
45
46
            String messageKey = LabyModPlugin.getInstance().getApi().readString( buf, Short.MAX_VALUE );
47
            String messageContents = LabyModPlugin.getInstance().getApi().readString( buf, Short.MAX_VALUE );
48
            JsonElement jsonMessage = JSON_PARSER.parse( messageContents );
49
50
            // Listening to the INFO (join) message
51 View Code Duplication
            if ( messageKey.equals( "INFO" ) && jsonMessage.isJsonObject() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
52
                JsonObject jsonObject = jsonMessage.getAsJsonObject();
53
                String version = jsonObject.has( "version" )
54
                        && jsonObject.get( "version" ).isJsonPrimitive()
55
                        && jsonObject.get( "version" ).getAsJsonPrimitive().isString() ? jsonObject.get( "version" ).getAsString() : "Unknown";
56
57
                boolean chunkCachingEnabled = false;
58
                int chunkCachingVersion = 0;
59
60
                if ( jsonObject.has( "ccp" ) && jsonObject.get( "ccp" ).isJsonObject() ) {
61
                    JsonObject chunkCachingObject = jsonObject.get( "ccp" ).getAsJsonObject();
62
63
                    if ( chunkCachingObject.has( "enabled" ) )
64
                        chunkCachingEnabled = chunkCachingObject.get( "enabled" ).getAsBoolean();
65
66
                    if ( chunkCachingObject.has( "version" ) )
67
                        chunkCachingVersion = chunkCachingObject.get( "version" ).getAsInt();
68
                }
69
70
                ProxyServer.getInstance().getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version,
71
                        chunkCachingEnabled, chunkCachingVersion, Addon.getAddons( jsonObject ) ) );
0 ignored issues
show
Obsolete introduced by
Remove this use of "getAddons"; it is deprecated.
Loading history...
72
                return;
73
            }
74
75
            // Calling the LabyModPlayerJoinEvent
76
            ProxyServer.getInstance().getPluginManager().callEvent( new MessageReceiveEvent( player, messageKey, jsonMessage ) );
77
        }
78
    }
79
}
80