Class AbstractConfigEntity

java.lang.Object
com.ultikits.ultitools.abstracts.AbstractConfigEntity

public abstract class AbstractConfigEntity extends Object
Abstract class representing a configuration entity.

配置实体的抽象类。

  • Field Details

    • LOGGER

      private static final Logger LOGGER
    • configFilePath

      private final String configFilePath
    • changeListeners

      private final List<ConfigChangeListener> changeListeners
    • ultiToolsPlugin

      private UltiToolsPlugin ultiToolsPlugin
    • config

      private org.bukkit.configuration.file.YamlConfiguration config
  • Constructor Details

    • AbstractConfigEntity

      public AbstractConfigEntity(String configFilePath)
      Constructor for AbstractConfigEntity.

      AbstractConfigEntity的构造函数。

      Parameters:
      configFilePath - the path to the configuration file, for example: config/config.yml
      配置文件在resource文件夹的路径,例如:config/config.yml
  • Method Details

    • save

      public void save() throws IOException
      Saves the configuration to the file.

      将配置保存到文件。

      Throws:
      IOException - if an I/O error occurs
      如果发生I/O错误
    • init

      public final void init(UltiToolsPlugin ultiToolsPlugin) throws IOException
      Initializes the configuration entity.

      初始化配置实体。

      Parameters:
      ultiToolsPlugin - the plugin instance
      插件实例
      Throws:
      IOException - if an I/O error occurs
      如果发生I/O错误
    • updateProperties

      public void updateProperties(com.google.gson.JsonObject jsonObject) throws IOException
      Updates the properties of the configuration entity.

      更新配置实体的属性。

      字段的遍历方式与路径推导必须与 init() / save() / reload() 完全一致, 否则会出现「写进去了、其实没写」:这四个方法各自遍历 @ConfigEntry 字段, 而本方法过去两处都跟它们不一样 —— 用 getDeclaredFields() 而非 ReflectionUtil.getFields(Class)(漏掉父类字段), 且不把空的 path 归一到字段名。后者的后果最隐蔽:@ConfigEntry 不写 path 是受支持的写法,init/save 会按字段名读写它, toJsonObject() 也按字段名把它发给面板,而这里去 JSON 里找空字符串键, 永远找不到——字段被跳过,随后 config.save 照常执行,调用方收到成功。

      Parameters:
      jsonObject - the JSON object containing the new properties
      包含新属性的JSON对象
      Throws:
      IOException - if an I/O error occurs
      如果发生I/O错误
    • toJsonObject

      public com.google.gson.JsonObject toJsonObject()
      Converts the configuration entity to a JSON object.

      将配置实体转换为JSON对象。

      Returns:
      the JSON object representation of the configuration entity
      配置实体的JSON对象表示
    • getComments

      public com.google.gson.JsonObject getComments()
      Gets the comments of the configuration entity.

      获取配置实体的注释。

      Returns:
      a JSON object containing the comments
      包含注释的JSON对象
    • validateFields

      protected void validateFields()
      Validates all fields annotated with validation annotations (@Range, @NotEmpty, @Size, @Pattern). Invalid values are reset to their Java field initializer defaults and a warning is logged.

      验证所有带验证注解的字段。无效值将重置为Java字段初始化默认值并记录警告。

    • validateSingleField

      private boolean validateSingleField(Field field, Object defaultInstance) throws IllegalAccessException
      Throws:
      IllegalAccessException
    • isRangeViolation

      private boolean isRangeViolation(Field field, Object value)
    • isNotEmptyViolation

      private boolean isNotEmptyViolation(Field field, Object value)
    • isSizeViolation

      private boolean isSizeViolation(Field field, Object value)
    • isPatternViolation

      private boolean isPatternViolation(Field field, Object value)
    • getValueLength

      private int getValueLength(Object value)
    • logResetWarning

      private void logResetWarning(String reason, Field field, Object val1, Object val2, Object val3, Object defaultValue)
    • addChangeListener

      public void addChangeListener(ConfigChangeListener listener)
      Adds a configuration change listener. The listener will be notified when the configuration is reloaded.

      添加配置变更监听器。当配置重载时,监听器将被通知。

      Parameters:
      listener - the listener to add
      要添加的监听器
    • removeChangeListener

      public void removeChangeListener(ConfigChangeListener listener)
      Removes a configuration change listener.

      移除配置变更监听器。

      Parameters:
      listener - the listener to remove
      要移除的监听器
    • clearChangeListeners

      public void clearChangeListeners()
      Removes all configuration change listeners.

      移除所有配置变更监听器。

    • getChangeListenerCount

      public int getChangeListenerCount()
      Gets the number of registered change listeners.

      获取已注册的变更监听器数量。

      Returns:
      the number of listeners
      监听器数量
    • notifyChangeListeners

      protected void notifyChangeListeners()
      Notifies all registered listeners about the configuration change. Individual listener exceptions do not affect other listeners.

      通知所有已注册的监听器配置已变更。单个监听器的异常不影响其他监听器。

    • reload

      public void reload() throws IOException
      Reloads the configuration from file and notifies all listeners.

      从文件重新加载配置并通知所有监听器。

      Throws:
      IOException - if an I/O error occurs
      如果发生I/O错误