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.configuration; 020 021import com.plotsquared.core.configuration.caption.Caption; 022import com.plotsquared.core.plot.BlockBucket; 023import com.plotsquared.core.util.StringMan; 024import com.sk89q.worldedit.world.block.BlockState; 025 026import java.util.ArrayList; 027import java.util.Arrays; 028import java.util.Collection; 029import java.util.List; 030 031/** 032 * Configuration Node. 033 */ 034public class ConfigurationNode { 035 036 private final String constant; 037 private final Object defaultValue; 038 private final Caption description; 039 private final ConfigurationUtil.SettingValue<?> type; 040 private final Collection<String> suggestions; 041 private Object value; 042 043 public ConfigurationNode( 044 String constant, Object defaultValue, Caption description, 045 ConfigurationUtil.SettingValue<?> type 046 ) { 047 this(constant, defaultValue, description, type, new ArrayList<>()); 048 } 049 050 public ConfigurationNode( 051 String constant, Object defaultValue, Caption description, 052 ConfigurationUtil.SettingValue<?> type, Collection<String> suggestions 053 ) { 054 this.constant = constant; 055 this.defaultValue = defaultValue; 056 this.description = description; 057 this.value = defaultValue; 058 this.type = type; 059 this.suggestions = suggestions; 060 } 061 062 public ConfigurationUtil.SettingValue<?> getType() { 063 return this.type; 064 } 065 066 public boolean isValid(String string) { 067 try { 068 Object result = this.type.parseString(string); 069 return result != null; 070 } catch (Exception e) { 071 if (e instanceof ConfigurationUtil.UnknownBlockException) { 072 throw e; 073 } 074 return false; 075 } 076 } 077 078 public boolean setValue(String string) { 079 if (!this.type.validateValue(string)) { 080 return false; 081 } 082 this.value = this.type.parseString(string); 083 return true; 084 } 085 086 public Object getValue() { 087 if (this.value instanceof String[]) { 088 return Arrays.asList((String[]) this.value); 089 } 090 if (this.value instanceof Object[]) { 091 List<String> values = new ArrayList<>(); 092 for (Object value : (Object[]) this.value) { 093 values.add(value.toString()); 094 } 095 return values; 096 } 097 if (this.value instanceof BlockBucket) { 098 return this.value.toString(); 099 } 100 if (this.value instanceof BlockState) { 101 return this.value.toString(); 102 } 103 return this.value; 104 } 105 106 public Object getDefaultValue() { 107 if (this.defaultValue instanceof Object[]) { 108 return StringMan.join((Object[]) this.defaultValue, ","); 109 } 110 return this.defaultValue; 111 } 112 113 public String getConstant() { 114 return this.constant; 115 } 116 117 public Caption getDescription() { 118 return this.description; 119 } 120 121 public Collection<String> getSuggestions() { 122 return this.suggestions; 123 } 124 125}