Better reflective editor for hels

This commit is contained in:
downthecrop 2025-10-02 04:17:13 -07:00
parent b61f4da214
commit bed52d968e
3 changed files with 94 additions and 57 deletions

View file

@ -340,48 +340,43 @@ class SettingsPanel(private val plugin: Plugin) : JPanel() {
maximumSize = Dimension(120, 30)
addActionListener {
try {
// Create a new HashMap from the table data
// We need to determine the key and value types from the field's generic type
val newHashMap = HashMap<Any, Any>()
// Commit any active cell editing before reading values
if (table.isEditing) {
table.cellEditor.stopCellEditing()
}
// Get the current HashMap from the field and modify it in place
val currentHashMap = field.get(plugin) as? HashMap<Any, Any> ?: HashMap<Any, Any>()
// Clear the current HashMap
currentHashMap.clear()
// Add the new entries from the table to the existing HashMap
for (i in 0 until tableModel.rowCount) {
val key = tableModel.getValueAt(i, 0).toString()
val value = tableModel.getValueAt(i, 1).toString()
// Only add non-empty keys
if (key.isNotBlank()) {
// Skip empty values
if (value.isBlank()) {
Helpers.showToast(
this@SettingsPanel,
"Skipping entry with empty value for key '$key'",
JOptionPane.WARNING_MESSAGE
)
continue
}
// Try to convert the value to the appropriate type based on the field's generic type
val convertedValue = try {
// For HashMap<String, Int> which is what FooTextPlugin uses
if (field.genericType.toString().contains("java.util.HashMap<java.lang.String, java.lang.Integer>") ||
field.genericType.toString().contains("HashMap<String, Int>") ||
field.genericType.toString().contains("HashMap<String, Integer>")) {
if (key.isNotBlank()) {
// Skip empty values
if (value.isBlank()) {
Helpers.showToast(
this@SettingsPanel,
"Skipping entry with empty value for key '$key'",
JOptionPane.WARNING_MESSAGE
)
continue
}
// Try to convert the value to the appropriate type based on the field's generic type
val convertedValue = try {
// For HashMap<String, Int> - treat all HashMap<String, Int> fields the same way regardless of plugin
val fieldTypeName = field.genericType.toString()
if (fieldTypeName.contains("java.util.HashMap<java.lang.String, java.lang.Integer>") ||
fieldTypeName.contains("HashMap<String, Int>") ||
fieldTypeName.contains("HashMap<String, Integer>")) {
try {
val intValue = value.toInt()
// For FooTextPlugin, account types should be 0-3
if (field.declaringClass.simpleName == "plugin" &&
field.declaringClass.`package`.name.contains("FooTextPlugin")) {
if (intValue < 0 || intValue > 3) {
Helpers.showToast(
this@SettingsPanel,
"Account type for '$key' should be 0-3. Using 0 as default.",
JOptionPane.WARNING_MESSAGE
)
0
} else {
intValue
}
} else {
intValue
}
intValue
} catch (e: NumberFormatException) {
Helpers.showToast(
this@SettingsPanel,
@ -392,20 +387,20 @@ class SettingsPanel(private val plugin: Plugin) : JPanel() {
}
}
// For other numeric types
else if (field.genericType.toString().contains("java.lang.Integer") ||
field.genericType.toString().contains("int")) {
else if (fieldTypeName.contains("java.lang.Integer") ||
fieldTypeName.contains("int")) {
value.toInt()
}
else if (field.genericType.toString().contains("java.lang.Double") ||
field.genericType.toString().contains("double")) {
else if (fieldTypeName.contains("java.lang.Double") ||
fieldTypeName.contains("double")) {
value.toDouble()
}
else if (field.genericType.toString().contains("java.lang.Float") ||
field.genericType.toString().contains("float")) {
else if (fieldTypeName.contains("java.lang.Float") ||
fieldTypeName.contains("float")) {
value.toFloat()
}
else if (field.genericType.toString().contains("java.lang.Boolean") ||
field.genericType.toString().contains("boolean")) {
else if (fieldTypeName.contains("java.lang.Boolean") ||
fieldTypeName.contains("boolean")) {
value.toBoolean()
}
// Default to string for other types
@ -416,12 +411,13 @@ class SettingsPanel(private val plugin: Plugin) : JPanel() {
// If conversion fails, keep as string
value
}
newHashMap[key] = convertedValue
currentHashMap[key] = convertedValue
}
}
// Update the field with the new HashMap
fieldNotifier.setFieldValue(field, newHashMap)
// Update the field to trigger notifications (even though the reference is the same)
// This ensures OnKondoValueUpdated() gets called if it exists
fieldNotifier.setFieldValue(field, currentHashMap)
Helpers.showToast(
this@SettingsPanel,
"${field.name} updated successfully!"