查看: 233|回復: 0

[閒聊閒語] 創建配置管理類

[複製鏈接]

66

主題

162

帖子

5629

金錢

火焰之影

Rank: 8Rank: 8

威望
0
精華
0
貢獻
0
鑽石
0
閱讀權限
50
積分
5791
在線時間
78 小時
相冊
0
日誌
0
好友
0
發表於 2026-1-9 21:43 | 顯示全部樓層 |閱讀模式
package com.lineage.bot.config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
public class BotConfig {
    private static final Logger log = LogManager.getLogger(BotConfig.class);
    private static BotConfig instance;
    private Properties properties;
    private BotConfig() {
        properties = new Properties();
    }
    public static BotConfig getInstance() {
        if (instance == null) {
            instance = new BotConfig();
        }
        return instance;
    }
    public void load() throws Exception {
        try (InputStream input = getClass().getClassLoader().getResourceAsStream("bot_system.properties")) {
            if (input == null) {
                throw new Exception("Unable to find bot_system.properties");
            }
            properties.load(input);
            log.info("Configuration loaded from bot_system.properties");
        } catch (Exception e) {
            log.error("Failed to load configuration", e);
            throw e;
        }
    }
    private String getProperty(String key) {
        return properties.getProperty(key);
    }
    private String getProperty(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }
    private int getIntProperty(String key) {
        return Integer.parseInt(getProperty(key));
    }
    private int getIntProperty(String key, int defaultValue) {
        try {
            return Integer.parseInt(getProperty(key));
        } catch (Exception e) {
            return defaultValue;
        }
    }
    private boolean getBooleanProperty(String key) {
        return Boolean.parseBoolean(getProperty(key));
    }
    private boolean getBooleanProperty(String key, boolean defaultValue) {
        try {
            return Boolean.parseBoolean(getProperty(key));
        } catch (Exception e) {
            return defaultValue;
        }
    }
    private double getDoubleProperty(String key) {
        return Double.parseDouble(getProperty(key));
    }
    private double getDoubleProperty(String key, double defaultValue) {
        try {
            return Double.parseDouble(getProperty(key));
        } catch (Exception e) {
            return defaultValue;
        }
    }
    private long getLongProperty(String key) {
        return Long.parseLong(getProperty(key));
    }
    private long getLongProperty(String key, long defaultValue) {
        try {
            return Long.parseLong(getProperty(key));
        } catch (Exception e) {
            return defaultValue;
        }
    }
    public String getServerHost() {
        return getProperty("bot.server_host");
    }
    public int getServerPort() {
        return getIntProperty("bot.server_port", 2000);
    }
    public int getConnectionTimeout() {
        return getIntProperty("bot.connection_timeout", 5000);
    }
    public int getMaxBotCount() {
        return getIntProperty("bot.max_count", 500);
    }
    public boolean isAutoSpawnOnStartup() {
        return getBooleanProperty("bot.auto_spawn_on_startup", false);
    }
    public String getAccountPrefix() {
        return getProperty("bot.login.account_prefix", "bot_");
    }
    public String getCharacterPrefix() {
        return getProperty("bot.login.character_prefix", "bot_char_");
    }
    public String getPassword() {
        return getProperty("bot.login.password", "bot_password");
    }
    public int getLoginConcurrentLogins() {
        return getIntProperty("bot.login.concurrent_logins", 10);
    }
    public int getRampupDelayBetweenLogin() {
        return getIntProperty("bot.login.rampup.delay_between_login", 100);
    }
    public boolean isAiEnabled() {
        return getBooleanProperty("bot.ai.enabled", true);
    }
    public int getDecisionInterval() {
        return getIntProperty("bot.ai.decision_interval", 500);
    }
    public boolean isHumanSimulationEnabled() {
        return getBooleanProperty("bot.human_simulation.enabled", true);
    }
    public double getErrorRate() {
        return getDoubleProperty("bot.human_simulation.error_rate", 0.03);
    }
    public double getDelayChance() {
        return getDoubleProperty("bot.human_simulation.delay_chance", 0.2);
    }
    public int getDelayMin() {
        return getIntProperty("bot.human_simulation.delay_min", 200);
    }
    public int getDelayMax() {
        return getIntProperty("bot.human_simulation.delay_max", 500);
    }
    public boolean isLearningEnabled() {
        return getBooleanProperty("bot.learning.enabled", true);
    }
    public boolean isDebug() {
        return getBooleanProperty("bot.debug", false);
    }
    public boolean isPacketTrace() {
        return getBooleanProperty("bot.debug.packet_trace", false);
    }
    public boolean isAiTrace() {
        return getBooleanProperty("bot.debug.ai_trace", false);
    }
    public String getDebugLogFile() {
        return getProperty("bot.debug.log_file", "bot_debug.log");
    }
    public int getThreadPoolCoreSize() {
        return getIntProperty("bot.thread_pool.core_size", 50);
    }
    public int getThreadPoolMaxSize() {
        return getIntProperty("bot.thread_pool.max_size", 200);
    }
    public int getThreadPoolQueueCapacity() {
        return getIntProperty("bot.thread_pool.queue_capacity", 1000);
    }
    public int getThreadPoolKeepAliveSeconds() {
        return getIntProperty("bot.thread_pool.keep_alive_seconds", 60);
    }
}
Thinking: 好的,配置管理類已創建。現在創建BotLauncher類,這個類負責啟動單個BOT。
7. 創建BOT啟動器
← Wrote C:\Users\skyal\OneDrive\Desktop\境界天堂\381模擬器\381模擬器\bot-client\src\main\java\com\lineage\bot\BotLauncher.java
package com.lineage.bot;
import com.lineage.bot.config.BotConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class BotLauncher implements Runnable {
    private static final Logger log = LogManager.getLogger(BotLauncher.class);
    private int botId;
    private String accountName;
    private String characterName;
    public BotLauncher(int botId) {
        this.botId = botId;
        BotConfig config = BotConfig.getInstance();
        this.accountName = config.getAccountPrefix() + botId;
        this.characterName = config.getCharacterPrefix() + botId;
    }
    @Override
    public void run() {
        log.info("Starting Bot {}: Account={}, Character={}", botId, accountName, characterName);
        try {
            BotConnection connection = new BotConnection(accountName, characterName);
            if (connection.connect()) {
                log.info("Bot {} connected successfully", botId);
                BotLoginManager loginManager = new BotLoginManager(connection);
                if (loginManager.login()) {
                    log.info("Bot {} logged in successfully", botId);
                    BotCommandExecutor commandExecutor = new BotCommandExecutor(connection);
                    BotPacketReceiver packetReceiver = new BotPacketReceiver(connection);
                    new Thread(packetReceiver).start();
                    log.info("Bot {} fully initialized", botId);
                    while (connection.isConnected()) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            log.error("Bot {} interrupted", botId);
                            break;
                        }
                    }
                    connection.disconnect();
                    log.info("Bot {} disconnected", botId);
                } else {
                    log.error("Bot {} failed to login", botId);
                }
            } else {
                log.error("Bot {} failed to connect", botId);
            }
        } catch (Exception e) {
            log.error("Bot {} encountered an error", botId, e);
        }
    }
    public int getBotId() {
        return botId;
    }
    public String getAccountName() {
        return accountName;
    }
    public String getCharacterName() {
        return characterName;
    }
}





上一篇︰搖一搖
下一篇︰搖一搖呀搖一搖
您需要登錄後才可以回帖 登錄 | 註冊會員

本版積分規則

天堂私服列表

45客服

Archiver| 45天堂私服論壇   分享到微博! 分享到臉書! 分享到噗浪! 分享到維特! 分享到Google+! 分享到LINE!

45天堂私服發佈站 ©    天堂私服架設教學  提供最新天堂私服最新資訊

流量最高、品質最好、服務最優、玩家首選、最新天堂私服資訊,都在45天堂私服發佈站.    免責聲明

Sitetag
line客服聯繫
掃一掃二碼
Line客服聯繫
24H專人回覆
返回頂部 返回列表