Implement duplicated actions click type (#1919)

Some addons may want to introduce two different actions with the same click type.
This change will allow to do it, as now actions may be defined as lists instead of configuration sections.

To use it, action must be defined a bit differently:
```
      actions:
        - click-type: <value of enum ClickType>  # required
          type: <value of String> # not required
          content: <value of String>  # not required
          tooltip: <value of String>  # not required
        - click-type: <value of enum ClickType>  # required
          type: <value of String> # not required
          content: <value of String>  # not required
          tooltip: <value of String>  # not required
```

Co-authored-by: tastybento <tastybento@users.noreply.github.com>
This commit is contained in:
BONNe 2022-01-29 04:35:27 +02:00 committed by GitHub
parent 56c9f5c28e
commit 54869a400a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,12 +6,6 @@
package world.bentobox.bentobox.api.panels.reader; package world.bentobox.bentobox.api.panels.reader;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
@ -20,6 +14,12 @@ import org.bukkit.event.inventory.ClickType;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.base.Enums; import com.google.common.base.Enums;
import world.bentobox.bentobox.api.panels.Panel; import world.bentobox.bentobox.api.panels.Panel;
@ -345,6 +345,30 @@ public class TemplateReader
}); });
} }
} }
else if (section.isList("actions"))
{
// Read Click data as list which allows to have duplicate click types.
List<Map<?, ?>> actionList = section.getMapList("actions");
if (!actionList.isEmpty())
{
actionList.forEach(valueMap -> {
ClickType clickType = Enums.getIfPresent(ClickType.class,
String.valueOf(valueMap.get("click-type")).toUpperCase()).orNull();
if (clickType != null)
{
ItemTemplateRecord.ActionRecords actionData =
new ItemTemplateRecord.ActionRecords(clickType,
valueMap.containsKey("type") ? String.valueOf(valueMap.get("type")) : null,
valueMap.containsKey("content") ? String.valueOf(valueMap.get("content")) : null,
valueMap.containsKey("tooltip") ? String.valueOf(valueMap.get("tooltip")) : null);
itemRecord.addAction(actionData);
}
});
}
}
// Add item to the map // Add item to the map
if (reusableItemMap != null && itemKey != null) if (reusableItemMap != null && itemKey != null)