Commits

Parker Hawke authored and md_5 committed 1e843b72bbd
#510: Add NamespacedKey#fromString() to fetch from user input
No tags

src/main/java/org/bukkit/NamespacedKey.java

Modified
1 1 package org.bukkit;
2 2
3 3 import com.google.common.base.Preconditions;
4 4 import java.util.Locale;
5 5 import java.util.UUID;
6 6 import java.util.regex.Pattern;
7 7 import org.bukkit.plugin.Plugin;
8 8 import org.jetbrains.annotations.NotNull;
9 +import org.jetbrains.annotations.Nullable;
9 10
10 11 /**
11 12 * Represents a String based key which consists of two components - a namespace
12 13 * and a key.
13 14 *
14 15 * Namespaces may only contain lowercase alphanumeric characters, periods,
15 16 * underscores, and hyphens.
16 17 * <p>
17 18 * Keys may only contain lowercase alphanumeric characters, periods,
18 19 * underscores, hyphens, and forward slashes.
132 133 /**
133 134 * Get a key in the Minecraft namespace.
134 135 *
135 136 * @param key the key to use
136 137 * @return new key in the Minecraft namespace
137 138 */
138 139 @NotNull
139 140 public static NamespacedKey minecraft(@NotNull String key) {
140 141 return new NamespacedKey(MINECRAFT, key);
141 142 }
143 +
144 + /**
145 + * Get a NamespacedKey from the supplied string with a default namespace if
146 + * a namespace is not defined. This is a utility method meant to fetch a
147 + * NamespacedKey from user input. Please note that casing does matter and
148 + * any instance of uppercase characters will be considered invalid. The
149 + * input contract is as follows:
150 + * <pre>
151 + * fromString("foo", plugin) -{@literal >} "plugin:foo"
152 + * fromString("foo:bar", plugin) -{@literal >} "foo:bar"
153 + * fromString(":foo", null) -{@literal >} "minecraft:foo"
154 + * fromString("foo", null) -{@literal >} "minecraft:foo"
155 + * fromString("Foo", plugin) -{@literal >} null
156 + * fromString(":Foo", plugin) -{@literal >} null
157 + * fromString("foo:bar:bazz", plugin) -{@literal >} null
158 + * fromString("", plugin) -{@literal >} null
159 + * </pre>
160 + *
161 + * @param string the string to convert to a NamespacedKey
162 + * @param defaultNamespace the default namespace to use if none was
163 + * supplied. If null, the {@code minecraft} namespace
164 + * ({@link #minecraft(String)}) will be used
165 + * @return the created NamespacedKey. null if invalid key
166 + * @see #fromString(String)
167 + */
168 + @Nullable
169 + public static NamespacedKey fromString(@NotNull String string, @Nullable Plugin defaultNamespace) {
170 + Preconditions.checkArgument(string != null && !string.isEmpty(), "Input string must not be empty or null");
171 +
172 + String[] components = string.split(":", 3);
173 + if (components.length > 2) {
174 + return null;
175 + }
176 +
177 + String key = (components.length == 2) ? components[1] : "";
178 + if (components.length == 1) {
179 + String value = components[0];
180 + if (value.isEmpty() || !VALID_KEY.matcher(value).matches()) {
181 + return null;
182 + }
183 +
184 + return (defaultNamespace != null) ? new NamespacedKey(defaultNamespace, value) : minecraft(value);
185 + } else if (components.length == 2 && !VALID_KEY.matcher(key).matches()) {
186 + return null;
187 + }
188 +
189 + String namespace = components[0];
190 + if (namespace.isEmpty()) {
191 + return (defaultNamespace != null) ? new NamespacedKey(defaultNamespace, key) : minecraft(key);
192 + }
193 +
194 + if (!VALID_KEY.matcher(namespace).matches()) {
195 + return null;
196 + }
197 +
198 + return new NamespacedKey(namespace, key);
199 + }
200 +
201 + /**
202 + * Get a NamespacedKey from the supplied string.
203 + *
204 + * The default namespace will be Minecraft's (i.e.
205 + * {@link #minecraft(String)}).
206 + *
207 + * @param key the key to convert to a NamespacedKey
208 + * @return the created NamespacedKey. null if invalid
209 + * @see #fromString(String, Plugin)
210 + */
211 + @Nullable
212 + public static NamespacedKey fromString(@NotNull String key) {
213 + return fromString(key, null);
214 + }
142 215 }

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut