Search in sources :

Example 1 with StandardByteReader

use of com.floweytf.utils.stdstreams.StandardByteReader in project scripted-quests by TeamMonumenta.

the class ClientChatProtocol method onPluginMessageReceived.

@Override
public void onPluginMessageReceived(String s, Player player, byte[] bytes) {
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    IStandardByteReader out = new StandardByteReader(stream);
    try {
        String mode = out.readString();
        JsonObject request = GSON.fromJson(mode, JsonElement.class).getAsJsonObject();
        // implements `C->S 'version'` and `C->S 'mode'`
        switch(request.get("type").getAsString()) {
            case "version":
                // implements `S->C 'version'`
                sendJson(player, JsonObjectBuilder.get().add("type", "version").add("version", VERSION).build());
                break;
            case "mode":
                boolean newMode = request.get("send").getAsJsonPrimitive().getAsBoolean();
                if (newMode) {
                    mShouldSendMessage.add(player.getUniqueId());
                } else {
                    mShouldSendMessage.remove(player.getUniqueId());
                }
                break;
            default:
                // Silently ignore other type requests
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : IStandardByteReader(com.floweytf.utils.stdstreams.IStandardByteReader) StandardByteReader(com.floweytf.utils.stdstreams.StandardByteReader) IStandardByteReader(com.floweytf.utils.stdstreams.IStandardByteReader) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Aggregations

IStandardByteReader (com.floweytf.utils.stdstreams.IStandardByteReader)1 StandardByteReader (com.floweytf.utils.stdstreams.StandardByteReader)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1