All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.plotsquared.core.components.ComponentPreset Maven / Gradle / Ivy

There is a newer version: 6.11.2
Show newest version
/*
 *       _____  _       _    _____                                _
 *      |  __ \| |     | |  / ____|                              | |
 *      | |__) | | ___ | |_| (___   __ _ _   _  __ _ _ __ ___  __| |
 *      |  ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
 *      | |    | | (_) | |_ ____) | (_| | |_| | (_| | | |  __/ (_| |
 *      |_|    |_|\___/ \__|_____/ \__, |\__,_|\__,_|_|  \___|\__,_|
 *                                    | |
 *                                    |_|
 *            PlotSquared plot management system for Minecraft
 *               Copyright (C) 2014 - 2022 IntellectualSites
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see .
 */
package com.plotsquared.core.components;

import com.plotsquared.core.configuration.serialization.ConfigurationSerializable;
import com.plotsquared.core.configuration.serialization.SerializableAs;
import com.plotsquared.core.generator.ClassicPlotManagerComponent;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A preset that can be used to set a component from
 * the component GUI
 */
@SerializableAs("preset")
public class ComponentPreset implements ConfigurationSerializable {

    private final ClassicPlotManagerComponent component;
    private final String pattern;
    private final double cost;
    private final String permission;
    private final String displayName;
    private final List description;
    private final ItemType icon;

    public ComponentPreset(
            ClassicPlotManagerComponent component, String pattern, double cost,
            String permission, String displayName, List description, final ItemType icon
    ) {
        this.component = component;
        this.pattern = pattern;
        this.cost = cost;
        this.permission = permission;
        this.displayName = displayName;
        this.description = description;
        this.icon = icon;
    }

    @SuppressWarnings("unchecked")
    public static ComponentPreset deserialize(final @NonNull Map map) {
        final ClassicPlotManagerComponent classicPlotManagerComponent = ClassicPlotManagerComponent
                .fromString(map.getOrDefault("component", "").toString()).orElseThrow(() ->
                        new IllegalArgumentException("The preset needs a valid target component"));
        final String pattern = map.getOrDefault("pattern", "").toString();
        final double cost = Double.parseDouble(map.getOrDefault("cost", "0.0").toString());
        final String permission = map.getOrDefault("permission", "").toString();
        final String displayName = map.getOrDefault("name", "New Package").toString();
        final List description = (List) map.getOrDefault("description", new ArrayList<>());
        final ItemType icon = ItemTypes.get(map.getOrDefault("icon", "dirt").toString());
        return new ComponentPreset(classicPlotManagerComponent, pattern, cost, permission,
                displayName, description, icon
        );
    }

    public ClassicPlotManagerComponent getComponent() {
        return this.component;
    }

    public String getPattern() {
        return this.pattern;
    }

    public double getCost() {
        return this.cost;
    }

    public String getPermission() {
        return this.permission;
    }

    public String getDisplayName() {
        return this.displayName;
    }

    public List getDescription() {
        return this.description;
    }

    public ItemType getIcon() {
        return this.icon;
    }

    @Override
    public Map serialize() {
        final Map map = new HashMap<>();
        map.put("component", this.component.name().toLowerCase());
        map.put("pattern", this.pattern);
        map.put("cost", this.cost);
        map.put("permission", this.permission);
        map.put("name", this.displayName);
        map.put("description", this.description);
        map.put("icon", this.icon.getId().replace("minecraft:", ""));
        return map;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy