start plugin stuff

This commit is contained in:
EOT3000 2019-07-08 15:40:49 -04:00
parent c7b36bd834
commit 6f3c00c6ab
3 changed files with 98 additions and 4 deletions

View file

@ -14,11 +14,10 @@
package org.geysermc.connector.console;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -37,7 +36,7 @@ public class LoggingOutputStream extends ByteArrayOutputStream {
super.write(b);
try {
String contents = toString(Charsets.UTF_8.name());
String contents = toString(StandardCharsets.UTF_8.name());
if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator")))
logger.logp(level, "", "", contents);
} catch (UnsupportedEncodingException ex) {
@ -47,7 +46,7 @@ public class LoggingOutputStream extends ByteArrayOutputStream {
@Override
public synchronized void flush() throws IOException {
String contents = toString(Charsets.UTF_8.name());
String contents = toString(StandardCharsets.UTF_8.name());
super.reset();
if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator")))

View file

@ -0,0 +1,81 @@
package org.geysermc.connector.plugin;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.geysermc.api.plugin.Plugin;
import java.io.File;
import java.io.InputStream;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Loader extends ClassLoader {
static {
System.out.println("a");
Loader l = new Loader();
System.out.println("b");
File dir = new File("plugins");
System.out.println(dir.getAbsoluteFile());
if(!dir.exists()) {
dir.mkdir();
}
for(File f : dir.listFiles()) {
if(f.getName().endsWith(".jar")) {
try {
ZipFile file = new ZipFile(f);
ZipEntry e = file.getEntry("plugin.yml");
if(e == null || e.isDirectory()) {
System.err.println("Plugin " + f.getName() + " has no valid plugin.yml!");
continue;
}
file.stream().forEach((x) -> {
if(x.getName().endsWith(".class")) {
try {
InputStream is = file.getInputStream(x);
byte[] b = new byte[is.available()];
is.read(b);
l.defineClass(x.getName().replace(".class", "").replaceAll("/", "."), b, 0, b.length);
is.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
InputStream is = file.getInputStream(e);
PluginYML yml;
yml = mapper.readValue(is, PluginYML.class);
is.close();
((Plugin) Class.forName(yml.main, true, l).newInstance()).onEnable();
} catch (Exception e) {
System.out.println("Error loading plugin " + f.getName());
e.printStackTrace();
}
}
}
LOADER = l;
}
public static final Loader LOADER;
public static void start() {
}
private Loader() {
}
}

View file

@ -0,0 +1,14 @@
package org.geysermc.connector.plugin;
import com.fasterxml.jackson.annotation.JsonProperty;
public class PluginYML {
@JsonProperty("name")
String name;
@JsonProperty("version")
String version;
@JsonProperty("main")
String main;
}