| 1 | package net.minecraft.resources;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import io.netty.buffer.ByteBuf;
|
| 9 | import java.nio.file.Path;
|
| 10 | import java.util.Locale;
|
| 11 | import java.util.function.UnaryOperator;
|
| 12 | import net.minecraft.IdentifierException;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 15 | import net.minecraft.network.codec.StreamCodec;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public final class Identifier implements Comparable<Identifier> {
|
| 19 | public static final Codec<Identifier> CODEC = Codec.STRING.<Identifier>comapFlatMap(Identifier::read, Identifier::toString).stable();
|
| 20 | public static final StreamCodec<ByteBuf, Identifier> STREAM_CODEC = ByteBufCodecs.STRING_UTF8.map(Identifier::parse, Identifier::toString);
|
| 21 | public static final SimpleCommandExceptionType ERROR_INVALID = new SimpleCommandExceptionType(Component.translatable("argument.id.invalid"));
|
| 22 | public static final char NAMESPACE_SEPARATOR = ':';
|
| 23 | public static final String DEFAULT_NAMESPACE = "minecraft";
|
| 24 | public static final String REALMS_NAMESPACE = "realms";
|
| 25 | public static final String ALLOWED_NAMESPACE_CHARACTERS = "[a-z0-9_.-]";
|
| 26 | private final String namespace;
|
| 27 | private final String path;
|
| 28 |
|
| 29 | private Identifier(final String namespace, final String path) {
|
| 30 | assert isValidNamespace(namespace);
|
| 31 | assert isValidPath(path);
|
| 32 | this.namespace = namespace;
|
| 33 | this.path = path;
|
| 34 | }
|
| 35 |
|
| 36 | private static Identifier createUntrusted(final String namespace, final String path) {
|
| 37 | return new Identifier(assertValidNamespace(namespace, path), assertValidPath(namespace, path));
|
| 38 | }
|
| 39 |
|
| 40 | public static Identifier fromNamespaceAndPath(final String namespace, final String path) {
|
| 41 | return createUntrusted(namespace, path);
|
| 42 | }
|
| 43 |
|
| 44 | public static Identifier parse(final String identifier) {
|
| 45 | return bySeparator(identifier, ':');
|
| 46 | }
|
| 47 |
|
| 48 | public static Identifier withDefaultNamespace(final String path) {
|
| 49 | return new Identifier("minecraft", assertValidPath("minecraft", path));
|
| 50 | }
|
| 51 |
|
| 52 | public static @Nullable Identifier tryParse(final String identifier) {
|
| 53 | return tryBySeparator(identifier, ':');
|
| 54 | }
|
| 55 |
|
| 56 | public static @Nullable Identifier tryBuild(final String namespace, final String path) {
|
| 57 | return isValidNamespace(namespace) && isValidPath(path) ? new Identifier(namespace, path) : null;
|
| 58 | }
|
| 59 |
|
| 60 | public static Identifier bySeparator(final String identifier, final char separator) {
|
| 61 | int separatorIndex = identifier.indexOf(separator);
|
| 62 | if (separatorIndex >= 0) {
|
| 63 | String path = identifier.substring(separatorIndex + 1);
|
| 64 | if (separatorIndex != 0) {
|
| 65 | String namespace = identifier.substring(0, separatorIndex);
|
| 66 | return createUntrusted(namespace, path);
|
| 67 | } else {
|
| 68 | return withDefaultNamespace(path);
|
| 69 | }
|
| 70 | } else {
|
| 71 | return withDefaultNamespace(identifier);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public static @Nullable Identifier tryBySeparator(final String identifier, final char separator) {
|
| 76 | int separatorIndex = identifier.indexOf(separator);
|
| 77 | if (separatorIndex >= 0) {
|
| 78 | String path = identifier.substring(separatorIndex + 1);
|
| 79 | if (!isValidPath(path)) {
|
| 80 | return null;
|
| 81 | } else if (separatorIndex != 0) {
|
| 82 | String namespace = identifier.substring(0, separatorIndex);
|
| 83 | return isValidNamespace(namespace) ? new Identifier(namespace, path) : null;
|
| 84 | } else {
|
| 85 | return new Identifier("minecraft", path);
|
| 86 | }
|
| 87 | } else {
|
| 88 | return isValidPath(identifier) ? new Identifier("minecraft", identifier) : null;
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | public static DataResult<Identifier> read(final String input) {
|
| 93 | try {
|
| 94 | return DataResult.success(parse(input));
|
| 95 | } catch (IdentifierException e) {
|
| 96 | return DataResult.error(() -> "Not a valid resource location: " + input + " " + e.getMessage());
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public String getPath() {
|
| 101 | return this.path;
|
| 102 | }
|
| 103 |
|
| 104 | public String getNamespace() {
|
| 105 | return this.namespace;
|
| 106 | }
|
| 107 |
|
| 108 | public Identifier withPath(final String newPath) {
|
| 109 | return new Identifier(this.namespace, assertValidPath(this.namespace, newPath));
|
| 110 | }
|
| 111 |
|
| 112 | public Identifier withPath(final UnaryOperator<String> modifier) {
|
| 113 | return this.withPath(modifier.apply(this.path));
|
| 114 | }
|
| 115 |
|
| 116 | public Identifier withPrefix(final String prefix) {
|
| 117 | return this.withPath(prefix + this.path);
|
| 118 | }
|
| 119 |
|
| 120 | public Identifier withSuffix(final String suffix) {
|
| 121 | return this.withPath(this.path + suffix);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public String toString() {
|
| 126 | return this.namespace + ":" + this.path;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public boolean equals(final @Nullable Object o) {
|
| 131 | if (this == o) {
|
| 132 | return true;
|
| 133 | } else {
|
| 134 | return !(o instanceof Identifier that) ? false : this.namespace.equals(that.namespace) && this.path.equals(that.path);
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public int hashCode() {
|
| 140 | return 31 * this.namespace.hashCode() + this.path.hashCode();
|
| 141 | }
|
| 142 |
|
| 143 | public int compareTo(final Identifier o) {
|
| 144 | int result = this.path.compareTo(o.path);
|
| 145 | if (result == 0) {
|
| 146 | result = this.namespace.compareTo(o.namespace);
|
| 147 | }
|
| 148 |
|
| 149 | return result;
|
| 150 | }
|
| 151 |
|
| 152 | public Path resolveAgainst(final Path root) {
|
| 153 | Path resultingPath = root.resolve(this.getNamespace(), this.getPath());
|
| 154 | Path normalizedPath = resultingPath.normalize();
|
| 155 | Path normalizedRoot = root.normalize();
|
| 156 | if (!normalizedPath.startsWith(normalizedRoot)) {
|
| 157 | throw new IllegalStateException(
|
| 158 | String.format(Locale.ROOT, "Identifier \"%s\" tried to access path \"%s\" from root \"%s\"", this, normalizedPath, normalizedRoot)
|
| 159 | );
|
| 160 | } else {
|
| 161 | return resultingPath;
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | public String toDebugFileName() {
|
| 166 | return this.toString().replace('/', '_').replace(':', '_');
|
| 167 | }
|
| 168 |
|
| 169 | public String toLanguageKey() {
|
| 170 | return this.namespace + "." + this.path;
|
| 171 | }
|
| 172 |
|
| 173 | public String toShortLanguageKey() {
|
| 174 | return this.namespace.equals("minecraft") ? this.path : this.toLanguageKey();
|
| 175 | }
|
| 176 |
|
| 177 | public String toShortString() {
|
| 178 | return this.namespace.equals("minecraft") ? this.path : this.toString();
|
| 179 | }
|
| 180 |
|
| 181 | public String toLanguageKey(final String prefix) {
|
| 182 | return prefix + "." + this.toLanguageKey();
|
| 183 | }
|
| 184 |
|
| 185 | public String toLanguageKey(final String prefix, final String suffix) {
|
| 186 | return prefix + "." + this.toLanguageKey() + "." + suffix;
|
| 187 | }
|
| 188 |
|
| 189 | private static String readGreedy(final StringReader reader) {
|
| 190 | int start = reader.getCursor();
|
| 191 |
|
| 192 | while (reader.canRead() && isAllowedInIdentifier(reader.peek())) {
|
| 193 | reader.skip();
|
| 194 | }
|
| 195 |
|
| 196 | return reader.getString().substring(start, reader.getCursor());
|
| 197 | }
|
| 198 |
|
| 199 | public static Identifier read(final StringReader reader) throws CommandSyntaxException {
|
| 200 | int start = reader.getCursor();
|
| 201 | String raw = readGreedy(reader);
|
| 202 |
|
| 203 | try {
|
| 204 | return parse(raw);
|
| 205 | } catch (IdentifierException ex) {
|
| 206 | reader.setCursor(start);
|
| 207 | throw ERROR_INVALID.createWithContext(reader);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | public static Identifier readNonEmpty(final StringReader reader) throws CommandSyntaxException {
|
| 212 | int start = reader.getCursor();
|
| 213 | String raw = readGreedy(reader);
|
| 214 | if (raw.isEmpty()) {
|
| 215 | throw ERROR_INVALID.createWithContext(reader);
|
| 216 | }
|
| 217 |
|
| 218 | try {
|
| 219 | return parse(raw);
|
| 220 | } catch (IdentifierException ex) {
|
| 221 | reader.setCursor(start);
|
| 222 | throw ERROR_INVALID.createWithContext(reader);
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | public static boolean isAllowedInIdentifier(final char c) {
|
| 227 | return c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c == '_' || c == ':' || c == '/' || c == '.' || c == '-';
|
| 228 | }
|
| 229 |
|
| 230 | public static boolean isValidPath(final String path) {
|
| 231 | for (int i = 0; i < path.length(); i++) {
|
| 232 | if (!validPathChar(path.charAt(i))) {
|
| 233 | return false;
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | return true;
|
| 238 | }
|
| 239 |
|
| 240 | public static boolean isValidNamespace(final String namespace) {
|
| 241 | if (namespace.equals("..")) {
|
| 242 | return false;
|
| 243 | }
|
| 244 |
|
| 245 | for (int i = 0; i < namespace.length(); i++) {
|
| 246 | if (!validNamespaceChar(namespace.charAt(i))) {
|
| 247 | return false;
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | return true;
|
| 252 | }
|
| 253 |
|
| 254 | private static String assertValidNamespace(final String namespace, final String path) {
|
| 255 | if (!isValidNamespace(namespace)) {
|
| 256 | throw new IdentifierException("Non [a-z0-9_.-] character in namespace of identifier: " + namespace + ":" + path);
|
| 257 | } else {
|
| 258 | return namespace;
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | public static boolean validPathChar(final char c) {
|
| 263 | return c == '_' || c == '-' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '/' || c == '.';
|
| 264 | }
|
| 265 |
|
| 266 | private static boolean validNamespaceChar(final char c) {
|
| 267 | return c == '_' || c == '-' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '.';
|
| 268 | }
|
| 269 |
|
| 270 | private static String assertValidPath(final String namespace, final String path) {
|
| 271 | if (!isValidPath(path)) {
|
| 272 | throw new IdentifierException("Non [a-z0-9/._-] character in path of location: " + namespace + ":" + path);
|
| 273 | } else {
|
| 274 | return path;
|
| 275 | }
|
| 276 | }
|
| 277 | }
|
| 278 |
|