More mod setup
This commit is contained in:
@@ -13,6 +13,7 @@ import io.anuke.mindustry.game.EventType.*;
|
|||||||
import io.anuke.mindustry.gen.*;
|
import io.anuke.mindustry.gen.*;
|
||||||
import io.anuke.mindustry.graphics.*;
|
import io.anuke.mindustry.graphics.*;
|
||||||
import io.anuke.mindustry.maps.*;
|
import io.anuke.mindustry.maps.*;
|
||||||
|
import io.anuke.mindustry.mod.*;
|
||||||
import io.anuke.mindustry.net.Net;
|
import io.anuke.mindustry.net.Net;
|
||||||
|
|
||||||
import static io.anuke.arc.Core.*;
|
import static io.anuke.arc.Core.*;
|
||||||
@@ -108,6 +109,7 @@ public abstract class ClientLauncher extends ApplicationCore implements Platform
|
|||||||
listener.init();
|
listener.init();
|
||||||
}
|
}
|
||||||
super.resize(graphics.getWidth(), graphics.getHeight());
|
super.resize(graphics.getWidth(), graphics.getHeight());
|
||||||
|
mods.each(Mod::init);
|
||||||
finished = true;
|
finished = true;
|
||||||
Events.fire(new ClientLoadEvent());
|
Events.fire(new ClientLoadEvent());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,6 +245,7 @@ public class Vars implements Loadable{
|
|||||||
|
|
||||||
modDirectory.mkdirs();
|
modDirectory.mkdirs();
|
||||||
|
|
||||||
|
mods.load();
|
||||||
maps.load();
|
maps.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -200,6 +200,11 @@ public class NetServer implements ApplicationListener{
|
|||||||
registerCommands();
|
registerCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(){
|
||||||
|
mods.each(mod -> mod.registerClientCommands(clientCommands));
|
||||||
|
}
|
||||||
|
|
||||||
private void registerCommands(){
|
private void registerCommands(){
|
||||||
clientCommands.<Player>register("help", "[page]", "Lists all commands.", (args, player) -> {
|
clientCommands.<Player>register("help", "[page]", "Lists all commands.", (args, player) -> {
|
||||||
if(args.length > 0 && !Strings.canParseInt(args[0])){
|
if(args.length > 0 && !Strings.canParseInt(args[0])){
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import io.anuke.arc.files.*;
|
|||||||
import io.anuke.arc.function.*;
|
import io.anuke.arc.function.*;
|
||||||
import io.anuke.arc.util.*;
|
import io.anuke.arc.util.*;
|
||||||
import io.anuke.mindustry.io.*;
|
import io.anuke.mindustry.io.*;
|
||||||
import io.anuke.mindustry.plugin.*;
|
|
||||||
import io.anuke.mindustry.plugin.Plugins.*;
|
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
|
|||||||
@@ -1,93 +0,0 @@
|
|||||||
package io.anuke.mindustry.plugin;
|
|
||||||
|
|
||||||
import io.anuke.annotations.Annotations.*;
|
|
||||||
import io.anuke.arc.collection.*;
|
|
||||||
import io.anuke.arc.files.*;
|
|
||||||
import io.anuke.arc.function.*;
|
|
||||||
import io.anuke.arc.util.*;
|
|
||||||
import io.anuke.mindustry.io.*;
|
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
|
|
||||||
import static io.anuke.mindustry.Vars.pluginDirectory;
|
|
||||||
|
|
||||||
public class Plugins{
|
|
||||||
private Array<LoadedPlugin> loaded = new Array<>();
|
|
||||||
private ObjectMap<Class<?>, PluginMeta> metas = new ObjectMap<>();
|
|
||||||
|
|
||||||
/** Returns a file named 'config.json' in a special folder for the specified plugin.
|
|
||||||
* Call this in init(). */
|
|
||||||
public FileHandle getConfig(Plugin plugin){
|
|
||||||
PluginMeta load = metas.get(plugin.getClass());
|
|
||||||
if(load == null) throw new IllegalArgumentException("Plugin is not loaded yet (or missing)!");
|
|
||||||
return pluginDirectory.child(load.name).child("config.json");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @return the loaded plugin found by class, or null if not found. */
|
|
||||||
public @Nullable LoadedPlugin getPlugin(Class<? extends Plugin> type){
|
|
||||||
return loaded.find(l -> l.plugin.getClass() == type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Loads all plugins from the folder, but does call any methods on them.*/
|
|
||||||
public void load(){
|
|
||||||
for(FileHandle file : pluginDirectory.list()){
|
|
||||||
if(!file.extension().equals("jar")) continue;
|
|
||||||
|
|
||||||
try{
|
|
||||||
loaded.add(loadPlugin(file));
|
|
||||||
}catch(IllegalArgumentException ignored){
|
|
||||||
}catch(Exception e){
|
|
||||||
Log.err("Failed to load plugin file {0}. Skipping.", file);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @return all loaded plugins. */
|
|
||||||
public Array<LoadedPlugin> all(){
|
|
||||||
return loaded;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Iterates through each plugin.*/
|
|
||||||
public void each(Consumer<Plugin> cons){
|
|
||||||
loaded.each(p -> cons.accept(p.plugin));
|
|
||||||
}
|
|
||||||
|
|
||||||
private LoadedPlugin loadPlugin(FileHandle jar) throws Exception{
|
|
||||||
FileHandle zip = new ZipFileHandle(jar);
|
|
||||||
|
|
||||||
FileHandle metaf = zip.child("plugin.json");
|
|
||||||
if(!metaf.exists()){
|
|
||||||
Log.warn("Plugin {0} doesn't have a 'plugin.json' file, skipping.", jar);
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
|
|
||||||
PluginMeta meta = JsonIO.read(PluginMeta.class, metaf.readString());
|
|
||||||
|
|
||||||
URLClassLoader classLoader = new URLClassLoader(new URL[]{jar.file().toURI().toURL()}, ClassLoader.getSystemClassLoader());
|
|
||||||
Class<?> main = classLoader.loadClass(meta.main);
|
|
||||||
metas.put(main, meta);
|
|
||||||
return new LoadedPlugin(jar, zip, (Plugin)main.getDeclaredConstructor().newInstance(), meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Represents a plugin that has been loaded from a jar file.*/
|
|
||||||
public static class LoadedPlugin{
|
|
||||||
public final FileHandle jarFile;
|
|
||||||
public final FileHandle zipRoot;
|
|
||||||
public final Plugin plugin;
|
|
||||||
public final PluginMeta meta;
|
|
||||||
|
|
||||||
public LoadedPlugin(FileHandle jarFile, FileHandle zipRoot, Plugin plugin, PluginMeta meta){
|
|
||||||
this.zipRoot = zipRoot;
|
|
||||||
this.jarFile = jarFile;
|
|
||||||
this.plugin = plugin;
|
|
||||||
this.meta = meta;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Plugin metadata information.*/
|
|
||||||
public static class PluginMeta{
|
|
||||||
public String name, author, main, description;
|
|
||||||
public String version;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package io.anuke.mindustry.server;
|
package io.anuke.mindustry.server;
|
||||||
|
|
||||||
import io.anuke.arc.*;
|
import io.anuke.arc.*;
|
||||||
import io.anuke.arc.util.*;
|
|
||||||
import io.anuke.mindustry.*;
|
import io.anuke.mindustry.*;
|
||||||
import io.anuke.mindustry.core.*;
|
import io.anuke.mindustry.core.*;
|
||||||
|
import io.anuke.mindustry.mod.*;
|
||||||
|
|
||||||
import static io.anuke.mindustry.Vars.*;
|
import static io.anuke.mindustry.Vars.*;
|
||||||
|
|
||||||
@@ -28,5 +28,9 @@ public class MindustryServer implements ApplicationListener{
|
|||||||
Core.app.addListener(logic = new Logic());
|
Core.app.addListener(logic = new Logic());
|
||||||
Core.app.addListener(netServer = new NetServer());
|
Core.app.addListener(netServer = new NetServer());
|
||||||
Core.app.addListener(new ServerControl(args));
|
Core.app.addListener(new ServerControl(args));
|
||||||
|
|
||||||
|
mods.each(Mod::init);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ import io.anuke.mindustry.maps.*;
|
|||||||
import io.anuke.mindustry.mod.Mods.*;
|
import io.anuke.mindustry.mod.Mods.*;
|
||||||
import io.anuke.mindustry.net.Administration.*;
|
import io.anuke.mindustry.net.Administration.*;
|
||||||
import io.anuke.mindustry.net.Packets.*;
|
import io.anuke.mindustry.net.Packets.*;
|
||||||
import io.anuke.mindustry.plugin.*;
|
|
||||||
import io.anuke.mindustry.plugin.Plugins.*;
|
|
||||||
import io.anuke.mindustry.type.*;
|
import io.anuke.mindustry.type.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -52,7 +50,6 @@ public class ServerControl implements ApplicationListener{
|
|||||||
private PrintWriter socketOutput;
|
private PrintWriter socketOutput;
|
||||||
|
|
||||||
public ServerControl(String[] args){
|
public ServerControl(String[] args){
|
||||||
|
|
||||||
Core.settings.defaults(
|
Core.settings.defaults(
|
||||||
"shufflemode", "normal",
|
"shufflemode", "normal",
|
||||||
"bans", "",
|
"bans", "",
|
||||||
@@ -110,9 +107,6 @@ public class ServerControl implements ApplicationListener{
|
|||||||
Effects.setScreenShakeProvider((a, b) -> {});
|
Effects.setScreenShakeProvider((a, b) -> {});
|
||||||
Effects.setEffectProvider((a, b, c, d, e, f) -> {});
|
Effects.setEffectProvider((a, b, c, d, e, f) -> {});
|
||||||
|
|
||||||
//load plugins
|
|
||||||
plugins.load();
|
|
||||||
|
|
||||||
registerCommands();
|
registerCommands();
|
||||||
|
|
||||||
Core.app.post(() -> {
|
Core.app.post(() -> {
|
||||||
@@ -176,9 +170,6 @@ public class ServerControl implements ApplicationListener{
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//initialize plugins
|
|
||||||
plugins.each(Plugin::init);
|
|
||||||
|
|
||||||
if(!mods.all().isEmpty()){
|
if(!mods.all().isEmpty()){
|
||||||
info("&lc{0} plugins loaded.", mods.all().size);
|
info("&lc{0} plugins loaded.", mods.all().size);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user