TF-PlotSquared/src/main/java/com/intellectualcrafters/plot/config/Configuration.java

249 lines
9.4 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// 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, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.config;
2015-07-31 00:25:16 +10:00
import java.util.ArrayList;
2015-01-14 03:38:15 +11:00
import com.intellectualcrafters.plot.object.PlotBlock;
2015-02-22 23:24:48 +11:00
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.StringComparison;
2014-12-16 16:03:20 +11:00
2014-11-16 09:56:42 +01:00
/**
* Main Configuration Utility
*
* @author Empire92
*/
2015-02-20 17:34:19 +11:00
@SuppressWarnings("unused")
public class Configuration {
2015-07-06 01:44:10 +10:00
public static final SettingValue<String> STRING = new SettingValue<String>("STRING") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
return true;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public String parseString(final String string) {
2014-11-05 14:42:08 +11:00
return string;
}
};
public static final SettingValue<String[]> STRINGLIST = new SettingValue<String[]>("STRINGLIST") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
return true;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public String[] parseString(final String string) {
2014-11-05 14:42:08 +11:00
return string.split(",");
}
};
public static final SettingValue<Integer> INTEGER = new SettingValue<Integer>("INTEGER") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
try {
2014-12-16 16:03:20 +11:00
Integer.parseInt(string);
2014-11-05 14:42:08 +11:00
return true;
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2014-11-05 14:42:08 +11:00
return false;
}
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public Integer parseString(final String string) {
2014-11-05 14:42:08 +11:00
return Integer.parseInt(string);
}
};
public static final SettingValue<Boolean> BOOLEAN = new SettingValue<Boolean>("BOOLEAN") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
try {
2014-12-16 16:03:20 +11:00
Boolean.parseBoolean(string);
2014-11-05 14:42:08 +11:00
return true;
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2014-11-05 14:42:08 +11:00
return false;
}
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public Boolean parseString(final String string) {
2014-11-05 14:42:08 +11:00
return Boolean.parseBoolean(string);
}
};
public static final SettingValue<Double> DOUBLE = new SettingValue<Double>("DOUBLE") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
try {
2014-12-16 16:03:20 +11:00
Double.parseDouble(string);
2014-11-05 14:42:08 +11:00
return true;
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2014-11-05 14:42:08 +11:00
return false;
}
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public Double parseString(final String string) {
2014-11-05 14:42:08 +11:00
return Double.parseDouble(string);
}
};
public static final SettingValue<String> BIOME = new SettingValue<String>("BIOME") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
try {
2015-02-23 12:32:27 +11:00
final int biome = BlockManager.manager.getBiomeFromString(string.toUpperCase());
2015-07-27 19:50:04 +02:00
return biome != -1;
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2014-11-05 14:42:08 +11:00
return false;
}
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public String parseString(final String string) {
2015-02-22 23:24:48 +11:00
if (validateValue(string)) {
return string.toUpperCase();
2014-11-05 14:42:08 +11:00
}
2015-02-22 23:24:48 +11:00
return "FOREST";
2014-11-05 14:42:08 +11:00
}
};
public static final SettingValue<PlotBlock> BLOCK = new SettingValue<PlotBlock>("BLOCK") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
try {
if (string.contains(":")) {
final String[] split = string.split(":");
2014-12-16 16:03:20 +11:00
Short.parseShort(split[0]);
Short.parseShort(split[1]);
2014-12-17 20:15:11 -06:00
} else {
2014-12-16 16:03:20 +11:00
Short.parseShort(string);
2014-11-05 14:42:08 +11:00
}
return true;
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2014-11-05 14:42:08 +11:00
return false;
}
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public PlotBlock parseString(final String string) {
2014-11-05 14:42:08 +11:00
if (string.contains(":")) {
final String[] split = string.split(":");
return new PlotBlock(Short.parseShort(split[0]), Byte.parseByte(split[1]));
2014-12-17 20:15:11 -06:00
} else {
2014-11-05 14:42:08 +11:00
return new PlotBlock(Short.parseShort(string), (byte) 0);
}
}
};
public static final SettingValue<PlotBlock[]> BLOCKLIST = new SettingValue<PlotBlock[]>("BLOCKLIST") {
2014-11-05 14:42:08 +11:00
@Override
public boolean validateValue(final String string) {
try {
for (String block : string.split(",")) {
if (block.contains("%")) {
final String[] split = block.split("%");
2014-12-16 16:03:20 +11:00
Integer.parseInt(split[0]);
2014-11-05 14:42:08 +11:00
block = split[1];
}
StringComparison<PlotBlock>.ComparisonResult value = BlockManager.manager.getClosestBlock(block);
if (value == null || value.match > 1) {
return false;
2014-11-05 14:42:08 +11:00
}
}
return true;
2014-12-17 20:15:11 -06:00
} catch (final Exception e) {
2014-11-05 14:42:08 +11:00
return false;
}
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
public PlotBlock[] parseString(final String string) {
2014-11-05 14:42:08 +11:00
final String[] blocks = string.split(",");
2014-11-16 09:56:42 +01:00
final ArrayList<PlotBlock> parsedvalues = new ArrayList<>();
2014-11-05 14:42:08 +11:00
final PlotBlock[] values = new PlotBlock[blocks.length];
final int[] counts = new int[blocks.length];
int min = 100;
for (int i = 0; i < blocks.length; i++) {
try {
if (blocks[i].contains("%")) {
final String[] split = blocks[i].split("%");
blocks[i] = split[1];
final int value = Integer.parseInt(split[0]);
counts[i] = value;
if (value < min) {
min = value;
}
} else {
counts[i] = 1;
if (1 < min) {
min = 1;
}
}
StringComparison<PlotBlock>.ComparisonResult result = BlockManager.manager.getClosestBlock(blocks[i]);
if (result != null && result.match < 2) {
values[i] = result.best;
2014-11-05 14:42:08 +11:00
}
}
catch (Exception e) {}
2014-11-05 14:42:08 +11:00
}
final int gcd = gcd(counts);
for (int i = 0; i < counts.length; i++) {
final int num = counts[i];
for (int j = 0; j < (num / gcd); j++) {
parsedvalues.add(values[i]);
}
}
2014-11-16 09:56:42 +01:00
return parsedvalues.toArray(new PlotBlock[parsedvalues.size()]);
2014-11-05 14:42:08 +11:00
}
};
2015-02-23 12:32:27 +11:00
2014-11-16 09:56:42 +01:00
public static int gcd(final int a, final int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
2015-02-23 12:32:27 +11:00
2014-11-16 09:56:42 +01:00
private static int gcd(final int[] a) {
int result = a[0];
for (int i = 1; i < a.length; i++) {
result = gcd(result, a[i]);
}
return result;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
/**
2014-12-17 20:15:11 -06:00
* Create your own SettingValue object to make the management of plotworld configuration easier
2014-11-05 14:42:08 +11:00
*/
public static abstract class SettingValue<T> {
2014-11-05 14:42:08 +11:00
private final String type;
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
public SettingValue(final String type) {
this.type = type;
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
public String getType() {
return this.type;
}
2015-02-23 12:32:27 +11:00
public abstract T parseString(final String string);
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
public abstract boolean validateValue(final String string);
}
}