1
|
|
|
package net.labymod.serverapi.bungee; |
2
|
|
|
|
3
|
|
|
import com.google.gson.JsonElement; |
4
|
|
|
import com.google.gson.JsonParser; |
5
|
|
|
import lombok.Getter; |
6
|
|
|
import net.labymod.serverapi.LabyModAPI; |
7
|
|
|
import net.labymod.serverapi.LabyModConfig; |
8
|
|
|
import net.labymod.serverapi.Permission; |
9
|
|
|
import net.labymod.serverapi.bungee.event.MessageSendEvent; |
10
|
|
|
import net.labymod.serverapi.bungee.event.PermissionsSendEvent; |
11
|
|
|
import net.labymod.serverapi.bungee.listener.PlayerJoinListener; |
12
|
|
|
import net.labymod.serverapi.bungee.listener.PluginMessageListener; |
13
|
|
|
import net.md_5.bungee.api.connection.ProxiedPlayer; |
14
|
|
|
import net.md_5.bungee.api.plugin.Plugin; |
15
|
|
|
import net.md_5.bungee.protocol.packet.PluginMessage; |
16
|
|
|
|
17
|
|
|
import java.io.File; |
18
|
|
|
import java.util.HashMap; |
19
|
|
|
import java.util.Map; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class created by qlow | Jan |
23
|
|
|
*/ |
24
|
|
|
public class LabyModPlugin extends Plugin { |
25
|
|
|
|
26
|
|
|
@Getter |
27
|
|
|
private static LabyModPlugin instance; |
28
|
|
|
|
29
|
|
|
private static final JsonParser JSON_PARSER = new JsonParser(); |
30
|
|
|
|
31
|
|
|
@Getter |
32
|
|
|
private LabyModConfig labyModConfig; |
33
|
|
|
|
34
|
|
|
@Getter |
35
|
|
|
private LabyModAPI api = new LabyModAPI(); |
36
|
|
|
|
37
|
|
|
@Override |
38
|
|
|
public void onEnable() { |
39
|
|
|
instance = this; |
|
|
|
|
40
|
|
|
|
41
|
|
|
this.initialize(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private void initialize() { |
45
|
|
|
// Creating the data folder |
46
|
|
|
if ( !getDataFolder().exists() ) |
47
|
|
|
getDataFolder().mkdir(); |
48
|
|
|
|
49
|
|
|
// Initializing the config |
50
|
|
|
this.labyModConfig = new BungeecordLabyModConfig( new File( getDataFolder(), "config.yml" ) ); |
51
|
|
|
|
52
|
|
|
// Registering the listeners |
53
|
|
|
getProxy().getPluginManager().registerListener( this, new PlayerJoinListener() ); |
54
|
|
|
getProxy().getPluginManager().registerListener( this, new PluginMessageListener() ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sends the modified permissions to the given player |
59
|
|
|
* |
60
|
|
|
* @param player the player the permissions should be sent to |
61
|
|
|
*/ |
62
|
|
|
public void sendPermissions( ProxiedPlayer player ) { |
63
|
|
|
Map<Permission, Boolean> modifiedPermissions = new HashMap<>( labyModConfig.getPermissions() ); |
|
|
|
|
64
|
|
|
|
65
|
|
|
// Calling the Bukkit event |
66
|
|
|
PermissionsSendEvent sendEvent = new PermissionsSendEvent( player, modifiedPermissions, false ); |
67
|
|
|
getProxy().getPluginManager().callEvent( sendEvent ); |
68
|
|
|
|
69
|
|
|
// Sending the packet |
70
|
|
|
if ( !sendEvent.isCancelled() ) |
71
|
|
|
player.unsafe().sendPacket( new PluginMessage( "LMC", api.getBytesToSend( modifiedPermissions ), false ) ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Sends a JSON server-message to the player |
76
|
|
|
* |
77
|
|
|
* @param player the player the message should be sent to |
78
|
|
|
* @param messageKey the message's key |
79
|
|
|
* @param messageContents the message's contents |
80
|
|
|
*/ |
81
|
|
|
public void sendServerMessage( ProxiedPlayer player, String messageKey, JsonElement messageContents ) { |
82
|
|
|
messageContents = JSON_PARSER.parse( messageContents.toString() ); |
83
|
|
|
|
84
|
|
|
// Calling the Bukkit event |
85
|
|
|
MessageSendEvent sendEvent = new MessageSendEvent( player, messageKey, messageContents, false ); |
86
|
|
|
getProxy().getPluginManager().callEvent( sendEvent ); |
87
|
|
|
|
88
|
|
|
// Sending the packet |
89
|
|
|
if ( !sendEvent.isCancelled() ) |
90
|
|
|
player.unsafe().sendPacket( new PluginMessage( "LMC", api.getBytesToSend( messageKey, messageContents.toString() ), false ) ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
If you really need to set this static field, consider writing a thread-safe setter and atomic getter.