Skip to content

Commit

Permalink
Add Version support to YAMLConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Jan 17, 2019
1 parent 8b77434 commit fcde0bc
Show file tree
Hide file tree
Showing 30 changed files with 326 additions and 40 deletions.
Expand Up @@ -101,7 +101,10 @@ public boolean create(UUID player, String name, ServerTemplate template, Version

YAMLSection server = new YAMLSection();
YAMLSection config = new YAMLSection((Map<String, ?>) convert(data.getSection("c").get(), new NamedContainer<>("$player$", (player == null)?"":player.toString()), new NamedContainer<>("$name$", name),
new NamedContainer<>("$template$", template.getName()), new NamedContainer<>("$type$", template.getType().toString()), new NamedContainer<>("$version$", version.toString().replace(" ", "@")), new NamedContainer<>("$port$", Integer.toString(fport))));
new NamedContainer<>("$template$", template.getName()), new NamedContainer<>("$type$", template.getType().toString()), new NamedContainer<>("$version$", version.toString().replace(" ", "@")),
new NamedContainer<>("$address$", data.getSection("c").getRawString("\033address", "null")), new NamedContainer<>("$port$", Integer.toString(fport))));

config.remove("\033address");

server.set("Enabled", true);
server.set("Display", "");
Expand Down
Expand Up @@ -192,8 +192,9 @@ public void run() {
if (host.plugin.exServers.keySet().contains(name.toLowerCase()))
host.plugin.exServers.remove(name.toLowerCase());

config = new YAMLSection((Map<String, ?>) convert(config.get(), new NamedContainer<>("$player$", (player == null)?"":player.toString()), new NamedContainer<>("$name$", name), new NamedContainer<>("$template$", template.getName()),
new NamedContainer<>("$type$", template.getType().toString()), new NamedContainer<>("$version$", version.toString().replace(" ", "@")), new NamedContainer<>("$port$", Integer.toString(port))));
config = new YAMLSection((Map<String, ?>) convert(config.get(), new NamedContainer<>("$player$", (player == null)?"":player.toString()), new NamedContainer<>("$name$", name),
new NamedContainer<>("$template$", template.getName()), new NamedContainer<>("$type$", template.getType().toString()), new NamedContainer<>("$version$", version.toString().replace(" ", "@")),
new NamedContainer<>("$address$", host.getAddress().getHostAddress()), new NamedContainer<>("$port$", Integer.toString(port))));

server.set("Enabled", true);
server.set("Display", "");
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import net.ME1312.SubServers.Bungee.Library.Util;
import net.ME1312.SubServers.Bungee.Library.Version.Version;
import org.msgpack.value.MapValue;
import org.msgpack.value.Value;
import org.msgpack.value.ValueFactory;
Expand Down Expand Up @@ -211,6 +212,8 @@ private Object convert(Object value) {
return list;
} else if (value instanceof UUID) {
return value.toString();
} else if (value instanceof Version) {
return ((Version) value).toFullString();
} else {
return value;
}
Expand Down Expand Up @@ -958,6 +961,48 @@ public List<UUID> getUUIDList(String handle, List<UUID> def) {
return get(handle, def).asUUIDList();
}

/**
* Get a Version by Handle
*
* @param handle Handle
* @return Version
*/
public Version getVersion(String handle) {
return get(handle).asVersion();
}

/**
* Get a Version by Handle
*
* @param handle Handle
* @param def Default
* @return Version
*/
public Version getVersion(String handle, Version def) {
return get(handle, def).asVersion();
}

/**
* Get a Version List by Handle
*
* @param handle Handle
* @return Version List
*/
public List<Version> getVersionList(String handle) {
return get(handle).asVersionList();
}

/**
* Get a Version List by Handle
*
* @param handle Handle
* @param def Default
* @return Version List
*/
public List<Version> getVersionList(String handle, List<Version> def) {
return get(handle, def).asVersionList();
}

/**
* Check if object is Null by Handle
*
Expand Down
@@ -1,6 +1,7 @@
package net.ME1312.SubServers.Bungee.Library.Config;

import net.ME1312.SubServers.Bungee.Library.Util;
import net.ME1312.SubServers.Bungee.Library.Version.Version;
import net.md_5.bungee.api.ChatColor;
import org.yaml.snakeyaml.Yaml;

Expand Down Expand Up @@ -268,7 +269,7 @@ public List<String> asColoredStringList(char color) {
* @return UUID
*/
public UUID asUUID() {
if (obj != null) return UUID.fromString((String) obj);
if (obj != null) return UUID.fromString(asRawString());
else return null;
}

Expand All @@ -287,6 +288,30 @@ public List<UUID> asUUIDList() {
} else return null;
}

