Issues (29)

bungee/listener/PluginMessageListener.java (3 issues)

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 lombok.Getter;
9
import net.labymod.serverapi.Addon;
10
import net.labymod.serverapi.bungee.LabyModPlugin;
11
import net.labymod.serverapi.bungee.event.LabyModPlayerJoinEvent;
12
import net.labymod.serverapi.bungee.event.MessageReceiveEvent;
13
import net.md_5.bungee.api.ProxyServer;
14
import net.md_5.bungee.api.connection.ProxiedPlayer;
15
import net.md_5.bungee.api.event.PluginMessageEvent;
16
import net.md_5.bungee.api.plugin.Listener;
17
import net.md_5.bungee.event.EventHandler;
18
19
import java.util.ArrayList;
20
import java.util.concurrent.TimeUnit;
21
22
/**
23
 * Class created by qlow | Jan
24
 */
25
public class PluginMessageListener implements Listener {
26
27
    @Getter
28
    private final static JsonParser jsonParser = new JsonParser();
29
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();
0 ignored issues
show
Throwable.printStackTrace writes to the console which might not be available at runtime. Using a logger is preferred.
Loading history...
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 {
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
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();
0 ignored issues
show
Throwable.printStackTrace writes to the console which might not be available at runtime. Using a logger is preferred.
Loading history...
104
            }
105
        }
106
    }
107
108
}
109