I'm trying to add values to an ImmutableMap.
The field's name is commands
.
Here's my code:
Field field = this.getDescription().getClass().getDeclaredField("commands");
Map<String, Map<String, Object>> commands = new HashMap<>();
this.getDescription().getCommands().forEach((c, v) -> commands.put(c, v));
for (String alias : this.aliasManager.getAllAliases()) {
Map<String, Object> map = new HashMap<>();
map.put("description", "An alias for " + aliasManager.getAlias(alias).getCommand());
map.put("usage", "/<command>");
map.put("name", alias);
commands.put(alias, map);
}
Map<String, Map<String, Object>> immutableMap = ImmutableMap.copyOf(commands);
field.setAccessible(true);
field.set(this.getDescription().getCommands(), immutableMap);
The error I am getting is:
java.lang.IllegalArgumentException: Can not set java.util.Map field
org.bukkit.plugin.PluginDescriptionFile.commands to com.google.common.collect.SingletonImmutableBiMap
The field is declared in the following way:
private Map<String, Map<String, Object>> commands = (Map<String, Map<String, Object>>)ImmutableMap.of();
Any ideas on how I could fix the issue?