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 class ComponentPreset implements ConfigurationSerializable { 039 040 private final ClassicPlotManagerComponent component; 041 private final String pattern; 042 private final double cost; 043 private final String permission; 044 private final String displayName; 045 private final List<String> description; 046 private final ItemType icon; 047 048 public ComponentPreset( 049 ClassicPlotManagerComponent component, String pattern, double cost, 050 String permission, String displayName, List<String> description, final ItemType icon 051 ) { 052 this.component = component; 053 this.pattern = pattern; 054 this.cost = cost; 055 this.permission = permission; 056 this.displayName = displayName; 057 this.description = description; 058 this.icon = icon; 059 } 060 061 @SuppressWarnings("unchecked") 062 public static ComponentPreset deserialize(final @NonNull Map<String, Object> map) { 063 final ClassicPlotManagerComponent classicPlotManagerComponent = ClassicPlotManagerComponent 064 .fromString(map.getOrDefault("component", "").toString()).orElseThrow(() -> 065 new IllegalArgumentException("The preset in components.yml needs a valid target component, got: " + map.get("component"))); 066 final String pattern = map.getOrDefault("pattern", "").toString(); 067 final double cost = Double.parseDouble(map.getOrDefault("cost", "0.0").toString()); 068 final String permission = map.getOrDefault("permission", "").toString(); 069 final String displayName = map.getOrDefault("name", "New Package").toString(); 070 final List<String> description = (List<String>) map.getOrDefault("description", new ArrayList<>()); 071 final ItemType icon = ItemTypes.get(map.getOrDefault("icon", "dirt").toString()); 072 return new ComponentPreset(classicPlotManagerComponent, pattern, cost, permission, 073 displayName, description, icon 074 ); 075 } 076 077 public ClassicPlotManagerComponent getComponent() { 078 return this.component; 079 } 080 081 public String getPattern() { 082 return this.pattern; 083 } 084 085 public double getCost() { 086 return this.cost; 087 } 088 089 public String getPermission() { 090 return this.permission; 091 } 092 093 public String getDisplayName() { 094 return this.displayName; 095 } 096 097 public List<String> getDescription() { 098 return this.description; 099 } 100 101 public ItemType getIcon() { 102 return this.icon; 103 } 104 105 @Override 106 public Map<String, Object> serialize() { 107 final Map<String, Object> map = new HashMap<>(); 108 map.put("component", this.component.name().toLowerCase()); 109 map.put("pattern", this.pattern); 110 map.put("cost", this.cost); 111 map.put("permission", this.permission); 112 map.put("name", this.displayName); 113 map.put("description", this.description); 114 map.put("icon", this.icon.getId().replace("minecraft:", "")); 115 return map; 116 } 117 118}