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,67 +1,88 @@
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.*;
class ObjectValueWriter implements ValueWriter 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
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));
} }
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 MapValueWriter.MAP_VALUE_WRITER.write(to, context);
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();
} }
removeConstantsAndSyntheticFields(fields);
return fields; @Override
} public boolean isPrimitiveType()
{
private static void removeConstantsAndSyntheticFields(Set<Field> fields) { return false;
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 static Object getFieldValue(Field field, Object o) { private static Set<Field> getFields(Class<?> cls)
boolean isAccessible = field.isAccessible(); {
field.setAccessible(true); Set<Field> fields = new LinkedHashSet<Field>(Arrays.asList(cls.getDeclaredFields()));
Object value = null; while (cls != Object.class)
try { {
value = field.get(o); fields.addAll(Arrays.asList(cls.getDeclaredFields()));
} catch (IllegalAccessException ignored) { 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.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