Passed
Pull Request — master (#7)
by
unknown
02:39
created

net.labymod.serverapi.bungee.LabyModPlugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 36
dl 0
loc 87
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sendServerMessage(ProxiedPlayer,String,JsonElement) 0 10 2
A sendPermissions(ProxiedPlayer) 0 10 2
A cloneJson(JsonElement) 0 6 2
A sendRichPresence(ProxiedPlayer,RichPresence) 0 2 1
A onEnable() 0 14 2
1
package net.labymod.serverapi.bungee;
2
3
import com.google.gson.JsonElement;
4
import com.google.gson.JsonParseException;
5
import com.google.gson.JsonParser;
6
import lombok.Getter;
7
import net.labymod.serverapi.LabyModAPI;
8
import net.labymod.serverapi.LabyModConfig;
9
import net.labymod.serverapi.Permission;
10
import net.labymod.serverapi.bungee.event.MessageSendEvent;
11
import net.labymod.serverapi.bungee.event.PermissionsSendEvent;
12
import net.labymod.serverapi.bungee.listener.PlayerJoinListener;
13
import net.labymod.serverapi.bungee.listener.PluginMessageListener;
14
import net.labymod.serverapi.discord.RichPresence;
15
import net.md_5.bungee.api.connection.ProxiedPlayer;
16
import net.md_5.bungee.api.plugin.Plugin;
17
import net.md_5.bungee.protocol.packet.PluginMessage;
18
19
import java.io.File;
20
import java.util.HashMap;
21
import java.util.Map;
22
23
/**
24
 * Class created by qlow | Jan
25
 */
26
public class LabyModPlugin extends Plugin {
27
28
    @Getter
29
    private static LabyModPlugin instance;
30
31
    private static final JsonParser JSON_PARSER = new JsonParser();
32
33
    @Getter
34
    private LabyModConfig labyModConfig;
35
36
    @Getter
37
    private LabyModAPI api = new LabyModAPI();
38
39
    @Override
40
    public void onEnable() {
41
        instance = this;
0 ignored issues
show
Bug Multi Threading introduced by
Instance methods writing to static fields may lead to concurrency problems. Consider making the enclosing method static or removing this assignment to a static field.

If you really need to set this static field, consider writing a thread-safe setter and atomic getter.

Loading history...
42
43
        // Creating the data folder
44
        if ( !getDataFolder().exists() )
45
            getDataFolder().mkdir();
46
47
        // Initializing the config
48
        this.labyModConfig = new BungeecordLabyModConfig( new File( getDataFolder(), "config.yml" ) );
49
50
        // Registering the listeners
51
        getProxy().getPluginManager().registerListener( this, new PlayerJoinListener() );
52
        getProxy().getPluginManager().registerListener( this, new PluginMessageListener() );
53
    }
54
55
    /**
56
     * Sends the modified permissions to the given player
57
     *
58
     * @param player the player the permissions should be sent to
59
     */
60
    public void sendPermissions( ProxiedPlayer player ) {
61
        Map<Permission, Boolean> modifiedPermissions = new HashMap<>( labyModConfig.getPermissions() );
0 ignored issues
show
Performance introduced by
When using a map whose keys are EnumValues, consider using an EnumMap instead, which is more performant in this case.

The Java documentation explain EnumMap.

Loading history...
62
63
        // Calling the Bukkit event
64
        PermissionsSendEvent sendEvent = new PermissionsSendEvent( player, modifiedPermissions, false );
65
        getProxy().getPluginManager().callEvent( sendEvent );
66
67
        // Sending the packet
68
        if ( !sendEvent.isCancelled() )
69
            player.unsafe().sendPacket( new PluginMessage( "LMC", api.getBytesToSend( modifiedPermissions ), false ) );
70
    }
71
72
    /**
73
     * Sends a JSON server-message to the player
74
     *
75
     * @param player          the player the message should be sent to
76
     * @param messageKey      the message's key
77
     * @param messageContents the message's contents
78
     */
79
    public void sendServerMessage( ProxiedPlayer player, String messageKey, JsonElement messageContents ) {
80
        messageContents = cloneJson( messageContents );
81
82
        // Calling the Bukkit event
83
        MessageSendEvent sendEvent = new MessageSendEvent( player, messageKey, messageContents, false );
84
        getProxy().getPluginManager().callEvent( sendEvent );
85
86
        // Sending the packet
87
        if ( !sendEvent.isCancelled() )
88
            player.unsafe().sendPacket( new PluginMessage( "LMC", api.getBytesToSend( messageKey, messageContents.toString() ), false ) );
89
    }
90
    
91
    /**
92
     * Sends the Discord Rich Presence message to the player
93
     * 
94
     * @param player		the player the rich presence should be sent to
95
     * @param richPresence	the presence object
96
     */
97
    public void sendRichPresence (ProxiedPlayer player, RichPresence richPresence ) {
98
    	sendServerMessage(player, "discord_rpc", richPresence.toJson());
99
    }
100
101
    /**
102
     * Clones a JsonElement
103
     *
104
     * @param cloneElement the element that should be cloned
105
     * @return the cloned element
106
     */
107
    public JsonElement cloneJson( JsonElement cloneElement ) {
108
        try {
109
            return JSON_PARSER.parse( cloneElement.toString() );
110
        } catch ( JsonParseException ex ) {
111
            ex.printStackTrace();
0 ignored issues
show
Best Practice introduced by
Throwable.printStackTrace writes to the console which might not be available at runtime. Using a logger is preferred.
Loading history...
112
            return null;
113
        }
114
    }
115
116
}
117