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("%mcversion%", plugin.getServer().getVersion().getVersion().split(" ")[0]);
baseMotd = baseMotd.replace("%randomgradient%", "<gradient:" + RandomUtil.getRandomColor().toString() + ":" + RandomUtil.getRandomColor().toString() + ">"); 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()) if (plugin.getConfig().as(ServerSettings.class).getServer().isColorizeMotd())
{ {
AtomicReference<Component> motd = new AtomicReference<>(Component.empty()); 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.text(word).color(RandomUtil.getRandomColor())));
motd.set(motd.get().append(Component.space())); 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 { } 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) private String convertColorCodes(String code)

View File

@ -1,6 +1,7 @@
package dev.plex.settings; package dev.plex.settings;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gson.annotations.SerializedName;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
@ -18,5 +19,9 @@ public class ServerSettings
private boolean colorizeMotd = false; private boolean colorizeMotd = false;
private boolean debug = false; private boolean debug = false;
private final List<String> sample = Lists.newArrayList("example", "example"); 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; package dev.plex.toml;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.*; import java.util.*;
@ -9,29 +11,40 @@ class ObjectValueWriter implements ValueWriter
static final ValueWriter OBJECT_VALUE_WRITER = new ObjectValueWriter(); static final ValueWriter OBJECT_VALUE_WRITER = new ObjectValueWriter();
@Override @Override
public boolean canWrite(Object value) { public boolean canWrite(Object value)
{
return true; return true;
} }
@Override @Override
public void write(Object value, WriterContext context) { public void write(Object value, WriterContext context)
{
Map<String, Object> to = new LinkedHashMap<String, Object>(); Map<String, Object> to = new LinkedHashMap<String, Object>();
Set<Field> fields = getFields(value.getClass()); 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)); to.put(field.getName(), getFieldValue(field, value));
} }
}
MapValueWriter.MAP_VALUE_WRITER.write(to, context); MapValueWriter.MAP_VALUE_WRITER.write(to, context);
} }
@Override @Override
public boolean isPrimitiveType() { public boolean isPrimitiveType()
{
return false; 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())); Set<Field> fields = new LinkedHashSet<Field>(Arrays.asList(cls.getDeclaredFields()));
while (cls != Object.class) { while (cls != Object.class)
{
fields.addAll(Arrays.asList(cls.getDeclaredFields())); fields.addAll(Arrays.asList(cls.getDeclaredFields()));
cls = cls.getSuperclass(); cls = cls.getSuperclass();
} }
@ -40,28 +53,36 @@ class ObjectValueWriter implements ValueWriter
return fields; return fields;
} }
private static void removeConstantsAndSyntheticFields(Set<Field> fields) { private static void removeConstantsAndSyntheticFields(Set<Field> fields)
{
Iterator<Field> iterator = fields.iterator(); Iterator<Field> iterator = fields.iterator();
while (iterator.hasNext()) { while (iterator.hasNext())
{
Field field = iterator.next(); 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(); iterator.remove();
} }
} }
} }
private static Object getFieldValue(Field field, Object o) { private static Object getFieldValue(Field field, Object o)
{
boolean isAccessible = field.isAccessible(); boolean isAccessible = field.isAccessible();
field.setAccessible(true); field.setAccessible(true);
Object value = null; Object value = null;
try { try
{
value = field.get(o); value = field.get(o);
} catch (IllegalAccessException ignored) { } catch (IllegalAccessException ignored)
{
} }
field.setAccessible(isAccessible); field.setAccessible(isAccessible);
return value; 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.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import lombok.Getter; import lombok.Getter;
import org.json.JSONObject;
import java.io.*; import java.io.*;
import java.lang.reflect.Field;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* <p>Provides access to the keys and tables in a TOML data source.</p> * <p>Provides access to the keys and tables in a TOML data source.</p>

View File

@ -7,6 +7,7 @@
[server] [server]
name = "Plexus" name = "Plexus"
# Placeholders # Placeholders
# %mcversion% - The Velocity Version (i.e. 3.1.2-SNAPSHOT) # %mcversion% - The Velocity Version (i.e. 3.1.2-SNAPSHOT)
# %servername% - The name provided above # %servername% - The name provided above
@ -14,8 +15,19 @@
# Supports MiniMessage strings, no legacy & and § # Supports MiniMessage strings, no legacy & and §
motd = "%randomgradient%%servername% - %mcversion%" motd = "%randomgradient%%servername% - %mcversion%"
colorizeMotd = false colorizeMotd = false
# Enables debug messages
debug = false debug = false
# Due to game code only supporting legacy color codes for # Due to game code only supporting legacy color codes for
# player samples and not components, you may only use § or & here # player samples and not components, you may only use § or & here
# for colors. # for colors.
sample = ["example", "example"] 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