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