Search in sources :

Example 1 with YamlSequence

use of com.amihaiemil.eoyaml.YamlSequence in project MC-Server-Bot by polarbub.

the class config method readConfig.

public static void readConfig() throws IOException, InterruptedException, LoginException {
    YamlMapping config = Yaml.createYamlInput(new File(Main.runTimeArgs[0])).readYamlMapping();
    YamlMapping backupConfig = config.yamlMapping("BACKUP");
    YamlMapping discordConfig = config.yamlMapping("DISCORD_BOT");
    YamlMapping minecraftConfig = config.yamlMapping("MC_SERVER");
    permissionsConfig = config.yamlMapping("PERMISSIONS");
    serverArgs.clear();
    YamlSequence startCMD = minecraftConfig.yamlSequence("startCMD");
    if (System.getProperty("os.name").equals("Linux")) {
        StringBuilder s = new StringBuilder();
        int i = 0;
        for (YamlNode node : startCMD) {
            s.append(startCMD.string(i));
            s.append(" ");
            i++;
        }
        s.delete(s.length() - 1, s.length());
        serverArgs.add(s.toString());
    } else {
        int i = 0;
        for (YamlNode node : startCMD) {
            serverArgs.add(startCMD.string(i));
            i++;
        }
    }
    webHookURL = new URL(discordConfig.string("chatBridgeWebHookURL"));
    // Get the channel IDs
    token = discordConfig.string("TOKEN");
    pre = discordConfig.string("PREFIX");
    // init discord jda
    bot = JDABuilder.createLight(token, GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGES).addEventListeners(new Main()).build();
    while (!String.valueOf(bot.getStatus()).equals("CONNECTED")) {
        // wait for connected
        Thread.sleep(10);
    }
    Thread.sleep(1000);
    consoleChannel = bot.getTextChannelById(discordConfig.longNumber("CONSOLE_CHANNEL"));
    chatBridgeChannel = bot.getTextChannelById(discordConfig.longNumber("CHAT_CHANNEL"));
    test = discordConfig.string("test");
    // Add all normal regexs to list
    normalPatterns.clear();
    YamlSequence normalRegexes = minecraftConfig.yamlSequence("normalRegexes");
    for (YamlNode regexNode : normalRegexes) {
        net.polarbub.botv2.config.normalPattern NormalPattern = new normalPattern();
        YamlMapping regexSequence = regexNode.asMapping().yamlMapping("regex");
        NormalPattern.pattern = Pattern.compile(regexSequence.string("string"));
        NormalPattern.dataGroup = regexSequence.integer("contentGroup");
        normalPatterns.add(NormalPattern);
    }
    // Add all named regexs to list
    namedPatterns.clear();
    YamlSequence namedRegexs = minecraftConfig.yamlSequence("webHookRegexes");
    for (YamlNode regexNode : namedRegexs) {
        net.polarbub.botv2.config.namedPattern NamedPattern = new namedPattern();
        YamlMapping regexSequence = regexNode.asMapping().yamlMapping("regex");
        NamedPattern.pattern = Pattern.compile(regexSequence.string("string"));
        if (regexSequence.string("prefix").equals("\"\"")) {
            NamedPattern.prefix = "";
        } else {
            NamedPattern.prefix = regexSequence.string("prefix");
        }
        NamedPattern.dataGroup = regexSequence.integer("contentGroup");
        NamedPattern.nameGroup = regexSequence.integer("nameGroup");
        namedPatterns.add(NamedPattern);
    }
    startPattern = Pattern.compile(minecraftConfig.string("startRegex"));
    startPattern = Pattern.compile("^\\[\\d\\d:\\d\\d:\\d\\d] \\[Server thread\\/INFO\\]: Done \\(\\d+.\\d+s\\)! For help, type \"help");
    pingPort = minecraftConfig.integer("pingPort");
    queryPort = minecraftConfig.integer("queryPort");
    realIP = minecraftConfig.string("realIP");
    showIP = minecraftConfig.string("showIP");
    backupTime = backupConfig.longNumber("backup_time");
    if (backupTime < 0)
        throw new IllegalArgumentException("Backup Time cannon be less than zero");
    backupWarn = backupConfig.longNumber("backup_alert");
    serverDir = backupConfig.string("gitDirectory");
    gitsavereturnPattern = Pattern.compile(backupConfig.string("gitsavereturnRegex"));
}
Also used : URL(java.net.URL) YamlSequence(com.amihaiemil.eoyaml.YamlSequence) YamlNode(com.amihaiemil.eoyaml.YamlNode) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) File(java.io.File) Main(net.polarbub.botv2.Main)

Example 2 with YamlSequence

use of com.amihaiemil.eoyaml.YamlSequence in project MC-Server-Bot by polarbub.

the class permissions method getPermissions.

// Permissions grabber
public static boolean getPermissions(String permission, MessageReceivedEvent event) {
    String id = event.getAuthor().getId();
    List<Role> userRoles = Objects.requireNonNull(event.getMember()).getRoles();
    int i = 0;
    YamlSequence yamlSequence = permissionsConfig.yamlMapping("Global").yamlSequence("Users");
    for (YamlNode node : yamlSequence) {
        if (id.equals(yamlSequence.string(i)))
            return true;
        i++;
    }
    i = 0;
    yamlSequence = permissionsConfig.yamlMapping(permission).yamlSequence("Roles");
    for (YamlNode node : yamlSequence) {
        for (Role role : userRoles) {
            if (role.getId().equals(yamlSequence.string(i)))
                return true;
        }
        i++;
    }
    i = 0;
    yamlSequence = permissionsConfig.yamlMapping("Global").yamlSequence("Roles");
    for (YamlNode node : yamlSequence) {
        for (Role role : userRoles) {
            if (role.getId().equals(yamlSequence.string(i)))
                return true;
        }
        i++;
    }
    i = 0;
    yamlSequence = permissionsConfig.yamlMapping(permission).yamlSequence("Users");
    for (YamlNode node : yamlSequence) {
        if (id.equals(yamlSequence.string(i)))
            return true;
        i++;
    }
    return false;
}
Also used : Role(net.dv8tion.jda.api.entities.Role) YamlSequence(com.amihaiemil.eoyaml.YamlSequence) YamlNode(com.amihaiemil.eoyaml.YamlNode)

Aggregations

YamlNode (com.amihaiemil.eoyaml.YamlNode)2 YamlSequence (com.amihaiemil.eoyaml.YamlSequence)2 YamlMapping (com.amihaiemil.eoyaml.YamlMapping)1 File (java.io.File)1 URL (java.net.URL)1 Role (net.dv8tion.jda.api.entities.Role)1 Main (net.polarbub.botv2.Main)1