Better config?
This commit is contained in:
parent
e30ed17c44
commit
22cbd28379
6 changed files with 57 additions and 71 deletions
|
@ -1,6 +1,5 @@
|
|||
package land.chipmunk.chipmunkbot;
|
||||
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.protocol.MinecraftProtocol;
|
||||
import com.github.steveice10.packetlib.ProxyInfo;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
|
@ -12,17 +11,21 @@ import java.lang.reflect.Constructor;
|
|||
import java.lang.reflect.InvocationTargetException;
|
||||
import lombok.Getter;
|
||||
import land.chipmunk.chipmunkbot.plugins.*;
|
||||
import java.util.List;
|
||||
|
||||
public class ChipmunkBot extends Client {
|
||||
@Getter public final ChatPlugin chat = new ChatPlugin(this);
|
||||
@Getter public final QueryPlugin query = new QueryPlugin(this);
|
||||
@Getter public final PlayerListPlugin playerList = new PlayerListPlugin(this);
|
||||
@Getter public final CommandManager commandManager = new CommandManager(this);
|
||||
@Getter public final ChatCommandHandler chatCommandHandler = new ChatCommandHandler(this);
|
||||
@Getter public final ChatCommandHandler chatCommandHandler;
|
||||
@Getter public final PositionManager position = new PositionManager(this);
|
||||
@Getter public final CommandCore core = new CommandCore(this);
|
||||
@Getter public final SelfCarePlugin selfCare = new SelfCarePlugin(this);
|
||||
@Getter public final SongPlayer songPlayer = new SongPlayer(this);
|
||||
|
||||
public ChipmunkBot (ClientOptions options) { super(options); }
|
||||
public ChipmunkBot (Options options, List<Client> allClients) {
|
||||
super(options.host, options.port, new MinecraftProtocol(options.username), null, options.reconnectDelay, allClients);
|
||||
chatCommandHandler = new ChatCommandHandler(this, options.commands);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package land.chipmunk.chipmunkbot;
|
||||
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.protocol.MinecraftProtocol;
|
||||
import com.github.steveice10.packetlib.packet.PacketProtocol;
|
||||
import com.github.steveice10.packetlib.ProxyInfo;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.tcp.TcpClientSession;
|
||||
|
@ -12,21 +11,34 @@ import java.util.TimerTask;
|
|||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class Client {
|
||||
@Getter private Session session;
|
||||
@Getter private final ClientOptions options;
|
||||
@Getter private final List<SessionListener> listeners = new ArrayList<>();
|
||||
|
||||
public Client (ClientOptions options) {
|
||||
this.options = new ClientOptions(options.host(), options.port(), options.protocol(), options.proxy(), options.reconnectDelay(), options.allClients());
|
||||
private String host;
|
||||
private int port;
|
||||
private PacketProtocol protocol;
|
||||
private ProxyInfo proxy;
|
||||
|
||||
@Getter @Setter private long reconnectDelay;
|
||||
@Getter @Setter private List<Client> allClients;
|
||||
|
||||
public Client (String host, int port, PacketProtocol protocol, ProxyInfo proxy, long reconnectDelay, List<Client> allClients) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.protocol = protocol;
|
||||
this.proxy = proxy;
|
||||
this.reconnectDelay = reconnectDelay;
|
||||
this.allClients = allClients;
|
||||
reconnect();
|
||||
}
|
||||
|
||||
public void addListener (SessionListener listener) { listeners.add(listener); }
|
||||
|
||||
public void reconnect () { // ? Should this be public?
|
||||
final Session session = new TcpClientSession(options.host(), options.port(), options.protocol(), options.proxy());
|
||||
final Session session = new TcpClientSession(host, port, protocol, proxy);
|
||||
this.session = session;
|
||||
|
||||
session.addListener(new SessionAdapter () {
|
||||
|
@ -41,7 +53,7 @@ public class Client {
|
|||
public void disconnected (DisconnectedEvent event) {
|
||||
for (SessionListener listener : listeners) listener.disconnected(event);
|
||||
|
||||
if (options.reconnectDelay() < 0) return;
|
||||
if (reconnectDelay < 0) return;
|
||||
|
||||
TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
|
@ -50,13 +62,10 @@ public class Client {
|
|||
}
|
||||
};
|
||||
|
||||
new Timer().schedule(task, options.reconnectDelay());
|
||||
new Timer().schedule(task, reconnectDelay);
|
||||
}
|
||||
});
|
||||
|
||||
session.connect();
|
||||
}
|
||||
|
||||
public long reconnectDelay () { return options.reconnectDelay(); }
|
||||
public List<Client> allClients () { return options.allClients(); }
|
||||
}
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
package land.chipmunk.chipmunkbot;
|
||||
|
||||
import com.github.steveice10.mc.auth.service.AuthenticationService;
|
||||
import com.github.steveice10.mc.protocol.MinecraftProtocol;
|
||||
import com.github.steveice10.packetlib.ProxyInfo;
|
||||
import com.github.steveice10.packetlib.Session;
|
||||
import com.github.steveice10.packetlib.tcp.TcpClientSession;
|
||||
import lombok.Data;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ClientOptions {
|
||||
private String host;
|
||||
private int port;
|
||||
private MinecraftProtocol protocol;
|
||||
private ProxyInfo proxy;
|
||||
private long reconnectDelay = 1000l;
|
||||
private List<Client> allClients = new ArrayList<>();
|
||||
|
||||
/* public ClientOptions profile (GameProfile profile) {
|
||||
protocol(new MinecraftProtocol(profile));
|
||||
return this;
|
||||
} */
|
||||
|
||||
public ClientOptions username (String username) {
|
||||
protocol(new MinecraftProtocol(username));
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -18,14 +18,12 @@ import java.io.IOException;
|
|||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class Main {
|
||||
private static JsonObject getConfig (File file) throws IOException {
|
||||
private static Configuration getConfig (File file) throws IOException {
|
||||
final Gson gson = new Gson();
|
||||
|
||||
if (!file.exists()) {
|
||||
// Read the default config
|
||||
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json");
|
||||
|
@ -45,27 +43,19 @@ public class Main {
|
|||
System.out.println("The config.json file was not found, so a default one was created.");
|
||||
|
||||
// Return the default config (instead of reading again, for efficiency)
|
||||
return JsonParser.parseString(defaultConfig).getAsJsonObject();
|
||||
return gson.fromJson(defaultConfig, Configuration.class);
|
||||
}
|
||||
|
||||
InputStream opt = new FileInputStream(file);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
|
||||
|
||||
return JsonParser.parseReader(reader).getAsJsonObject();
|
||||
}
|
||||
|
||||
public static ClientOptions parseClientOptions (JsonObject options) {
|
||||
return new ClientOptions()
|
||||
.host(options.has("host") ? options.get("host").getAsString() : "0.0.0.0")
|
||||
.port(options.has("port") ? options.get("port").getAsInt() : 25565)
|
||||
.username(options.has("username") ? options.get("username").getAsString() : "Player")
|
||||
.reconnectDelay(options.has("reconnectDelay") ? options.get("reconnectDelay").getAsLong() : 0l);
|
||||
return gson.fromJson(reader, Configuration.class);
|
||||
}
|
||||
|
||||
public static void main (String[] arguments) {
|
||||
System.out.println("ChipmunkBot is starting...");
|
||||
|
||||
JsonObject config = null;
|
||||
Configuration config = null;
|
||||
try {
|
||||
config = getConfig(new File(arguments.length > 0 ? arguments[0] : "config.json"));
|
||||
} catch (Exception exception) {
|
||||
|
@ -75,12 +65,13 @@ public class Main {
|
|||
|
||||
List<Client> allClients = new ArrayList<>();
|
||||
|
||||
for (JsonElement element : config.get("bots").getAsJsonArray()) {
|
||||
ClientOptions options = parseClientOptions(element.getAsJsonObject());
|
||||
options.allClients(allClients);
|
||||
|
||||
final Client client = new ChipmunkBot(options); // TODO: Maybe create a list of some sort
|
||||
for (Options options : config.bots) {
|
||||
final Client client = new ChipmunkBot(options, allClients);
|
||||
allClients.add(client);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Configuration {
|
||||
public Options[] bots;
|
||||
}
|
||||
}
|
||||
|
|
15
src/main/java/land/chipmunk/chipmunkbot/Options.java
Normal file
15
src/main/java/land/chipmunk/chipmunkbot/Options.java
Normal file
|
@ -0,0 +1,15 @@
|
|||
package land.chipmunk.chipmunkbot;
|
||||
|
||||
public class Options {
|
||||
public String host = "0.0.0.0";
|
||||
public int port = 25565;
|
||||
public String username = "Player";
|
||||
// public ProxyInfo proxy;
|
||||
public long reconnectDelay = 1000;
|
||||
|
||||
public Commands commands = new Commands();
|
||||
|
||||
public class Commands {
|
||||
public String prefix = "default.";
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package land.chipmunk.chipmunkbot.plugins;
|
||||
|
||||
import land.chipmunk.chipmunkbot.ChipmunkBot;
|
||||
import land.chipmunk.chipmunkbot.Options;
|
||||
import land.chipmunk.chipmunkbot.command.CommandSource;
|
||||
import land.chipmunk.chipmunkbot.command.PlayerCommandSource;
|
||||
import land.chipmunk.chipmunkbot.command.ComponentMessage;
|
||||
|
@ -18,10 +19,11 @@ import lombok.Setter;
|
|||
|
||||
public class ChatCommandHandler extends ChatPlugin.Listener {
|
||||
private ChipmunkBot client;
|
||||
@Getter @Setter private String prefix = "'"; // TODO: Don't hardcode this
|
||||
@Getter @Setter private String prefix;
|
||||
|
||||
public ChatCommandHandler (ChipmunkBot client) {
|
||||
public ChatCommandHandler (ChipmunkBot client, Options.Commands options) {
|
||||
this.client = client;
|
||||
this.prefix = options.prefix;
|
||||
client.chat().addListener((ChatPlugin.Listener) this);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue