001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.components;
020
021import com.plotsquared.core.configuration.serialization.ConfigurationSerializable;
022import com.plotsquared.core.configuration.serialization.SerializableAs;
023import com.plotsquared.core.generator.ClassicPlotManagerComponent;
024import com.sk89q.worldedit.world.item.ItemType;
025import com.sk89q.worldedit.world.item.ItemTypes;
026import org.checkerframework.checker.nullness.qual.NonNull;
027
028import java.util.ArrayList;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * A preset that can be used to set a component from
035 * the component GUI
036 */
037@SerializableAs("preset")
038public record ComponentPreset(
039        ClassicPlotManagerComponent component,
040        String pattern,
041        double cost,
042        String permission,
043        String displayName,
044        List<String> description,
045        ItemType icon
046) implements ConfigurationSerializable {
047
048    @SuppressWarnings("unchecked")
049    public static ComponentPreset deserialize(final @NonNull Map<String, Object> map) {
050        final ClassicPlotManagerComponent classicPlotManagerComponent = ClassicPlotManagerComponent
051                .fromString(map.getOrDefault("component", "").toString()).orElseThrow(() ->
052                        new IllegalArgumentException("The preset in components.yml needs a valid target component, got: " + map.get(
053                                "component")));
054        final String pattern = map.getOrDefault("pattern", "").toString();
055        final double cost = Double.parseDouble(map.getOrDefault("cost", "0.0").toString());
056        final String permission = map.getOrDefault("permission", "").toString();
057        final String displayName = map.getOrDefault("name", "New Package").toString();
058        final List<String> description = (List<String>) map.getOrDefault("description", new ArrayList<>());
059        final ItemType icon = ItemTypes.get(map.getOrDefault("icon", "dirt").toString());
060        return new ComponentPreset(classicPlotManagerComponent, pattern, cost, permission,
061                displayName, description, icon
062        );
063    }
064
065    @Override
066    public Map<String, Object> serialize() {
067        final Map<String, Object> map = new HashMap<>();
068        map.put("component", this.component.name().toLowerCase());
069        map.put("pattern", this.pattern);
070        map.put("cost", this.cost);
071        map.put("permission", this.permission);
072        map.put("name", this.displayName);
073        map.put("description", this.description);
074        map.put("icon", this.icon.getId().replace("minecraft:", ""));
075        return map;
076    }
077
078}