1
|
|
|
package net.labymod.serverapi.bukkit; |
2
|
|
|
|
3
|
|
|
import com.google.gson.JsonElement; |
4
|
|
|
import com.google.gson.JsonObject; |
5
|
|
|
import com.google.gson.JsonParseException; |
6
|
|
|
import com.google.gson.JsonParser; |
7
|
|
|
import io.netty.buffer.ByteBuf; |
8
|
|
|
import io.netty.buffer.Unpooled; |
9
|
|
|
import lombok.Getter; |
10
|
|
|
import lombok.NonNull; |
11
|
|
|
import net.labymod.serverapi.Addon; |
12
|
|
|
import net.labymod.serverapi.LabyModAPI; |
13
|
|
|
import net.labymod.serverapi.LabyModConfig; |
14
|
|
|
import net.labymod.serverapi.Permission; |
15
|
|
|
import net.labymod.serverapi.bukkit.event.LabyModPlayerJoinEvent; |
16
|
|
|
import net.labymod.serverapi.bukkit.event.MessageReceiveEvent; |
17
|
|
|
import net.labymod.serverapi.bukkit.event.MessageSendEvent; |
18
|
|
|
import net.labymod.serverapi.bukkit.event.PermissionsSendEvent; |
19
|
|
|
import net.labymod.serverapi.bukkit.listener.PlayerJoinListener; |
20
|
|
|
import net.labymod.serverapi.bukkit.utils.PacketUtils; |
21
|
|
|
import net.labymod.serverapi.discord.RichPresence; |
22
|
|
|
|
23
|
|
|
import org.bukkit.Bukkit; |
24
|
|
|
import org.bukkit.entity.Player; |
25
|
|
|
import org.bukkit.plugin.java.JavaPlugin; |
26
|
|
|
import org.bukkit.plugin.messaging.PluginMessageListener; |
27
|
|
|
|
28
|
|
|
import java.io.File; |
29
|
|
|
import java.util.ArrayList; |
30
|
|
|
import java.util.HashMap; |
31
|
|
|
import java.util.Map; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class created by qlow | Jan |
35
|
|
|
*/ |
36
|
|
|
public class LabyModPlugin extends JavaPlugin { |
37
|
|
|
|
38
|
|
|
@Getter |
39
|
|
|
private static LabyModPlugin instance; |
40
|
|
|
|
41
|
|
|
private static final JsonParser JSON_PARSER = new JsonParser(); |
42
|
|
|
|
43
|
|
|
@Getter |
44
|
|
|
private LabyModConfig labyModConfig; |
45
|
|
|
|
46
|
|
|
@Getter |
47
|
|
|
private LabyModAPI api = new LabyModAPI(); |
48
|
|
|
|
49
|
|
|
@Getter |
50
|
|
|
private PacketUtils packetUtils; |
51
|
|
|
|
52
|
|
|
@Override |
53
|
|
|
public void onEnable() { |
54
|
|
|
instance = this; |
|
|
|
|
55
|
|
|
|
56
|
|
|
// Initializing packet utils |
57
|
|
|
this.packetUtils = new PacketUtils(); |
58
|
|
|
|
59
|
|
|
// Creating the data folder |
60
|
|
|
if ( !getDataFolder().exists() ) |
61
|
|
|
getDataFolder().mkdir(); |
62
|
|
|
|
63
|
|
|
// Initializing the config |
64
|
|
|
this.labyModConfig = new BukkitLabyModConfig( new File( getDataFolder(), "config.yml" ) ); |
65
|
|
|
|
66
|
|
|
// Registering the listeners |
67
|
|
|
Bukkit.getPluginManager().registerEvents( new PlayerJoinListener(), this ); |
68
|
|
|
|
69
|
|
|
// The LABYMOD plugin channel is higly deprecated and shouldn't be used - we just listen to it to retrieve old labymod clients. |
70
|
|
|
// Registering the incoming plugin messages listeners |
71
|
|
|
getServer().getMessenger().registerIncomingPluginChannel( this, "LABYMOD", new PluginMessageListener() { |
72
|
|
|
@Override |
73
|
|
|
public void onPluginMessageReceived( String channel, final Player player, byte[] bytes ) { |
74
|
|
|
// Converting the byte array into a byte buffer |
75
|
|
|
ByteBuf buf = Unpooled.wrappedBuffer( bytes ); |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
// Reading the version from the buffer |
79
|
|
|
final String version = api.readString( buf, Short.MAX_VALUE ); |
80
|
|
|
|
81
|
|
|
// Calling the event synchronously |
82
|
|
|
Bukkit.getScheduler().runTask( LabyModPlugin.this, new Runnable() { |
83
|
|
|
@Override |
84
|
|
|
public void run() { |
85
|
|
|
// Checking whether the player is still online |
86
|
|
|
if ( !player.isOnline() ) |
87
|
|
|
return; |
88
|
|
|
|
89
|
|
|
// Calling the LabyModPlayerJoinEvent |
90
|
|
|
Bukkit.getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, false, 0, new ArrayList<Addon>() ) ); |
91
|
|
|
} |
92
|
|
|
} ); |
93
|
|
|
} catch ( RuntimeException ex ) { |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} ); |
97
|
|
|
|
98
|
|
|
getServer().getMessenger().registerIncomingPluginChannel( this, "LMC", new PluginMessageListener() { |
99
|
|
|
@Override |
100
|
|
|
public void onPluginMessageReceived( String channel, final Player player, byte[] bytes ) { |
101
|
|
|
// Converting the byte array into a byte buffer |
102
|
|
|
ByteBuf buf = Unpooled.wrappedBuffer( bytes ); |
103
|
|
|
|
104
|
|
View Code Duplication |
try { |
|
|
|
|
105
|
|
|
// Reading the message key |
106
|
|
|
final String messageKey = api.readString( buf, Short.MAX_VALUE ); |
107
|
|
|
final String messageContents = api.readString( buf, Short.MAX_VALUE ); |
108
|
|
|
final JsonElement jsonMessage = JSON_PARSER.parse( messageContents ); |
109
|
|
|
|
110
|
|
|
// Calling the event synchronously |
111
|
|
|
Bukkit.getScheduler().runTask( LabyModPlugin.this, new Runnable() { |
112
|
|
|
@Override |
113
|
|
|
public void run() { |
114
|
|
|
// Checking whether the player is still online |
115
|
|
|
if ( !player.isOnline() ) |
116
|
|
|
return; |
117
|
|
|
|
118
|
|
|
// Listening to the INFO (join) message |
119
|
|
|
if ( messageKey.equals( "INFO" ) && jsonMessage.isJsonObject() ) { |
120
|
|
|
JsonObject jsonObject = jsonMessage.getAsJsonObject(); |
121
|
|
|
String version = jsonObject.has( "version" ) |
122
|
|
|
&& jsonObject.get( "version" ).isJsonPrimitive() |
123
|
|
|
&& jsonObject.get( "version" ).getAsJsonPrimitive().isString() ? jsonObject.get( "version" ).getAsString() : "Unknown"; |
124
|
|
|
|
125
|
|
|
boolean chunkCachingEnabled = false; |
126
|
|
|
int chunkCachingVersion = 0; |
127
|
|
|
|
128
|
|
|
if ( jsonObject.has( "ccp" ) && jsonObject.get( "ccp" ).isJsonObject() ) { |
129
|
|
|
JsonObject chunkCachingObject = jsonObject.get( "ccp" ).getAsJsonObject(); |
130
|
|
|
|
131
|
|
|
if ( chunkCachingObject.has( "enabled" ) ) |
132
|
|
|
chunkCachingEnabled = chunkCachingObject.get( "enabled" ).getAsBoolean(); |
133
|
|
|
|
134
|
|
|
if ( chunkCachingObject.has( "version" ) ) |
135
|
|
|
chunkCachingVersion = chunkCachingObject.get( "version" ).getAsInt(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
Bukkit.getPluginManager().callEvent( new LabyModPlayerJoinEvent( player, version, |
139
|
|
|
chunkCachingEnabled, chunkCachingVersion, Addon.getAddons( jsonObject ) ) ); |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Calling the MessageReceiveEvent |
144
|
|
|
Bukkit.getPluginManager().callEvent( new MessageReceiveEvent( player, messageKey, jsonMessage ) ); |
145
|
|
|
} |
146
|
|
|
} ); |
147
|
|
|
} catch ( RuntimeException ignored ) { |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
@Override |
154
|
|
|
public void onDisable() { |
155
|
|
|
// Unregistering the plugin-message listeners |
156
|
|
|
getServer().getMessenger().unregisterIncomingPluginChannel( this, "LABYMOD" ); |
157
|
|
|
getServer().getMessenger().unregisterIncomingPluginChannel( this, "LMC" ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sends the modified permissions to the given player |
162
|
|
|
* |
163
|
|
|
* @param player the player the permissions should be sent to |
164
|
|
|
*/ |
165
|
|
|
public void sendPermissions( Player player ) { |
166
|
|
|
Map<Permission, Boolean> modifiedPermissions = new HashMap<>( labyModConfig.getPermissions() ); |
|
|
|
|
167
|
|
|
|
168
|
|
|
// Calling the Bukkit event |
169
|
|
|
PermissionsSendEvent sendEvent = new PermissionsSendEvent( player, modifiedPermissions, false ); |
170
|
|
|
Bukkit.getPluginManager().callEvent( sendEvent ); |
171
|
|
|
|
172
|
|
|
// Sending the packet |
173
|
|
|
if ( !sendEvent.isCancelled() && sendEvent.getPermissions().size() > 0 ) |
174
|
|
|
packetUtils.sendPacket( player, packetUtils.getPluginMessagePacket( "LMC", api.getBytesToSend( modifiedPermissions ) ) ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Sends a JSON server-message to the player |
179
|
|
|
* |
180
|
|
|
* @param player the player the message should be sent to |
181
|
|
|
* @param messageKey the message's key |
182
|
|
|
* @param messageContents the message's contents |
183
|
|
|
*/ |
184
|
|
|
public void sendServerMessage( Player player, String messageKey, JsonElement messageContents ) { |
185
|
|
|
messageContents = cloneJson( messageContents ); |
186
|
|
|
|
187
|
|
|
// Calling the Bukkit event |
188
|
|
|
MessageSendEvent sendEvent = new MessageSendEvent( player, messageKey, messageContents, false ); |
189
|
|
|
Bukkit.getPluginManager().callEvent( sendEvent ); |
190
|
|
|
|
191
|
|
|
// Sending the packet |
192
|
|
|
if ( !sendEvent.isCancelled() ) |
193
|
|
|
packetUtils.sendPacket( player, packetUtils.getPluginMessagePacket( "LMC", api.getBytesToSend( messageKey, messageContents.toString() ) ) ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Sends the Discord Rich Presence message to the player |
198
|
|
|
* |
199
|
|
|
* @param player the player the rich presence should be sent to |
200
|
|
|
* @param richPresence the presence object |
201
|
|
|
*/ |
202
|
|
|
public void sendRichPresence ( @NonNull Player player, @NonNull RichPresence richPresence ) { |
203
|
|
|
sendServerMessage(player, "discord_rpc", richPresence.toJson()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Clones a JsonElement |
208
|
|
|
* |
209
|
|
|
* @param cloneElement the element that should be cloned |
210
|
|
|
* @return the cloned element |
211
|
|
|
*/ |
212
|
|
|
public JsonElement cloneJson( JsonElement cloneElement ) { |
213
|
|
|
try { |
214
|
|
|
return JSON_PARSER.parse( cloneElement.toString() ); |
215
|
|
|
} catch ( JsonParseException ex ) { |
216
|
|
|
ex.printStackTrace(); |
|
|
|
|
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
If you really need to set this static field, consider writing a thread-safe setter and atomic getter.