/**
* Get Object as Version
*
* @return Version
*/
public Version asVersion() {
if (obj != null) return Version.fromString(asRawString());
else return null;
}

/**
* Get Object as Version List
*
* @return Version List
*/
public List<Version> asVersionList() {
if (obj != null) {
List<Version> values = new ArrayList<Version>();
for (String value : (List<String>) obj) {
values.add(Version.fromString(value));
}
return values;
} else return null;
}

/**
* Check if object is Null
Expand Down Expand Up @@ -348,7 +373,7 @@ public boolean isString() {
* @return UUID Status
*/
public boolean isUUID() {
return (obj instanceof String && !Util.isException(() -> UUID.fromString((String) obj)));
return (obj instanceof String && !Util.isException(() -> UUID.fromString(asRawString())));
}

@Override
Expand Down
Expand Up @@ -68,12 +68,12 @@ public void execute(Client client, YAMLSection data) {
client.sendPacket(new PacketCreateServer(8, "There is no template with that name", (data.contains("id")) ? data.getRawString("id") : null));
} else if (!plugin.hosts.get(data.getSection("creator").getString("host").toLowerCase()).getCreator().getTemplate(data.getSection("creator").getString("template")).isEnabled()) {
client.sendPacket(new PacketCreateServer(8, "That Template is not enabled", (data.contains("id")) ? data.getRawString("id") : null));
} else if (new Version("1.8").compareTo(new Version(data.getSection("creator").getString("version"))) > 0) {
} else if (new Version("1.8").compareTo(data.getSection("creator").getVersion("version")) > 0) {
client.sendPacket(new PacketCreateServer(10, "SubCreator cannot create servers before Minecraft 1.8", (data.contains("id")) ? data.getRawString("id") : null));
} else if (data.getSection("creator").contains("port") && (data.getSection("creator").getInt("port") <= 0 || data.getSection("creator").getInt("port") > 65535)) {
client.sendPacket(new PacketCreateServer(11, "Invalid Port Number", (data.contains("id")) ? data.getRawString("id") : null));
} else {
if (plugin.hosts.get(data.getSection("creator").getString("host").toLowerCase()).getCreator().create((data.contains("player"))?data.getUUID("player"):null, data.getSection("creator").getString("name"), plugin.hosts.get(data.getSection("creator").getString("host").toLowerCase()).getCreator().getTemplate(data.getSection("creator").getString("template")), new Version(data.getSection("creator").getString("version")), (data.getSection("creator").contains("port"))?data.getSection("creator").getInt("port"):null)) {
if (plugin.hosts.get(data.getSection("creator").getString("host").toLowerCase()).getCreator().create((data.contains("player"))?data.getUUID("player"):null, data.getSection("creator").getString("name"), plugin.hosts.get(data.getSection("creator").getString("host").toLowerCase()).getCreator().getTemplate(data.getSection("creator").getString("template")), data.getSection("creator").getVersion("version"), (data.getSection("creator").contains("port"))?data.getSection("creator").getInt("port"):null)) {
if (data.contains("wait") && data.getBoolean("wait")) {
new Thread(() -> {
try {
Expand Down
Expand Up @@ -65,7 +65,7 @@ public YAMLSection generate() {
YAMLSection creator = new YAMLSection();
creator.set("name", name);
creator.set("template", template.getName());
creator.set("version", version.toString());
creator.set("version", version);
creator.set("port", port);
creator.set("log", log.toString());
data.set("creator", creator);
Expand Down
Expand Up @@ -457,7 +457,7 @@ protected static List<PacketIn> decodePacket(Client client, YAMLSection data) th

List<PacketIn> list = new ArrayList<PacketIn>();
for (PacketIn packet : pIn.get(data.getRawString("n")).get(data.getRawString("h"))) {
if (packet.isCompatible(new Version(data.getRawString("v")))) {
if (packet.isCompatible(data.getVersion("v"))) {
list.add(packet);
} else {
new IllegalPacketException(client.getAddress().toString() + ": Packet Version Mismatch in " + data.getRawString("h") + ": " + data.getRawString("v") + " =/= " + packet.getVersion().toString()).printStackTrace();
Expand Down
Expand Up @@ -88,7 +88,7 @@ protected SubPlugin(PrintStream out, boolean isPatched) throws IOException {
if (!(new UniversalFile(dir, "config.yml").exists())) {
Util.copyFromJar(SubPlugin.class.getClassLoader(), "net/ME1312/SubServers/Bungee/Library/Files/config.yml", new UniversalFile(dir, "config.yml").getPath());
System.out.println("SubServers > Created ~/SubServers/config.yml");
} else if ((new Version((new YAMLConfig(new UniversalFile(dir, "config.yml"))).get().getSection("Settings").getString("Version", "0")).compareTo(new Version("2.11.2a+"))) != 0) {
} else if (((new YAMLConfig(new UniversalFile(dir, "config.yml"))).get().getSection("Settings").getVersion("Version", new Version(0))).compareTo(new Version("2.11.2a+")) != 0) {
Files.move(new UniversalFile(dir, "config.yml").toPath(), new UniversalFile(dir, "config.old" + Math.round(Math.random() * 100000) + ".yml").toPath());

Util.copyFromJar(SubPlugin.class.getClassLoader(), "net/ME1312/SubServers/Bungee/Library/Files/config.yml", new UniversalFile(dir, "config.yml").getPath());
Expand All @@ -99,7 +99,7 @@ protected SubPlugin(PrintStream out, boolean isPatched) throws IOException {
if (!(new UniversalFile(dir, "lang.yml").exists())) {
Util.copyFromJar(SubPlugin.class.getClassLoader(), "net/ME1312/SubServers/Bungee/Library/Files/lang.yml", new UniversalFile(dir, "lang.yml").getPath());
System.out.println("SubServers > Created ~/SubServers/lang.yml");
} else if ((new Version((new YAMLConfig(new UniversalFile(dir, "lang.yml"))).get().getString("Version", "0")).compareTo(new Version("2.13.2c+"))) != 0) {
} else if (((new YAMLConfig(new UniversalFile(dir, "lang.yml"))).get().getVersion("Version", new Version(9))).compareTo(new Version("2.13.2c+")) != 0) {
Files.move(new UniversalFile(dir, "lang.yml").toPath(), new UniversalFile(dir, "lang.old" + Math.round(Math.random() * 100000) + ".yml").toPath());
Util.copyFromJar(SubPlugin.class.getClassLoader(), "net/ME1312/SubServers/Bungee/Library/Files/lang.yml", new UniversalFile(dir, "lang.yml").getPath());
System.out.println("SubServers > Updated ~/SubServers/lang.yml");
Expand All @@ -121,22 +121,22 @@ protected SubPlugin(PrintStream out, boolean isPatched) throws IOException {
Util.unzip(SubPlugin.class.getResourceAsStream("/net/ME1312/SubServers/Bungee/Library/Files/Templates/sponge.zip"), new UniversalFile(dir, "Templates"));
System.out.println("SubServers > Created ~/SubServers/Templates/Sponge");
} else {
if (new UniversalFile(dir, "Templates:Vanilla:template.yml").exists() && (new Version((new YAMLConfig(new UniversalFile(dir, "Templates:Vanilla:template.yml"))).get().getString("Version", "0")).compareTo(new Version("2.13.2c+"))) != 0) {
if (new UniversalFile(dir, "Templates:Vanilla:template.yml").exists() && ((new YAMLConfig(new UniversalFile(dir, "Templates:Vanilla:template.yml"))).get().getVersion("Version", new Version(0))).compareTo(new Version("2.13.2c+")) != 0) {
Files.move(new UniversalFile(dir, "Templates:Vanilla").toPath(), new UniversalFile(dir, "Templates:Vanilla.old" + Math.round(Math.random() * 100000) + ".x").toPath());
Util.unzip(SubPlugin.class.getResourceAsStream("/net/ME1312/SubServers/Bungee/Library/Files/Templates/vanilla.zip"), new UniversalFile(dir, "Templates"));
System.out.println("SubServers > Updated ~/SubServers/Templates/Vanilla");
}
if (new UniversalFile(dir, "Templates:Spigot:template.yml").exists() && (new Version((new YAMLConfig(new UniversalFile(dir, "Templates:Spigot:template.yml"))).get().getString("Version", "0")).compareTo(new Version("2.13.2c+"))) != 0) {
if (new UniversalFile(dir, "Templates:Spigot:template.yml").exists() && ((new YAMLConfig(new UniversalFile(dir, "Templates:Spigot:template.yml"))).get().getVersion("Version", new Version(0))).compareTo(new Version("2.13.2c+")) != 0) {
Files.move(new UniversalFile(dir, "Templates:Spigot").toPath(), new UniversalFile(dir, "Templates:Spigot.old" + Math.round(Math.random() * 100000) + ".x").toPath());
Util.unzip(SubPlugin.class.getResourceAsStream("/net/ME1312/SubServers/Bungee/Library/Files/Templates/spigot.zip"), new UniversalFile(dir, "Templates"));
System.out.println("SubServers > Updated ~/SubServers/Templates/Spigot");
}
if (new UniversalFile(dir, "Templates:Forge:template.yml").exists() && (new Version((new YAMLConfig(new UniversalFile(dir, "Templates:Forge:template.yml"))).get().getString("Version", "0")).compareTo(new Version("2.13.2c+"))) != 0) {
if (new UniversalFile(dir, "Templates:Forge:template.yml").exists() && ((new YAMLConfig(new UniversalFile(dir, "Templates:Forge:template.yml"))).get().getVersion("Version", new Version(0))).compareTo(new Version("2.13.2c+")) != 0) {
Files.move(new UniversalFile(dir, "Templates:Forge").toPath(), new UniversalFile(dir, "Templates:Forge.old" + Math.round(Math.random() * 100000) + ".x").toPath());
Util.unzip(SubPlugin.class.getResourceAsStream("/net/ME1312/SubServers/Bungee/Library/Files/Templates/forge.zip"), new UniversalFile(dir, "Templates"));
System.out.println("SubServers > Updated ~/SubServers/Templates/Forge");
}
if (new UniversalFile(dir, "Templates:Sponge:template.yml").exists() && (new Version((new YAMLConfig(new UniversalFile(dir, "Templates:Sponge:template.yml"))).get().getString("Version", "0")).compareTo(new Version("2.13.2c+"))) != 0) {
if (new UniversalFile(dir, "Templates:Sponge:template.yml").exists() && ((new YAMLConfig(new UniversalFile(dir, "Templates:Sponge:template.yml"))).get().getVersion("Version", new Version(0))).compareTo(new Version("2.13.2c+")) != 0) {
Files.move(new UniversalFile(dir, "Templates:Sponge").toPath(), new UniversalFile(dir, "Templates:Sponge.old" + Math.round(Math.random() * 100000) + ".x").toPath());
Util.unzip(SubPlugin.class.getResourceAsStream("/net/ME1312/SubServers/Bungee/Library/Files/Templates/sponge.zip"), new UniversalFile(dir, "Templates"));
System.out.println("SubServers > Updated ~/SubServers/Templates/Sponge");
Expand Down
@@ -1,6 +1,7 @@
package net.ME1312.SubServers.Client.Bukkit.Library.Config;

import net.ME1312.SubServers.Client.Bukkit.Library.Util;
import net.ME1312.SubServers.Client.Bukkit.Library.Version.Version;
import org.msgpack.value.MapValue;
import org.msgpack.value.Value;
import org.msgpack.value.ValueFactory;
Expand Down Expand Up @@ -213,6 +214,8 @@ private Object convert(Object value) {
return list;
} else if (value instanceof UUID) {
return value.toString();
} else if (value instanceof Version) {
return ((Version) value).toFullString();
} else {
return value;
}
Expand Down Expand Up @@ -960,6 +963,48 @@ public List<UUID> getUUIDList(String handle, List<UUID> def) {
return get(handle, def).asUUIDList();
}

/**
* Get a Version by Handle
*
* @param handle Handle
* @return Version
*/
public Version getVersion(String handle) {
return get(handle).asVersion();
}

/**
* Get a Version by Handle
*
* @param handle Handle
* @param def Default
* @return Version
*/
public Version getVersion(String handle, Version def) {
return get(handle, def).asVersion();
}

/**
* Get a Version List by Handle
*
* @param handle Handle
* @return Version List
*/
public List<Version> getVersionList(String handle) {
return get(handle).asVersionList();
}

/**
* Get a Version List by Handle
*
* @param handle Handle
* @param def Default
* @return Version List
*/
public List<Version> getVersionList(String handle, List<Version> def) {
return get(handle, def).asVersionList();
}

/**
* Check if object is Null by Handle
*
Expand Down
@@ -1,6 +1,7 @@
package net.ME1312.SubServers.Client.Bukkit.Library.Config;

import net.ME1312.SubServers.Client.Bukkit.Library.Util;
import net.ME1312.SubServers.Client.Bukkit.Library.Version.Version;
import org.bukkit.ChatColor;
import org.yaml.snakeyaml.Yaml;

Expand Down Expand Up @@ -290,6 +291,30 @@ public List<UUID> asUUIDList() {
} else return null;
}

/**
* Get Object as Version
*
* @return Version
*/
public Version asVersion() {
if (obj != null) return Version.fromString(asRawString());
else return null;
}

/**
* Get Object as Version List
*
* @return Version List
*/
public List<Version> asVersionList() {
if (obj != null) {
List<Version> values = new ArrayList<Version>();
for (String value : (List<String>) obj) {
values.add(Version.fromString(value));
}
return values;
} else return null;
}

/**
* Check if object is Null
Expand Down
Expand Up @@ -83,7 +83,7 @@ public YAMLSection generate() {
creator.set("name", name);
creator.set("host", host);
creator.set("template", template);
creator.set("version", version.toString());
creator.set("version", version);
if (port != null) creator.set("port", port);
data.set("creator", creator);
return data;
Expand Down

0 comments on commit fcde0bc

Please sign in to comment.