forked from afspiegeling/NitoriBot
move functions that i call at boot to their own classes
This commit is contained in:
@@ -2,34 +2,30 @@ package land.chipmunk.parker2991.nitoribot;
|
||||
|
||||
import land.chipmunk.parker2991.nitoribot.data.buildstring.BotBuildInfo;
|
||||
import land.chipmunk.parker2991.nitoribot.data.buildstring.RepoCommitInfo;
|
||||
import land.chipmunk.parker2991.nitoribot.listeners.Listener;
|
||||
import land.chipmunk.parker2991.nitoribot.listeners.ListenerManager;
|
||||
import land.chipmunk.parker2991.nitoribot.logger.Logger;
|
||||
import land.chipmunk.parker2991.nitoribot.boot.GetProxiesList;
|
||||
import land.chipmunk.parker2991.nitoribot.modules.*;
|
||||
import land.chipmunk.parker2991.nitoribot.util.ComponentUtil;
|
||||
import land.chipmunk.parker2991.nitoribot.listeners.*;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import org.geysermc.mcprotocollib.network.ProxyInfo;
|
||||
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
|
||||
import org.geysermc.mcprotocollib.auth.GameProfile;
|
||||
import org.geysermc.mcprotocollib.network.Session;
|
||||
import org.geysermc.mcprotocollib.network.event.session.DisconnectingEvent;
|
||||
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
|
||||
import org.geysermc.mcprotocollib.network.event.session.DisconnectingEvent;
|
||||
import org.geysermc.mcprotocollib.network.event.session.PacketErrorEvent;
|
||||
import org.geysermc.mcprotocollib.network.event.session.SessionAdapter;
|
||||
import org.geysermc.mcprotocollib.network.session.ClientNetworkSession;
|
||||
import org.geysermc.mcprotocollib.network.factory.ClientNetworkSessionFactory;
|
||||
import org.geysermc.mcprotocollib.network.packet.Packet;
|
||||
import org.geysermc.mcprotocollib.auth.GameProfile;
|
||||
import org.geysermc.mcprotocollib.network.session.ClientNetworkSession;
|
||||
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundPlayerLoadedPacket;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundLoginFinishedPacket;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class Bot extends SessionAdapter {
|
||||
public final ListenerManager listenerManager = new ListenerManager();
|
||||
@@ -82,36 +78,6 @@ public class Bot extends SessionAdapter {
|
||||
|
||||
public MCServerModule mcServer;
|
||||
|
||||
public ProxyInfo randomProxyIp () throws IOException {
|
||||
Path proxiesPath = Paths.get("proxies.txt");
|
||||
|
||||
int countLines = Math.round(
|
||||
Files.lines(proxiesPath).count()
|
||||
);
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
int randomIndex = random.nextInt(countLines);
|
||||
|
||||
List<String> lines = Files.lines(proxiesPath).toList();//.toList();
|
||||
|
||||
String[] ip = lines.stream()
|
||||
.skip(randomIndex)
|
||||
.findAny()
|
||||
.get()
|
||||
.split(":");
|
||||
|
||||
InetSocketAddress address = new InetSocketAddress(
|
||||
ip[0],
|
||||
Integer.parseInt(ip[1])
|
||||
);
|
||||
|
||||
return new ProxyInfo(
|
||||
ProxyInfo.Type.SOCKS5,
|
||||
address
|
||||
);
|
||||
}
|
||||
|
||||
public void loadModules () {
|
||||
try {
|
||||
modules = List.of(
|
||||
@@ -153,7 +119,7 @@ public class Bot extends SessionAdapter {
|
||||
options.host,
|
||||
options.port
|
||||
)
|
||||
.setProxy(randomProxyIp())
|
||||
.setProxy(new GetProxiesList().randomProxyIp())
|
||||
.setProtocol(protocol)
|
||||
.create();
|
||||
|
||||
|
||||
@@ -1,122 +1,46 @@
|
||||
package land.chipmunk.parker2991.nitoribot;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import land.chipmunk.parker2991.nitoribot.boot.*;
|
||||
import land.chipmunk.parker2991.nitoribot.data.buildstring.BotBuildInfo;
|
||||
import land.chipmunk.parker2991.nitoribot.data.buildstring.RepoCommitInfo;
|
||||
import land.chipmunk.parker2991.nitoribot.modules.ConsoleModule;
|
||||
import land.chipmunk.parker2991.nitoribot.logger.Logger;
|
||||
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class Main {
|
||||
public final Object obj = new Object();
|
||||
|
||||
public static final ExecutorService executorService = Executors.newFixedThreadPool(
|
||||
Runtime.getRuntime().availableProcessors()
|
||||
);
|
||||
|
||||
public static final ExecutorService virtualThreads = Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
public static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(
|
||||
Runtime.getRuntime().availableProcessors()
|
||||
);
|
||||
|
||||
public static final ConcurrentMap<String, Future<?>> services = new ConcurrentHashMap<>();
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
public static RepoCommitInfo repoCommitInfo = null;
|
||||
|
||||
public static BotBuildInfo getBuildInfo () {
|
||||
Reader reader = new InputStreamReader(
|
||||
Objects.requireNonNull(Main.class
|
||||
.getResourceAsStream("/info.json"))
|
||||
);
|
||||
|
||||
return GSON.fromJson(reader, BotBuildInfo.class);
|
||||
}
|
||||
|
||||
public static RepoCommitInfo getRepoInfo () {
|
||||
final String repo = "https://code.chipmunk.land/api/v1/repos/Parker2991/NitoriBot/commits?sha=main";
|
||||
HttpClient client = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.build();
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(repo))
|
||||
.timeout(Duration.ofSeconds(20))
|
||||
.header("accept", "application/json")
|
||||
.build();
|
||||
|
||||
RepoCommitInfo info = null;
|
||||
try {
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
String body = response.body();
|
||||
JsonElement e = JsonParser.parseString(response.body());
|
||||
|
||||
info = GSON.fromJson(e.getAsJsonArray().get(0), RepoCommitInfo.class);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
public static RepoCommitInfo repoCommitInfo = null;// = getRepoInfo();
|
||||
|
||||
public static BotBuildInfo botBuildInfo = null;// = getBuildInfo();
|
||||
public static BotBuildInfo botBuildInfo = null;
|
||||
|
||||
public Config config;
|
||||
|
||||
public static final List<Bot> Bots = new ArrayList<>();
|
||||
|
||||
public Config loadConfig() throws IOException {
|
||||
final Constructor yamlConfig = new Constructor(Config.class, new LoaderOptions());
|
||||
final Yaml yaml = new Yaml(yamlConfig);
|
||||
final Path configPath = Path.of("config.yaml");
|
||||
if (!Files.exists(configPath)) {
|
||||
|
||||
Logger.INFO(null, "config not found making config now");
|
||||
|
||||
InputStream defaultConfig = Main.class.getClassLoader().getResourceAsStream("default_config.yaml");
|
||||
Files.copy(defaultConfig, configPath);
|
||||
}
|
||||
|
||||
InputStream configFile = Files.newInputStream(configPath);
|
||||
|
||||
config = yaml.load(configFile);
|
||||
return config;
|
||||
}
|
||||
|
||||
public static ConsoleModule console;
|
||||
|
||||
public void main(String[] args) {
|
||||
try {
|
||||
config = loadConfig();
|
||||
config = new LoadConfig().loadConfig();
|
||||
Config.Options[] bots = config.bots;
|
||||
|
||||
console = new ConsoleModule(config);
|
||||
|
||||
repoCommitInfo = getRepoInfo();
|
||||
botBuildInfo = getBuildInfo();
|
||||
repoCommitInfo = new GetRepoInfo().getRepoInfo();
|
||||
botBuildInfo = new GetBotBuildInfo().getBuildInfo();
|
||||
|
||||
for (Config.Options options : bots) {
|
||||
final Bot bot = new Bot(options, Bots, config);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package land.chipmunk.parker2991.nitoribot.boot;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import land.chipmunk.parker2991.nitoribot.Main;
|
||||
import land.chipmunk.parker2991.nitoribot.data.buildstring.BotBuildInfo;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.Objects;
|
||||
|
||||
public class GetBotBuildInfo {
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
public BotBuildInfo getBuildInfo () {
|
||||
Reader reader = new InputStreamReader(
|
||||
Objects.requireNonNull(Main.class
|
||||
.getResourceAsStream("/info.json"))
|
||||
);
|
||||
|
||||
return GSON.fromJson(reader, BotBuildInfo.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package land.chipmunk.parker2991.nitoribot.boot;
|
||||
|
||||
import org.geysermc.mcprotocollib.network.ProxyInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class GetProxiesList {
|
||||
public ProxyInfo randomProxyIp () throws IOException {
|
||||
Path proxiesPath = Paths.get("proxies.txt");
|
||||
|
||||
int countLines = Math.round(
|
||||
Files.lines(proxiesPath).count()
|
||||
);
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
int randomIndex = random.nextInt(countLines);
|
||||
|
||||
List<String> lines = Files.lines(proxiesPath).toList();//.toList();
|
||||
|
||||
String[] ip = lines.stream()
|
||||
.skip(randomIndex)
|
||||
.findAny()
|
||||
.get()
|
||||
.split(":");
|
||||
|
||||
InetSocketAddress address = new InetSocketAddress(
|
||||
ip[0],
|
||||
Integer.parseInt(ip[1])
|
||||
);
|
||||
|
||||
return new ProxyInfo(
|
||||
ProxyInfo.Type.SOCKS5,
|
||||
address
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package land.chipmunk.parker2991.nitoribot.boot;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import land.chipmunk.parker2991.nitoribot.data.buildstring.RepoCommitInfo;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
|
||||
public class GetRepoInfo {
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
public RepoCommitInfo getRepoInfo () {
|
||||
final String repo = "https://code.chipmunk.land/api/v1/repos/Parker2991/NitoriBot/commits?sha=main";
|
||||
HttpClient client = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.build();
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(repo))
|
||||
.timeout(Duration.ofSeconds(20))
|
||||
.header("accept", "application/json")
|
||||
.build();
|
||||
|
||||
RepoCommitInfo info = null;
|
||||
try {
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
String body = response.body();
|
||||
JsonElement e = JsonParser.parseString(response.body());
|
||||
|
||||
info = GSON.fromJson(e.getAsJsonArray().get(0), RepoCommitInfo.class);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package land.chipmunk.parker2991.nitoribot.boot;
|
||||
|
||||
import land.chipmunk.parker2991.nitoribot.Config;
|
||||
import land.chipmunk.parker2991.nitoribot.Main;
|
||||
import land.chipmunk.parker2991.nitoribot.logger.Logger;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class LoadConfig {
|
||||
public Config loadConfig () throws IOException {
|
||||
final Constructor yamlConfig = new Constructor(Config.class, new LoaderOptions());
|
||||
final Yaml yaml = new Yaml(yamlConfig);
|
||||
final Path configPath = Path.of("config.yaml");
|
||||
if (! Files.exists(configPath)) {
|
||||
|
||||
Logger.INFO(null, "config not found making config now");
|
||||
|
||||
InputStream defaultConfig = Main.class.getClassLoader().getResourceAsStream("default_config.yaml");
|
||||
Files.copy(defaultConfig, configPath);
|
||||
}
|
||||
|
||||
InputStream configFile = Files.newInputStream(configPath);
|
||||
|
||||
//config = yaml.load(configFile);
|
||||
return yaml.load(configFile);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user