2014-09-23 12:08:31 +10:00
|
|
|
|
package com.intellectualcrafters.plot;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
|
|
|
|
public class Flag {
|
2014-11-05 14:42:08 +11:00
|
|
|
|
private final AbstractFlag key;
|
|
|
|
|
private final String value;
|
2014-09-24 22:21:43 +10:00
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
|
/**
|
|
|
|
|
* Flag object used to store basic information for a Plot. Flags are a
|
|
|
|
|
* key/value pair. For a flag to be usable by a player, you need to register
|
|
|
|
|
* it with PlotSquared.
|
|
|
|
|
*
|
|
|
|
|
* @param key
|
|
|
|
|
* AbstractFlag
|
|
|
|
|
* @param value
|
|
|
|
|
* Value must be alphanumerical (can have spaces) and be <= 48
|
|
|
|
|
* characters
|
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
|
* if you provide inadequate inputs
|
|
|
|
|
*/
|
|
|
|
|
public Flag(final AbstractFlag key, final String value) {
|
|
|
|
|
final char[] allowedCharacters = new char[] { '[', ']', '(', ')', ',', '_', '-', '.', ',', '?', '!', '&', '<27>' };
|
2014-10-25 16:30:37 +02:00
|
|
|
|
String tempValue = value;
|
2014-11-05 14:42:08 +11:00
|
|
|
|
for (final char c : allowedCharacters) {
|
2014-10-25 16:34:25 +02:00
|
|
|
|
tempValue = tempValue.replace(c, 'c');
|
2014-11-05 14:42:08 +11:00
|
|
|
|
}
|
|
|
|
|
if (!StringUtils.isAlphanumericSpace(tempValue)) {
|
|
|
|
|
throw new IllegalArgumentException("Flag must be alphanumerical (colours and some special characters are allowed)");
|
|
|
|
|
}
|
|
|
|
|
if (value.length() > 48) {
|
|
|
|
|
throw new IllegalArgumentException("Value must be <= 48 characters");
|
|
|
|
|
}
|
|
|
|
|
this.key = key;
|
|
|
|
|
this.value = key.parseValue(value);
|
|
|
|
|
if (this.value == null) {
|
|
|
|
|
throw new IllegalArgumentException(key.getValueDesc());
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
|
/**
|
|
|
|
|
* Get the AbstractFlag used in creating the flag
|
|
|
|
|
*
|
|
|
|
|
* @return AbstractFlag
|
|
|
|
|
*/
|
|
|
|
|
public AbstractFlag getAbstractFlag() {
|
|
|
|
|
return this.key;
|
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
|
/**
|
|
|
|
|
* Get the key for the AbstractFlag
|
|
|
|
|
*
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public String getKey() {
|
|
|
|
|
return this.key.getKey();
|
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
|
/**
|
|
|
|
|
* Get the value
|
|
|
|
|
*
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public String getValue() {
|
|
|
|
|
return this.value;
|
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
if (this.value.equals("")) {
|
|
|
|
|
return this.key.getKey();
|
|
|
|
|
}
|
|
|
|
|
return this.key + ":" + this.value;
|
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(final Object obj) {
|
|
|
|
|
if (this == obj) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (getClass() != obj.getClass()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
final Flag other = (Flag) obj;
|
|
|
|
|
return (this.key.getKey().equals(other.key.getKey()) && this.value.equals(other.value));
|
|
|
|
|
}
|
2014-09-24 22:21:43 +10:00
|
|
|
|
|
2014-11-05 14:42:08 +11:00
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
return this.key.getKey().hashCode();
|
|
|
|
|
}
|
2014-09-23 12:08:31 +10:00
|
|
|
|
}
|