Issues (29)

net/labymod/serverapi/bungee/LabyModPlugin.java (3 issues)

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.md_5.bungee.api.connection.ProxiedPlayer;
15
import net.md_5.bungee.api.plugin.Plugin;
16
import net.md_5.bungee.protocol.packet.PluginMessage;
17
18
import java.io.File;
19
import java.util.HashMap;
20
import java.util.Map;
21
22
/**
23
 * Class created by qlow | Jan
24
 */
25
public class LabyModPlugin extends Plugin {
26
27
    @Getter
28
    private static LabyModPlugin instance;
29
30
    private static final JsonParser JSON_PARSER = new JsonParser();
31
32
    @Getter
33
    private LabyModConfig labyModConfig;
34
35
    @Getter
36
    private LabyModAPI api = new LabyModAPI();
37
38
    @Override
39
    public void onEnable() {
40
        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...
41
42
        // Creating the data folder
43
        if ( !getDataFolder().exists() )
44
            getDataFolder().mkdir();
45
46
        // Initializing the config
47
        this.labyModConfig = new BungeecordLabyModConfig( new File( getDataFolder(), "config.yml" ) );
48
49
        // Registering the listeners
50
        getProxy().getPluginManager().registerListener( this, new PlayerJoinListener() );
51
        getProxy().getPluginManager().registerListener( this, new PluginMessageListener() );
52
    }
53
54
    /**
55
     * Sends the modified permissions to the given player
56
     *
57
     * @param player the player the permissions should be sent to
58
     */
59
    public void sendPermissions( ProxiedPlayer player ) {
60
        Map<Permission, Boolean> modifiedPermissions = new HashMap<>( labyModConfig.getPermissions() );
0 ignored issues
show
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...
61
62
        // Calling the Bukkit event
63
        PermissionsSendEvent sendEvent = new PermissionsSendEvent( player, modifiedPermissions, false );
64
        getProxy().getPluginManager().callEvent( sendEvent );
65
66
        // Sending the packet
67
        if ( !sendEvent.isCancelled() )
68
            player.unsafe().sendPacket( new PluginMessage( "LMC", api.getBytesToSend( modifiedPermissions ), false ) );
69
    }
70
71
    /**
72
     * Sends a JSON server-message to the player
73
     *
74
     * @param player          the player the message should be sent to
75
     * @param messageKey      the message's key
76
     * @param messageContents the message's contents
77
     */
78
    public void sendServerMessage( ProxiedPlayer player, String messageKey, JsonElement messageContents ) {
79
        messageContents = cloneJson( messageContents );
80
81
        // Calling the Bukkit event
82
        MessageSendEvent sendEvent = new MessageSendEvent( player, messageKey, messageContents, false );
83
        getProxy().getPluginManager().callEvent( sendEvent );
84
85
        // Sending the packet
86
        if ( !sendEvent.isCancelled() )
87
            player.unsafe().sendPacket( new PluginMessage( "LMC", api.getBytesToSend( messageKey, messageContents.toString() ), false ) );
88
    }
89
90
    /**
91
     * Clones a JsonElement
92
     *
93
     * @param cloneElement the element that should be cloned
94
     * @return the cloned element
95
     */
96
    public JsonElement cloneJson( JsonElement cloneElement ) {
97
        try {
98
            return JSON_PARSER.parse( cloneElement.toString() );
99
        } catch ( JsonParseException ex ) {
100
            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...
101
            return null;
102
        }
103
    }
104
105
}
106