actually indent array types in toml

add a default config file instead of generation for commenting purposes
add a random gradient placeholder and a converter for legacy colorcodes
This commit is contained in:
Taah 2022-05-05 00:49:03 -07:00
parent 4e9984f251
commit f52c8462ae
38 changed files with 245 additions and 211 deletions

View File

@ -1,37 +0,0 @@
package com.moandjiezana.toml;
import java.util.concurrent.atomic.AtomicInteger;
import static com.moandjiezana.toml.ArrayValueReader.ARRAY_VALUE_READER;
import static com.moandjiezana.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER;
import static com.moandjiezana.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
import static com.moandjiezana.toml.InlineTableValueReader.INLINE_TABLE_VALUE_READER;
import static com.moandjiezana.toml.LiteralStringValueReader.LITERAL_STRING_VALUE_READER;
import static com.moandjiezana.toml.MultilineLiteralStringValueReader.MULTILINE_LITERAL_STRING_VALUE_READER;
import static com.moandjiezana.toml.MultilineStringValueReader.MULTILINE_STRING_VALUE_READER;
import static com.moandjiezana.toml.NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER;
import static com.moandjiezana.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER;
class ValueReaders {
static final ValueReaders VALUE_READERS = new ValueReaders();
Object convert(String value, AtomicInteger index, Context context) {
String substring = value.substring(index.get());
for (ValueReader valueParser : READERS) {
if (valueParser.canRead(substring)) {
return valueParser.read(value, index, context);
}
}
Results.Errors errors = new Results.Errors();
errors.invalidValue(context.identifier.getName(), substring, context.line.get());
return errors;
}
private ValueReaders() {}
private static final ValueReader[] READERS = {
MULTILINE_STRING_VALUE_READER, MULTILINE_LITERAL_STRING_VALUE_READER, LITERAL_STRING_VALUE_READER, STRING_VALUE_READER_WRITER, DATE_VALUE_READER_WRITER, NUMBER_VALUE_READER_WRITER, BOOLEAN_VALUE_READER_WRITER, ARRAY_VALUE_READER, INLINE_TABLE_VALUE_READER
};
}

View File

@ -1,31 +0,0 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER;
import static com.moandjiezana.toml.NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER;
class ValueWriters {
static final ValueWriters WRITERS = new ValueWriters();
ValueWriter findWriterFor(Object value) {
for (ValueWriter valueWriter : VALUE_WRITERS) {
if (valueWriter.canWrite(value)) {
return valueWriter;
}
}
return ObjectValueWriter.OBJECT_VALUE_WRITER;
}
private ValueWriters() {}
private static DateValueReaderWriter getPlatformSpecificDateConverter() {
String specificationVersion = Runtime.class.getPackage().getSpecificationVersion();
return specificationVersion != null && specificationVersion.startsWith("1.6") ? DateValueReaderWriter.DATE_PARSER_JDK_6 : DateValueReaderWriter.DATE_VALUE_READER_WRITER;
}
private static final ValueWriter[] VALUE_WRITERS = {
StringValueReaderWriter.STRING_VALUE_READER_WRITER, NUMBER_VALUE_READER_WRITER, BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER, getPlatformSpecificDateConverter(),
MAP_VALUE_WRITER, PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER, TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER
};
}

View File

@ -14,9 +14,17 @@ import lombok.Getter;
import java.io.File; import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.logging.Logger; import java.util.logging.Logger;
/**
* Credits for TOML library go to https://github.com/mwanji/toml4j
* I was unable to add it back to the package without it glitching, so
* I kept it in a separate package.
*
* Modifications: Properly indent arrays in TOML as well as only append
* missing object fields into the file
*/
@Plugin( @Plugin(
name = "Plex", name = "Plex",
id = "plex", id = "plex",
@ -62,7 +70,7 @@ public class Plex
{ {
PlexLog.log("Loaded configuration 'config.toml'"); PlexLog.log("Loaded configuration 'config.toml'");
}); });
this.config.create(false); this.config.create(true);
this.config.write(new ServerSettings()); this.config.write(new ServerSettings());
new ListenerHandler(); new ListenerHandler();
} }

View File

