-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMessageComponentGsonAdapter.java
More file actions
59 lines (53 loc) · 1.93 KB
/
MiniMessageComponentGsonAdapter.java
File metadata and controls
59 lines (53 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package net.theevilreaper.aves.file.gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.jetbrains.annotations.Contract;
import java.io.IOException;
/**
* This class is used to serialize and deserialize a {@link Component} object into mini message string representation.
* The {@link Component} object is represented as a JSON object with the key "minimessage".
* The JSON object is represented as follows:
* <pre>
* {
* "minimessage": "miniMessage serialized component"
* }
* </pre>
* A component can be created by calling the {@link MiniMessage#miniMessage()} method.
* Use {@link #create()} to create a default adapter.
*
* @since 1.0.0
* @version 1.0.0
* @see Component
* @see MiniMessage
* @see MiniMessage#miniMessage()
* @author TheMeinerLP
*/
public class MiniMessageComponentGsonAdapter extends TypeAdapter<Component> {
/**
* Creates a new instance of the {@link MiniMessageComponentGsonAdapter}.
* @return the new instance of the {@link MiniMessageComponentGsonAdapter}
*/
@Contract(value = " -> new", pure = true)
public static MiniMessageComponentGsonAdapter create() {
return new MiniMessageComponentGsonAdapter();
}
@Override
public void write(JsonWriter out, Component value) throws IOException {
out.beginObject();
out.name("minimessage").value(MiniMessage.miniMessage().serialize(value));
out.endObject();
}
@Override
public Component read(JsonReader in) throws IOException {
in.beginObject();
if (!in.nextName().equals("minimessage")) {
throw new IOException("Expected component");
}
var component = MiniMessage.miniMessage().deserialize(in.nextString());
in.endObject();
return component;
}
}