Compare commits

...

5 commits

13 changed files with 108 additions and 81 deletions

View file

@ -98,16 +98,9 @@ public class Bot {
this.bots = bots;
this.config = config;
ConsolePlugin.addListener(new ConsolePlugin.Listener() {
@Override
public void ready() {
Bot.this.ready();
}
});
}
public void ready () {
public void connect () {
this.tick = new TickPlugin(this);
this.chat = new ChatPlugin(this);
this.commandSpy = new CommandSpyPlugin(this);

View file

@ -169,6 +169,7 @@ public class Configuration {
public boolean creayun = false;
public String serverName;
public boolean useCore = true;
public boolean useCorePlaceBlock = false;
public boolean useChat = false;
public boolean coreCommandSpy = false;
public boolean resolveSRV = true;

View file

@ -2,14 +2,13 @@ package me.chayapak1.chomens_bot;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import me.chayapak1.chomens_bot.plugins.ConsolePlugin;
import me.chayapak1.chomens_bot.plugins.DiscordPlugin;
import me.chayapak1.chomens_bot.plugins.IRCPlugin;
import me.chayapak1.chomens_bot.plugins.LoggerPlugin;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.HttpUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import me.chayapak1.chomens_bot.util.PersistentDataUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.kyori.adventure.text.Component;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
@ -49,7 +48,7 @@ public class Main {
private static final List<Thread> alreadyAddedThreads = new ArrayList<>();
private static JDA jda = null;
private static DiscordPlugin discord;
public static void main(String[] args) throws IOException {
final Path configPath = Path.of("config.yml");
@ -84,9 +83,6 @@ public class Main {
config = yaml.load(reader);
PersistentDataUtilities.init();
ComponentUtilities.stringify(Component.empty()); // best way to initialize the class 2024
executor.scheduleAtFixedRate(() -> {
try {
checkInternet();
@ -141,28 +137,25 @@ public class Main {
alreadyStarted = true;
try {
Configuration.BotOption[] botsOptions = config.bots;
// idk if these should be here lol, but it is just the discord stuff
if (config.discord.enabled) {
JDABuilder builder = JDABuilder.createDefault(config.discord.token);
builder.enableIntents(GatewayIntent.MESSAGE_CONTENT);
try {
jda = builder.build();
jda.awaitReady();
} catch (InterruptedException ignored) {
System.exit(1);
}
jda.getPresence().setPresence(Activity.playing(config.discord.statusMessage), false);
}
final Configuration.BotOption[] botsOptions = config.bots;
for (Configuration.BotOption botOption : botsOptions) {
final Bot bot = new Bot(botOption, bots, config);
bots.add(bot);
}
// fard
new ConsolePlugin(bots, config, jda);
// initialize util classes and plugins
PersistentDataUtilities.init();
ComponentUtilities.init();
new ConsolePlugin();
LoggerPlugin.init();
if (config.discord.enabled) discord = new DiscordPlugin(config);
if (config.irc.enabled) new IRCPlugin(config);
LoggerUtilities.info("Initialized all bots. Now connecting");
for (Bot bot : bots) bot.connect();
} catch (Exception e) {
e.printStackTrace();
@ -233,7 +226,7 @@ public class Main {
} catch (Exception ignored) {}
}
if (jda != null) jda.shutdown();
if (discord.jda != null) discord.jda.shutdown();
if (discordEnabled) {
for (int i = 0; i < 150; i++) {

View file

@ -48,8 +48,6 @@ public class ConsoleCommand extends Command {
if (server.equalsIgnoreCase("all")) {
eachBot.console.consoleServer = "all";
context.sendOutput(Component.text("Set the console server to all servers").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)));
continue;
}
@ -59,8 +57,6 @@ public class ConsoleCommand extends Command {
.filter(eachServer -> eachServer.toLowerCase().contains(server))
.findFirst()
.orElse("all");
context.sendOutput(Component.text("Set the console server to " + bot.console.consoleServer).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)));
} catch (ArrayIndexOutOfBoundsException e) {
throw new CommandException(Component.text("Invalid server: " + server));
}

View file

@ -39,6 +39,8 @@ public class FindAltsCommand extends Command {
else {
final CompletableFuture<String> future = bot.players.getPlayerIP(playerEntry);
if (future == null) return null;
future.thenApplyAsync(targetIP -> {
context.sendOutput(handle(bot, targetIP, false, player));

View file

@ -1,17 +1,14 @@
package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Configuration;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.ConsoleCommandContext;
import me.chayapak1.chomens_bot.util.ColorUtilities;
import net.dv8tion.jda.api.JDA;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.jline.reader.*;
import java.util.ArrayList;
import java.util.List;
public class ConsolePlugin implements Completer {
@ -23,10 +20,8 @@ public class ConsolePlugin implements Completer {
public String prefix;
private static final List<Listener> listeners = new ArrayList<>();
public ConsolePlugin (List<Bot> allBots, Configuration config, JDA jda) {
this.allBots = allBots;
public ConsolePlugin () {
this.allBots = Main.bots;
this.reader = LineReaderBuilder
.builder()
.completer(this)
@ -38,13 +33,8 @@ public class ConsolePlugin implements Completer {
prefix = bot.config.consoleCommandPrefix;
bot.console = this;
bot.logger = new LoggerPlugin(bot);
}
new DiscordPlugin(config, jda);
if (config.irc.enabled) new IRCPlugin(config);
final String prompt = "> ";
Main.executorService.submit(() -> {
@ -59,8 +49,6 @@ public class ConsolePlugin implements Completer {
handleLine(line);
}
});
for (Listener listener : listeners) { listener.ready(); }
}
@Override
@ -112,10 +100,4 @@ public class ConsolePlugin implements Completer {
);
}
}
public static void addListener (Listener listener) { listeners.add(listener); }
public static class Listener {
public void ready () {}
}
}

View file

@ -30,10 +30,7 @@ import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.cloudburstmc.math.vector.Vector3i;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@ -55,6 +52,8 @@ public class CorePlugin extends PositionPlugin.Listener {
public Vector3i block = null;
public final List<String> placeBlockQueue = Collections.synchronizedList(new ArrayList<>());
private int nextTransactionId = 0;
private final Map<Integer, CompletableFuture<Component>> transactions = new HashMap<>();
@ -120,6 +119,27 @@ public class CorePlugin extends PositionPlugin.Listener {
return CorePlugin.this.systemMessageReceived(component);
}
});
bot.tick.addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
try {
final List<String> clonedQueue = new ArrayList<>(placeBlockQueue);
if (clonedQueue.isEmpty()) return;
if (clonedQueue.size() > 500) {
placeBlockQueue.clear();
return;
}
forceRunPlaceBlock(clonedQueue.get(0));
placeBlockQueue.remove(0);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public boolean hasRateLimit () {
@ -171,6 +191,11 @@ public class CorePlugin extends PositionPlugin.Listener {
if (!ready || command.length() > 32767) return;
if (bot.options.useCore) {
if (bot.options.useCorePlaceBlock) {
runPlaceBlock(command);
return;
}
if (isRateLimited() && hasRateLimit()) return;
forceRun(command);
@ -185,6 +210,11 @@ public class CorePlugin extends PositionPlugin.Listener {
public CompletableFuture<Component> runTracked (String command) {
if (!bot.options.useCore) return null;
if (bot.options.useCorePlaceBlock) {
runPlaceBlock(command);
return null;
}
final Vector3i coreBlock = block;
run(command);
@ -248,6 +278,10 @@ public class CorePlugin extends PositionPlugin.Listener {
}
public void runPlaceBlock (String command) {
bot.executorService.submit(() -> placeBlockQueue.add(command));
}
private void forceRunPlaceBlock (String command) {
if (!ready || !bot.options.useCore) return;
final NbtMapBuilder blockEntityTagBuilder = NbtMap.builder();

View file

@ -9,14 +9,13 @@ import me.chayapak1.chomens_bot.util.ColorUtilities;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.session.ShutdownEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.event.ClickEvent;
@ -35,7 +34,7 @@ import java.util.*;
// please ignore my ohio code
// also this is one of the classes which has >100 lines or actually >400 LMAO
public class DiscordPlugin {
public final JDA jda;
public JDA jda;
public final Map<String, String> servers;
@ -49,11 +48,10 @@ public class DiscordPlugin {
public boolean shuttedDown = false;
public DiscordPlugin (Configuration config, JDA jda) {
public DiscordPlugin (Configuration config) {
final Configuration.Discord options = config.discord;
this.prefix = options.prefix;
this.servers = options.servers;
this.jda = jda;
this.discordUrl = config.discord.inviteLink;
this.messagePrefix = Component.empty()
.append(Component.text("ChomeNS ").color(NamedTextColor.YELLOW))
@ -65,8 +63,17 @@ public class DiscordPlugin {
)
.clickEvent(ClickEvent.openUrl(discordUrl));
final JDABuilder builder = JDABuilder.createDefault(config.discord.token);
builder.enableIntents(GatewayIntent.MESSAGE_CONTENT);
try {
jda = builder.build();
jda.awaitReady();
} catch (InterruptedException ignored) {}
if (jda == null) return;
jda.getPresence().setPresence(Activity.playing(config.discord.statusMessage), false);
for (Bot bot : Main.bots) {
final String channelId = servers.get(bot.host + ":" + bot.port);

View file

@ -36,6 +36,8 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
}
private void check (PlayerEntry target) {
if (bot.options.useCorePlaceBlock) return; // it will spam the place block core so i ignored this
final CompletableFuture<String> future = bot.players.getPlayerIP(target);
if (future == null) return;

View file

@ -1,6 +1,7 @@
package me.chayapak1.chomens_bot.plugins;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.util.ComponentUtilities;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import net.kyori.adventure.text.Component;
@ -8,6 +9,10 @@ import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
public class LoggerPlugin extends ChatPlugin.Listener {
public static void init () {
for (Bot bot : Main.bots) new LoggerPlugin(bot);
}
private final Bot bot;
private boolean addedListener = false;
@ -75,6 +80,8 @@ public class LoggerPlugin extends ChatPlugin.Listener {
log(message + string, true, false);
}
});
bot.logger = this;
}
public void log (String message) {

View file

@ -24,6 +24,8 @@ public class ComponentUtilities {
return component1.toString().equals(component2.toString());
}
public static void init () {}
// component parsing
public static final Map<String, String> language = loadJsonStringMap("language.json");
private static final Map<String, String> voiceChatLanguage = loadJsonStringMap("voiceChatLanguage.json");

View file

@ -38,6 +38,8 @@ public class LoggerUtilities {
}
public static void log (String message) { log(null, message, true, true); }
public static void log (Component message) { log(null, ComponentUtilities.stringifyAnsi(message)); }
public static void log (Bot bot, Component message) { log(bot, ComponentUtilities.stringifyAnsi(message)); }
public static void log (Bot bot, String message) { log(bot, message, true, true); }
public static void log (Bot bot, String message, boolean logToFile, boolean logToConsole) {
final String component = prefix(bot, Component.text("Log").color(NamedTextColor.GOLD), message);
@ -58,6 +60,8 @@ public class LoggerUtilities {
}
public static void info (String message) { info(null, message); }
public static void info (Component message) { info(null, ComponentUtilities.stringifyAnsi(message)); }
public static void info (Bot bot, Component message) { info(bot, ComponentUtilities.stringifyAnsi(message)); }
public static void info (Bot bot, String message) {
final String component = prefix(bot, Component.text("Info").color(NamedTextColor.GREEN), message);
@ -66,6 +70,8 @@ public class LoggerUtilities {
}
public static void error (String message) { error(null, message); }
public static void error (Component message) { error(null, ComponentUtilities.stringifyAnsi(message)); }
public static void error (Bot bot, Component message) { error(bot, ComponentUtilities.stringifyAnsi(message)); }
public static void error (Bot bot, String message) {
final String component = prefix(bot, Component.text("Error").color(NamedTextColor.RED), message);

View file

@ -131,7 +131,8 @@ bots:
# username - optional, if not specified it will just use a random username
# creayun - defaults to false
# serverName - name it whatever you like, it will be used as server name in trusted broadcast and in console
# useCore - if enabled it just sends the command using chat instead of using core. recommended to enable useChat too when this is enabled
# useCore - if enabled it just sends the command using chat instead of using core. recommended to enable useChat too when this is disabled
# useCorePlaceBlock - uses the place block core instead of the main core. only used if useCore is enabled
# useChat - when the bot tellraws it will chat instead of using the core to run tellraw
# coreCommandSpy - set to true if server supports enabling player's commandspy though command block
# resolveSRV - whether to resolve SRV records on the server. the notchian minecraft doesn't resolve them
@ -140,22 +141,23 @@ bots:
- host: 'localhost'
port: 25565
username: 'ChomeNS_Bot'
creayun: false
serverName: 'Localhost'
useCore: true
useChat: false
coreCommandSpy: false
resolveSRV: true
reconnectDelay: 2000
removeNamespaces: false
chatQueueDelay: 125
coreRateLimit:
limit: 10
reset: 1000 # in milliseconds
# or without the optional ones:
# or with the optional ones
# - host: 'localhost'
# port: 25565
# username: 'ChomeNS_Bot'
# creayun: false
# serverName: 'Localhost'
# useCore: true
# useCorePlaceBlock: false
# useChat: false
# coreCommandSpy: false
# resolveSRV: true
# reconnectDelay: 2000
# removeNamespaces: false
# chatQueueDelay: 125
# coreRateLimit:
# limit: 10
# reset: 1000 # in milliseconds