skyalcon 發表於 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;
    }
}

頁: [1]
查看完整版本: 創建配置管理類