add stuff and fix stuff yet again

MORE STUFF YUP
This commit is contained in:
ChomeNS 2023-03-19 17:24:48 +07:00
parent 91ac26714d
commit fe3b399498
11 changed files with 214 additions and 44 deletions

12
chomens_bot.iml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>ADVENTURE</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
</module>

View file

@ -84,6 +84,12 @@
<artifactId>cowjar-extra</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.29</version>
</dependency>
</dependencies>
<build>

View file

@ -7,11 +7,15 @@ import com.github.steveice10.packetlib.event.session.*;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
import lombok.Getter;
import lombok.Setter;
import me.chayapak1.chomensbot_mabe.plugins.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Bot {
private final ArrayList<SessionListener>listeners = new ArrayList<>();
@ -19,25 +23,35 @@ public class Bot {
@Getter private final String host;
@Getter private final int port;
@Getter private final String username;
@Getter private final List<Bot> allBots;
@Getter private final Session session;
@Getter private Session session;
@Getter private final int reconnectDelay;
@Getter private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
@Getter private final ChatPlugin chat = new ChatPlugin(this);
@Getter private final LoggerPlugin logger = new LoggerPlugin(this);
@Getter @Setter private LoggerPlugin logger; // in ConsolePlugin
@Getter private final SelfCarePlugin selfCare = new SelfCarePlugin(this);
@Getter private final ConsolePlugin console = new ConsolePlugin(this);
@Getter @Setter private ConsolePlugin console;
@Getter private final PositionPlugin position = new PositionPlugin(this);
@Getter private final CorePlugin core = new CorePlugin(this);
@Getter private final CommandHandlerPlugin commandHandler = new CommandHandlerPlugin();
@Getter private final ChatCommandHandlerPlugin chatCommandHandler = new ChatCommandHandlerPlugin(this);
@Getter private final HashingPlugin hashing = new HashingPlugin(this);
public Bot (String host, int port, String username) {
public Bot (String host, int port, int reconnectDelay, String username, List<Bot> allBots) {
this.host = host;
this.port = port;
this.reconnectDelay = reconnectDelay;
this.username = username;
this.allBots = allBots;
reconnect();
}
public void reconnect () {
Session session = new TcpClientSession(host, port, new MinecraftProtocol(username), null);
this.session = session;
@ -87,6 +101,12 @@ public class Bot {
for (SessionListener listener : listeners) {
listener.disconnected(disconnectedEvent);
}
if (reconnectDelay < 0) return; // to disable reconnecting
Runnable task = () -> reconnect();
executor.schedule(task, reconnectDelay(), TimeUnit.MILLISECONDS);
}
});

View file

@ -1,11 +1,69 @@
package me.chayapak1.chomensbot_mabe;
public class Main {
public static void main(String[] args) {
final String host = args[0];
final int port = Integer.parseInt(args[1]);
final String username = args[2];
import me.chayapak1.chomensbot_mabe.plugins.ConsolePlugin;
import org.yaml.snakeyaml.Yaml;
new Bot(host, port, username);
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
final File file = new File("config.yml");
final Yaml yaml = new Yaml();
Map<String, List<Map<String, Object>>> config;
if (!file.exists()) {
// creates config file from default-config.yml
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default-config.yml");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuilder = new StringBuilder();
while (reader.ready()) {
char character = (char) reader.read();
stringBuilder.append(character);
}
String defaultConfig = stringBuilder.toString();
// writes it
BufferedWriter configWriter = new BufferedWriter(new FileWriter(file));
configWriter.write(defaultConfig);
configWriter.close();
System.out.println("config.yml file not found, so the default one was created");
config = yaml.load(is);
}
InputStream opt = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
config = yaml.load(reader);
final Object reconnectDelayObject = config.get("reconnectDelay");
final int reconnectDelay = (int) reconnectDelayObject;
List<Map<String, Object>> botsOptions = config.get("bots");
final List<Bot> allBots = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(botsOptions.size());
for (Map<String, Object> botOption : botsOptions) {
final String host = (String) botOption.get("host");
final int port = (int) botOption.get("port");
final String username = (String) botOption.get("username");
new Thread(() -> {
final Bot bot = new Bot(host, port, reconnectDelay, username, allBots);
allBots.add(bot);
latch.countDown();
}).start();
}
latch.await();
new ConsolePlugin(allBots);
}
}

View file

@ -3,7 +3,6 @@ package me.chayapak1.chomensbot_mabe.commands;
import me.chayapak1.chomensbot_mabe.Bot;
import me.chayapak1.chomensbot_mabe.command.Command;
import me.chayapak1.chomensbot_mabe.command.CommandContext;
import me.chayapak1.chomensbot_mabe.plugins.CommandHandlerPlugin;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.format.NamedTextColor;
@ -38,7 +37,10 @@ public class HelpCommand implements Command {
return 0;
}
private Bot bot;
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
this.bot = context.bot();
if (args.length == 0) {
sendCommandList(context);
return Component.text("success");
@ -72,9 +74,11 @@ public class HelpCommand implements Command {
public List<Component> getCommandListByTrustLevel (int trustLevel) {
final List<Component> list = new ArrayList<>();
for (Command command : CommandHandlerPlugin.commands()) {
for (Command command : bot.commandHandler().commands()) {
final String name = command.name();
bot.logger().log(name);
if (command.trustLevel() != trustLevel) continue;
list.add(Component.text(name).color(getColorByTrustLevel(trustLevel)));
}
@ -98,7 +102,7 @@ public class HelpCommand implements Command {
final String commandName = args[0];
for (Command command : CommandHandlerPlugin.commands()) {
for (Command command : bot.commandHandler().commands()) {
if (!command.name().equals(commandName)) continue;
final List<Component> usages = new ArrayList<>();

View file

@ -33,7 +33,7 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener {
final PlayerCommandContext context = new PlayerCommandContext(bot, displayName);
final Component output = CommandHandlerPlugin.executeCommand(commandString, context, bot.hashing().hash(), bot.hashing().ownerHash());
final Component output = bot.commandHandler().executeCommand(commandString, context, bot.hashing().hash(), bot.hashing().ownerHash());
final String textOutput = ((TextComponent) output).content();
if (!textOutput.equals("success")) {

View file

@ -10,10 +10,12 @@ import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CommandHandlerPlugin {
@Getter private static final List<Command> commands = new ArrayList<>();
@Getter private final List<Command> commands = new ArrayList<>();
public CommandHandlerPlugin () {
registerCommand(new CommandBlockCommand());
@ -31,7 +33,7 @@ public class CommandHandlerPlugin {
commands.add(command);
}
public static Component executeCommand (String input, CommandContext context, String hash, String ownerHash) {
public Component executeCommand (String input, CommandContext context, String hash, String ownerHash) {
final String[] splitInput = input.split("\\s+");
final String commandName = splitInput[0];

View file

@ -10,17 +10,28 @@ import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import java.util.Arrays;
import java.util.List;
public class ConsolePlugin {
private final Bot bot;
private final List<Bot> allBots;
@Getter public final LineReader reader;
@Getter private String consoleServer = "all";
@Getter private final String prefix = ".";
@Getter private final String consoleServerPrefix = "/";
public ConsolePlugin (Bot bot) {
this.bot = bot;
public ConsolePlugin (List<Bot> allBots) {
this.allBots = allBots;
this.reader = LineReaderBuilder.builder().build();
for (Bot bot : allBots) {
bot.console(this);
bot.logger(new LoggerPlugin(bot));
}
String prompt = "> ";
new Thread(() -> {
@ -42,17 +53,40 @@ public class ConsolePlugin {
public void handleLine (String line) {
if (line == null) return;
if (line.startsWith(consoleServerPrefix)) {
final String substringLine = line.substring(consoleServerPrefix.length());
final String[] splitInput = substringLine.split("\\s+");
final String commandName = splitInput[0];
final String[] args = Arrays.copyOfRange(splitInput, 1, splitInput.length);
if (commandName.equals("csvr") || commandName.equals("consoleserver")) {
for (Bot bot : allBots) {
if (args.length == 0) {
bot.logger().log("No server specified");
return;
}
consoleServer = args[0];
bot.logger().log("Set the console server to " + consoleServer);
}
}
return;
}
for (Bot bot : allBots) {
if (!bot.host().equals(consoleServer) && !consoleServer.equals("all")) continue;
if (line.startsWith(prefix)) {
final ConsoleCommandContext context = new ConsoleCommandContext(bot);
final Component output = CommandHandlerPlugin.executeCommand(line.substring(prefix.length()), context, "h", "o");
final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, "h", "o");
final String textOutput = ((TextComponent) output).content();
if (!textOutput.equals("success")) {
context.sendOutput(output);
}
return;
continue;
}
bot.chat().tellraw(
@ -65,3 +99,4 @@ public class ConsolePlugin {
);
}
}
}

View file

@ -1,5 +1,8 @@
package me.chayapak1.chomensbot_mabe.plugins;
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import me.chayapak1.chomensbot_mabe.Bot;
import net.kyori.adventure.text.Component;
@ -8,11 +11,30 @@ public class LoggerPlugin extends ChatPlugin.ChatListener {
public LoggerPlugin(Bot bot) {
this.bot = bot;
bot.addListener(new SessionAdapter() {
@Override
public void connected (ConnectedEvent event) {
log("Successfully connected to: " + bot.host() + ":" + bot.port());
}
@Override
public void disconnected (DisconnectedEvent event) {
log("Disconnected from " + bot.host() + ":" + bot.port() + ", reason: " + event.getReason());
}
});
bot.chat().addListener(this);
}
public void log (String message) {
bot.console().reader().printAbove(message);
bot.console().reader().printAbove(
String.format(
"[%s] %s",
bot.host() + ":" + bot.port(),
message
)
);
}
@Override

View file

@ -7,7 +7,13 @@ import java.util.List;
public class ElementUtilities {
public static Command findCommand(List<Command> commands, String searchTerm) {
for (Command command : commands) {
if (command.name().equals(searchTerm) || command.alias().contains(searchTerm)) {
if (
(
command.name().equals(searchTerm.toLowerCase()) ||
command.alias().contains(searchTerm.toLowerCase())
) &&
!searchTerm.equals("") // ig yup
) {
return command;
}
}

View file

@ -0,0 +1,5 @@
reconnectDelay: 7000
bots:
- host: 'localhost'
port: 25565
username: 'ChomeNS_Bot'