Package com.ultikits.ultitools.abstracts
Class AbstractConfigEntity
java.lang.Object
com.ultikits.ultitools.abstracts.AbstractConfigEntity
Abstract class representing a configuration entity.
配置实体的抽象类。
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<ConfigChangeListener> private org.bukkit.configuration.file.YamlConfigurationprivate final Stringprivate static final Loggerprivate UltiToolsPlugin -
Constructor Summary
ConstructorsConstructorDescriptionAbstractConfigEntity(String configFilePath) Constructor for AbstractConfigEntity. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChangeListener(ConfigChangeListener listener) Adds a configuration change listener.voidRemoves all configuration change listeners.intGets the number of registered change listeners.com.google.gson.JsonObjectGets the comments of the configuration entity.private intgetValueLength(Object value) final voidinit(UltiToolsPlugin ultiToolsPlugin) Initializes the configuration entity.private booleanisNotEmptyViolation(Field field, Object value) private booleanisPatternViolation(Field field, Object value) private booleanisRangeViolation(Field field, Object value) private booleanisSizeViolation(Field field, Object value) private voidlogResetWarning(String reason, Field field, Object val1, Object val2, Object val3, Object defaultValue) protected voidNotifies all registered listeners about the configuration change.voidreload()Reloads the configuration from file and notifies all listeners.voidremoveChangeListener(ConfigChangeListener listener) Removes a configuration change listener.voidsave()Saves the configuration to the file.com.google.gson.JsonObjectConverts the configuration entity to a JSON object.voidupdateProperties(com.google.gson.JsonObject jsonObject) Updates the properties of the configuration entity.protected voidValidates all fields annotated with validation annotations (@Range, @NotEmpty, @Size, @Pattern).private booleanvalidateSingleField(Field field, Object defaultInstance)
-
Field Details
-
LOGGER
-
configFilePath
-
changeListeners
-
ultiToolsPlugin
-
config
private org.bukkit.configuration.file.YamlConfiguration config
-
-
Constructor Details
-
AbstractConfigEntity
Constructor for AbstractConfigEntity.AbstractConfigEntity的构造函数。
- Parameters:
configFilePath- the path to the configuration file, for example: config/config.yml
配置文件在resource文件夹的路径,例如:config/config.yml
-
-
Method Details
-
save
Saves the configuration to the file.将配置保存到文件。
- Throws:
IOException- if an I/O error occurs
如果发生I/O错误
-
init
Initializes the configuration entity.初始化配置实体。
- Parameters:
ultiToolsPlugin- the plugin instance
插件实例- Throws:
IOException- if an I/O error occurs
如果发生I/O错误
-
updateProperties
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
-
isNotEmptyViolation
-
isSizeViolation
-
isPatternViolation
-
getValueLength
-
logResetWarning
-
addChangeListener
Adds a configuration change listener. The listener will be notified when the configuration is reloaded.添加配置变更监听器。当配置重载时,监听器将被通知。
- Parameters:
listener- the listener to add
要添加的监听器
-
removeChangeListener
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
Reloads the configuration from file and notifies all listeners.从文件重新加载配置并通知所有监听器。
- Throws:
IOException- if an I/O error occurs
如果发生I/O错误
-