2017-05-14 12:00:00 +10:00
|
|
|
package org.bukkit;
|
|
|
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.UUID;
|
2018-07-10 12:21:23 +10:00
|
|
|
import java.util.regex.Pattern;
|
2017-05-14 12:00:00 +10:00
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a String based key which consists of two components - a namespace
|
|
|
|
* and a key.
|
2018-07-10 12:21:23 +10:00
|
|
|
*
|
|
|
|
* Namespaces may only contain lowercase alphanumeric characters, periods,
|
|
|
|
* underscores, and hyphens.
|
|
|
|
* <p>
|
|
|
|
* Keys may only contain lowercase alphanumeric characters, periods,
|
|
|
|
* underscores, hyphens, and forward slashes.
|
|
|
|
*
|
2017-05-14 12:00:00 +10:00
|
|
|
*/
|
|
|
|
public final class NamespacedKey {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The namespace representing all inbuilt keys.
|
|
|
|
*/
|
|
|
|
public static final String MINECRAFT = "minecraft";
|
|
|
|
/**
|
|
|
|
* The namespace representing all keys generated by Bukkit for backwards
|
2018-01-15 09:28:45 +11:00
|
|
|
* compatibility measures.
|
2017-05-14 12:00:00 +10:00
|
|
|
*/
|
|
|
|
public static final String BUKKIT = "bukkit";
|
|
|
|
//
|
2018-07-10 12:21:23 +10:00
|
|
|
private static final Pattern VALID_NAMESPACE = Pattern.compile("[a-z0-9._-]+");
|
|
|
|
private static final Pattern VALID_KEY = Pattern.compile("[a-z0-9/._-]+");
|
|
|
|
//
|
2017-05-14 12:00:00 +10:00
|
|
|
private final String namespace;
|
|
|
|
private final String key;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a key in a specific namespace.
|
|
|
|
*
|
|
|
|
* @param namespace
|
|
|
|
* @param key
|
|
|
|
* @deprecated should never be used by plugins, for internal use only!!
|
|
|
|
*/
|
|
|
|
@Deprecated
|
|
|
|
public NamespacedKey(String namespace, String key) {
|
2018-07-10 12:21:23 +10:00
|
|
|
Preconditions.checkArgument(namespace != null && VALID_NAMESPACE.matcher(namespace).matches(), "namespace");
|
|
|
|
Preconditions.checkArgument(key != null && VALID_KEY.matcher(key).matches(), "key");
|
2017-05-14 12:00:00 +10:00
|
|
|
|
|
|
|
this.namespace = namespace;
|
|
|
|
this.key = key;
|
|
|
|
|
|
|
|
String string = toString();
|
|
|
|
Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a key in the plugin's namespace.
|
|
|
|
*
|
|
|
|
* @param plugin the plugin to use for the namespace
|
|
|
|
* @param key the key to create
|
|
|
|
*/
|
|
|
|
public NamespacedKey(Plugin plugin, String key) {
|
|
|
|
Preconditions.checkArgument(plugin != null, "plugin");
|
|
|
|
Preconditions.checkArgument(key != null, "key");
|
|
|
|
|
|
|
|
this.namespace = plugin.getName().toLowerCase(Locale.ROOT);
|
|
|
|
this.key = key.toLowerCase().toLowerCase(Locale.ROOT);
|
|
|
|
|
2018-07-10 12:21:23 +10:00
|
|
|
// Check validity after normalization
|
2018-07-12 11:18:19 +10:00
|
|
|
Preconditions.checkArgument(VALID_NAMESPACE.matcher(this.namespace).matches(), "namespace");
|
|
|
|
Preconditions.checkArgument(VALID_KEY.matcher(this.key).matches(), "key");
|
2018-07-10 12:21:23 +10:00
|
|
|
|
2017-05-14 12:00:00 +10:00
|
|
|
String string = toString();
|
|
|
|
Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters (%s)", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNamespace() {
|
|
|
|
return namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getKey() {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int hash = 5;
|
|
|
|
hash = 47 * hash + this.namespace.hashCode();
|
|
|
|
hash = 47 * hash + this.key.hashCode();
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getClass() != obj.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final NamespacedKey other = (NamespacedKey) obj;
|
|
|
|
return this.namespace.equals(other.namespace) && this.key.equals(other.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return this.namespace + ":" + this.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new random key in the {@link #BUKKIT} namespace.
|
|
|
|
*
|
|
|
|
* @return new key
|
|
|
|
* @deprecated should never be used by plugins, for internal use only!!
|
|
|
|
*/
|
|
|
|
@Deprecated
|
|
|
|
public static NamespacedKey randomKey() {
|
|
|
|
return new NamespacedKey(BUKKIT, UUID.randomUUID().toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a key in the Minecraft namespace.
|
|
|
|
*
|
|
|
|
* @param key the key to use
|
|
|
|
* @return new key in the Minecraft namespace
|
|
|
|
*/
|
|
|
|
public static NamespacedKey minecraft(String key) {
|
|
|
|
return new NamespacedKey(MINECRAFT, key);
|
|
|
|
}
|
|
|
|
}
|