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;
|
2014-10-08 16:53:09 +11:00
|
|
|
|
2015-01-14 03:38:15 +11:00
|
|
|
import java.util.ArrayList;
|
2014-12-20 15:44:24 +11:00
|
|
|
|
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;
|
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 {
|
2014-12-17 20:15:11 -06:00
|
|
|
public static final SettingValue STRING = new SettingValue("STRING") {
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public boolean validateValue(final String string) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseString(final String string) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
public static final SettingValue STRINGLIST = new SettingValue("STRINGLIST") {
|
|
|
|
@Override
|
|
|
|
public boolean validateValue(final String string) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseString(final String string) {
|
|
|
|
return string.split(",");
|
|
|
|
}
|
|
|
|
};
|
2014-12-17 20:15:11 -06:00
|
|
|
public static final SettingValue INTEGER = new SettingValue("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-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseString(final String string) {
|
|
|
|
return Integer.parseInt(string);
|
|
|
|
}
|
|
|
|
};
|
2014-12-17 20:15:11 -06:00
|
|
|
public static final SettingValue BOOLEAN = new SettingValue("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-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseString(final String string) {
|
|
|
|
return Boolean.parseBoolean(string);
|
|
|
|
}
|
|
|
|
};
|
2014-12-17 20:15:11 -06:00
|
|
|
public static final SettingValue DOUBLE = new SettingValue("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-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseString(final String string) {
|
|
|
|
return Double.parseDouble(string);
|
|
|
|
}
|
|
|
|
};
|
2014-12-17 20:15:11 -06:00
|
|
|
public static final SettingValue BIOME = new SettingValue("BIOME") {
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public boolean validateValue(final String string) {
|
|
|
|
try {
|
2015-02-22 23:24:48 +11:00
|
|
|
int biome = BlockManager.manager.getBiomeFromString(string.toUpperCase());
|
|
|
|
if (biome == -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-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object 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
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseObject(final Object object) {
|
2015-02-22 23:24:48 +11:00
|
|
|
return object.toString();
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
|
|
|
};
|
2014-12-17 20:15:11 -06:00
|
|
|
public static final SettingValue BLOCK = new SettingValue("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-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseString(final String string) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseObject(final Object object) {
|
2014-12-20 15:44:24 +11:00
|
|
|
return object;
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
|
|
|
};
|
2014-12-17 20:15:11 -06:00
|
|
|
public static final SettingValue BLOCKLIST = new SettingValue("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];
|
|
|
|
}
|
|
|
|
if (block.contains(":")) {
|
|
|
|
final String[] split = block.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(block);
|
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-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseString(final String string) {
|
|
|
|
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++) {
|
|
|
|
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) {
|
2014-10-24 11:29:18 +11:00
|
|
|
min = value;
|
|
|
|
}
|
2014-12-17 20:15:11 -06:00
|
|
|
} else {
|
2014-11-05 14:42:08 +11:00
|
|
|
counts[i] = 1;
|
|
|
|
if (1 < min) {
|
|
|
|
min = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (blocks[i].contains(":")) {
|
|
|
|
final String[] split = blocks[i].split(":");
|
|
|
|
values[i] = 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
|
|
|
values[i] = new PlotBlock(Short.parseShort(blocks[i]), (byte) 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
@Override
|
|
|
|
public Object parseObject(final Object object) {
|
2014-12-20 15:44:24 +11:00
|
|
|
return object;
|
2014-11-05 14:42:08 +11:00
|
|
|
}
|
|
|
|
};
|
2015-02-20 17:34:19 +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-20 17:34:19 +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-20 17:34:19 +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 {
|
|
|
|
private final String type;
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
public SettingValue(final String type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
public String getType() {
|
|
|
|
return this.type;
|
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
public Object parseObject(final Object object) {
|
|
|
|
return object;
|
|
|
|
}
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
public abstract Object parseString(final String string);
|
2015-02-20 17:34:19 +11:00
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
public abstract boolean validateValue(final String string);
|
|
|
|
}
|
2014-10-08 16:53:09 +11:00
|
|
|
}
|