mirror of
https://code.chipmunk.land/Parker2991/NitoriBot/
synced 2026-11-29 13:45:38 -05:00
chat logging!
This commit is contained in:
@@ -25,6 +25,7 @@ repositories {
|
||||
|
||||
maven("https://maven.maxhenkel.de/repository/public/")
|
||||
}
|
||||
|
||||
kotlin {
|
||||
dependencies {
|
||||
implementation("org.geysermc.mcprotocollib:protocol:1.21.11-SNAPSHOT")
|
||||
@@ -37,6 +38,8 @@ kotlin {
|
||||
implementation("net.kyori:adventure-text-serializer-plain:4.26.1")
|
||||
implementation("net.kyori:adventure-text-serializer-legacy:4.26.1")
|
||||
implementation("net.kyori:adventure-text-serializer-gson:4.26.1")
|
||||
implementation("net.lenni0451:Reflect:1.6.2")
|
||||
implementation("org.ow2.asm:asm:9.9.1")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package land.chipmunk.parker2991.nitoribot;
|
||||
|
||||
import land.chipmunk.parker2991.nitoribot.modules.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
|
||||
@@ -13,8 +14,10 @@ import org.geysermc.mcprotocollib.network.factory.ClientNetworkSessionFactory;
|
||||
import org.geysermc.mcprotocollib.network.packet.Packet;
|
||||
|
||||
|
||||
// credits to chayapak for the listeners from chomens
|
||||
|
||||
public class Bot extends SessionAdapter {
|
||||
// public final List<Listener> listeners = new ArrayList<>();
|
||||
private final List<SessionAdapter> listeners = new ArrayList<>();
|
||||
|
||||
public ClientNetworkSession session;
|
||||
|
||||
@@ -26,8 +29,11 @@ public class Bot extends SessionAdapter {
|
||||
|
||||
public ChatModule chat;
|
||||
|
||||
public ConsoleModule console;
|
||||
|
||||
public void loadModules () {
|
||||
this.chat = new ChatModule(this);
|
||||
this.console = new ConsoleModule(this);
|
||||
}
|
||||
|
||||
public Bot (Config.Options options, List<Bot> bots, Config config) {
|
||||
@@ -59,7 +65,10 @@ public class Bot extends SessionAdapter {
|
||||
|
||||
@Override
|
||||
public void packetReceived(Session session, Packet packet) {
|
||||
session.send(packet);
|
||||
// session.send(packet);
|
||||
}
|
||||
|
||||
public void addListener (SessionAdapter event) {
|
||||
listeners.add(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ package land.chipmunk.parker2991.nitoribot;
|
||||
import land.chipmunk.parker2991.nitoribot.logger.LoggerManager;
|
||||
import land.chipmunk.parker2991.nitoribot.util.ErrorToString;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
@@ -19,6 +21,8 @@ import java.util.List;
|
||||
public class Main {
|
||||
public final Object obj = new Object();
|
||||
|
||||
public final Component host = Component.text("Nitori Jar");
|
||||
|
||||
public static Config config;
|
||||
|
||||
public final List<Bot> Bots = new ArrayList<>();
|
||||
@@ -29,7 +33,8 @@ public class Main {
|
||||
final Yaml yaml = new Yaml(yamlConfig);
|
||||
final Path configPath = Path.of("config.yaml");
|
||||
if (!Files.exists(configPath)) {
|
||||
LoggerManager.INFO("Nitori Jar", "config not found making config now");
|
||||
|
||||
LoggerManager.INFO(host, "config not found making config now");
|
||||
|
||||
InputStream defaultConfig = Main.class.getClassLoader().getResourceAsStream("default_config.yaml");//.getResource("default_config.yaml");//.getResource("default_config.yaml");
|
||||
Files.copy(defaultConfig, Paths.get("config.yaml"));
|
||||
@@ -55,7 +60,7 @@ public class Main {
|
||||
};
|
||||
} catch (Exception e) {
|
||||
String Error = ErrorToString.ErrorToString(e);
|
||||
LoggerManager.ERROR("NitoriBot Jar", Error);
|
||||
LoggerManager.ERROR(host, Error);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -7,10 +7,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.LocalDate;
|
||||
@@ -20,11 +17,11 @@ import java.time.format.DateTimeFormatter;
|
||||
public class LoggerManager {
|
||||
private static final Logger logger = LoggerFactory.getLogger(LoggerManager.class);
|
||||
|
||||
private static final DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
private static final DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("hh:mm:ss");
|
||||
|
||||
private static final DateTimeFormatter formatDate = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
||||
private static final DateTimeFormatter formatDate = DateTimeFormatter.ofPattern("MM/dd/yyyy");
|
||||
|
||||
public static String prefix (Component type, String host, String message) {
|
||||
public static String prefix (Component type, Component host, String message) {
|
||||
Component component;
|
||||
|
||||
ZoneId timezone = ZoneId.of("America/Chicago");
|
||||
@@ -36,40 +33,40 @@ public class LoggerManager {
|
||||
|
||||
component = Component.translatable(
|
||||
"[%s %s %s] [%s] %s",
|
||||
Component.translatable(formatTime.format(time)).color(NamedTextColor.BLUE),
|
||||
Component.translatable(formatDate.format(date)).color(NamedTextColor.DARK_BLUE),
|
||||
Component.translatable(formatTime.format(time)).color(NamedTextColor.DARK_BLUE),
|
||||
Component.translatable(formatDate.format(date)).color(NamedTextColor.AQUA),
|
||||
type,
|
||||
Component.translatable(host).color(NamedTextColor.AQUA),
|
||||
host,
|
||||
Component.translatable(message).color(NamedTextColor.WHITE)
|
||||
).color(NamedTextColor.DARK_GRAY);
|
||||
|
||||
return ComponentUtil.componentToAnsi(component);
|
||||
};
|
||||
|
||||
public static void LOG (String host, String message) {
|
||||
public static void LOG (Component host, String message) {
|
||||
Component type = Component.text("log").color(NamedTextColor.GOLD);
|
||||
|
||||
System.out.println(prefix(type, host, message));
|
||||
};
|
||||
|
||||
public static void INFO (String host, String message) {
|
||||
public static void INFO (Component host, String message) {
|
||||
Component type = Component.text("info").color(NamedTextColor.GREEN);
|
||||
|
||||
System.out.println(prefix(type, host, message));
|
||||
};
|
||||
|
||||
public static void COMMAND (String host, String message) {
|
||||
public static void COMMAND (Component host, String message) {
|
||||
Component type = Component.text("command").color(NamedTextColor.YELLOW);
|
||||
|
||||
System.out.println(prefix(type, host, message));
|
||||
};
|
||||
|
||||
public static void WARN (String host, String message) {
|
||||
public static void WARN (Component host, String message) {
|
||||
Component type = Component.text("warn").color(NamedTextColor.YELLOW);
|
||||
System.out.println(prefix(type, host, message));
|
||||
};
|
||||
|
||||
public static void ERROR (String host, String message) {
|
||||
public static void ERROR (Component host, String message) {
|
||||
Component type = Component.text("error").color(NamedTextColor.DARK_RED);
|
||||
|
||||
System.out.println(prefix(type, host, message));
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package land.chipmunk.parker2991.nitoribot.modules;
|
||||
|
||||
import land.chipmunk.parker2991.nitoribot.Bot;
|
||||
import land.chipmunk.parker2991.nitoribot.logger.LoggerManager;
|
||||
import land.chipmunk.parker2991.nitoribot.util.ComponentUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -16,21 +14,12 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.Clientbound
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatCommandPacket;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundChatPacket;
|
||||
|
||||
import net.lenni0451.lambdaevents.EventHandler;
|
||||
import net.lenni0451.lambdaevents.EventManager;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class ChatModule extends SessionAdapter {
|
||||
private Bot bot;
|
||||
|
||||
private SessionAdapter session;
|
||||
|
||||
public void systemChat (ClientboundSystemChatPacket packet) {
|
||||
|
||||
}
|
||||
|
||||
public void diguisedChat (ClientboundDisguisedChatPacket packet) {
|
||||
|
||||
}
|
||||
private final List<Listener> listeners = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void packetReceived (Session session, Packet packet) {
|
||||
@@ -40,25 +29,44 @@ public class ChatModule extends SessionAdapter {
|
||||
else if (packet instanceof ClientboundDisguisedChatPacket) diguisedChat((ClientboundDisguisedChatPacket) packet);
|
||||
}
|
||||
|
||||
@EventHandler(events = {EventHandler.class})
|
||||
public void playerChat (ClientboundPlayerChatPacket packet) {
|
||||
EventHandler
|
||||
// System.out.println(ComponentUtil.componentToAnsi(packet.getUnsignedContent()));
|
||||
// String stringify = ComponentUtil.componentToJSON(packet);
|
||||
// System.out.println();
|
||||
public void systemChat (ClientboundSystemChatPacket packet) {
|
||||
final Component message = packet.getContent();
|
||||
|
||||
for (Listener listener : listeners) {
|
||||
final boolean received = listener.systemChatReceived(message);
|
||||
}
|
||||
}
|
||||
//public void packetReceived (ClientboundPlayerChatPacket packet) {
|
||||
//String server = bot.options.host + ':' + bot.options.port;
|
||||
//System.out.println(packet);
|
||||
// }
|
||||
|
||||
public void diguisedChat (ClientboundDisguisedChatPacket packet) {
|
||||
Component message = packet.getMessage();
|
||||
|
||||
for (Listener listener : listeners) {
|
||||
final boolean received = listener.disguisedChatReceived(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void playerChat (ClientboundPlayerChatPacket packet) {
|
||||
final Component unsignedContent = packet.getUnsignedContent();
|
||||
|
||||
for (Listener listener : listeners) {
|
||||
final boolean received = listener.playerChatReceived(unsignedContent);
|
||||
}
|
||||
}// session.sendPacket(new ServerboundChatPacket(message, Instant.now().toEpochMilli(), 0, null, 0, new BitSet(), 0));
|
||||
|
||||
public ChatModule (Bot bot) {
|
||||
this.bot = bot;
|
||||
bot.session.addListener(this);
|
||||
// this.packetReceived(null, null);
|
||||
//bot.addListener(this);
|
||||
System.out.println("meow");
|
||||
}
|
||||
};
|
||||
|
||||
public static class Listener {
|
||||
public boolean playerChatReceived (Component message) { return true; };
|
||||
|
||||
public boolean systemChatReceived (Component message) { return true; };
|
||||
|
||||
public boolean disguisedChatReceived (Component message) { return true; };
|
||||
}
|
||||
|
||||
public void addListener (Listener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package land.chipmunk.parker2991.nitoribot.modules;
|
||||
|
||||
import land.chipmunk.parker2991.nitoribot.Bot;
|
||||
import land.chipmunk.parker2991.nitoribot.logger.LoggerManager;
|
||||
import land.chipmunk.parker2991.nitoribot.util.ComponentUtil;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import org.geysermc.mcprotocollib.network.Session;
|
||||
|
||||
public class ConsoleModule extends ChatModule.Listener {
|
||||
private Bot bot;
|
||||
|
||||
@Override
|
||||
public boolean playerChatReceived (Component message) {
|
||||
handleMessages(message);
|
||||
return true;
|
||||
};
|
||||
|
||||
@Override
|
||||
public boolean disguisedChatReceived (Component message) {
|
||||
handleMessages(message);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean systemChatReceived (Component message) {
|
||||
handleMessages(message);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void handleMessages (Component message) {
|
||||
final String host = bot.options.host;
|
||||
final int port = bot.options.port;
|
||||
String parsed = ComponentUtil.componentToAnsi(message);
|
||||
Component component = Component.text(host + ":" + port);
|
||||
LoggerManager.LOG(component, parsed);
|
||||
|
||||
}
|
||||
|
||||
public ConsoleModule (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
bot.chat.addListener(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user