SubServers-2/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/SubPlugin.java

163 lines
8.3 KiB
Java
Raw Normal View History

2016-12-20 00:31:01 +01:00
package net.ME1312.SubServers.Client.Bukkit;
import com.google.gson.Gson;
import net.ME1312.SubServers.Client.Bukkit.Graphic.InternalUIHandler;
2017-01-21 17:49:37 +01:00
import net.ME1312.SubServers.Client.Bukkit.Graphic.UIHandler;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLConfig;
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLSection;
2018-03-21 21:45:59 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Metrics;
import net.ME1312.SubServers.Client.Bukkit.Library.NamedContainer;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.UniversalFile;
2016-12-27 17:03:19 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Util;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Version.Version;
2018-03-21 21:45:59 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Version.VersionType;
import net.ME1312.SubServers.Client.Bukkit.Network.Cipher;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.Network.SubDataClient;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
2017-11-22 22:58:33 +01:00
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
2016-12-20 00:31:01 +01:00
2017-11-22 22:58:33 +01:00
import javax.xml.parsers.DocumentBuilderFactory;
2016-12-20 00:31:01 +01:00
import java.io.*;
import java.lang.reflect.InvocationTargetException;
2016-12-20 00:31:01 +01:00
import java.net.InetAddress;
2017-11-22 22:58:33 +01:00
import java.net.URL;
import java.nio.charset.Charset;
2016-12-20 00:31:01 +01:00
import java.nio.file.Files;
2017-05-25 21:55:47 +02:00
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
2017-11-22 22:58:33 +01:00
import java.util.concurrent.TimeUnit;
2016-12-20 00:31:01 +01:00
2017-01-08 20:42:40 +01:00
/**
* SubServers Client Plugin Class
*/
2016-12-20 00:31:01 +01:00
public final class SubPlugin extends JavaPlugin {
protected NamedContainer<Long, Map<String, Map<String, String>>> lang = null;
2017-01-26 23:19:48 +01:00
public YAMLConfig config;
2016-12-20 00:31:01 +01:00
public SubDataClient subdata = null;
2017-01-21 17:49:37 +01:00
public UIHandler gui = null;
2017-01-08 20:42:40 +01:00
public final Version version;
2017-01-21 17:49:37 +01:00
public final SubAPI api = new SubAPI(this);
2016-12-20 00:31:01 +01:00
2017-01-08 20:42:40 +01:00
public SubPlugin() {
super();
2018-07-28 19:01:28 +02:00
version = Version.fromString(getDescription().getVersion());
//version = new Version(Version.fromString(getDescription().getVersion()), VersionType.SNAPSHOT, (SubPlugin.class.getPackage().getSpecificationTitle() == null)?"custom":SubPlugin.class.getPackage().getSpecificationTitle()); // TODO Snapshot Version
2017-01-08 20:42:40 +01:00
}
/**
* Enable Plugin
*/
2016-12-20 00:31:01 +01:00
@Override
@SuppressWarnings("unchecked")
2016-12-20 00:31:01 +01:00
public void onEnable() {
try {
Bukkit.getLogger().info("SubServers > Loading SubServers.Client.Bukkit v" + version.toString() + " Libraries (for Minecraft " + api.getGameVersion() + ")");
2016-12-20 00:31:01 +01:00
getDataFolder().mkdirs();
2017-05-25 21:55:47 +02:00
if (new UniversalFile(getDataFolder().getParentFile(), "SubServers-Client:config.yml").exists()) {
Files.move(new UniversalFile(getDataFolder().getParentFile(), "SubServers-Client:config.yml").toPath(), new UniversalFile(getDataFolder(), "config.yml").toPath(), StandardCopyOption.REPLACE_EXISTING);
Util.deleteDirectory(new UniversalFile(getDataFolder().getParentFile(), "SubServers-Client"));
}
2016-12-20 00:31:01 +01:00
if (!(new UniversalFile(getDataFolder(), "config.yml").exists())) {
2016-12-27 17:03:19 +01:00
Util.copyFromJar(SubPlugin.class.getClassLoader(), "config.yml", new UniversalFile(getDataFolder(), "config.yml").getPath());
2016-12-20 00:31:01 +01:00
Bukkit.getLogger().info("SubServers > Created ~/plugins/SubServers/config.yml");
2016-12-28 01:15:36 +01:00
} else if ((new Version((new YAMLConfig(new UniversalFile(getDataFolder(), "config.yml"))).get().getSection("Settings").getString("Version", "0")).compareTo(new Version("2.11.2a+"))) != 0) {
2016-12-20 00:31:01 +01:00
Files.move(new UniversalFile(getDataFolder(), "config.yml").toPath(), new UniversalFile(getDataFolder(), "config.old" + Math.round(Math.random() * 100000) + ".yml").toPath());
2016-12-27 17:03:19 +01:00
Util.copyFromJar(SubPlugin.class.getClassLoader(), "config.yml", new UniversalFile(getDataFolder(), "config.yml").getPath());
2016-12-20 00:31:01 +01:00
Bukkit.getLogger().info("SubServers > Updated ~/plugins/SubServers/config.yml");
}
2017-01-26 23:19:48 +01:00
config = new YAMLConfig(new UniversalFile(getDataFolder(), "config.yml"));
if (new UniversalFile(new File(System.getProperty("user.dir")), "subservers.client").exists()) {
FileReader reader = new FileReader(new UniversalFile(new File(System.getProperty("user.dir")), "subservers.client"));
config.get().getSection("Settings").set("SubData", new YAMLSection(new Gson().fromJson(Util.readAll(reader), Map.class)));
config.save();
reader.close();
new UniversalFile(new File(System.getProperty("user.dir")), "subservers.client").delete();
}
reload(false);
2016-12-20 00:31:01 +01:00
2017-01-26 23:19:48 +01:00
if (config.get().getSection("Settings").getBoolean("Ingame-Access", true)) {
gui = new InternalUIHandler(this);
2017-01-26 23:19:48 +01:00
SubCommand cmd = new SubCommand(this);
getCommand("subservers").setExecutor(cmd);
getCommand("subserver").setExecutor(cmd);
getCommand("sub").setExecutor(cmd);
}
2017-11-22 22:58:33 +01:00
2018-03-21 21:45:59 +01:00
new Metrics(this);
2017-11-22 22:58:33 +01:00
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
try {
2018-07-27 21:36:51 +02:00
YAMLSection tags = new YAMLSection(new Gson().fromJson("{\"tags\":" + Util.readAll(new BufferedReader(new InputStreamReader(new URL("https://api.github.com/repos/ME1312/SubServers-2/git/refs/tags").openStream(), Charset.forName("UTF-8")))) + '}', Map.class));
2017-11-22 22:58:33 +01:00
Version updversion = version;
2018-03-14 22:50:15 +01:00
int updcount = 0;
2018-07-27 21:36:51 +02:00
for (YAMLSection tag : tags.getSectionList("tags")) {
Version version = Version.fromString(tag.getString("ref").substring(10));
if (!version.equals(version) && version.compareTo(updversion) > 0) {
updversion = version;
updcount++;
2017-11-22 22:58:33 +01:00
}
}
if (updcount > 0) Bukkit.getLogger().info("SubServers > SubServers.Client.Bukkit v" + updversion + " is available. You are " + updcount + " version" + ((updcount == 1)?"":"s") + " behind.");
2017-11-22 22:58:33 +01:00
} catch (Exception e) {}
}, 0, TimeUnit.DAYS.toSeconds(2) * 20);
2016-12-20 00:31:01 +01:00
} catch (IOException e) {
e.printStackTrace();
}
}
public void reload(boolean notifyPlugins) throws IOException {
if (subdata != null)
subdata.destroy(0);
config.reload();
Cipher cipher = null;
if (!config.get().getSection("Settings").getSection("SubData").getRawString("Encryption", "NONE").equalsIgnoreCase("NONE")) {
if (config.get().getSection("Settings").getSection("SubData").getString("Password", "").length() == 0) {
Bukkit.getLogger().info("SubData > Cannot encrypt connection without a password");
} else if (!SubDataClient.getCiphers().keySet().contains(config.get().getSection("Settings").getSection("SubData").getRawString("Encryption").toUpperCase().replace('-', '_').replace(' ', '_'))) {
Bukkit.getLogger().info("SubData > Unknown encryption type: " + config.get().getSection("Settings").getSection("SubData").getRawString("Encryption"));
} else {
cipher = SubDataClient.getCipher(config.get().getSection("Settings").getSection("SubData").getRawString("Encryption"));
}
}
subdata = new SubDataClient(this, config.get().getSection("Settings").getSection("SubData").getString("Name", null),
InetAddress.getByName(config.get().getSection("Settings").getSection("SubData").getString("Address", "127.0.0.1:4391").split(":")[0]),
Integer.parseInt(config.get().getSection("Settings").getSection("SubData").getString("Address", "127.0.0.1:4391").split(":")[1]), cipher);
if (notifyPlugins) {
List<Runnable> listeners = api.reloadListeners;
if (listeners.size() > 0) {
for (Object obj : listeners) {
try {
((Runnable) obj).run();
} catch (Throwable e) {
new InvocationTargetException(e, "Problem reloading plugin").printStackTrace();
}
}
}
}
}
2017-01-08 20:42:40 +01:00
/**
* Disable Plugin
*/
2016-12-20 00:31:01 +01:00
@Override
public void onDisable() {
2017-01-21 17:49:37 +01:00
if (subdata != null) try {
subdata.destroy(0);
2017-01-21 17:49:37 +01:00
} catch (IOException e) {
e.printStackTrace();
}
setEnabled(false);
2016-12-20 00:31:01 +01:00
}
}