@ -1,8 +1,8 @@
package dev.plex.config; package dev.plex.config;
import dev.plex.Plex; import dev.plex.Plex;
import com.moandjiezana.toml.Toml; import dev.plex.toml.Toml;
import com.moandjiezana.toml.TomlWriter; import dev.plex.toml.TomlWriter;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -30,6 +30,8 @@ public class TomlConfig
} }
public void create(boolean loadFromFile) public void create(boolean loadFromFile)
{
if (!this.file.exists())
{ {
if (loadFromFile) if (loadFromFile)
{ {
@ -46,8 +48,7 @@ public class TomlConfig
e.printStackTrace(); e.printStackTrace();
} }
return; return;
} else if (!this.file.exists()) }
{
try try
{ {
this.file.createNewFile(); this.file.createNewFile();
@ -81,7 +82,9 @@ public class TomlConfig
public <T> void write(T object) public <T> void write(T object)
{ {
TomlWriter writer = new TomlWriter(); TomlWriter writer = new TomlWriter.Builder()
.indentValuesBy(2)
.build();
try try
{ {
writer.write(object, this.file); writer.write(object, this.file);

View File

@ -3,6 +3,7 @@ package dev.plex.listener.impl;
import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyPingEvent; import com.velocitypowered.api.event.proxy.ProxyPingEvent;
import com.velocitypowered.api.proxy.server.ServerPing;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.listener.PlexListener; import dev.plex.listener.PlexListener;
import dev.plex.settings.ServerSettings; import dev.plex.settings.ServerSettings;
@ -11,7 +12,11 @@ import dev.plex.util.RandomUtil;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ServerListener extends PlexListener public class ServerListener extends PlexListener
{ {
@ -21,9 +26,8 @@ public class ServerListener extends PlexListener
String baseMotd = plugin.getConfig().as(ServerSettings.class).getServer().getMotd(); String baseMotd = plugin.getConfig().as(ServerSettings.class).getServer().getMotd();
baseMotd = baseMotd.replace("\\n", "\n"); baseMotd = baseMotd.replace("\\n", "\n");
baseMotd = baseMotd.replace("%servername%", plugin.getConfig().as(ServerSettings.class).getServer().getName()); baseMotd = baseMotd.replace("%servername%", plugin.getConfig().as(ServerSettings.class).getServer().getName());
baseMotd = baseMotd.replace("%mcversion%", plugin.getServer().getVersion().getVersion()); baseMotd = baseMotd.replace("%mcversion%", plugin.getServer().getVersion().getVersion().split(" ")[0]);
baseMotd = baseMotd.replace("%randomgradient%", "<gradient:" + RandomUtil.getRandomColor().toString() + ":" + RandomUtil.getRandomColor().toString() + ">");
PlexLog.log(baseMotd);
if (plugin.getConfig().as(ServerSettings.class).getServer().isColorizeMotd()) if (plugin.getConfig().as(ServerSettings.class).getServer().isColorizeMotd())
{ {
@ -33,10 +37,16 @@ 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()).build()); 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());
} else { } else {
event.setPing(event.getPing().asBuilder().description(MiniMessage.miniMessage().deserialize(baseMotd)).build()); 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());
} }
} }
private String convertColorCodes(String code)
{
Matcher matcher = Pattern.compile("[&][0-9a-fk-or]{1}").matcher(code);
return matcher.replaceAll(matchResult -> "§" + matcher.group().substring(1));
}
} }

View File

@ -13,8 +13,8 @@ public class ServerSettings
@Data @Data
public static class Server { public static class Server {
private String name = "Plexus"; private String name = "Server";
private String motd = "Test MOTD"; private String motd = "%randomgradient%%servername% - %mcversion%";
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");

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -13,14 +13,14 @@ public class ArrayValueReader implements ValueReader {
} }
@Override @Override
public Object read(String s, AtomicInteger index, Context context) { public Object read(String s, AtomicInteger index, dev.plex.toml.Context context) {
AtomicInteger line = context.line; AtomicInteger line = context.line;
int startLine = line.get(); int startLine = line.get();
int startIndex = index.get(); int startIndex = index.get();
List<Object> arrayItems = new ArrayList<Object>(); List<Object> arrayItems = new ArrayList<Object>();
boolean terminated = false; boolean terminated = false;
boolean inComment = false; boolean inComment = false;
Results.Errors errors = new Results.Errors(); dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
for (int i = index.incrementAndGet(); i < s.length(); i = index.incrementAndGet()) { for (int i = index.incrementAndGet(); i < s.length(); i = index.incrementAndGet()) {
@ -35,8 +35,8 @@ public class ArrayValueReader implements ValueReader {
continue; continue;
} else if (c == '[') { } else if (c == '[') {
Object converted = read(s, index, context); Object converted = read(s, index, context);
if (converted instanceof Results.Errors) { if (converted instanceof dev.plex.toml.Results.Errors) {
errors.add((Results.Errors) converted); errors.add((dev.plex.toml.Results.Errors) converted);
} else if (!isHomogenousArray(converted, arrayItems)) { } else if (!isHomogenousArray(converted, arrayItems)) {
errors.heterogenous(context.identifier.getName(), line.get()); errors.heterogenous(context.identifier.getName(), line.get());
} else { } else {
@ -48,8 +48,8 @@ public class ArrayValueReader implements ValueReader {
break; break;
} else { } else {
Object converted = ValueReaders.VALUE_READERS.convert(s, index, context); Object converted = ValueReaders.VALUE_READERS.convert(s, index, context);
if (converted instanceof Results.Errors) { if (converted instanceof dev.plex.toml.Results.Errors) {
errors.add((Results.Errors) converted); errors.add((dev.plex.toml.Results.Errors) converted);
} else if (!isHomogenousArray(converted, arrayItems)) { } else if (!isHomogenousArray(converted, arrayItems)) {
errors.heterogenous(context.identifier.getName(), line.get()); errors.heterogenous(context.identifier.getName(), line.get());
} else { } else {

View File

@ -1,13 +1,14 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import static com.moandjiezana.toml.ValueWriters.WRITERS; import static dev.plex.toml.ValueWriters.WRITERS;
public abstract class ArrayValueWriter implements ValueWriter { public abstract class ArrayValueWriter implements dev.plex.toml.ValueWriter
{
static protected boolean isArrayish(Object value) { static protected boolean isArrayish(Object value) {
return value instanceof Collection || value.getClass().isArray(); return value instanceof Collection || value.getClass().isArray();
} }
@ -20,7 +21,7 @@ public abstract class ArrayValueWriter implements ValueWriter {
static boolean isArrayOfPrimitive(Object array) { static boolean isArrayOfPrimitive(Object array) {
Object first = peek(array); Object first = peek(array);
if (first != null) { if (first != null) {
ValueWriter valueWriter = WRITERS.findWriterFor(first); dev.plex.toml.ValueWriter valueWriter = WRITERS.findWriterFor(first);
return valueWriter.isPrimitiveType() || isArrayish(first); return valueWriter.isPrimitiveType() || isArrayish(first);
} }

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;

View File

@ -1,19 +1,19 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class Context { public class Context {
final Identifier identifier; final dev.plex.toml.Identifier identifier;
final AtomicInteger line; final AtomicInteger line;
final Results.Errors errors; final Results.Errors errors;
public Context(Identifier identifier, AtomicInteger line, Results.Errors errors) { public Context(dev.plex.toml.Identifier identifier, AtomicInteger line, Results.Errors errors) {
this.identifier = identifier; this.identifier = identifier;
this.line = line; this.line = line;
this.errors = errors; this.errors = errors;
} }
public Context with(Identifier identifier) { public Context with(dev.plex.toml.Identifier identifier) {
return new Context(identifier, line, errors); return new Context(identifier, line, errors);
} }
} }

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.TimeZone; import java.util.TimeZone;

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
public class Identifier public class Identifier
{ {

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
/** /**
* Controls how a {@link TomlWriter} indents tables and key/value pairs. * Controls how a {@link TomlWriter} indents tables and key/value pairs.

View File

@ -1,9 +1,10 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
class InlineTableValueReader implements ValueReader { class InlineTableValueReader implements dev.plex.toml.ValueReader
{
static final InlineTableValueReader INLINE_TABLE_VALUE_READER = new InlineTableValueReader(); static final InlineTableValueReader INLINE_TABLE_VALUE_READER = new InlineTableValueReader();
@ -13,7 +14,7 @@ class InlineTableValueReader implements ValueReader {
} }
@Override @Override
public Object read(String s, AtomicInteger sharedIndex, Context context) { public Object read(String s, AtomicInteger sharedIndex, dev.plex.toml.Context context) {
AtomicInteger line = context.line; AtomicInteger line = context.line;
int startLine = line.get(); int startLine = line.get();
int startIndex = sharedIndex.get(); int startIndex = sharedIndex.get();
@ -22,16 +23,16 @@ class InlineTableValueReader implements ValueReader {
boolean terminated = false; boolean terminated = false;
StringBuilder currentKey = new StringBuilder(); StringBuilder currentKey = new StringBuilder();
HashMap<String, Object> results = new HashMap<String, Object>(); HashMap<String, Object> results = new HashMap<String, Object>();
Results.Errors errors = new Results.Errors(); dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet()) { for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet()) {
char c = s.charAt(i); char c = s.charAt(i);
if (inValue && !Character.isWhitespace(c)) { if (inValue && !Character.isWhitespace(c)) {
Object converted = ValueReaders.VALUE_READERS.convert(s, sharedIndex, context.with(Identifier.from(currentKey.toString(), context))); Object converted = dev.plex.toml.ValueReaders.VALUE_READERS.convert(s, sharedIndex, context.with(dev.plex.toml.Identifier.from(currentKey.toString(), context)));
if (converted instanceof Results.Errors) { if (converted instanceof dev.plex.toml.Results.Errors) {
errors.add((Results.Errors) converted); errors.add((dev.plex.toml.Results.Errors) converted);
return errors; return errors;
} }

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -12,7 +12,7 @@ public class LiteralStringValueReader implements ValueReader
} }
@Override @Override
public Object read(String s, AtomicInteger index, Context context) public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
{ {
int startLine = context.line.get(); int startLine = context.line.get();
boolean terminated = false; boolean terminated = false;

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.io.File; import java.io.File;
@ -6,9 +6,9 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
class MapValueWriter implements ValueWriter class MapValueWriter implements dev.plex.toml.ValueWriter
{ {
static final ValueWriter MAP_VALUE_WRITER = new MapValueWriter(); static final dev.plex.toml.ValueWriter MAP_VALUE_WRITER = new MapValueWriter();
private static final Pattern REQUIRED_QUOTING_PATTERN = Pattern.compile("^.*[^A-Za-z\\d_-].*$"); private static final Pattern REQUIRED_QUOTING_PATTERN = Pattern.compile("^.*[^A-Za-z\\d_-].*$");
@ -75,15 +75,16 @@ class MapValueWriter implements ValueWriter
} }
} }
ValueWriter valueWriter = ValueWriters.WRITERS.findWriterFor(fromValue); dev.plex.toml.ValueWriter valueWriter = dev.plex.toml.ValueWriters.WRITERS.findWriterFor(fromValue);
if (valueWriter.isPrimitiveType()) if (valueWriter.isPrimitiveType())
{ {
context.indent(); context.indent();
context.write(quoteKey(key)).write(" = "); context.write(quoteKey(key)).write(" = ");
valueWriter.write(fromValue, context); valueWriter.write(fromValue, context);
context.write('\n'); context.write('\n');
} else if (valueWriter == PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER) } else if (valueWriter == dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER)
{ {
context.indent();
context.setArrayKey(key.toString()); context.setArrayKey(key.toString());
context.write(quoteKey(key)).write(" = "); context.write(quoteKey(key)).write(" = ");
valueWriter.write(fromValue, context); valueWriter.write(fromValue, context);
@ -109,8 +110,8 @@ class MapValueWriter implements ValueWriter
} }
} }
ValueWriter valueWriter = ValueWriters.WRITERS.findWriterFor(fromValue); dev.plex.toml.ValueWriter valueWriter = dev.plex.toml.ValueWriters.WRITERS.findWriterFor(fromValue);
if (valueWriter == this || valueWriter == ObjectValueWriter.OBJECT_VALUE_WRITER || valueWriter == TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER) if (valueWriter == this || valueWriter == dev.plex.toml.ObjectValueWriter.OBJECT_VALUE_WRITER || valueWriter == dev.plex.toml.TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER)
{ {
WriterContext context1 = context.pushTable(quoteKey(key)); WriterContext context1 = context.pushTable(quoteKey(key));
context1.parentName = key.toString(); context1.parentName = key.toString();
@ -149,8 +150,8 @@ class MapValueWriter implements ValueWriter
continue; continue;
} }
ValueWriter valueWriter = ValueWriters.WRITERS.findWriterFor(fromValue); dev.plex.toml.ValueWriter valueWriter = dev.plex.toml.ValueWriters.WRITERS.findWriterFor(fromValue);
if (valueWriter.isPrimitiveType() || valueWriter == PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER) if (valueWriter.isPrimitiveType() || valueWriter == dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER)
{ {
return true; return true;
} }

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -13,7 +13,7 @@ class MultilineStringValueReader implements ValueReader
} }
@Override @Override
public Object read(String s, AtomicInteger index, Context context) { public Object read(String s, AtomicInteger index, dev.plex.toml.Context context) {
AtomicInteger line = context.line; AtomicInteger line = context.line;
int startLine = line.get(); int startLine = line.get();
int originalStartIndex = index.get(); int originalStartIndex = index.get();
@ -38,15 +38,15 @@ class MultilineStringValueReader implements ValueReader
} }
if (endIndex == -1) { if (endIndex == -1) {
Results.Errors errors = new Results.Errors(); dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine); errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
return errors; return errors;
} }
s = s.substring(startIndex, endIndex); s = s.substring(startIndex, endIndex);
s = s.replaceAll("\\\\\\s+", ""); s = s.replaceAll("\\\\\\s+", "");
s = StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(s); s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(s);
s = StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceSpecialCharacters(s); s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceSpecialCharacters(s);
return s; return s;
} }

View File

@ -1,8 +1,9 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
class NumberValueReaderWriter implements ValueReader, ValueWriter { class NumberValueReaderWriter implements dev.plex.toml.ValueReader, dev.plex.toml.ValueWriter
{
static final NumberValueReaderWriter NUMBER_VALUE_READER_WRITER = new NumberValueReaderWriter(); static final NumberValueReaderWriter NUMBER_VALUE_READER_WRITER = new NumberValueReaderWriter();
@Override @Override
@ -13,7 +14,7 @@ class NumberValueReaderWriter implements ValueReader, ValueWriter {
} }
@Override @Override
public Object read(String s, AtomicInteger index, Context context) { public Object read(String s, AtomicInteger index, dev.plex.toml.Context context) {
boolean signable = true; boolean signable = true;
boolean dottable = false; boolean dottable = false;
boolean exponentable = false; boolean exponentable = false;
@ -77,7 +78,7 @@ class NumberValueReaderWriter implements ValueReader, ValueWriter {
return Double.parseDouble(exponentString[0]) * Math.pow(10, Double.parseDouble(exponentString[1])); return Double.parseDouble(exponentString[0]) * Math.pow(10, Double.parseDouble(exponentString[1]));
} else { } else {
Results.Errors errors = new Results.Errors(); dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
errors.invalidValue(context.identifier.getName(), sb.toString(), context.line.get()); errors.invalidValue(context.identifier.getName(), sb.toString(), context.line.get());
return errors; return errors;
} }
@ -89,7 +90,7 @@ class NumberValueReaderWriter implements ValueReader, ValueWriter {
} }
@Override @Override
public void write(Object value, WriterContext context) { public void write(Object value, dev.plex.toml.WriterContext context) {
context.write(value.toString()); context.write(value.toString());
} }

View File

@ -1,11 +1,9 @@
package com.moandjiezana.toml; package dev.plex.toml;
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.*;
import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER;
class ObjectValueWriter implements ValueWriter class ObjectValueWriter implements ValueWriter
{ {
static final ValueWriter OBJECT_VALUE_WRITER = new ObjectValueWriter(); static final ValueWriter OBJECT_VALUE_WRITER = new ObjectValueWriter();
@ -23,7 +21,7 @@ class ObjectValueWriter implements ValueWriter
to.put(field.getName(), getFieldValue(field, value)); to.put(field.getName(), getFieldValue(field, value));
} }
MAP_VALUE_WRITER.write(to, context); MapValueWriter.MAP_VALUE_WRITER.write(to, context);
} }
@Override @Override

View File

@ -1,9 +1,7 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.Collection; import java.util.Collection;
import static com.moandjiezana.toml.ValueWriters.WRITERS;
class PrimitiveArrayValueWriter extends ArrayValueWriter class PrimitiveArrayValueWriter extends ArrayValueWriter
{ {
static final ValueWriter PRIMITIVE_ARRAY_VALUE_WRITER = new PrimitiveArrayValueWriter(); static final ValueWriter PRIMITIVE_ARRAY_VALUE_WRITER = new PrimitiveArrayValueWriter();
@ -25,10 +23,10 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter
for (Object value : values) { for (Object value : values) {
if (first) { if (first) {
firstWriter = WRITERS.findWriterFor(value); firstWriter = ValueWriters.WRITERS.findWriterFor(value);
first = false; first = false;
} else { } else {
ValueWriter writer = WRITERS.findWriterFor(value); ValueWriter writer = ValueWriters.WRITERS.findWriterFor(value);
if (writer != firstWriter) { if (writer != firstWriter) {
throw new IllegalStateException( throw new IllegalStateException(
context.getContextPath() + context.getContextPath() +
@ -39,7 +37,7 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter
context.write(", "); context.write(", ");
} }
WRITERS.findWriterFor(value).write(value, context); ValueWriters.WRITERS.findWriterFor(value).write(value, context);
} }
context.writeArrayDelimiterPadding(); context.writeArrayDelimiterPadding();

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -55,7 +55,7 @@ class Results {
.append(key); .append(key);
} }
void invalidTextAfterIdentifier(Identifier identifier, char text, int line) { void invalidTextAfterIdentifier(dev.plex.toml.Identifier identifier, char text, int line) {
sb.append("Invalid text after key ") sb.append("Invalid text after key ")
.append(identifier.getName()) .append(identifier.getName())
.append(" on line ") .append(" on line ")
@ -138,9 +138,9 @@ class Results {
if (path == null) { if (path == null) {
startTable(key, line); startTable(key, line);
} else if (path.isEmpty()) { } else if (path.isEmpty()) {
startTables(Identifier.from(key, null), line); startTables(dev.plex.toml.Identifier.from(key, null), line);
} else { } else {
startTables(Identifier.from(path, null), line); startTables(dev.plex.toml.Identifier.from(path, null), line);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> valueMap = (Map<String, Object>) value; Map<String, Object> valueMap = (Map<String, Object>) value;
@ -159,13 +159,13 @@ class Results {
} }
} }
void startTableArray(Identifier identifier, AtomicInteger line) { void startTableArray(dev.plex.toml.Identifier identifier, AtomicInteger line) {
String tableName = identifier.getBareName(); String tableName = identifier.getBareName();
while (stack.size() > 1) { while (stack.size() > 1) {
stack.pop(); stack.pop();
} }
Keys.Key[] tableParts = Keys.split(tableName); dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
for (int i = 0; i < tableParts.length; i++) { for (int i = 0; i < tableParts.length; i++) {
String tablePart = tableParts[i].name; String tablePart = tableParts[i].name;
Container currentContainer = stack.peek(); Container currentContainer = stack.peek();
@ -198,14 +198,14 @@ class Results {
} }
} }
void startTables(Identifier id, AtomicInteger line) { void startTables(dev.plex.toml.Identifier id, AtomicInteger line) {
String tableName = id.getBareName(); String tableName = id.getBareName();
while (stack.size() > 1) { while (stack.size() > 1) {
stack.pop(); stack.pop();
} }
Keys.Key[] tableParts = Keys.split(tableName); dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
for (int i = 0; i < tableParts.length; i++) { for (int i = 0; i < tableParts.length; i++) {
String tablePart = tableParts[i].name; String tablePart = tableParts[i].name;
Container currentContainer = stack.peek(); Container currentContainer = stack.peek();

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;

View File

@ -1,8 +1,8 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.Collection; import java.util.Collection;
import static com.moandjiezana.toml.ValueWriters.WRITERS; import static dev.plex.toml.ValueWriters.WRITERS;
class TableArrayValueWriter extends ArrayValueWriter class TableArrayValueWriter extends ArrayValueWriter
{ {

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@ -139,7 +139,7 @@ public class Toml
*/ */
public Toml read(String tomlString) throws IllegalStateException public Toml read(String tomlString) throws IllegalStateException
{ {
Results results = TomlParser.run(tomlString); dev.plex.toml.Results results = dev.plex.toml.TomlParser.run(tomlString);
if (results.errors.hasErrors()) if (results.errors.hasErrors())
{ {
throw new IllegalStateException(results.errors.toString()); throw new IllegalStateException(results.errors.toString());
@ -443,9 +443,9 @@ public class Toml
Object current = new HashMap<>(values); Object current = new HashMap<>(values);
Keys.Key[] keys = Keys.split(key); dev.plex.toml.Keys.Key[] keys = dev.plex.toml.Keys.split(key);
for (Keys.Key k : keys) for (dev.plex.toml.Keys.Key k : keys)
{ {
if (k.index == -1 && current instanceof Map && ((Map<String, Object>) current).containsKey(k.path)) if (k.index == -1 && current instanceof Map && ((Map<String, Object>) current).containsKey(k.path))
{ {

View File

@ -1,11 +1,11 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
class TomlParser { class TomlParser {
static Results run(String tomlString) { static dev.plex.toml.Results run(String tomlString) {
final Results results = new Results(); final dev.plex.toml.Results results = new dev.plex.toml.Results();
if (tomlString.isEmpty()) { if (tomlString.isEmpty()) {
return results; return results;
@ -14,7 +14,7 @@ class TomlParser {
AtomicInteger index = new AtomicInteger(); AtomicInteger index = new AtomicInteger();
boolean inComment = false; boolean inComment = false;
AtomicInteger line = new AtomicInteger(1); AtomicInteger line = new AtomicInteger(1);
Identifier identifier = null; dev.plex.toml.Identifier identifier = null;
Object value = null; Object value = null;
for (int i = index.get(); i < tomlString.length(); i = index.incrementAndGet()) { for (int i = index.get(); i < tomlString.length(); i = index.incrementAndGet()) {
@ -27,9 +27,9 @@ class TomlParser {
if (c == '#' && !inComment) { if (c == '#' && !inComment) {
inComment = true; inComment = true;
} else if (!Character.isWhitespace(c) && !inComment && identifier == null) { } else if (!Character.isWhitespace(c) && !inComment && identifier == null) {
Identifier id = IdentifierConverter.IDENTIFIER_CONVERTER.convert(tomlString, index, new Context(null, line, results.errors)); dev.plex.toml.Identifier id = dev.plex.toml.IdentifierConverter.IDENTIFIER_CONVERTER.convert(tomlString, index, new dev.plex.toml.Context(null, line, results.errors));
if (id != Identifier.INVALID) { if (id != dev.plex.toml.Identifier.INVALID) {
if (id.isKey()) { if (id.isKey()) {
identifier = id; identifier = id;
} else if (id.isTable()) { } else if (id.isTable()) {
@ -44,10 +44,10 @@ class TomlParser {
value = null; value = null;
line.incrementAndGet(); line.incrementAndGet();
} else if (!inComment && identifier != null && identifier.isKey() && value == null && !Character.isWhitespace(c)) { } else if (!inComment && identifier != null && identifier.isKey() && value == null && !Character.isWhitespace(c)) {
value = ValueReaders.VALUE_READERS.convert(tomlString, index, new Context(identifier, line, results.errors)); value = ValueReaders.VALUE_READERS.convert(tomlString, index, new dev.plex.toml.Context(identifier, line, results.errors));
if (value instanceof Results.Errors) { if (value instanceof dev.plex.toml.Results.Errors) {
results.errors.add((Results.Errors) value); results.errors.add((dev.plex.toml.Results.Errors) value);
} else { } else {
results.addValue(identifier.getName(), value, line); results.addValue(identifier.getName(), value, line);
} }

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -7,8 +7,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER; import static dev.plex.toml.ValueWriters.WRITERS;
import static com.moandjiezana.toml.ValueWriters.WRITERS;
/** /**
* <p>Converts Objects to TOML</p> * <p>Converts Objects to TOML</p>
@ -83,7 +82,7 @@ public class TomlWriter
} }
private final IndentationPolicy indentationPolicy; private final IndentationPolicy indentationPolicy;
private final DatePolicy datePolicy; private final dev.plex.toml.DatePolicy datePolicy;
/** /**
* Creates a TomlWriter instance. * Creates a TomlWriter instance.
@ -96,7 +95,7 @@ public class TomlWriter
private TomlWriter(int keyIndentation, int tableIndentation, int arrayDelimiterPadding, TimeZone timeZone, boolean showFractionalSeconds) private TomlWriter(int keyIndentation, int tableIndentation, int arrayDelimiterPadding, TimeZone timeZone, boolean showFractionalSeconds)
{ {
this.indentationPolicy = new IndentationPolicy(keyIndentation, tableIndentation, arrayDelimiterPadding); this.indentationPolicy = new IndentationPolicy(keyIndentation, tableIndentation, arrayDelimiterPadding);
this.datePolicy = new DatePolicy(timeZone, showFractionalSeconds); this.datePolicy = new dev.plex.toml.DatePolicy(timeZone, showFractionalSeconds);
} }
/** /**
@ -162,8 +161,8 @@ public class TomlWriter
*/ */
public void write(Object from, Writer target, @Nullable File file) throws IOException public void write(Object from, Writer target, @Nullable File file) throws IOException
{ {
ValueWriter valueWriter = WRITERS.findWriterFor(from); dev.plex.toml.ValueWriter valueWriter = WRITERS.findWriterFor(from);
if (valueWriter == MAP_VALUE_WRITER || valueWriter == ObjectValueWriter.OBJECT_VALUE_WRITER) if (valueWriter == MapValueWriter.MAP_VALUE_WRITER || valueWriter == dev.plex.toml.ObjectValueWriter.OBJECT_VALUE_WRITER)
{ {
WriterContext context = new WriterContext(indentationPolicy, datePolicy, target); WriterContext context = new WriterContext(indentationPolicy, datePolicy, target);
if (file != null && file.exists()) if (file != null && file.exists())

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -15,7 +15,7 @@ interface ValueReader {
* @param s must already have been validated by {@link #canRead(String)} * @param s must already have been validated by {@link #canRead(String)}
* @param index where to start in s * @param index where to start in s
* @param line current line number, used for error reporting * @param line current line number, used for error reporting
* @return a value or a {@link Results.Errors} * @return a value or a {@link dev.plex.toml.Results.Errors}
*/ */
Object read(String s, AtomicInteger index, Context context); Object read(String s, AtomicInteger index, dev.plex.toml.Context context);
} }

View File

@ -0,0 +1,35 @@
package dev.plex.toml;
import java.util.concurrent.atomic.AtomicInteger;
import static dev.plex.toml.ArrayValueReader.ARRAY_VALUE_READER;
import static dev.plex.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER;
import static dev.plex.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
import static dev.plex.toml.LiteralStringValueReader.LITERAL_STRING_VALUE_READER;
import static dev.plex.toml.MultilineLiteralStringValueReader.MULTILINE_LITERAL_STRING_VALUE_READER;
import static dev.plex.toml.MultilineStringValueReader.MULTILINE_STRING_VALUE_READER;
import static dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER;
class ValueReaders {
static final ValueReaders VALUE_READERS = new ValueReaders();
Object convert(String value, AtomicInteger index, dev.plex.toml.Context context) {
String substring = value.substring(index.get());
for (dev.plex.toml.ValueReader valueParser : READERS) {
if (valueParser.canRead(substring)) {
return valueParser.read(value, index, context);
}
}
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
errors.invalidValue(context.identifier.getName(), substring, context.line.get());
return errors;
}
private ValueReaders() {}
private static final dev.plex.toml.ValueReader[] READERS = {
MULTILINE_STRING_VALUE_READER, MULTILINE_LITERAL_STRING_VALUE_READER, LITERAL_STRING_VALUE_READER, STRING_VALUE_READER_WRITER, DATE_VALUE_READER_WRITER, NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER, BOOLEAN_VALUE_READER_WRITER, ARRAY_VALUE_READER, InlineTableValueReader.INLINE_TABLE_VALUE_READER
};
}

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
interface ValueWriter { interface ValueWriter {
boolean canWrite(Object value); boolean canWrite(Object value);

View File

@ -0,0 +1,28 @@
package dev.plex.toml;
class ValueWriters {
static final ValueWriters WRITERS = new ValueWriters();
ValueWriter findWriterFor(Object value) {
for (ValueWriter valueWriter : VALUE_WRITERS) {
if (valueWriter.canWrite(value)) {
return valueWriter;
}
}
return ObjectValueWriter.OBJECT_VALUE_WRITER;
}
private ValueWriters() {}
private static dev.plex.toml.DateValueReaderWriter getPlatformSpecificDateConverter() {
String specificationVersion = Runtime.class.getPackage().getSpecificationVersion();
return specificationVersion != null && specificationVersion.startsWith("1.6") ? dev.plex.toml.DateValueReaderWriter.DATE_PARSER_JDK_6 : dev.plex.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
}
private static final ValueWriter[] VALUE_WRITERS = {
StringValueReaderWriter.STRING_VALUE_READER_WRITER, NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER, dev.plex.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER, getPlatformSpecificDateConverter(),
MapValueWriter.MAP_VALUE_WRITER, dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER, TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER
};
}

View File

@ -1,4 +1,4 @@
package com.moandjiezana.toml; package dev.plex.toml;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -14,14 +14,14 @@ class WriterContext
private final String currentTableIndent; private final String currentTableIndent;
private final String currentFieldIndent; private final String currentFieldIndent;
private final Writer output; private final Writer output;
private final IndentationPolicy indentationPolicy; private final dev.plex.toml.IndentationPolicy indentationPolicy;
private final DatePolicy datePolicy; private final dev.plex.toml.DatePolicy datePolicy;
public File file; public File file;
public String parentName; public String parentName;
public boolean hasRun = false; public boolean hasRun = false;
WriterContext(IndentationPolicy indentationPolicy, DatePolicy datePolicy, Writer output) WriterContext(dev.plex.toml.IndentationPolicy indentationPolicy, dev.plex.toml.DatePolicy datePolicy, Writer output)
{ {
this("", "", output, indentationPolicy, datePolicy); this("", "", output, indentationPolicy, datePolicy);
} }
@ -135,7 +135,7 @@ class WriterContext
} }
} }
DatePolicy getDatePolicy() dev.plex.toml.DatePolicy getDatePolicy()
{ {
return datePolicy; return datePolicy;
} }
@ -157,7 +157,7 @@ class WriterContext
return key.isEmpty() ? arrayKey : key + "." + arrayKey; return key.isEmpty() ? arrayKey : key + "." + arrayKey;
} }
private String growIndent(IndentationPolicy indentationPolicy) private String growIndent(dev.plex.toml.IndentationPolicy indentationPolicy)
{ {
return currentTableIndent + fillStringWithSpaces(indentationPolicy.getTableIndent()); return currentTableIndent + fillStringWithSpaces(indentationPolicy.getTableIndent());
} }
@ -170,7 +170,7 @@ class WriterContext
return new String(chars); return new String(chars);
} }
private WriterContext(String key, String tableIndent, Writer output, IndentationPolicy indentationPolicy, DatePolicy datePolicy) private WriterContext(String key, String tableIndent, Writer output, dev.plex.toml.IndentationPolicy indentationPolicy, dev.plex.toml.DatePolicy datePolicy)
{ {
this.key = key; this.key = key;
this.output = output; this.output = output;

View File

@ -0,0 +1,21 @@
#############################
# #
# Plex Velocity #
# v0.1 #
# #
#############################
[server]
name = "Plexus"
# Placeholders
# %mcversion% - The Velocity Version (i.e. 3.1.2-SNAPSHOT)
# %servername% - The name provided above
# %randomgradient% - Creates a random gradient every ping of two random colors for the whole string
# Supports MiniMessage strings, no legacy & and §
motd = "%randomgradient%%servername% - %mcversion%"
colorizeMotd = false
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"]

View File

@ -4,6 +4,19 @@ import dev.plex.Plex;
import dev.plex.PlexBase; import dev.plex.PlexBase;
import dev.plex.cache.PlayerCache; import dev.plex.cache.PlayerCache;
import dev.plex.storage.StorageType; import dev.plex.storage.StorageType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.Month; import java.time.Month;
@ -12,21 +25,6 @@ import java.time.ZonedDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.time.DateUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class PlexUtils implements PlexBase public class PlexUtils implements PlexBase
{ {