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