diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java b/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java index 2739e36..2b4b72d 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java @@ -12,18 +12,19 @@ import me.chayapak1.chomensbot_mabe.plugins.*; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CountDownLatch; +import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Bot { - private final ArrayListlisteners = new ArrayList<>(); + private final ArrayList listeners = new ArrayList<>(); @Getter private final String host; @Getter private final int port; @Getter private final String username; @Getter private final List allBots; + @Getter private final Map keys; @Getter private Session session; @@ -42,12 +43,13 @@ public class Bot { @Getter private final HashingPlugin hashing = new HashingPlugin(this); @Getter private final MusicPlayerPlugin music = new MusicPlayerPlugin(this); - public Bot (String host, int port, int reconnectDelay, String username, List allBots) { + public Bot (String host, int port, int reconnectDelay, String username, List allBots, Map keys) { this.host = host; this.port = port; this.reconnectDelay = reconnectDelay; this.username = username; this.allBots = allBots; + this.keys = keys; reconnect(); } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/Configuration.java b/src/main/java/me/chayapak1/chomensbot_mabe/Configuration.java new file mode 100644 index 0000000..ef0e2a5 --- /dev/null +++ b/src/main/java/me/chayapak1/chomensbot_mabe/Configuration.java @@ -0,0 +1,17 @@ +package me.chayapak1.chomensbot_mabe; + +import lombok.Getter; + +import java.util.Map; + +public class Configuration { + @Getter public int reconnectDelay = 7000; + @Getter public Map keys; + @Getter public Bots[] bots = new Bots[]{}; + + public static class Bots { + @Getter public String host; + @Getter public int port; + @Getter public String username; + } +} diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/Main.java b/src/main/java/me/chayapak1/chomensbot_mabe/Main.java index 10a8bff..9c2681c 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/Main.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/Main.java @@ -2,6 +2,7 @@ package me.chayapak1.chomensbot_mabe; import me.chayapak1.chomensbot_mabe.plugins.ConsolePlugin; import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; import java.io.*; import java.util.ArrayList; @@ -12,8 +13,9 @@ 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>> config; + final Constructor constructor = new Constructor(Configuration.class); + final Yaml yaml = new Yaml(constructor); + Configuration config; if (!file.exists()) { // creates config file from default-config.yml @@ -41,21 +43,21 @@ public class Main { config = yaml.load(reader); - final int reconnectDelay = (int) ((Object) config.get("reconnectDelay")); - - List> botsOptions = config.get("bots"); + final int reconnectDelay = config.reconnectDelay(); + final Map keys = config.keys(); + Configuration.Bots[] botsOptions = config.bots(); final List allBots = new ArrayList<>(); - final CountDownLatch latch = new CountDownLatch(botsOptions.size()); + final CountDownLatch latch = new CountDownLatch(botsOptions.length); - for (Map botOption : botsOptions) { - final String host = (String) botOption.get("host"); - final int port = (int) botOption.get("port"); - final String username = (String) botOption.get("username"); + for (Configuration.Bots botOption : botsOptions) { + final String host = botOption.host(); + final int port = botOption.port(); + final String username = botOption.username(); new Thread(() -> { - final Bot bot = new Bot(host, port, reconnectDelay, username, allBots); + final Bot bot = new Bot(host, port, reconnectDelay, username, allBots, keys); allBots.add(bot); latch.countDown(); diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/command/CommandContext.java b/src/main/java/me/chayapak1/chomensbot_mabe/command/CommandContext.java index 2f8960a..42f6e7a 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/command/CommandContext.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/command/CommandContext.java @@ -7,8 +7,13 @@ import net.kyori.adventure.text.Component; public class CommandContext { @Getter public final Bot bot; - public CommandContext(Bot bot) { + @Getter private final String hash; + @Getter private final String ownerHash; + + public CommandContext(Bot bot, String hash, String ownerHash) { this.bot = bot; + this.hash = hash; + this.ownerHash = ownerHash; } public Component displayName () { return Component.empty(); } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/command/ConsoleCommandContext.java b/src/main/java/me/chayapak1/chomensbot_mabe/command/ConsoleCommandContext.java index 0ea5d08..df3d2df 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/command/ConsoleCommandContext.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/command/ConsoleCommandContext.java @@ -1,5 +1,6 @@ package me.chayapak1.chomensbot_mabe.command; +import lombok.Getter; import me.chayapak1.chomensbot_mabe.Bot; import me.chayapak1.chomensbot_mabe.util.ComponentUtilities; import net.kyori.adventure.text.Component; @@ -8,8 +9,8 @@ import net.kyori.adventure.text.format.NamedTextColor; public class ConsoleCommandContext extends CommandContext { private final Bot bot; - public ConsoleCommandContext (Bot bot) { - super(bot); + public ConsoleCommandContext (Bot bot, String hash, String ownerHash) { + super(bot, hash, ownerHash); this.bot = bot; } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/command/PlayerCommandContext.java b/src/main/java/me/chayapak1/chomensbot_mabe/command/PlayerCommandContext.java index 3812479..cb49ee7 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/command/PlayerCommandContext.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/command/PlayerCommandContext.java @@ -9,8 +9,8 @@ public class PlayerCommandContext extends CommandContext { private final Bot bot; - public PlayerCommandContext (Bot bot, String playerName) { - super(bot); + public PlayerCommandContext (Bot bot, String playerName, String hash, String ownerHash) { + super(bot, hash, ownerHash); this.bot = bot; this.playerName = playerName; } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java index 0143912..da1ba58 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/ValidateCommand.java @@ -38,8 +38,8 @@ public class ValidateCommand implements Command { final Bot bot = context.bot(); final String hash = fullArgs[0]; - if (hash.equals(bot.hashing().hash())) context.sendOutput(Component.text("Valid hash").color(NamedTextColor.GREEN)); - else if (hash.equals(bot.hashing().ownerHash())) context.sendOutput(Component.text("Valid OwnerHash").color(NamedTextColor.GREEN)); + if (hash.equals(context.hash())) context.sendOutput(Component.text("Valid hash").color(NamedTextColor.GREEN)); + else if (hash.equals(context.ownerHash())) context.sendOutput(Component.text("Valid OwnerHash").color(NamedTextColor.GREEN)); return Component.text("success"); } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java index 5d7df5c..49c1df0 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java @@ -31,7 +31,7 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener { if (!contents.startsWith(prefix)) return; final String commandString = contents.substring(prefix.length()); - final PlayerCommandContext context = new PlayerCommandContext(bot, displayName); + final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, 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(); diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java index aab2489..16c8f94 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java @@ -77,7 +77,7 @@ public class ConsolePlugin { if (!bot.host().equals(consoleServer) && !consoleServer.equals("all")) continue; if (line.startsWith(prefix)) { - final ConsoleCommandContext context = new ConsoleCommandContext(bot); + final ConsoleCommandContext context = new ConsoleCommandContext(bot, "h", "o"); // ? should the hashes be hardcoded? final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, "h", "o"); final String textOutput = ((TextComponent) output).content(); diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java index 2e4a5b5..8cdcbbb 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java @@ -9,17 +9,22 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; public class HashingPlugin { + private final Bot bot; + @Getter private String hash; @Getter private String ownerHash; public HashingPlugin (Bot bot) { - + this.bot = bot; bot.executor().schedule(this::update, 2, TimeUnit.SECONDS); } public void update () { - final String ownerHashKey = "b)R��nF�CW���#�\\[�S*8\"t^eia�Z��k����K1�8zȢ�"; - final String normalHashKey = "�iB_D���k��j8H�{?[/ڭ�f�}Ѣ�^-=�Ț��v]��g>��=c"; + // final String ownerHashKey = "b)R��nF�CW���#�\\[�S*8\"t^eia�Z��k����K1�8zȢ�"; + // final String normalHashKey = "�iB_D���k��j8H�{?[/ڭ�f�}Ѣ�^-=�Ț��v]��g>��=c"; + + final String normalHashKey = bot.keys().get("normalKey"); + final String ownerHashKey = bot.keys().get("ownerKey"); final String hashValue = System.currentTimeMillis() / 10_000 + normalHashKey; hash = Hashing.sha256() diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index 9025aa5..d7f61f2 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -1,4 +1,7 @@ reconnectDelay: 7000 +keys: + normalKey: '�iB_D���k��j8H�{?[/ڭ�f�}Ѣ�^-=�Ț��v]��g>��=c' + ownerKey: 'b)R��nF�CW���#�\\[�S*8"t^eia�Z��k����K1�8zȢ�' bots: - host: 'localhost' port: 25565