package com.elmakers.mine.bukkit.action.builtin;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;

import com.elmakers.mine.bukkit.api.action.CastContext;

public class ShopAction extends SelectorAction {

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters) {
        super.prepare(context, parameters);
        loadItems(context, parameters, "items");
        loadItems(context, parameters, "spells");
    }

    protected void loadItems(CastContext context, ConfigurationSection parameters, String key) {
        if (parameters.contains(key)) {
            List<ConfigurationSection> itemConfigs = new ArrayList<>();
            List<? extends Object> objects = parameters.getList(key);
            for (Object object : objects) {
                if (object instanceof ConfigurationSection) {
                    itemConfigs.add((ConfigurationSection)object);
                } else if (object instanceof String) {
                    ConfigurationSection itemConfig = new MemoryConfiguration();
                    itemConfig.set("item", object);
                    itemConfigs.add(itemConfig);
                } else {
                    context.getLogger().warning("Invalid item in shop config: " + object);
                }
            }

            loadOptions(itemConfigs);
        }
    }
}

@@ -93,16 +93,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -223,16 +223,55 @@
 uration;
+%0Aimport org.bukkit.inventory.ItemStack;
 %0A%0Aimport
@@ -324,193 +324,1065 @@
 xt;%0A
-%0Apublic class ShopAction extends SelectorAction %7B%0A%0A    @Override%0A    public void prepare(CastContext context, ConfigurationSection parameters) %7B%0A        super.prepare(context, parameter
+import com.elmakers.mine.bukkit.block.MaterialAndData;%0Aimport com.elmakers.mine.bukkit.utility.ConfigurationUtils;%0Aimport com.elmakers.mine.bukkit.wand.Wand;%0A%0Apublic class ShopAction extends SelectorAction %7B%0A%0A    @Override%0A    public void prepare(CastContext context, ConfigurationSection parameters) %7B%0A        // Don't load items as defaults%0A        Object itemDefaults = parameters.get(%22items%22);%0A        parameters.set(%22items%22, null);%0A%0A        // Sell shop overrides%0A        if (parameters.getBoolean(%22sell%22)) %7B%0A            // Support scale parameter%0A            parameters.set(%22earn_scale%22, parameters.get(%22earn_scale%22));%0A            parameters.set(%22scale%22, null);%0A%0A            // Supply selected message%0A            if (!parameters.contains(%22selected%22)) %7B%0A                parameters.set(%22selected%22, context.getController().getMessages().get(%22shops.sold%22));%0A            %7D%0A        %7D%0A%0A        super.prepare(context, parameters);%0A%0A        // Restore items list. This is kind of messy, but so is this whole action.%0A        parameters.set(%22items%22, itemDefault
 s);%0A
@@ -1931,32 +1931,184 @@
 ection)object);%0A
+                %7D else if (object instanceof Map) %7B%0A                     itemConfigs.add(ConfigurationUtils.toConfigurationSection((Map%3C?, ?%3E)object));%0A
                 
@@ -2469,32 +2469,1444 @@
 %0A            %7D%0A%0A
+            if (parameters.getBoolean(%22sell%22, false)) %7B%0A                for (ConfigurationSection itemConfig : itemConfigs) %7B%0A                    String itemName = itemConfig.getString(%22item%22);%0A                    if (itemName == null %7C%7C itemName.equalsIgnoreCase(%22none%22)) continue;%0A%0A                    itemConfig.set(%22item%22, null);%0A                    itemConfig.set(%22icon%22, itemName);%0A                    ItemStack item = parseItem(itemName);%0A                    Object costs = itemConfig.get(%22cost%22);%0A                    if (costs != null) %7B%0A                        itemConfig.set(%22earn%22, costs);%0A                        itemConfig.set(%22cost%22, null);%0A                    %7D else if (item != null) %7B%0A                        double worth = context.getController().getWorth(item);%0A                        if (worth %3E 0) %7B%0A                            itemConfig.set(%22earn%22, worth);%0A                        %7D%0A                    %7D%0A                    ConfigurationSection costSection = itemConfig.createSection(%22costs%22);%0A                    Integer sp = Wand.getSP(item);%0A                    if (sp != null) %7B%0A                        costSection.set(%22sp%22, sp);%0A                    %7D else %7B%0A                        MaterialAndData materialAndData = new MaterialAndData(item);%0A                        costSection.set(materialAndData.getKey(), item.getAmount());%0A                    %7D%0A                %7D%0A            %7D%0A
             load

Support sell shops using a simple toggle flag, auto-convert