-
Notifications
You must be signed in to change notification settings - Fork 95
/
StringLoadable.java
41 lines (35 loc) · 1.19 KB
/
StringLoadable.java
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
package slimeknights.mantle.data.loadable.primitive;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.GsonHelper;
import slimeknights.mantle.data.loadable.Loadable;
/**
* Loadable for an integer
* @param maxLength Maximum length of string allowed
*/
public record StringLoadable(int maxLength) implements Loadable<String> {
/** Loadable for the default max string length */
public static final StringLoadable DEFAULT = new StringLoadable(Short.MAX_VALUE);
@Override
public String convert(JsonElement element, String key) {
String value = GsonHelper.convertToString(element, key);
if (value.length() > maxLength) {
throw new JsonSyntaxException(key + " may not be longer than " + maxLength);
}
return value;
}
@Override
public JsonElement serialize(String object) {
return new JsonPrimitive(object);
}
@Override
public String decode(FriendlyByteBuf buffer) {
return buffer.readUtf(maxLength);
}
@Override
public void encode(FriendlyByteBuf buffer, String object) {
buffer.writeUtf(object, maxLength);
}
}