add gson serializedname support for toml

This commit is contained in:
Taah 2022-05-05 01:12:24 -07:00
parent f52c8462ae
commit 24e031acae
5 changed files with 102 additions and 49 deletions

View File

@ -29,6 +29,8 @@ public class ServerListener extends PlexListener
baseMotd = baseMotd.replace("%mcversion%", plugin.getServer().getVersion().getVersion().split(" ")[0]);
baseMotd = baseMotd.replace("%randomgradient%", "<gradient:" + RandomUtil.getRandomColor().toString() + ":" + RandomUtil.getRandomColor().toString() + ">");
ServerPing.Builder builder = event.getPing().asBuilder();
if (plugin.getConfig().as(ServerSettings.class).getServer().isColorizeMotd())
{
AtomicReference<Component> motd = new AtomicReference<>(Component.empty());
@ -37,10 +39,20 @@ public class ServerListener extends PlexListener
motd.set(motd.get().append(Component.text(word).color(RandomUtil.getRandomColor())));
motd.set(motd.get().append(Component.space()));
}
event.setPing(event.getPing().asBuilder().description(motd.get()).samplePlayers(plugin.getConfig().as(ServerSettings.class).getServer().getSample().stream().map(s -> new ServerPing.SamplePlayer(convertColorCodes(s), UUID.randomUUID())).toArray(ServerPing.SamplePlayer[]::new)).build());
builder.description(motd.get());
} else {
event.setPing(event.getPing().asBuilder().description(MiniMessage.miniMessage().deserialize(baseMotd)).samplePlayers(plugin.getConfig().as(ServerSettings.class).getServer().getSample().stream().map(s -> new ServerPing.SamplePlayer(convertColorCodes(s), UUID.randomUUID())).toArray(ServerPing.SamplePlayer[]::new)).build());
builder.description(MiniMessage.miniMessage().deserialize(baseMotd));
}
builder.samplePlayers(plugin.getConfig().as(ServerSettings.class).getServer().getSample().stream().map(s -> new ServerPing.SamplePlayer(convertColorCodes(s), UUID.randomUUID())).toArray(ServerPing.SamplePlayer[]::new));
builder.onlinePlayers(plugin.getServer().getPlayerCount() + plugin.getConfig().as(ServerSettings.class).getServer().getAddPlayerCount());
if (plugin.getConfig().as(ServerSettings.class).getServer().isPlusOneMaxPlayer())
{
builder.maximumPlayers(builder.getOnlinePlayers() + 1);
}
event.setPing(builder.build());
}
private String convertColorCodes(String code)

View File

@ -1,6 +1,7 @@
package dev.plex.settings;
import com.google.common.collect.Lists;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.Getter;
@ -18,5 +19,9 @@ public class ServerSettings
private boolean colorizeMotd = false;
private boolean debug = false;
private final List<String> sample = Lists.newArrayList("example", "example");
@SerializedName(value = "add-player-count")
private int addPlayerCount = 0;
@SerializedName(value = "plus-one-max-count")
private boolean plusOneMaxPlayer = true;
}
}

View File

@ -1,5 +1,7 @@
package dev.plex.toml;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
@ -9,29 +11,40 @@ class ObjectValueWriter implements ValueWriter
static final ValueWriter OBJECT_VALUE_WRITER = new ObjectValueWriter();
@Override
public boolean canWrite(Object value) {
public boolean canWrite(Object value)
{
return true;
}
@Override
public void write(Object value, WriterContext context) {
public void write(Object value, WriterContext context)
{
Map<String, Object> to = new LinkedHashMap<String, Object>();
Set<Field> fields = getFields(value.getClass());
for (Field field : fields) {
for (Field field : fields)
{
if (field.isAnnotationPresent(SerializedName.class))
{
to.put(field.getDeclaredAnnotation(SerializedName.class).value(), getFieldValue(field, value));
} else {
to.put(field.getName(), getFieldValue(field, value));
}
}
MapValueWriter.MAP_VALUE_WRITER.write(to, context);
}
@Override
public boolean isPrimitiveType() {
public boolean isPrimitiveType()
{
return false;
}
private static Set<Field> getFields(Class<?> cls) {
private static Set<Field> getFields(Class<?> cls)
{
Set<Field> fields = new LinkedHashSet<Field>(Arrays.asList(cls.getDeclaredFields()));
while (cls != Object.class) {
while (cls != Object.class)
{
fields.addAll(Arrays.asList(cls.getDeclaredFields()));
cls = cls.getSuperclass();
}
@ -40,28 +53,36 @@ class ObjectValueWriter implements ValueWriter
return fields;
}
private static void removeConstantsAndSyntheticFields(Set<Field> fields) {
private static void removeConstantsAndSyntheticFields(Set<Field> fields)
{
Iterator<Field> iterator = fields.iterator();
while (iterator.hasNext()) {
while (iterator.hasNext())
{
Field field = iterator.next();
if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic() || Modifier.isTransient(field.getModifiers())) {
if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic() || Modifier.isTransient(field.getModifiers()))
{
iterator.remove();
}
}
}
private static Object getFieldValue(Field field, Object o) {
private static Object getFieldValue(Field field, Object o)
{
boolean isAccessible = field.isAccessible();
field.setAccessible(true);
Object value = null;
try {
try
{
value = field.get(o);
} catch (IllegalAccessException ignored) {
} catch (IllegalAccessException ignored)
{
}
field.setAccessible(isAccessible);
return value;
}
private ObjectValueWriter() {}
private ObjectValueWriter()
{
}
}

View File

@ -3,9 +3,12 @@ package dev.plex.toml;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import lombok.Getter;
import org.json.JSONObject;
import java.io.*;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* <p>Provides access to the keys and tables in a TOML data source.</p>

View File

@ -7,6 +7,7 @@
[server]
name = "Plexus"
# Placeholders
# %mcversion% - The Velocity Version (i.e. 3.1.2-SNAPSHOT)
# %servername% - The name provided above
@ -14,8 +15,19 @@
# Supports MiniMessage strings, no legacy & and §
motd = "%randomgradient%%servername% - %mcversion%"
colorizeMotd = false
# Enables debug messages
debug = false
# Due to game code only supporting legacy color codes for
# player samples and not components, you may only use § or & here
# for colors.
sample = ["example", "example"]
# Adds this amount to the current player count
add-player-count = 0
# The max player count will always display as +1 more than the player count
plus-one-max-count = true