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; |
|
|
|
|
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() ); |
|
|
|
|
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(); |
|
|
|
|
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
If you really need to set this static field, consider writing a thread-safe setter and atomic getter.