mirror of
https://github.com/plexusorg/Plex.git
synced 2026-06-04 05:26:55 +00:00
Debloat the Velocity project
This commit is contained in:
@@ -9,12 +9,20 @@ public interface PlexConfiguration
|
||||
{
|
||||
String getString(String path);
|
||||
|
||||
String getString(String path, String fallback);
|
||||
|
||||
boolean getBoolean(String path);
|
||||
|
||||
boolean getBoolean(String path, boolean fallback);
|
||||
|
||||
int getInt(String path);
|
||||
|
||||
int getInt(String path, int fallback);
|
||||
|
||||
List<String> getStringList(String path);
|
||||
|
||||
List<String> getStringList(String path, List<String> fallback);
|
||||
|
||||
void set(String path, Object value);
|
||||
|
||||
void setComments(String path, List<String> comments);
|
||||
|
||||
+8
-11
@@ -1,6 +1,5 @@
|
||||
plugins {
|
||||
java
|
||||
`maven-publish`
|
||||
id("org.jetbrains.gradle.plugin.idea-ext")
|
||||
id("net.kyori.blossom")
|
||||
id("com.gradleup.shadow")
|
||||
@@ -19,12 +18,18 @@ tasks.getByName<Jar>("jar") {
|
||||
}
|
||||
|
||||
tasks {
|
||||
build {
|
||||
dependsOn(shadowJar)
|
||||
}
|
||||
|
||||
jar {
|
||||
finalizedBy(rootProject.tasks["copyJars"])
|
||||
enabled = false
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
enabled = false
|
||||
archiveBaseName.set("Plex-Velocity")
|
||||
archiveClassifier.set("")
|
||||
finalizedBy(rootProject.tasks["copyJars"])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,14 +43,6 @@ sourceSets {
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
from(components["java"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":api"))
|
||||
compileOnly("org.projectlombok:lombok:1.18.46")
|
||||
|
||||
@@ -8,24 +8,14 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import dev.plex.api.PlexApi;
|
||||
import dev.plex.api.impl.DefaultPlexApi;
|
||||
import dev.plex.config.TomlConfig;
|
||||
import dev.plex.config.YamlConfig;
|
||||
import dev.plex.handlers.ListenerHandler;
|
||||
import dev.plex.settings.ServerSettings;
|
||||
import dev.plex.util.PlexLog;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.logging.Logger;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* <p>
|
||||
* Modifications: Properly indent arrays in TOML as well as only append
|
||||
* missing object fields into the file
|
||||
*/
|
||||
|
||||
@Plugin(
|
||||
name = "Plex",
|
||||
id = "plex",
|
||||
@@ -44,7 +34,8 @@ public class Plex
|
||||
private final Logger logger;
|
||||
private final File dataFolder;
|
||||
|
||||
private TomlConfig config;
|
||||
private YamlConfig config;
|
||||
private YamlConfig messages;
|
||||
private PlexApi api;
|
||||
|
||||
@Inject
|
||||
@@ -64,21 +55,23 @@ public class Plex
|
||||
@Subscribe
|
||||
public void onStart(ProxyInitializeEvent event)
|
||||
{
|
||||
this.config = new TomlConfig("config.toml");
|
||||
this.config.setOnCreate(toml ->
|
||||
{
|
||||
PlexLog.log("Created configuration 'config.toml'");
|
||||
});
|
||||
this.config.setOnLoad(toml ->
|
||||
{
|
||||
PlexLog.log("Loaded configuration 'config.toml'");
|
||||
});
|
||||
this.config.create(true);
|
||||
this.config.write(new ServerSettings());
|
||||
this.config = loadConfig("config.yml");
|
||||
this.messages = loadConfig("messages.yml");
|
||||
this.api = new DefaultPlexApi(this, MODULE_API_COMPATIBILITY_VERSION);
|
||||
new ListenerHandler();
|
||||
}
|
||||
|
||||
private YamlConfig loadConfig(String name)
|
||||
{
|
||||
YamlConfig yamlConfig = new YamlConfig(dataFolder, name);
|
||||
if (yamlConfig.create())
|
||||
{
|
||||
PlexLog.log("Created configuration '" + name + "'");
|
||||
}
|
||||
PlexLog.log("Loaded configuration '" + name + "'");
|
||||
return yamlConfig;
|
||||
}
|
||||
|
||||
public static Plex get()
|
||||
{
|
||||
return plugin;
|
||||
|
||||
@@ -16,13 +16,13 @@ final class DefaultConfigurationApi implements ConfigurationApi
|
||||
@Override
|
||||
public PlexConfiguration mainConfig()
|
||||
{
|
||||
return new DefaultPlexConfiguration(plugin.getConfig());
|
||||
return plugin.getConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlexConfiguration messages()
|
||||
{
|
||||
throw new UnsupportedOperationException("Proxy does not provide messages configuration");
|
||||
return plugin.getMessages();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package dev.plex.api.impl;
|
||||
|
||||
import dev.plex.api.config.PlexConfiguration;
|
||||
import dev.plex.config.TomlConfig;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
final class DefaultPlexConfiguration implements PlexConfiguration
|
||||
{
|
||||
private final TomlConfig config;
|
||||
|
||||
DefaultPlexConfiguration(TomlConfig config)
|
||||
{
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path)
|
||||
{
|
||||
return config.getToml().getString(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path)
|
||||
{
|
||||
return config.getToml().getBoolean(path, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String path)
|
||||
{
|
||||
Long value = config.getToml().getLong(path, 0L);
|
||||
return value.intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path)
|
||||
{
|
||||
return config.getToml().getList(path, List.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value)
|
||||
{
|
||||
throw new UnsupportedOperationException("Proxy TOML configuration writes are not supported through PlexConfiguration");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComments(String path, List<String> comments)
|
||||
{
|
||||
throw new UnsupportedOperationException("Proxy TOML configuration comments are not supported through PlexConfiguration");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save()
|
||||
{
|
||||
throw new UnsupportedOperationException("Proxy TOML configuration saves are not supported through PlexConfiguration");
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package dev.plex.config;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.toml.Toml;
|
||||
import dev.plex.toml.TomlWriter;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.function.Consumer;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
public class TomlConfig
|
||||
{
|
||||
private final File file;
|
||||
private Toml toml;
|
||||
|
||||
@Setter
|
||||
private Consumer<Toml> onCreate;
|
||||
|
||||
@Setter
|
||||
private Consumer<Toml> onLoad;
|
||||
|
||||
public TomlConfig(String fileName)
|
||||
{
|
||||
this.file = new File(Plex.get().getDataFolder(), fileName);
|
||||
this.toml = new Toml();
|
||||
}
|
||||
|
||||
public void create(boolean loadFromFile)
|
||||
{
|
||||
if (!this.file.exists())
|
||||
{
|
||||
if (loadFromFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
Files.copy(Plex.get().getClass().getResourceAsStream("/" + this.file.getName()), this.file.toPath());
|
||||
this.load();
|
||||
if (this.onCreate != null)
|
||||
{
|
||||
this.onCreate.accept(this.toml);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
this.file.createNewFile();
|
||||
this.load();
|
||||
if (this.onCreate != null)
|
||||
{
|
||||
this.onCreate.accept(this.toml);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.load();
|
||||
}
|
||||
|
||||
public void load()
|
||||
{
|
||||
this.toml = new Toml().read(this.file);
|
||||
if (onLoad != null)
|
||||
{
|
||||
this.onLoad.accept(this.toml);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T as(Class<T> clazz)
|
||||
{
|
||||
return this.toml.to(clazz);
|
||||
}
|
||||
|
||||
public <T> void write(T object)
|
||||
{
|
||||
TomlWriter writer = new TomlWriter.Builder()
|
||||
.indentValuesBy(2)
|
||||
.build();
|
||||
try
|
||||
{
|
||||
writer.write(object, this.file);
|
||||
this.load();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package dev.plex.config;
|
||||
|
||||
import dev.plex.api.config.PlexConfiguration;
|
||||
import dev.plex.settings.ServerSettings;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
public class YamlConfig implements PlexConfiguration
|
||||
{
|
||||
private final File file;
|
||||
private final YamlConfigurationLoader loader;
|
||||
private CommentedConfigurationNode root;
|
||||
|
||||
public YamlConfig(File dataFolder, String fileName)
|
||||
{
|
||||
this.file = new File(dataFolder, fileName);
|
||||
this.loader = YamlConfigurationLoader.builder()
|
||||
.path(this.file.toPath())
|
||||
.build();
|
||||
}
|
||||
|
||||
public boolean create()
|
||||
{
|
||||
boolean created = false;
|
||||
try
|
||||
{
|
||||
Files.createDirectories(file.toPath().getParent());
|
||||
if (!file.exists())
|
||||
{
|
||||
copyDefault();
|
||||
created = true;
|
||||
}
|
||||
load();
|
||||
return created;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new IllegalStateException("Could not create configuration '" + file.getName() + "'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void load()
|
||||
{
|
||||
try
|
||||
{
|
||||
this.root = loader.load();
|
||||
}
|
||||
catch (ConfigurateException ex)
|
||||
{
|
||||
throw new IllegalStateException("Could not load configuration '" + file.getName() + "'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ServerSettings settings()
|
||||
{
|
||||
return new ServerSettings(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path)
|
||||
{
|
||||
return node(path).getString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path, String fallback)
|
||||
{
|
||||
return node(path).getString(fallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path)
|
||||
{
|
||||
return getBoolean(path, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path, boolean fallback)
|
||||
{
|
||||
return node(path).getBoolean(fallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String path)
|
||||
{
|
||||
return getInt(path, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String path, int fallback)
|
||||
{
|
||||
return node(path).getInt(fallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path)
|
||||
{
|
||||
return getStringList(path, List.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path, List<String> fallback)
|
||||
{
|
||||
try
|
||||
{
|
||||
return List.copyOf(node(path).getList(String.class, fallback));
|
||||
}
|
||||
catch (SerializationException ex)
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value)
|
||||
{
|
||||
try
|
||||
{
|
||||
node(path).set(value);
|
||||
}
|
||||
catch (SerializationException ex)
|
||||
{
|
||||
throw new IllegalArgumentException("Could not set configuration path '" + path + "'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComments(String path, List<String> comments)
|
||||
{
|
||||
node(path).comment(comments == null || comments.isEmpty() ? null : String.join(System.lineSeparator(), comments));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save()
|
||||
{
|
||||
try
|
||||
{
|
||||
loader.save(root);
|
||||
}
|
||||
catch (ConfigurateException ex)
|
||||
{
|
||||
throw new IllegalStateException("Could not save configuration '" + file.getName() + "'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void copyDefault() throws IOException
|
||||
{
|
||||
try (InputStream input = YamlConfig.class.getResourceAsStream("/" + file.getName()))
|
||||
{
|
||||
if (input == null)
|
||||
{
|
||||
throw new IllegalStateException("Missing bundled configuration '" + file.getName() + "'");
|
||||
}
|
||||
Files.copy(input, file.toPath());
|
||||
}
|
||||
}
|
||||
|
||||
private CommentedConfigurationNode node(String path)
|
||||
{
|
||||
if (root == null)
|
||||
{
|
||||
throw new IllegalStateException("Configuration '" + file.getName() + "' has not been loaded");
|
||||
}
|
||||
if (path == null || path.isBlank())
|
||||
{
|
||||
return root;
|
||||
}
|
||||
return root.node((Object[]) path.split("\\."));
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import com.velocitypowered.api.event.PostOrder;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||
import com.velocitypowered.api.event.player.ServerConnectedEvent;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.listener.ProxyListener;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
@@ -16,14 +15,16 @@ public class ConnectionListener extends ProxyListener
|
||||
{
|
||||
if (event.getPreviousServer().isPresent())
|
||||
{
|
||||
Plex.get().server.sendMessage(miniMessage("<dark_gray>[<#ffbf00>o<dark_gray>] <yellow>"
|
||||
+ event.getPlayer().getUsername() + " switched from " + event.getPreviousServer().get().getServerInfo().getName()
|
||||
+ " to " + event.getServer().getServerInfo().getName()));
|
||||
plugin.server.sendMessage(message("server_switch",
|
||||
"player", event.getPlayer().getUsername(),
|
||||
"from", event.getPreviousServer().get().getServerInfo().getName(),
|
||||
"to", event.getServer().getServerInfo().getName()));
|
||||
}
|
||||
else
|
||||
{
|
||||
Plex.get().server.sendMessage(miniMessage("<dark_gray>[<green>+<dark_gray>] <yellow>"
|
||||
+ event.getPlayer().getUsername() + " joined server " + event.getServer().getServerInfo().getName()));
|
||||
plugin.server.sendMessage(message("server_join",
|
||||
"player", event.getPlayer().getUsername(),
|
||||
"server", event.getServer().getServerInfo().getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,14 +33,19 @@ public class ConnectionListener extends ProxyListener
|
||||
{
|
||||
if (event.getPlayer().getCurrentServer().isPresent())
|
||||
{
|
||||
Plex.get().server.sendMessage(miniMessage("<dark_gray>[<red>-<dark_gray>] <yellow>"
|
||||
+ event.getPlayer().getUsername() + " left server " +
|
||||
event.getPlayer().getCurrentServer().get().getServerInfo().getName()));
|
||||
plugin.server.sendMessage(message("server_leave",
|
||||
"player", event.getPlayer().getUsername(),
|
||||
"server", event.getPlayer().getCurrentServer().get().getServerInfo().getName()));
|
||||
}
|
||||
}
|
||||
|
||||
private Component miniMessage(String message)
|
||||
private Component message(String key, String... replacements)
|
||||
{
|
||||
String message = plugin.getMessages().getString(key, "");
|
||||
for (int i = 0; i < replacements.length; i += 2)
|
||||
{
|
||||
message = message.replace("{" + replacements[i] + "}", replacements[i + 1]);
|
||||
}
|
||||
return MiniMessage.miniMessage().deserialize(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.velocitypowered.api.proxy.server.ServerPing;
|
||||
import dev.plex.listener.ProxyListener;
|
||||
import dev.plex.settings.ServerSettings;
|
||||
import dev.plex.util.RandomUtil;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -20,15 +21,17 @@ public class ServerListener extends ProxyListener
|
||||
@Subscribe(order = PostOrder.FIRST)
|
||||
public void onPing(ProxyPingEvent event)
|
||||
{
|
||||
String baseMotd = plugin.getConfig().as(ServerSettings.class).getServer().getMotd().get(ThreadLocalRandom.current().nextInt(plugin.getConfig().as(ServerSettings.class).getServer().getMotd().size()));
|
||||
ServerSettings.Server config = plugin.getConfig().settings().getServer();
|
||||
List<String> motds = config.getMotd();
|
||||
String baseMotd = motds.get(ThreadLocalRandom.current().nextInt(motds.size()));
|
||||
baseMotd = baseMotd.replace("\\n", "\n");
|
||||
baseMotd = baseMotd.replace("%servername%", plugin.getConfig().as(ServerSettings.class).getServer().getName());
|
||||
baseMotd = baseMotd.replace("%servername%", config.getName());
|
||||
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())
|
||||
if (config.isColorizeMotd())
|
||||
{
|
||||
AtomicReference<Component> motd = new AtomicReference<>(Component.empty());
|
||||
for (final String word : baseMotd.split(" "))
|
||||
@@ -43,9 +46,9 @@ public class ServerListener extends ProxyListener
|
||||
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.samplePlayers(config.getSample().stream().map(s -> new ServerPing.SamplePlayer(convertColorCodes(s), UUID.randomUUID())).toArray(ServerPing.SamplePlayer[]::new));
|
||||
builder.onlinePlayers(plugin.getServer().getPlayerCount() + config.getAddPlayerCount());
|
||||
if (config.isPlusOneMaxPlayer())
|
||||
{
|
||||
builder.maximumPlayers(builder.getOnlinePlayers() + 1);
|
||||
}
|
||||
|
||||
@@ -1,27 +1,42 @@
|
||||
package dev.plex.settings;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import dev.plex.api.config.PlexConfiguration;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class ServerSettings
|
||||
{
|
||||
private final Server server = new Server();
|
||||
private static final List<String> DEFAULT_MOTD = List.of("%randomgradient%%servername% - %mcversion%", "Another motd");
|
||||
private static final List<String> DEFAULT_SAMPLE = List.of("example", "example");
|
||||
|
||||
@Data
|
||||
private final Server server;
|
||||
|
||||
public ServerSettings(PlexConfiguration config)
|
||||
{
|
||||
this.server = new Server(config);
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class Server
|
||||
{
|
||||
private String name = "Server";
|
||||
private List<String> motd = Lists.newArrayList("%randomgradient%%servername% - %mcversion%", "Another motd");
|
||||
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;
|
||||
private final String name;
|
||||
private final List<String> motd;
|
||||
private final boolean colorizeMotd;
|
||||
private final boolean debug;
|
||||
private final List<String> sample;
|
||||
private final int addPlayerCount;
|
||||
private final boolean plusOneMaxPlayer;
|
||||
|
||||
private Server(PlexConfiguration config)
|
||||
{
|
||||
this.name = config.getString("server.name", "Plexus");
|
||||
this.motd = config.getStringList("server.motd", DEFAULT_MOTD);
|
||||
this.colorizeMotd = config.getBoolean("server.colorize_motd", false);
|
||||
this.debug = config.getBoolean("server.debug", false);
|
||||
this.sample = config.getStringList("server.sample", DEFAULT_SAMPLE);
|
||||
this.addPlayerCount = config.getInt("server.add_player_count", 0);
|
||||
this.plusOneMaxPlayer = config.getBoolean("server.plus_one_max_count", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class ArrayValueReader implements ValueReader
|
||||
{
|
||||
|
||||
public static final ArrayValueReader ARRAY_VALUE_READER = new ArrayValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("[");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int startIndex = index.get();
|
||||
List<Object> arrayItems = new ArrayList<Object>();
|
||||
boolean terminated = false;
|
||||
boolean inComment = false;
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
|
||||
for (int i = index.incrementAndGet(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '#' && !inComment)
|
||||
{
|
||||
inComment = true;
|
||||
}
|
||||
else if (c == '\n')
|
||||
{
|
||||
inComment = false;
|
||||
line.incrementAndGet();
|
||||
}
|
||||
else if (inComment || Character.isWhitespace(c) || c == ',')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (c == '[')
|
||||
{
|
||||
Object converted = read(s, index, context);
|
||||
if (converted instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
errors.add((dev.plex.toml.Results.Errors) converted);
|
||||
}
|
||||
else if (!isHomogenousArray(converted, arrayItems))
|
||||
{
|
||||
errors.heterogenous(context.identifier.getName(), line.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
arrayItems.add(converted);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else if (c == ']')
|
||||
{
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
Object converted = ValueReaders.VALUE_READERS.convert(s, index, context);
|
||||
if (converted instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
errors.add((dev.plex.toml.Results.Errors) converted);
|
||||
}
|
||||
else if (!isHomogenousArray(converted, arrayItems))
|
||||
{
|
||||
errors.heterogenous(context.identifier.getName(), line.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
arrayItems.add(converted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!terminated)
|
||||
{
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex), startLine);
|
||||
}
|
||||
|
||||
if (errors.hasErrors())
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
return arrayItems;
|
||||
}
|
||||
|
||||
private boolean isHomogenousArray(Object o, List<?> values)
|
||||
{
|
||||
return values.isEmpty() || values.get(0).getClass().isAssignableFrom(o.getClass()) || o.getClass().isAssignableFrom(values.get(0).getClass());
|
||||
}
|
||||
|
||||
private ArrayValueReader()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import static dev.plex.toml.ValueWriters.WRITERS;
|
||||
|
||||
public abstract class ArrayValueWriter implements dev.plex.toml.ValueWriter
|
||||
{
|
||||
static protected boolean isArrayish(Object value)
|
||||
{
|
||||
return value instanceof Collection || value.getClass().isArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean isArrayOfPrimitive(Object array)
|
||||
{
|
||||
Object first = peek(array);
|
||||
if (first != null)
|
||||
{
|
||||
dev.plex.toml.ValueWriter valueWriter = WRITERS.findWriterFor(first);
|
||||
return valueWriter.isPrimitiveType() || isArrayish(first);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Collection<?> normalize(Object value)
|
||||
{
|
||||
Collection<Object> collection;
|
||||
|
||||
if (value.getClass().isArray())
|
||||
{
|
||||
// Arrays.asList() interprets an array as a single element,
|
||||
// so convert it to a list by hand
|
||||
collection = new ArrayList<Object>(Array.getLength(value));
|
||||
for (int i = 0; i < Array.getLength(value); i++)
|
||||
{
|
||||
Object elem = Array.get(value, i);
|
||||
collection.add(elem);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
collection = (Collection<Object>) value;
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
private static Object peek(Object value)
|
||||
{
|
||||
if (value.getClass().isArray())
|
||||
{
|
||||
if (Array.getLength(value) > 0)
|
||||
{
|
||||
return Array.get(value, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Collection<?> collection = (Collection<?>) value;
|
||||
if (collection.size() > 0)
|
||||
{
|
||||
return collection.iterator().next();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
||||
class BooleanValueReaderWriter implements ValueReader, ValueWriter
|
||||
{
|
||||
|
||||
static final BooleanValueReaderWriter BOOLEAN_VALUE_READER_WRITER = new BooleanValueReaderWriter();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("true") || s.startsWith("false");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
s = s.substring(index.get());
|
||||
Boolean b = s.startsWith("true") ? Boolean.TRUE : Boolean.FALSE;
|
||||
|
||||
int endIndex = b == Boolean.TRUE ? 4 : 5;
|
||||
|
||||
index.addAndGet(endIndex - 1);
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof Boolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
context.write(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private BooleanValueReaderWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "boolean";
|
||||
}
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class Container
|
||||
{
|
||||
|
||||
abstract boolean accepts(String key);
|
||||
|
||||
abstract void put(String key, Object value);
|
||||
|
||||
abstract Object get(String key);
|
||||
|
||||
abstract boolean isImplicit();
|
||||
|
||||
static class Table extends Container
|
||||
{
|
||||
private final Map<String, Object> values = new HashMap<String, Object>();
|
||||
final String name;
|
||||
final boolean implicit;
|
||||
|
||||
Table()
|
||||
{
|
||||
this(null, false);
|
||||
}
|
||||
|
||||
public Table(String name)
|
||||
{
|
||||
this(name, false);
|
||||
}
|
||||
|
||||
public Table(String tableName, boolean implicit)
|
||||
{
|
||||
this.name = tableName;
|
||||
this.implicit = implicit;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean accepts(String key)
|
||||
{
|
||||
return !values.containsKey(key) || values.get(key) instanceof TableArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
void put(String key, Object value)
|
||||
{
|
||||
values.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
Object get(String key)
|
||||
{
|
||||
return values.get(key);
|
||||
}
|
||||
|
||||
boolean isImplicit()
|
||||
{
|
||||
return implicit;
|
||||
}
|
||||
|
||||
/**
|
||||
* This modifies the Table's internal data structure, such that it is no longer usable.
|
||||
* <p>
|
||||
* Therefore, this method must only be called when all data has been gathered.
|
||||
*
|
||||
* @return A Map-and-List-based of the TOML data
|
||||
*/
|
||||
Map<String, Object> consume()
|
||||
{
|
||||
for (Map.Entry<String, Object> entry : values.entrySet())
|
||||
{
|
||||
if (entry.getValue() instanceof Table)
|
||||
{
|
||||
entry.setValue(((Table) entry.getValue()).consume());
|
||||
}
|
||||
else if (entry.getValue() instanceof TableArray)
|
||||
{
|
||||
entry.setValue(((TableArray) entry.getValue()).getValues());
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return values.toString();
|
||||
}
|
||||
}
|
||||
|
||||
static class TableArray extends Container
|
||||
{
|
||||
private final List<Table> values = new ArrayList<Table>();
|
||||
|
||||
TableArray()
|
||||
{
|
||||
values.add(new Table());
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean accepts(String key)
|
||||
{
|
||||
return getCurrent().accepts(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
void put(String key, Object value)
|
||||
{
|
||||
values.add((Table) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
Object get(String key)
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
boolean isImplicit()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Map<String, Object>> getValues()
|
||||
{
|
||||
ArrayList<Map<String, Object>> unwrappedValues = new ArrayList<Map<String, Object>>();
|
||||
for (Table table : values)
|
||||
{
|
||||
unwrappedValues.add(table.consume());
|
||||
}
|
||||
return unwrappedValues;
|
||||
}
|
||||
|
||||
Table getCurrent()
|
||||
{
|
||||
return values.get(values.size() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return values.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private Container()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Context
|
||||
{
|
||||
final dev.plex.toml.Identifier identifier;
|
||||
final AtomicInteger line;
|
||||
final Results.Errors errors;
|
||||
|
||||
public Context(dev.plex.toml.Identifier identifier, AtomicInteger line, Results.Errors errors)
|
||||
{
|
||||
this.identifier = identifier;
|
||||
this.line = line;
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public Context with(dev.plex.toml.Identifier identifier)
|
||||
{
|
||||
return new Context(identifier, line, errors);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DatePolicy
|
||||
{
|
||||
|
||||
private final TimeZone timeZone;
|
||||
private final boolean showFractionalSeconds;
|
||||
|
||||
DatePolicy(TimeZone timeZone, boolean showFractionalSeconds)
|
||||
{
|
||||
this.timeZone = timeZone;
|
||||
this.showFractionalSeconds = showFractionalSeconds;
|
||||
}
|
||||
|
||||
TimeZone getTimeZone()
|
||||
{
|
||||
return timeZone;
|
||||
}
|
||||
|
||||
boolean isShowFractionalSeconds()
|
||||
{
|
||||
return showFractionalSeconds;
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DateValueReaderWriter implements ValueReader, ValueWriter
|
||||
{
|
||||
|
||||
static final DateValueReaderWriter DATE_VALUE_READER_WRITER = new DateValueReaderWriter();
|
||||
static final DateValueReaderWriter DATE_PARSER_JDK_6 = new DateConverterJdk6();
|
||||
private static final Pattern DATE_REGEX = Pattern.compile("(\\d{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9])(\\.\\d*)?(Z|(?:[+\\-]\\d{2}:\\d{2}))(.*)");
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
if (s.length() < 5)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (i < 4)
|
||||
{
|
||||
if (!Character.isDigit(c))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (c != '-')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String original, AtomicInteger index, Context context)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = index.get(); i < original.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = original.charAt(i);
|
||||
if (Character.isDigit(c) || c == '-' || c == '+' || c == ':' || c == '.' || c == 'T' || c == 'Z')
|
||||
{
|
||||
sb.append(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String s = sb.toString();
|
||||
Matcher matcher = DATE_REGEX.matcher(s);
|
||||
|
||||
if (!matcher.matches())
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), s, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
String dateString = matcher.group(1);
|
||||
String zone = matcher.group(3);
|
||||
String fractionalSeconds = matcher.group(2);
|
||||
String format = "yyyy-MM-dd'T'HH:mm:ss";
|
||||
if (fractionalSeconds != null && !fractionalSeconds.isEmpty())
|
||||
{
|
||||
format += ".SSS";
|
||||
dateString += fractionalSeconds;
|
||||
}
|
||||
format += "Z";
|
||||
if ("Z".equals(zone))
|
||||
{
|
||||
dateString += "+0000";
|
||||
}
|
||||
else if (zone.contains(":"))
|
||||
{
|
||||
dateString += zone.replace(":", "");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
||||
dateFormat.setLenient(false);
|
||||
return dateFormat.parse(dateString);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), s, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof Date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
DateFormat formatter = getFormatter(context.getDatePolicy());
|
||||
context.write(formatter.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private DateFormat getFormatter(DatePolicy datePolicy)
|
||||
{
|
||||
boolean utc = "UTC".equals(datePolicy.getTimeZone().getID());
|
||||
String format;
|
||||
|
||||
if (utc && datePolicy.isShowFractionalSeconds())
|
||||
{
|
||||
format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
|
||||
}
|
||||
else if (utc)
|
||||
{
|
||||
format = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||
}
|
||||
else if (datePolicy.isShowFractionalSeconds())
|
||||
{
|
||||
format = getTimeZoneAndFractionalSecondsFormat();
|
||||
}
|
||||
else
|
||||
{
|
||||
format = getTimeZoneFormat();
|
||||
}
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(format);
|
||||
formatter.setTimeZone(datePolicy.getTimeZone());
|
||||
|
||||
return formatter;
|
||||
}
|
||||
|
||||
String getTimeZoneFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ssXXX";
|
||||
}
|
||||
|
||||
String getTimeZoneAndFractionalSecondsFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
|
||||
}
|
||||
|
||||
private DateValueReaderWriter()
|
||||
{
|
||||
}
|
||||
|
||||
private static class DateConverterJdk6 extends DateValueReaderWriter
|
||||
{
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
DateFormat formatter = super.getFormatter(context.getDatePolicy());
|
||||
String date = formatter.format(value);
|
||||
|
||||
if ("UTC".equals(context.getDatePolicy().getTimeZone().getID()))
|
||||
{
|
||||
context.write(date);
|
||||
}
|
||||
else
|
||||
{
|
||||
int insertionIndex = date.length() - 2;
|
||||
context.write(date.substring(0, insertionIndex)).write(':').write(date.substring(insertionIndex));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
String getTimeZoneAndFractionalSecondsFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
|
||||
}
|
||||
|
||||
@Override
|
||||
String getTimeZoneFormat()
|
||||
{
|
||||
return "yyyy-MM-dd'T'HH:mm:ssZ";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "datetime";
|
||||
}
|
||||
}
|
||||
@@ -1,341 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
public class Identifier
|
||||
{
|
||||
|
||||
static final Identifier INVALID = new Identifier("", null);
|
||||
|
||||
private static final String ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-";
|
||||
|
||||
private final String name;
|
||||
private final Type type;
|
||||
|
||||
static Identifier from(String name, Context context)
|
||||
{
|
||||
Type type;
|
||||
boolean valid;
|
||||
name = name.trim();
|
||||
if (name.startsWith("[["))
|
||||
{
|
||||
type = Type.TABLE_ARRAY;
|
||||
valid = isValidTableArray(name, context);
|
||||
}
|
||||
else if (name.startsWith("["))
|
||||
{
|
||||
type = Type.TABLE;
|
||||
valid = isValidTable(name, context);
|
||||
}
|
||||
else
|
||||
{
|
||||
type = Type.KEY;
|
||||
valid = isValidKey(name, context);
|
||||
}
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
return Identifier.INVALID;
|
||||
}
|
||||
|
||||
return new Identifier(extractName(name), type);
|
||||
}
|
||||
|
||||
private Identifier(String name, Type type)
|
||||
{
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
String getBareName()
|
||||
{
|
||||
if (isKey())
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
if (isTable())
|
||||
{
|
||||
return name.substring(1, name.length() - 1);
|
||||
}
|
||||
|
||||
return name.substring(2, name.length() - 2);
|
||||
}
|
||||
|
||||
boolean isKey()
|
||||
{
|
||||
return type == Type.KEY;
|
||||
}
|
||||
|
||||
boolean isTable()
|
||||
{
|
||||
return type == Type.TABLE;
|
||||
}
|
||||
|
||||
boolean isTableArray()
|
||||
{
|
||||
return type == Type.TABLE_ARRAY;
|
||||
}
|
||||
|
||||
private enum Type
|
||||
{
|
||||
KEY, TABLE, TABLE_ARRAY
|
||||
}
|
||||
|
||||
private static String extractName(String raw)
|
||||
{
|
||||
boolean quoted = false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < raw.length(); i++)
|
||||
{
|
||||
char c = raw.charAt(i);
|
||||
if (c == '"' && (i == 0 || raw.charAt(i - 1) != '\\'))
|
||||
{
|
||||
quoted = !quoted;
|
||||
sb.append('"');
|
||||
}
|
||||
else if (quoted || !Character.isWhitespace(c))
|
||||
{
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
return StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(sb.toString());
|
||||
}
|
||||
|
||||
private static boolean isValidKey(String name, Context context)
|
||||
{
|
||||
if (name.trim().isEmpty())
|
||||
{
|
||||
context.errors.invalidKey(name, context.line.get());
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean quoted = false;
|
||||
for (int i = 0; i < name.length(); i++)
|
||||
{
|
||||
char c = name.charAt(i);
|
||||
|
||||
if (c == '"' && (i == 0 || name.charAt(i - 1) != '\\'))
|
||||
{
|
||||
if (!quoted && i > 0 && name.charAt(i - 1) != '.')
|
||||
{
|
||||
context.errors.invalidKey(name, context.line.get());
|
||||
return false;
|
||||
}
|
||||
quoted = !quoted;
|
||||
}
|
||||
else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1))
|
||||
{
|
||||
context.errors.invalidKey(name, context.line.get());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isValidTable(String name, Context context)
|
||||
{
|
||||
boolean valid = name.endsWith("]");
|
||||
|
||||
String trimmed = name.substring(1, name.length() - 1).trim();
|
||||
if (trimmed.isEmpty() || trimmed.charAt(0) == '.' || trimmed.endsWith("."))
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
context.errors.invalidTable(name, context.line.get());
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean quoted = false;
|
||||
boolean dotAllowed = false;
|
||||
boolean quoteAllowed = true;
|
||||
boolean charAllowed = true;
|
||||
|
||||
for (int i = 0; i < trimmed.length(); i++)
|
||||
{
|
||||
char c = trimmed.charAt(i);
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (Keys.isQuote(c))
|
||||
{
|
||||
if (!quoteAllowed)
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
else if (quoted && trimmed.charAt(i - 1) != '\\')
|
||||
{
|
||||
charAllowed = false;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
}
|
||||
else if (!quoted)
|
||||
{
|
||||
quoted = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
}
|
||||
else if (quoted)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (c == '.')
|
||||
{
|
||||
if (dotAllowed)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = false;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
context.errors.emptyImplicitTable(name, context.line.get());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (Character.isWhitespace(c))
|
||||
{
|
||||
char prev = trimmed.charAt(i - 1);
|
||||
if (!Character.isWhitespace(prev) && prev != '.')
|
||||
{
|
||||
charAllowed = false;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (charAllowed && ALLOWED_CHARS.indexOf(c) > -1)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
context.errors.invalidTable(name, context.line.get());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isValidTableArray(String line, Context context)
|
||||
{
|
||||
boolean valid = line.endsWith("]]");
|
||||
|
||||
String trimmed = line.substring(2, line.length() - 2).trim();
|
||||
if (trimmed.isEmpty() || trimmed.charAt(0) == '.' || trimmed.endsWith("."))
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
context.errors.invalidTableArray(line, context.line.get());
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean quoted = false;
|
||||
boolean dotAllowed = false;
|
||||
boolean quoteAllowed = true;
|
||||
boolean charAllowed = true;
|
||||
|
||||
for (int i = 0; i < trimmed.length(); i++)
|
||||
{
|
||||
char c = trimmed.charAt(i);
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '"')
|
||||
{
|
||||
if (!quoteAllowed)
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
else if (quoted && trimmed.charAt(i - 1) != '\\')
|
||||
{
|
||||
charAllowed = false;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
}
|
||||
else if (!quoted)
|
||||
{
|
||||
quoted = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
}
|
||||
else if (quoted)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (c == '.')
|
||||
{
|
||||
if (dotAllowed)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = false;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
context.errors.emptyImplicitTable(line, context.line.get());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (Character.isWhitespace(c))
|
||||
{
|
||||
char prev = trimmed.charAt(i - 1);
|
||||
if (!Character.isWhitespace(prev) && prev != '.' && prev != '"')
|
||||
{
|
||||
charAllowed = false;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (charAllowed && ALLOWED_CHARS.indexOf(c) > -1)
|
||||
{
|
||||
charAllowed = true;
|
||||
dotAllowed = true;
|
||||
quoteAllowed = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
context.errors.invalidTableArray(line, context.line.get());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class IdentifierConverter
|
||||
{
|
||||
|
||||
static final IdentifierConverter IDENTIFIER_CONVERTER = new IdentifierConverter();
|
||||
|
||||
Identifier convert(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
boolean quoted = false;
|
||||
StringBuilder name = new StringBuilder();
|
||||
boolean terminated = false;
|
||||
boolean isKey = s.charAt(index.get()) != '[';
|
||||
boolean isTableArray = !isKey && s.length() > index.get() + 1 && s.charAt(index.get() + 1) == '[';
|
||||
boolean inComment = false;
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
if (Keys.isQuote(c) && (i == 0 || s.charAt(i - 1) != '\\'))
|
||||
{
|
||||
quoted = !quoted;
|
||||
name.append(c);
|
||||
}
|
||||
else if (c == '\n')
|
||||
{
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
else if (quoted)
|
||||
{
|
||||
name.append(c);
|
||||
}
|
||||
else if (c == '=' && isKey)
|
||||
{
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
else if (c == ']' && !isKey)
|
||||
{
|
||||
if (!isTableArray || s.length() > index.get() + 1 && s.charAt(index.get() + 1) == ']')
|
||||
{
|
||||
terminated = true;
|
||||
name.append(']');
|
||||
if (isTableArray)
|
||||
{
|
||||
name.append(']');
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (terminated && c == '#')
|
||||
{
|
||||
inComment = true;
|
||||
}
|
||||
else if (terminated && !Character.isWhitespace(c) && !inComment)
|
||||
{
|
||||
terminated = false;
|
||||
break;
|
||||
}
|
||||
else if (!terminated)
|
||||
{
|
||||
name.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
if (!terminated)
|
||||
{
|
||||
if (isKey)
|
||||
{
|
||||
context.errors.unterminatedKey(name.toString(), context.line.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
context.errors.invalidKey(name.toString(), context.line.get());
|
||||
}
|
||||
|
||||
return Identifier.INVALID;
|
||||
}
|
||||
|
||||
return Identifier.from(name.toString(), context);
|
||||
}
|
||||
|
||||
private IdentifierConverter()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
/**
|
||||
* Controls how a {@link TomlWriter} indents tables and key/value pairs.
|
||||
* <p>
|
||||
* The default policy is to not indent.
|
||||
*/
|
||||
public class IndentationPolicy
|
||||
{
|
||||
private final int tableIndent;
|
||||
private final int keyValueIndent;
|
||||
private final int arrayDelimiterPadding;
|
||||
|
||||
IndentationPolicy(int keyIndentation, int tableIndentation, int arrayDelimiterPadding)
|
||||
{
|
||||
this.keyValueIndent = keyIndentation;
|
||||
this.tableIndent = tableIndentation;
|
||||
this.arrayDelimiterPadding = arrayDelimiterPadding;
|
||||
}
|
||||
|
||||
int getTableIndent()
|
||||
{
|
||||
return tableIndent;
|
||||
}
|
||||
|
||||
int getKeyValueIndent()
|
||||
{
|
||||
return keyValueIndent;
|
||||
}
|
||||
|
||||
int getArrayDelimiterPadding()
|
||||
{
|
||||
return arrayDelimiterPadding;
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class InlineTableValueReader implements dev.plex.toml.ValueReader
|
||||
{
|
||||
|
||||
static final InlineTableValueReader INLINE_TABLE_VALUE_READER = new InlineTableValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("{");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger sharedIndex, dev.plex.toml.Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int startIndex = sharedIndex.get();
|
||||
boolean inKey = true;
|
||||
boolean inValue = false;
|
||||
boolean terminated = false;
|
||||
StringBuilder currentKey = new StringBuilder();
|
||||
HashMap<String, Object> results = new HashMap<String, Object>();
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
|
||||
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (inValue && !Character.isWhitespace(c))
|
||||
{
|
||||
Object converted = dev.plex.toml.ValueReaders.VALUE_READERS.convert(s, sharedIndex, context.with(dev.plex.toml.Identifier.from(currentKey.toString(), context)));
|
||||
|
||||
if (converted instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
errors.add((dev.plex.toml.Results.Errors) converted);
|
||||
return errors;
|
||||
}
|
||||
|
||||
String currentKeyTrimmed = currentKey.toString().trim();
|
||||
Object previous = results.put(currentKeyTrimmed, converted);
|
||||
|
||||
if (previous != null)
|
||||
{
|
||||
errors.duplicateKey(currentKeyTrimmed, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
currentKey = new StringBuilder();
|
||||
inValue = false;
|
||||
}
|
||||
else if (c == ',')
|
||||
{
|
||||
inKey = true;
|
||||
inValue = false;
|
||||
currentKey = new StringBuilder();
|
||||
}
|
||||
else if (c == '=')
|
||||
{
|
||||
inKey = false;
|
||||
inValue = true;
|
||||
}
|
||||
else if (c == '}')
|
||||
{
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
else if (inKey)
|
||||
{
|
||||
currentKey.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
if (!terminated)
|
||||
{
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex), startLine);
|
||||
}
|
||||
|
||||
if (errors.hasErrors())
|
||||
{
|
||||
return errors;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private InlineTableValueReader()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Keys
|
||||
{
|
||||
|
||||
static class Key
|
||||
{
|
||||
final String name;
|
||||
final int index;
|
||||
final String path;
|
||||
|
||||
Key(String name, int index, Key next)
|
||||
{
|
||||
this.name = name;
|
||||
this.index = index;
|
||||
if (next != null)
|
||||
{
|
||||
this.path = name + "." + next.path;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.path = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Key[] split(String key)
|
||||
{
|
||||
List<Key> splitKey = new ArrayList<Key>();
|
||||
StringBuilder current = new StringBuilder();
|
||||
boolean quoted = false;
|
||||
boolean indexable = true;
|
||||
boolean inIndex = false;
|
||||
int index = -1;
|
||||
|
||||
for (int i = key.length() - 1; i > -1; i--)
|
||||
{
|
||||
char c = key.charAt(i);
|
||||
if (c == ']' && indexable)
|
||||
{
|
||||
inIndex = true;
|
||||
continue;
|
||||
}
|
||||
indexable = false;
|
||||
if (c == '[' && inIndex)
|
||||
{
|
||||
inIndex = false;
|
||||
index = Integer.parseInt(current.toString());
|
||||
current = new StringBuilder();
|
||||
continue;
|
||||
}
|
||||
if (isQuote(c) && (i == 0 || key.charAt(i - 1) != '\\'))
|
||||
{
|
||||
quoted = !quoted;
|
||||
indexable = false;
|
||||
}
|
||||
if (c != '.' || quoted)
|
||||
{
|
||||
current.insert(0, c);
|
||||
}
|
||||
else
|
||||
{
|
||||
splitKey.add(0, new Key(current.toString(), index, !splitKey.isEmpty() ? splitKey.get(0) : null));
|
||||
indexable = true;
|
||||
index = -1;
|
||||
current = new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
splitKey.add(0, new Key(current.toString(), index, !splitKey.isEmpty() ? splitKey.get(0) : null));
|
||||
|
||||
return splitKey.toArray(new Key[0]);
|
||||
}
|
||||
|
||||
static boolean isQuote(char c)
|
||||
{
|
||||
return c == '"' || c == '\'';
|
||||
}
|
||||
|
||||
private Keys()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class LiteralStringValueReader implements ValueReader
|
||||
{
|
||||
public static final LiteralStringValueReader LITERAL_STRING_VALUE_READER = new LiteralStringValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
int startLine = context.line.get();
|
||||
boolean terminated = false;
|
||||
int startIndex = index.incrementAndGet();
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '\'')
|
||||
{
|
||||
terminated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!terminated)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex), startLine);
|
||||
return errors;
|
||||
}
|
||||
|
||||
String substring = s.substring(startIndex, index.get());
|
||||
|
||||
return substring;
|
||||
}
|
||||
|
||||
private LiteralStringValueReader()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class MapValueWriter implements dev.plex.toml.ValueWriter
|
||||
{
|
||||
static final dev.plex.toml.ValueWriter MAP_VALUE_WRITER = new MapValueWriter();
|
||||
|
||||
private static final Pattern REQUIRED_QUOTING_PATTERN = Pattern.compile("^.*[^A-Za-z\\d_-].*$");
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof Map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
File file = null;
|
||||
if (context.file != null)
|
||||
{
|
||||
file = context.file;
|
||||
}
|
||||
|
||||
Map<?, ?> from = (Map<?, ?>) value;
|
||||
|
||||
Toml toml = null;
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
toml = new Toml().read(file);
|
||||
}
|
||||
|
||||
if (hasPrimitiveValues(from, context))
|
||||
{
|
||||
if (context.hasRun)
|
||||
{
|
||||
if (toml != null)
|
||||
{
|
||||
if (!toml.getValues().containsKey(context.key))
|
||||
{
|
||||
context.writeKey();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
context.writeKey();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Render primitive types and arrays of primitive first so they are
|
||||
// grouped under the same table (if there is one)
|
||||
for (Map.Entry<?, ?> entry : from.entrySet())
|
||||
{
|
||||
Object key = entry.getKey();
|
||||
Object fromValue = entry.getValue();
|
||||
if (fromValue == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (context.hasRun && toml != null)
|
||||
{
|
||||
if (context.key != null)
|
||||
{
|
||||
if (key.toString().equalsIgnoreCase(context.key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (toml.contains(context.key + "." + key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dev.plex.toml.ValueWriter valueWriter = dev.plex.toml.ValueWriters.WRITERS.findWriterFor(fromValue);
|
||||
if (valueWriter.isPrimitiveType())
|
||||
{
|
||||
context.indent();
|
||||
context.write(quoteKey(key)).write(" = ");
|
||||
valueWriter.write(fromValue, context);
|
||||
context.write('\n');
|
||||
}
|
||||
else if (valueWriter == dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER)
|
||||
{
|
||||
context.indent();
|
||||
context.setArrayKey(key.toString());
|
||||
context.write(quoteKey(key)).write(" = ");
|
||||
valueWriter.write(fromValue, context);
|
||||
context.write('\n');
|
||||
}
|
||||
}
|
||||
|
||||
// Now render (sub)tables and arrays of tables
|
||||
for (Object key : from.keySet())
|
||||
{
|
||||
Object fromValue = from.get(key);
|
||||
if (fromValue == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (context.hasRun && toml != null)
|
||||
{
|
||||
if (context.key != null)
|
||||
{
|
||||
if (key.toString().equalsIgnoreCase(context.key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (toml.contains(context.key + "." + key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dev.plex.toml.ValueWriter valueWriter = dev.plex.toml.ValueWriters.WRITERS.findWriterFor(fromValue);
|
||||
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));
|
||||
context1.parentName = key.toString();
|
||||
context1.hasRun = true;
|
||||
context1.file = context.file;
|
||||
valueWriter.write(fromValue, context1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String quoteKey(Object key)
|
||||
{
|
||||
String stringKey = key.toString();
|
||||
Matcher matcher = REQUIRED_QUOTING_PATTERN.matcher(stringKey);
|
||||
if (matcher.matches())
|
||||
{
|
||||
stringKey = "\"" + stringKey + "\"";
|
||||
}
|
||||
|
||||
return stringKey;
|
||||
}
|
||||
|
||||
private static boolean hasPrimitiveValues(Map<?, ?> values, WriterContext context)
|
||||
{
|
||||
for (Object key : values.keySet())
|
||||
{
|
||||
Object fromValue = values.get(key);
|
||||
if (fromValue == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
dev.plex.toml.ValueWriter valueWriter = dev.plex.toml.ValueWriters.WRITERS.findWriterFor(fromValue);
|
||||
if (valueWriter.isPrimitiveType() || valueWriter == dev.plex.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private MapValueWriter()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class MultilineLiteralStringValueReader implements ValueReader
|
||||
{
|
||||
|
||||
static final MultilineLiteralStringValueReader MULTILINE_LITERAL_STRING_VALUE_READER = new MultilineLiteralStringValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("'''");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int originalStartIndex = index.get();
|
||||
int startIndex = index.addAndGet(3);
|
||||
int endIndex = -1;
|
||||
|
||||
if (s.charAt(startIndex) == '\n')
|
||||
{
|
||||
startIndex = index.incrementAndGet();
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
for (int i = startIndex; i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
if (c == '\'' && s.length() > i + 2 && s.charAt(i + 1) == '\'' && s.charAt(i + 2) == '\'')
|
||||
{
|
||||
endIndex = i;
|
||||
index.addAndGet(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
|
||||
return errors;
|
||||
}
|
||||
|
||||
return s.substring(startIndex, endIndex);
|
||||
}
|
||||
|
||||
private MultilineLiteralStringValueReader()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class MultilineStringValueReader implements ValueReader
|
||||
{
|
||||
|
||||
static final MultilineStringValueReader MULTILINE_STRING_VALUE_READER = new MultilineStringValueReader();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("\"\"\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
AtomicInteger line = context.line;
|
||||
int startLine = line.get();
|
||||
int originalStartIndex = index.get();
|
||||
int startIndex = index.addAndGet(3);
|
||||
int endIndex = -1;
|
||||
|
||||
if (s.charAt(startIndex) == '\n')
|
||||
{
|
||||
startIndex = index.incrementAndGet();
|
||||
line.incrementAndGet();
|
||||
}
|
||||
|
||||
for (int i = startIndex; i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
line.incrementAndGet();
|
||||
}
|
||||
else if (c == '"' && s.length() > i + 2 && s.charAt(i + 1) == '"' && s.charAt(i + 2) == '"')
|
||||
{
|
||||
endIndex = i;
|
||||
index.addAndGet(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1)
|
||||
{
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(originalStartIndex), startLine);
|
||||
return errors;
|
||||
}
|
||||
|
||||
s = s.substring(startIndex, endIndex);
|
||||
s = s.replaceAll("\\\\\\s+", "");
|
||||
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(s);
|
||||
s = dev.plex.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceSpecialCharacters(s);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
private MultilineStringValueReader()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class NumberValueReaderWriter implements dev.plex.toml.ValueReader, dev.plex.toml.ValueWriter
|
||||
{
|
||||
static final NumberValueReaderWriter NUMBER_VALUE_READER_WRITER = new NumberValueReaderWriter();
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
char firstChar = s.charAt(0);
|
||||
|
||||
return firstChar == '+' || firstChar == '-' || Character.isDigit(firstChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, dev.plex.toml.Context context)
|
||||
{
|
||||
boolean signable = true;
|
||||
boolean dottable = false;
|
||||
boolean exponentable = false;
|
||||
boolean terminatable = false;
|
||||
boolean underscorable = false;
|
||||
String type = "";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
boolean notLastChar = s.length() > i + 1;
|
||||
|
||||
if (Character.isDigit(c))
|
||||
{
|
||||
sb.append(c);
|
||||
signable = false;
|
||||
terminatable = true;
|
||||
if (type.isEmpty())
|
||||
{
|
||||
type = "integer";
|
||||
dottable = true;
|
||||
}
|
||||
underscorable = notLastChar;
|
||||
exponentable = !type.equals("exponent");
|
||||
}
|
||||
else if ((c == '+' || c == '-') && signable && notLastChar)
|
||||
{
|
||||
signable = false;
|
||||
terminatable = false;
|
||||
if (c == '-')
|
||||
{
|
||||
sb.append('-');
|
||||
}
|
||||
}
|
||||
else if (c == '.' && dottable && notLastChar)
|
||||
{
|
||||
sb.append('.');
|
||||
type = "float";
|
||||
terminatable = false;
|
||||
dottable = false;
|
||||
exponentable = false;
|
||||
underscorable = false;
|
||||
}
|
||||
else if ((c == 'E' || c == 'e') && exponentable && notLastChar)
|
||||
{
|
||||
sb.append('E');
|
||||
type = "exponent";
|
||||
terminatable = false;
|
||||
signable = true;
|
||||
dottable = false;
|
||||
exponentable = false;
|
||||
underscorable = false;
|
||||
}
|
||||
else if (c == '_' && underscorable && notLastChar && Character.isDigit(s.charAt(i + 1)))
|
||||
{
|
||||
underscorable = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!terminatable)
|
||||
{
|
||||
type = "";
|
||||
}
|
||||
index.decrementAndGet();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type.equals("integer"))
|
||||
{
|
||||
return Long.valueOf(sb.toString());
|
||||
}
|
||||
else if (type.equals("float"))
|
||||
{
|
||||
return Double.valueOf(sb.toString());
|
||||
}
|
||||
else if (type.equals("exponent"))
|
||||
{
|
||||
String[] exponentString = sb.toString().split("E");
|
||||
|
||||
return Double.parseDouble(exponentString[0]) * Math.pow(10, Double.parseDouble(exponentString[1]));
|
||||
}
|
||||
else
|
||||
{
|
||||
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), sb.toString(), context.line.get());
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof Number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, dev.plex.toml.WriterContext context)
|
||||
{
|
||||
context.write(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "number";
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
class ObjectValueWriter implements ValueWriter
|
||||
{
|
||||
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)
|
||||
{
|
||||
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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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 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()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
class PrimitiveArrayValueWriter extends ArrayValueWriter
|
||||
{
|
||||
static final ValueWriter PRIMITIVE_ARRAY_VALUE_WRITER = new PrimitiveArrayValueWriter();
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return isArrayish(value) && isArrayOfPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object o, WriterContext context)
|
||||
{
|
||||
Collection<?> values = normalize(o);
|
||||
|
||||
context.write('[');
|
||||
context.writeArrayDelimiterPadding();
|
||||
|
||||
boolean first = true;
|
||||
ValueWriter firstWriter = null;
|
||||
|
||||
for (Object value : values)
|
||||
{
|
||||
if (first)
|
||||
{
|
||||
firstWriter = ValueWriters.WRITERS.findWriterFor(value);
|
||||
first = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
ValueWriter writer = ValueWriters.WRITERS.findWriterFor(value);
|
||||
if (writer != firstWriter)
|
||||
{
|
||||
throw new IllegalStateException(
|
||||
context.getContextPath() +
|
||||
": cannot write a heterogeneous array; first element was of type " + firstWriter +
|
||||
" but found " + writer
|
||||
);
|
||||
}
|
||||
context.write(", ");
|
||||
}
|
||||
|
||||
ValueWriters.WRITERS.findWriterFor(value).write(value, context);
|
||||
}
|
||||
|
||||
context.writeArrayDelimiterPadding();
|
||||
context.write(']');
|
||||
}
|
||||
|
||||
private PrimitiveArrayValueWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "primitive-array";
|
||||
}
|
||||
}
|
||||
@@ -1,361 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class Results
|
||||
{
|
||||
|
||||
static class Errors
|
||||
{
|
||||
|
||||
private final StringBuilder sb = new StringBuilder();
|
||||
|
||||
void duplicateTable(String table, int line)
|
||||
{
|
||||
sb.append("Duplicate table definition on line ")
|
||||
.append(line)
|
||||
.append(": [")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
|
||||
public void tableDuplicatesKey(String table, AtomicInteger line)
|
||||
{
|
||||
sb.append("Key already exists for table defined on line ")
|
||||
.append(line.get())
|
||||
.append(": [")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
|
||||
public void keyDuplicatesTable(String key, AtomicInteger line)
|
||||
{
|
||||
sb.append("Table already exists for key defined on line ")
|
||||
.append(line.get())
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void emptyImplicitTable(String table, int line)
|
||||
{
|
||||
sb.append("Invalid table definition due to empty implicit table name: ")
|
||||
.append(table);
|
||||
}
|
||||
|
||||
void invalidTable(String table, int line)
|
||||
{
|
||||
sb.append("Invalid table definition on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(table)
|
||||
.append("]");
|
||||
}
|
||||
|
||||
void duplicateKey(String key, int line)
|
||||
{
|
||||
sb.append("Duplicate key");
|
||||
if (line > -1)
|
||||
{
|
||||
sb.append(" on line ")
|
||||
.append(line);
|
||||
}
|
||||
sb.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void invalidTextAfterIdentifier(dev.plex.toml.Identifier identifier, char text, int line)
|
||||
{
|
||||
sb.append("Invalid text after key ")
|
||||
.append(identifier.getName())
|
||||
.append(" on line ")
|
||||
.append(line)
|
||||
.append(". Make sure to terminate the value or add a comment (#).");
|
||||
}
|
||||
|
||||
void invalidKey(String key, int line)
|
||||
{
|
||||
sb.append("Invalid key on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void invalidTableArray(String tableArray, int line)
|
||||
{
|
||||
sb.append("Invalid table array definition on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(tableArray);
|
||||
}
|
||||
|
||||
void invalidValue(String key, String value, int line)
|
||||
{
|
||||
sb.append("Invalid value on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key)
|
||||
.append(" = ")
|
||||
.append(value);
|
||||
}
|
||||
|
||||
void unterminatedKey(String key, int line)
|
||||
{
|
||||
sb.append("Key is not followed by an equals sign on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key);
|
||||
}
|
||||
|
||||
void unterminated(String key, String value, int line)
|
||||
{
|
||||
sb.append("Unterminated value on line ")
|
||||
.append(line)
|
||||
.append(": ")
|
||||
.append(key)
|
||||
.append(" = ")
|
||||
.append(value.trim());
|
||||
}
|
||||
|
||||
public void heterogenous(String key, int line)
|
||||
{
|
||||
sb.append(key)
|
||||
.append(" becomes a heterogeneous array on line ")
|
||||
.append(line);
|
||||
}
|
||||
|
||||
boolean hasErrors()
|
||||
{
|
||||
return sb.length() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void add(Errors other)
|
||||
{
|
||||
sb.append(other.sb);
|
||||
}
|
||||
}
|
||||
|
||||
final Errors errors = new Errors();
|
||||
private final Set<String> tables = new HashSet<String>();
|
||||
private final Deque<Container> stack = new ArrayDeque<Container>();
|
||||
|
||||
Results()
|
||||
{
|
||||
stack.push(new Container.Table(""));
|
||||
}
|
||||
|
||||
void addValue(String key, Object value, AtomicInteger line)
|
||||
{
|
||||
Container currentTable = stack.peek();
|
||||
|
||||
if (value instanceof Map)
|
||||
{
|
||||
String path = getInlineTablePath(key);
|
||||
if (path == null)
|
||||
{
|
||||
startTable(key, line);
|
||||
}
|
||||
else if (path.isEmpty())
|
||||
{
|
||||
startTables(dev.plex.toml.Identifier.from(key, null), line);
|
||||
}
|
||||
else
|
||||
{
|
||||
startTables(dev.plex.toml.Identifier.from(path, null), line);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> valueMap = (Map<String, Object>) value;
|
||||
for (Map.Entry<String, Object> entry : valueMap.entrySet())
|
||||
{
|
||||
addValue(entry.getKey(), entry.getValue(), line);
|
||||
}
|
||||
stack.pop();
|
||||
}
|
||||
else if (currentTable.accepts(key))
|
||||
{
|
||||
currentTable.put(key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentTable.get(key) instanceof Container)
|
||||
{
|
||||
errors.keyDuplicatesTable(key, line);
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.duplicateKey(key, line != null ? line.get() : -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startTableArray(dev.plex.toml.Identifier identifier, AtomicInteger line)
|
||||
{
|
||||
String tableName = identifier.getBareName();
|
||||
while (stack.size() > 1)
|
||||
{
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
|
||||
for (int i = 0; i < tableParts.length; i++)
|
||||
{
|
||||
String tablePart = tableParts[i].name;
|
||||
Container currentContainer = stack.peek();
|
||||
|
||||
if (currentContainer.get(tablePart) instanceof Container.TableArray)
|
||||
{
|
||||
Container.TableArray currentTableArray = (Container.TableArray) currentContainer.get(tablePart);
|
||||
stack.push(currentTableArray);
|
||||
|
||||
if (i == tableParts.length - 1)
|
||||
{
|
||||
currentTableArray.put(tablePart, new Container.Table());
|
||||
}
|
||||
|
||||
stack.push(currentTableArray.getCurrent());
|
||||
currentContainer = stack.peek();
|
||||
}
|
||||
else if (currentContainer.get(tablePart) instanceof Container.Table && i < tableParts.length - 1)
|
||||
{
|
||||
Container nextTable = (Container) currentContainer.get(tablePart);
|
||||
stack.push(nextTable);
|
||||
}
|
||||
else if (currentContainer.accepts(tablePart))
|
||||
{
|
||||
Container newContainer = i == tableParts.length - 1 ? new Container.TableArray() : new Container.Table();
|
||||
addValue(tablePart, newContainer, line);
|
||||
stack.push(newContainer);
|
||||
|
||||
if (newContainer instanceof Container.TableArray)
|
||||
{
|
||||
stack.push(((Container.TableArray) newContainer).getCurrent());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.duplicateTable(tableName, line.get());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void startTables(dev.plex.toml.Identifier id, AtomicInteger line)
|
||||
{
|
||||
String tableName = id.getBareName();
|
||||
|
||||
while (stack.size() > 1)
|
||||
{
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
dev.plex.toml.Keys.Key[] tableParts = dev.plex.toml.Keys.split(tableName);
|
||||
for (int i = 0; i < tableParts.length; i++)
|
||||
{
|
||||
String tablePart = tableParts[i].name;
|
||||
Container currentContainer = stack.peek();
|
||||
if (currentContainer.get(tablePart) instanceof Container)
|
||||
{
|
||||
Container nextTable = (Container) currentContainer.get(tablePart);
|
||||
if (i == tableParts.length - 1 && !nextTable.isImplicit())
|
||||
{
|
||||
errors.duplicateTable(tableName, line.get());
|
||||
return;
|
||||
}
|
||||
stack.push(nextTable);
|
||||
if (stack.peek() instanceof Container.TableArray)
|
||||
{
|
||||
stack.push(((Container.TableArray) stack.peek()).getCurrent());
|
||||
}
|
||||
}
|
||||
else if (currentContainer.accepts(tablePart))
|
||||
{
|
||||
startTable(tablePart, i < tableParts.length - 1, line);
|
||||
}
|
||||
else
|
||||
{
|
||||
errors.tableDuplicatesKey(tablePart, line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning: After this method has been called, this instance is no longer usable.
|
||||
*/
|
||||
Map<String, Object> consume()
|
||||
{
|
||||
Container values = stack.getLast();
|
||||
stack.clear();
|
||||
|
||||
return ((Container.Table) values).consume();
|
||||
}
|
||||
|
||||
private Container startTable(String tableName, AtomicInteger line)
|
||||
{
|
||||
Container newTable = new Container.Table(tableName);
|
||||
addValue(tableName, newTable, line);
|
||||
stack.push(newTable);
|
||||
|
||||
return newTable;
|
||||
}
|
||||
|
||||
private Container startTable(String tableName, boolean implicit, AtomicInteger line)
|
||||
{
|
||||
Container newTable = new Container.Table(tableName, implicit);
|
||||
addValue(tableName, newTable, line);
|
||||
stack.push(newTable);
|
||||
|
||||
return newTable;
|
||||
}
|
||||
|
||||
private String getInlineTablePath(String key)
|
||||
{
|
||||
Iterator<Container> descendingIterator = stack.descendingIterator();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
while (descendingIterator.hasNext())
|
||||
{
|
||||
Container next = descendingIterator.next();
|
||||
if (next instanceof Container.TableArray)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Container.Table table = (Container.Table) next;
|
||||
|
||||
if (table.name == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (sb.length() > 0)
|
||||
{
|
||||
sb.append('.');
|
||||
}
|
||||
|
||||
sb.append(table.name);
|
||||
}
|
||||
|
||||
if (sb.length() > 0)
|
||||
{
|
||||
sb.append('.');
|
||||
}
|
||||
|
||||
sb.append(key)
|
||||
.insert(0, '[')
|
||||
.append(']');
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class StringValueReaderWriter implements ValueReader, ValueWriter
|
||||
{
|
||||
|
||||
static final StringValueReaderWriter STRING_VALUE_READER_WRITER = new StringValueReaderWriter();
|
||||
private static final Pattern UNICODE_REGEX = Pattern.compile("\\\\[uU](.{4})");
|
||||
|
||||
static private final String[] specialCharacterEscapes = new String[93];
|
||||
|
||||
static
|
||||
{
|
||||
specialCharacterEscapes['\b'] = "\\b";
|
||||
specialCharacterEscapes['\t'] = "\\t";
|
||||
specialCharacterEscapes['\n'] = "\\n";
|
||||
specialCharacterEscapes['\f'] = "\\f";
|
||||
specialCharacterEscapes['\r'] = "\\r";
|
||||
specialCharacterEscapes['"'] = "\\\"";
|
||||
specialCharacterEscapes['\\'] = "\\\\";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRead(String s)
|
||||
{
|
||||
return s.startsWith("\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(String s, AtomicInteger index, Context context)
|
||||
{
|
||||
int startIndex = index.incrementAndGet();
|
||||
int endIndex = -1;
|
||||
|
||||
for (int i = index.get(); i < s.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char ch = s.charAt(i);
|
||||
if (ch == '"' && s.charAt(i - 1) != '\\')
|
||||
{
|
||||
endIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endIndex == -1)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.unterminated(context.identifier.getName(), s.substring(startIndex - 1), context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
String raw = s.substring(startIndex, endIndex);
|
||||
s = replaceUnicodeCharacters(raw);
|
||||
s = replaceSpecialCharacters(s);
|
||||
|
||||
if (s == null)
|
||||
{
|
||||
Results.Errors errors = new Results.Errors();
|
||||
errors.invalidValue(context.identifier.getName(), raw, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
String replaceUnicodeCharacters(String value)
|
||||
{
|
||||
Matcher unicodeMatcher = UNICODE_REGEX.matcher(value);
|
||||
|
||||
while (unicodeMatcher.find())
|
||||
{
|
||||
value = value.replace(unicodeMatcher.group(), new String(Character.toChars(Integer.parseInt(unicodeMatcher.group(1), 16))));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
String replaceSpecialCharacters(String s)
|
||||
{
|
||||
for (int i = 0; i < s.length() - 1; i++)
|
||||
{
|
||||
char ch = s.charAt(i);
|
||||
char next = s.charAt(i + 1);
|
||||
|
||||
if (ch == '\\' && next == '\\')
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else if (ch == '\\' && !(next == 'b' || next == 'f' || next == 'n' || next == 't' || next == 'r' || next == '"' || next == '\\'))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return s.replace("\\n", "\n")
|
||||
.replace("\\\"", "\"")
|
||||
.replace("\\t", "\t")
|
||||
.replace("\\r", "\r")
|
||||
.replace("\\\\", "\\")
|
||||
.replace("\\/", "/")
|
||||
.replace("\\b", "\b")
|
||||
.replace("\\f", "\f");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return value instanceof String || value instanceof Character || value instanceof URL || value instanceof URI || value instanceof Enum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context)
|
||||
{
|
||||
context.write('"');
|
||||
escapeUnicode(value.toString(), context);
|
||||
context.write('"');
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private void escapeUnicode(String in, WriterContext context)
|
||||
{
|
||||
for (int i = 0; i < in.length(); i++)
|
||||
{
|
||||
int codePoint = in.codePointAt(i);
|
||||
if (codePoint < specialCharacterEscapes.length && specialCharacterEscapes[codePoint] != null)
|
||||
{
|
||||
context.write(specialCharacterEscapes[codePoint]);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.write(in.charAt(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private StringValueReaderWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "string";
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.Collection;
|
||||
import static dev.plex.toml.ValueWriters.WRITERS;
|
||||
|
||||
class TableArrayValueWriter extends ArrayValueWriter
|
||||
{
|
||||
static final ValueWriter TABLE_ARRAY_VALUE_WRITER = new TableArrayValueWriter();
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Object value)
|
||||
{
|
||||
return isArrayish(value) && !isArrayOfPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Object from, WriterContext context)
|
||||
{
|
||||
Collection<?> values = normalize(from);
|
||||
|
||||
WriterContext subContext = context.pushTableFromArray();
|
||||
|
||||
for (Object value : values)
|
||||
{
|
||||
WRITERS.findWriterFor(value).write(value, subContext);
|
||||
}
|
||||
}
|
||||
|
||||
private TableArrayValueWriter()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "table-array";
|
||||
}
|
||||
}
|
||||
@@ -1,500 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* <p>Provides access to the keys and tables in a TOML data source.</p>
|
||||
*
|
||||
* <p>All getters can fall back to default values if they have been provided as a constructor argument.
|
||||
* Getters for simple values (String, Date, etc.) will return null if no matching key exists.
|
||||
* {@link #getList(String)}, {@link #getTable(String)} and {@link #getTables(String)} return empty values if there is no matching key.</p>
|
||||
*
|
||||
* <p>All read methods throw an {@link IllegalStateException} if the TOML is incorrect.</p>
|
||||
*
|
||||
* <p>Example usage:</p>
|
||||
* <pre><code>
|
||||
* Toml toml = new Toml().read(getTomlFile());
|
||||
* String name = toml.getString("name");
|
||||
* Long port = toml.getLong("server.ip"); // compound key. Is equivalent to:
|
||||
* Long port2 = toml.getTable("server").getLong("ip");
|
||||
* MyConfig config = toml.to(MyConfig.class);
|
||||
* </code></pre>
|
||||
*/
|
||||
public class Toml
|
||||
{
|
||||
|
||||
private static final Gson DEFAULT_GSON = new Gson();
|
||||
|
||||
@Getter
|
||||
private Map<String, Object> values = new HashMap<String, Object>();
|
||||
private final Toml defaults;
|
||||
|
||||
/**
|
||||
* Creates Toml instance with no defaults.
|
||||
*/
|
||||
public Toml()
|
||||
{
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param defaults fallback values used when the requested key or table is not present in the TOML source that has been read.
|
||||
*/
|
||||
public Toml(Toml defaults)
|
||||
{
|
||||
this(defaults, new HashMap<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the current Toml instance with values from file.
|
||||
*
|
||||
* @param file The File to be read. Expected to be encoded as UTF-8.
|
||||
* @return this instance
|
||||
* @throws IllegalStateException If file contains invalid TOML
|
||||
*/
|
||||
public Toml read(File file)
|
||||
{
|
||||
try
|
||||
{
|
||||
return read(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the current Toml instance with values from inputStream.
|
||||
*
|
||||
* @param inputStream Closed after it has been read.
|
||||
* @return this instance
|
||||
* @throws IllegalStateException If file contains invalid TOML
|
||||
*/
|
||||
public Toml read(InputStream inputStream)
|
||||
{
|
||||
return read(new InputStreamReader(inputStream));
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the current Toml instance with values from reader.
|
||||
*
|
||||
* @param reader Closed after it has been read.
|
||||
* @return this instance
|
||||
* @throws IllegalStateException If file contains invalid TOML
|
||||
*/
|
||||
public Toml read(Reader reader)
|
||||
{
|
||||
BufferedReader bufferedReader = null;
|
||||
try
|
||||
{
|
||||
bufferedReader = new BufferedReader(reader);
|
||||
|
||||
StringBuilder w = new StringBuilder();
|
||||
String line = bufferedReader.readLine();
|
||||
while (line != null)
|
||||
{
|
||||
w.append(line).append('\n');
|
||||
line = bufferedReader.readLine();
|
||||
}
|
||||
read(w.toString());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
bufferedReader.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the current Toml instance with values from otherToml.
|
||||
*
|
||||
* @param otherToml
|
||||
* @return this instance
|
||||
*/
|
||||
public Toml read(Toml otherToml)
|
||||
{
|
||||
this.values = otherToml.values;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the current Toml instance with values from tomlString.
|
||||
*
|
||||
* @param tomlString String to be read.
|
||||
* @return this instance
|
||||
* @throws IllegalStateException If tomlString is not valid TOML
|
||||
*/
|
||||
public Toml read(String tomlString) throws IllegalStateException
|
||||
{
|
||||
dev.plex.toml.Results results = dev.plex.toml.TomlParser.run(tomlString);
|
||||
if (results.errors.hasErrors())
|
||||
{
|
||||
throw new IllegalStateException(results.errors.toString());
|
||||
}
|
||||
|
||||
this.values = results.consume();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getString(String key)
|
||||
{
|
||||
return (String) get(key);
|
||||
}
|
||||
|
||||
public String getString(String key, String defaultValue)
|
||||
{
|
||||
String val = getString(key);
|
||||
return val == null ? defaultValue : val;
|
||||
}
|
||||
|
||||
public Long getLong(String key)
|
||||
{
|
||||
return (Long) get(key);
|
||||
}
|
||||
|
||||
public Long getLong(String key, Long defaultValue)
|
||||
{
|
||||
Long val = getLong(key);
|
||||
return val == null ? defaultValue : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key a TOML key
|
||||
* @param <T> type of list items
|
||||
* @return <code>null</code> if the key is not found
|
||||
*/
|
||||
public <T> List<T> getList(String key)
|
||||
{
|
||||
@SuppressWarnings("unchecked")
|
||||
List<T> list = (List<T>) get(key);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key a TOML key
|
||||
* @param defaultValue a list of default values
|
||||
* @param <T> type of list items
|
||||
* @return <code>null</code> is the key is not found
|
||||
*/
|
||||
public <T> List<T> getList(String key, List<T> defaultValue)
|
||||
{
|
||||
List<T> list = getList(key);
|
||||
|
||||
return list != null ? list : defaultValue;
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key)
|
||||
{
|
||||
return (Boolean) get(key);
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key, Boolean defaultValue)
|
||||
{
|
||||
Boolean val = getBoolean(key);
|
||||
return val == null ? defaultValue : val;
|
||||
}
|
||||
|
||||
public Date getDate(String key)
|
||||
{
|
||||
return (Date) get(key);
|
||||
}
|
||||
|
||||
public Date getDate(String key, Date defaultValue)
|
||||
{
|
||||
Date val = getDate(key);
|
||||
return val == null ? defaultValue : val;
|
||||
}
|
||||
|
||||
public Double getDouble(String key)
|
||||
{
|
||||
return (Double) get(key);
|
||||
}
|
||||
|
||||
public Double getDouble(String key, Double defaultValue)
|
||||
{
|
||||
Double val = getDouble(key);
|
||||
return val == null ? defaultValue : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key A table name, not including square brackets.
|
||||
* @return A new Toml instance or <code>null</code> if no value is found for key.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Toml getTable(String key)
|
||||
{
|
||||
Map<String, Object> map = (Map<String, Object>) get(key);
|
||||
|
||||
return map != null ? new Toml(null, map) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key Name of array of tables, not including square brackets.
|
||||
* @return A {@link List} of Toml instances or <code>null</code> if no value is found for key.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Toml> getTables(String key)
|
||||
{
|
||||
List<Map<String, Object>> tableArray = (List<Map<String, Object>>) get(key);
|
||||
|
||||
if (tableArray == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<Toml> tables = new ArrayList<Toml>();
|
||||
|
||||
for (Map<String, Object> table : tableArray)
|
||||
{
|
||||
tables.add(new Toml(null, table));
|
||||
}
|
||||
|
||||
return tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key a key name, can be compound (eg. a.b.c)
|
||||
* @return true if key is present
|
||||
*/
|
||||
public boolean contains(String key)
|
||||
{
|
||||
return get(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key a key name, can be compound (eg. a.b.c)
|
||||
* @return true if key is present and is a primitive
|
||||
*/
|
||||
public boolean containsPrimitive(String key)
|
||||
{
|
||||
Object object = get(key);
|
||||
|
||||
return object != null && !(object instanceof Map) && !(object instanceof List);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key a key name, can be compound (eg. a.b.c)
|
||||
* @return true if key is present and is a table
|
||||
*/
|
||||
public boolean containsTable(String key)
|
||||
{
|
||||
Object object = get(key);
|
||||
|
||||
return object != null && (object instanceof Map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key a key name, can be compound (eg. a.b.c)
|
||||
* @return true if key is present and is a table array
|
||||
*/
|
||||
public boolean containsTableArray(String key)
|
||||
{
|
||||
Object object = get(key);
|
||||
|
||||
return object != null && (object instanceof List);
|
||||
}
|
||||
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return values.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Populates an instance of targetClass with the values of this Toml instance.
|
||||
* The target's field names must match keys or tables.
|
||||
* Keys not present in targetClass will be ignored.
|
||||
* </p>
|
||||
*
|
||||
* <p>Tables are recursively converted to custom classes or to {@link Map Map<String, Object>}.</p>
|
||||
*
|
||||
* <p>In addition to straight-forward conversion of TOML primitives, the following are also available:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>Integer -> int, long (or wrapper), {@link java.math.BigInteger}</li>
|
||||
* <li>Float -> float, double (or wrapper), {@link java.math.BigDecimal}</li>
|
||||
* <li>One-letter String -> char, {@link Character}</li>
|
||||
* <li>String -> {@link String}, enum, {@link java.net.URI}, {@link java.net.URL}</li>
|
||||
* <li>Multiline and Literal Strings -> {@link String}</li>
|
||||
* <li>Array -> {@link List}, {@link Set}, array. The generic type can be anything that can be converted.</li>
|
||||
* <li>Table -> Custom class, {@link Map Map<String, Object>}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param targetClass Class to deserialize TOML to.
|
||||
* @param <T> type of targetClass.
|
||||
* @return A new instance of targetClass.
|
||||
*/
|
||||
public <T> T to(Class<T> targetClass)
|
||||
{
|
||||
JsonElement json = DEFAULT_GSON.toJsonTree(toMap());
|
||||
|
||||
if (targetClass == JsonElement.class)
|
||||
{
|
||||
return targetClass.cast(json);
|
||||
}
|
||||
|
||||
return DEFAULT_GSON.fromJson(json, targetClass);
|
||||
}
|
||||
|
||||
public Map<String, Object> toMap()
|
||||
{
|
||||
HashMap<String, Object> valuesCopy = new HashMap<String, Object>(values);
|
||||
|
||||
if (defaults != null)
|
||||
{
|
||||
for (Map.Entry<String, Object> entry : defaults.values.entrySet())
|
||||
{
|
||||
if (!valuesCopy.containsKey(entry.getKey()))
|
||||
{
|
||||
valuesCopy.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return valuesCopy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a {@link Set} of Map.Entry instances. Modifications to the {@link Set} are not reflected in this Toml instance. Entries are immutable, so {@link Map.Entry#setValue(Object)} throws an UnsupportedOperationException.
|
||||
*/
|
||||
public Set<Map.Entry<String, Object>> entrySet()
|
||||
{
|
||||
Set<Map.Entry<String, Object>> entries = new LinkedHashSet<Map.Entry<String, Object>>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : values.entrySet())
|
||||
{
|
||||
Class<? extends Object> entryClass = entry.getValue().getClass();
|
||||
|
||||
if (Map.class.isAssignableFrom(entryClass))
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), getTable(entry.getKey())));
|
||||
}
|
||||
else if (List.class.isAssignableFrom(entryClass))
|
||||
{
|
||||
List<?> value = (List<?>) entry.getValue();
|
||||
if (!value.isEmpty() && value.get(0) instanceof Map)
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), getTables(entry.getKey())));
|
||||
}
|
||||
else
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
entries.add(new Entry(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
private class Entry implements Map.Entry<String, Object>
|
||||
{
|
||||
|
||||
private final String key;
|
||||
private final Object value;
|
||||
|
||||
@Override
|
||||
public String getKey()
|
||||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object setValue(Object value)
|
||||
{
|
||||
throw new UnsupportedOperationException("TOML entry values cannot be changed.");
|
||||
}
|
||||
|
||||
private Entry(String key, Object value)
|
||||
{
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object get(String key)
|
||||
{
|
||||
if (values.containsKey(key))
|
||||
{
|
||||
return values.get(key);
|
||||
}
|
||||
|
||||
Object current = new HashMap<>(values);
|
||||
|
||||
dev.plex.toml.Keys.Key[] keys = dev.plex.toml.Keys.split(key);
|
||||
|
||||
for (dev.plex.toml.Keys.Key k : keys)
|
||||
{
|
||||
if (k.index == -1 && current instanceof Map && ((Map<String, Object>) current).containsKey(k.path))
|
||||
{
|
||||
return ((Map<String, Object>) current).get(k.path);
|
||||
}
|
||||
|
||||
current = ((Map<String, Object>) current).get(k.name);
|
||||
|
||||
if (k.index > -1 && current != null)
|
||||
{
|
||||
if (k.index >= ((List<?>) current).size())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
current = ((List<?>) current).get(k.index);
|
||||
}
|
||||
|
||||
if (current == null)
|
||||
{
|
||||
return defaults != null ? defaults.get(key) : null;
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
private Toml(Toml defaults, Map<String, Object> values)
|
||||
{
|
||||
this.values = values;
|
||||
this.defaults = defaults;
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class TomlParser
|
||||
{
|
||||
|
||||
static dev.plex.toml.Results run(String tomlString)
|
||||
{
|
||||
final dev.plex.toml.Results results = new dev.plex.toml.Results();
|
||||
|
||||
if (tomlString.isEmpty())
|
||||
{
|
||||
return results;
|
||||
}
|
||||
|
||||
AtomicInteger index = new AtomicInteger();
|
||||
boolean inComment = false;
|
||||
AtomicInteger line = new AtomicInteger(1);
|
||||
dev.plex.toml.Identifier identifier = null;
|
||||
Object value = null;
|
||||
|
||||
for (int i = index.get(); i < tomlString.length(); i = index.incrementAndGet())
|
||||
{
|
||||
char c = tomlString.charAt(i);
|
||||
|
||||
if (results.errors.hasErrors())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '#' && !inComment)
|
||||
{
|
||||
inComment = true;
|
||||
}
|
||||
else if (!Character.isWhitespace(c) && !inComment && identifier == null)
|
||||
{
|
||||
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 != dev.plex.toml.Identifier.INVALID)
|
||||
{
|
||||
if (id.isKey())
|
||||
{
|
||||
identifier = id;
|
||||
}
|
||||
else if (id.isTable())
|
||||
{
|
||||
results.startTables(id, line);
|
||||
}
|
||||
else if (id.isTableArray())
|
||||
{
|
||||
results.startTableArray(id, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (c == '\n')
|
||||
{
|
||||
inComment = false;
|
||||
identifier = null;
|
||||
value = null;
|
||||
line.incrementAndGet();
|
||||
}
|
||||
else if (!inComment && identifier != null && identifier.isKey() && value == null && !Character.isWhitespace(c))
|
||||
{
|
||||
value = ValueReaders.VALUE_READERS.convert(tomlString, index, new dev.plex.toml.Context(identifier, line, results.errors));
|
||||
|
||||
if (value instanceof dev.plex.toml.Results.Errors)
|
||||
{
|
||||
results.errors.add((dev.plex.toml.Results.Errors) value);
|
||||
}
|
||||
else
|
||||
{
|
||||
results.addValue(identifier.getName(), value, line);
|
||||
}
|
||||
}
|
||||
else if (value != null && !inComment && !Character.isWhitespace(c))
|
||||
{
|
||||
results.errors.invalidTextAfterIdentifier(identifier, c, line.get());
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private TomlParser()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,186 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import static dev.plex.toml.ValueWriters.WRITERS;
|
||||
|
||||
/**
|
||||
* <p>Converts Objects to TOML</p>
|
||||
*
|
||||
* <p>An input Object can comprise arbitrarily nested combinations of Java primitive types,
|
||||
* other {@link Object}s, {@link Map}s, {@link List}s, and Arrays. {@link Object}s and {@link Map}s
|
||||
* are output to TOML tables, and {@link List}s and Array to TOML arrays.</p>
|
||||
*
|
||||
* <p>Example usage:</p>
|
||||
* <pre><code>
|
||||
* class AClass {
|
||||
* int anInt = 1;
|
||||
* int[] anArray = { 2, 3 };
|
||||
* }
|
||||
*
|
||||
* String tomlString = new TomlWriter().write(new AClass());
|
||||
* </code></pre>
|
||||
*/
|
||||
public class TomlWriter
|
||||
{
|
||||
|
||||
public static class Builder
|
||||
{
|
||||
private int keyIndentation;
|
||||
private int tableIndentation;
|
||||
private int arrayDelimiterPadding = 0;
|
||||
private TimeZone timeZone = TimeZone.getTimeZone("UTC");
|
||||
private boolean showFractionalSeconds = false;
|
||||
|
||||
public Builder indentValuesBy(int spaces)
|
||||
{
|
||||
this.keyIndentation = spaces;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder indentTablesBy(int spaces)
|
||||
{
|
||||
this.tableIndentation = spaces;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder timeZone(TimeZone timeZone)
|
||||
{
|
||||
this.timeZone = timeZone;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param spaces number of spaces to put between opening square bracket and first item and between closing square bracket and last item
|
||||
* @return this TomlWriter.Builder instance
|
||||
*/
|
||||
public Builder padArrayDelimitersBy(int spaces)
|
||||
{
|
||||
this.arrayDelimiterPadding = spaces;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public TomlWriter build()
|
||||
{
|
||||
return new TomlWriter(keyIndentation, tableIndentation, arrayDelimiterPadding, timeZone, showFractionalSeconds);
|
||||
}
|
||||
|
||||
public Builder showFractionalSeconds()
|
||||
{
|
||||
this.showFractionalSeconds = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private final IndentationPolicy indentationPolicy;
|
||||
private final dev.plex.toml.DatePolicy datePolicy;
|
||||
|
||||
/**
|
||||
* Creates a TomlWriter instance.
|
||||
*/
|
||||
public TomlWriter()
|
||||
{
|
||||
this(0, 0, 0, TimeZone.getTimeZone("UTC"), false);
|
||||
}
|
||||
|
||||
private TomlWriter(int keyIndentation, int tableIndentation, int arrayDelimiterPadding, TimeZone timeZone, boolean showFractionalSeconds)
|
||||
{
|
||||
this.indentationPolicy = new IndentationPolicy(keyIndentation, tableIndentation, arrayDelimiterPadding);
|
||||
this.datePolicy = new dev.plex.toml.DatePolicy(timeZone, showFractionalSeconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an Object into TOML String.
|
||||
*
|
||||
* @param from the object to be written
|
||||
* @return a string containing the TOML representation of the given Object
|
||||
*/
|
||||
public String write(Object from)
|
||||
{
|
||||
try
|
||||
{
|
||||
StringWriter output = new StringWriter();
|
||||
write(from, output, null);
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an Object in TOML to a {@link File}. Output is encoded as UTF-8.
|
||||
*
|
||||
* @param from the object to be written
|
||||
* @param target the File to which the TOML will be written
|
||||
* @throws IOException if any file operations fail
|
||||
*/
|
||||
public void write(Object from, File target) throws IOException
|
||||
{
|
||||
OutputStream outputStream = new FileOutputStream(target, true);
|
||||
try
|
||||
{
|
||||
write(from, outputStream, target);
|
||||
}
|
||||
finally
|
||||
{
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an Object in TOML to a {@link OutputStream}. Output is encoded as UTF-8.
|
||||
*
|
||||
* @param from the object to be written
|
||||
* @param target the OutputStream to which the TOML will be written. The stream is NOT closed after being written to.
|
||||
* @throws IOException if target.write() fails
|
||||
*/
|
||||
public void write(Object from, OutputStream target, @Nullable File file) throws IOException
|
||||
{
|
||||
OutputStreamWriter writer = new OutputStreamWriter(target, StandardCharsets.UTF_8);
|
||||
write(from, writer, file);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an Object in TOML to a {@link Writer}. You MUST ensure that the {@link Writer}s's encoding is set to UTF-8 for the TOML to be valid.
|
||||
*
|
||||
* @param from the object to be written. Can be a Map or a custom type. Must not be null.
|
||||
* @param target the Writer to which TOML will be written. The Writer is not closed.
|
||||
* @throws IOException if target.write() fails
|
||||
* @throws IllegalArgumentException if from is of an invalid type
|
||||
*/
|
||||
public void write(Object from, Writer target, @Nullable File file) throws IOException
|
||||
{
|
||||
dev.plex.toml.ValueWriter valueWriter = WRITERS.findWriterFor(from);
|
||||
if (valueWriter == MapValueWriter.MAP_VALUE_WRITER || valueWriter == dev.plex.toml.ObjectValueWriter.OBJECT_VALUE_WRITER)
|
||||
{
|
||||
WriterContext context = new WriterContext(indentationPolicy, datePolicy, target);
|
||||
if (file != null && file.exists())
|
||||
{
|
||||
context.file = file;
|
||||
}
|
||||
valueWriter.write(from, context);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("An object of class " + from.getClass().getSimpleName() + " cannot produce valid TOML. Please pass in a Map or a custom type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
interface ValueReader
|
||||
{
|
||||
|
||||
/**
|
||||
* @param s must already have been trimmed
|
||||
*/
|
||||
boolean canRead(String s);
|
||||
|
||||
/**
|
||||
* Partial validation. Stops after type terminator, rather than at EOI.
|
||||
*
|
||||
* @param s must already have been validated by {@link #canRead(String)}
|
||||
* @param index where to start in s
|
||||
* @return a value or a {@link dev.plex.toml.Results.Errors}
|
||||
*/
|
||||
Object read(String s, AtomicInteger index, dev.plex.toml.Context context);
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
interface ValueWriter
|
||||
{
|
||||
boolean canWrite(Object value);
|
||||
|
||||
void write(Object value, WriterContext context);
|
||||
|
||||
boolean isPrimitiveType();
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
package dev.plex.toml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
|
||||
class WriterContext
|
||||
{
|
||||
private String arrayKey = null;
|
||||
private boolean isArrayOfTable = false;
|
||||
private boolean empty = true;
|
||||
public final String key;
|
||||
private final String currentTableIndent;
|
||||
private final String currentFieldIndent;
|
||||
private final Writer output;
|
||||
private final dev.plex.toml.IndentationPolicy indentationPolicy;
|
||||
private final dev.plex.toml.DatePolicy datePolicy;
|
||||
|
||||
public File file;
|
||||
public String parentName;
|
||||
public boolean hasRun = false;
|
||||
|
||||
WriterContext(dev.plex.toml.IndentationPolicy indentationPolicy, dev.plex.toml.DatePolicy datePolicy, Writer output)
|
||||
{
|
||||
this("", "", output, indentationPolicy, datePolicy);
|
||||
}
|
||||
|
||||
WriterContext pushTable(String newKey)
|
||||
{
|
||||
String newIndent = "";
|
||||
if (!key.isEmpty())
|
||||
{
|
||||
newIndent = growIndent(indentationPolicy);
|
||||
}
|
||||
|
||||
String fullKey = key.isEmpty() ? newKey : key + "." + newKey;
|
||||
|
||||
WriterContext subContext = new WriterContext(fullKey, newIndent, output, indentationPolicy, datePolicy);
|
||||
if (!empty)
|
||||
{
|
||||
subContext.empty = false;
|
||||
}
|
||||
|
||||
return subContext;
|
||||
}
|
||||
|
||||
WriterContext pushTableFromArray()
|
||||
{
|
||||
WriterContext subContext = new WriterContext(key, currentTableIndent, output, indentationPolicy, datePolicy);
|
||||
if (!empty)
|
||||
{
|
||||
subContext.empty = false;
|
||||
}
|
||||
subContext.setIsArrayOfTable(true);
|
||||
|
||||
return subContext;
|
||||
}
|
||||
|
||||
WriterContext write(String s)
|
||||
{
|
||||
try
|
||||
{
|
||||
output.write(s);
|
||||
if (empty && !s.isEmpty())
|
||||
{
|
||||
empty = false;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
void write(char[] chars)
|
||||
{
|
||||
for (char c : chars)
|
||||
{
|
||||
write(c);
|
||||
}
|
||||
}
|
||||
|
||||
WriterContext write(char c)
|
||||
{
|
||||
try
|
||||
{
|
||||
output.write(c);
|
||||
empty = false;
|
||||
|
||||
return this;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
void writeKey()
|
||||
{
|
||||
if (key.isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty)
|
||||
{
|
||||
write('\n');
|
||||
}
|
||||
|
||||
write(currentTableIndent);
|
||||
|
||||
if (isArrayOfTable)
|
||||
{
|
||||
write("[[").write(key).write("]]\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
write('[').write(key).write("]\n");
|
||||
}
|
||||
}
|
||||
|
||||
void writeArrayDelimiterPadding()
|
||||
{
|
||||
for (int i = 0; i < indentationPolicy.getArrayDelimiterPadding(); i++)
|
||||
{
|
||||
write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void indent()
|
||||
{
|
||||
if (!key.isEmpty())
|
||||
{
|
||||
write(currentFieldIndent);
|
||||
}
|
||||
}
|
||||
|
||||
dev.plex.toml.DatePolicy getDatePolicy()
|
||||
{
|
||||
return datePolicy;
|
||||
}
|
||||
|
||||
WriterContext setIsArrayOfTable(boolean isArrayOfTable)
|
||||
{
|
||||
this.isArrayOfTable = isArrayOfTable;
|
||||
return this;
|
||||
}
|
||||
|
||||
WriterContext setArrayKey(String arrayKey)
|
||||
{
|
||||
this.arrayKey = arrayKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
String getContextPath()
|
||||
{
|
||||
return key.isEmpty() ? arrayKey : key + "." + arrayKey;
|
||||
}
|
||||
|
||||
private String growIndent(dev.plex.toml.IndentationPolicy indentationPolicy)
|
||||
{
|
||||
return currentTableIndent + fillStringWithSpaces(indentationPolicy.getTableIndent());
|
||||
}
|
||||
|
||||
private String fillStringWithSpaces(int count)
|
||||
{
|
||||
char[] chars = new char[count];
|
||||
Arrays.fill(chars, ' ');
|
||||
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
private WriterContext(String key, String tableIndent, Writer output, dev.plex.toml.IndentationPolicy indentationPolicy, dev.plex.toml.DatePolicy datePolicy)
|
||||
{
|
||||
this.key = key;
|
||||
this.output = output;
|
||||
this.indentationPolicy = indentationPolicy;
|
||||
this.currentTableIndent = tableIndent;
|
||||
this.datePolicy = datePolicy;
|
||||
this.currentFieldIndent = tableIndent + fillStringWithSpaces(this.indentationPolicy.getKeyValueIndent());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.settings.ServerSettings;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
@@ -58,7 +57,7 @@ public class PlexLog
|
||||
message = message.replace("{" + i + "}", strings[i].toString());
|
||||
}
|
||||
}
|
||||
if (Plex.get().getConfig().as(ServerSettings.class).getServer().isDebug())
|
||||
if (Plex.get().getConfig().settings().getServer().isDebug())
|
||||
{
|
||||
Plex.get().getServer().getConsoleCommandSource().sendMessage(MiniMessage.miniMessage().deserialize("<dark_purple>[Plex Debug] <gold>" + message));
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
#############################
|
||||
# #
|
||||
# Plex Velocity #
|
||||
# v1.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%", "Another motd"]
|
||||
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
|
||||
@@ -0,0 +1,31 @@
|
||||
# Plex Configuration File
|
||||
# For documentation, please visit: https://plex.us.org
|
||||
|
||||
server:
|
||||
name: "Plexus"
|
||||
|
||||
# Placeholders
|
||||
# %mcversion% - The Velocity Version (i.e. 3.5.0-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%"
|
||||
- "Another motd"
|
||||
colorize_motd: 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 legacy section-sign
|
||||
# color codes 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
|
||||
@@ -0,0 +1,3 @@
|
||||
server_switch: "<dark_gray>[<#ffbf00>o<dark_gray>] <yellow>{player} switched from {from} to {to}"
|
||||
server_join: "<dark_gray>[<green>+<dark_gray>] <yellow>{player} joined server {server}"
|
||||
server_leave: "<dark_gray>[<red>-<dark_gray>] <yellow>{player} left server {server}"
|
||||
+1
-27
@@ -6,7 +6,6 @@ val paperApiVersion = "26.1.2"
|
||||
|
||||
plugins {
|
||||
java
|
||||
`maven-publish`
|
||||
id("org.jetbrains.gradle.plugin.idea-ext")
|
||||
id("net.kyori.blossom")
|
||||
id("com.gradleup.shadow")
|
||||
@@ -132,7 +131,7 @@ tasks {
|
||||
property("author", if (System.getenv("JENKINS_URL") != null) "jenkins" else System.getProperty("user.name"))
|
||||
property("buildNumber", if (System.getenv("BUILD_NUMBER") != null) System.getenv("BUILD_NUMBER") else getBuildNumber())
|
||||
property("date", SimpleDateFormat("MM/dd/yyyy '<light_purple>at<gold>' hh:mm:ss a z").format(Date()))
|
||||
property("gitCommit", indraGit.commit().get().name.take(7))
|
||||
property("gitCommit", indraGit.commit().get().name)
|
||||
property("minecraftVersion", paperApiVersion)
|
||||
}
|
||||
}
|
||||
@@ -150,28 +149,3 @@ tasks {
|
||||
options.memberLevel = JavadocMemberLevel.PRIVATE
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
pom.withXml {
|
||||
val dependenciesNode = asNode().appendNode("dependencies")
|
||||
configurations.getByName("library").allDependencies.configureEach {
|
||||
dependenciesNode.appendNode("dependency")
|
||||
.appendNode("groupId", group).parent()
|
||||
.appendNode("artifactId", name).parent()
|
||||
.appendNode("version", version).parent()
|
||||
.appendNode("scope", "provided").parent()
|
||||
}
|
||||
configurations.getByName("implementation").allDependencies.configureEach {
|
||||
dependenciesNode.appendNode("dependency")
|
||||
.appendNode("groupId", group).parent()
|
||||
.appendNode("artifactId", name).parent()
|
||||
.appendNode("version", version).parent()
|
||||
.appendNode("scope", "provided").parent()
|
||||
}
|
||||
}
|
||||
artifacts.artifact(tasks.shadowJar)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,24 +20,48 @@ final class DefaultPlexConfiguration implements PlexConfiguration
|
||||
return config.getString(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path, String fallback)
|
||||
{
|
||||
return config.getString(path, fallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path)
|
||||
{
|
||||
return config.getBoolean(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path, boolean fallback)
|
||||
{
|
||||
return config.getBoolean(path, fallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String path)
|
||||
{
|
||||
return config.getInt(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String path, int fallback)
|
||||
{
|
||||
return config.getInt(path, fallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path)
|
||||
{
|
||||
return config.getStringList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path, List<String> fallback)
|
||||
{
|
||||
return config.contains(path) ? config.getStringList(path) : fallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value)
|
||||
{
|
||||
|
||||
@@ -37,7 +37,7 @@ public class PlexCMD extends ServerCommand
|
||||
if (args.length == 0)
|
||||
{
|
||||
send(sender, mmString("<light_purple>Plex - A new freedom plugin."));
|
||||
send(sender, mmString("<light_purple>Plugin version: <gold>" + plugin.getPluginMeta().getVersion() + " #" + BuildInfo.getNumber() + " <light_purple>Git: <gold>" + BuildInfo.getCommit()));
|
||||
send(sender, mmString("<light_purple>Plugin version: <gold>" + plugin.getPluginMeta().getVersion() + " #" + BuildInfo.getNumber() + " <light_purple>Git: <gold>" + BuildInfo.shortenCommit(BuildInfo.getCommit())));
|
||||
send(sender, mmString("<light_purple>Authors: <gold>Telesphoreo, Taahh"));
|
||||
send(sender, mmString("<light_purple>Built by: <gold>" + BuildInfo.getAuthor() + " <light_purple>on <gold>" + BuildInfo.getDate()));
|
||||
send(sender, mmString("<light_purple>Run <gold>/plex modules <light_purple>to see a list of modules."));
|
||||
|
||||
@@ -41,4 +41,13 @@ public class BuildInfo
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static String shortenCommit(String commit)
|
||||
{
|
||||
if (commit == null || commit.length() <= 7)
|
||||
{
|
||||
return commit;
|
||||
}
|
||||
return commit.substring(0, 7);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user