File size: 10,481 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package net.minecraft.resources;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import io.netty.buffer.ByteBuf;
import java.lang.reflect.Type;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;
import net.minecraft.ResourceLocationException;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.GsonHelper;

public final class ResourceLocation implements Comparable<ResourceLocation> {
    public static final Codec<ResourceLocation> CODEC = Codec.STRING.comapFlatMap(ResourceLocation::read, ResourceLocation::toString).stable();
    public static final StreamCodec<ByteBuf, ResourceLocation> STREAM_CODEC = ByteBufCodecs.STRING_UTF8
        .map(ResourceLocation::parse, ResourceLocation::toString);
    public static final SimpleCommandExceptionType ERROR_INVALID = new SimpleCommandExceptionType(Component.translatable("argument.id.invalid"));
    public static final char NAMESPACE_SEPARATOR = ':';
    public static final String DEFAULT_NAMESPACE = "minecraft";
    public static final String REALMS_NAMESPACE = "realms";
    private final String namespace;
    private final String path;

    private ResourceLocation(String p_135811_, String p_135812_) {
        assert isValidNamespace(p_135811_);

        assert isValidPath(p_135812_);

        this.namespace = p_135811_;
        this.path = p_135812_;
    }

    private static ResourceLocation createUntrusted(String p_344238_, String p_343734_) {
        return new ResourceLocation(assertValidNamespace(p_344238_, p_343734_), assertValidPath(p_344238_, p_343734_));
    }

    public static ResourceLocation fromNamespaceAndPath(String p_344609_, String p_343842_) {
        return createUntrusted(p_344609_, p_343842_);
    }

    public static ResourceLocation parse(String p_342815_) {
        return bySeparator(p_342815_, ':');
    }

    public static ResourceLocation withDefaultNamespace(String p_343785_) {
        return new ResourceLocation("minecraft", assertValidPath("minecraft", p_343785_));
    }

    @Nullable
    public static ResourceLocation tryParse(String p_135821_) {
        return tryBySeparator(p_135821_, ':');
    }

    @Nullable
    public static ResourceLocation tryBuild(String p_214294_, String p_214295_) {
        return isValidNamespace(p_214294_) && isValidPath(p_214295_) ? new ResourceLocation(p_214294_, p_214295_) : null;
    }

    public static ResourceLocation bySeparator(String p_342228_, char p_344234_) {
        int i = p_342228_.indexOf(p_344234_);
        if (i >= 0) {
            String s = p_342228_.substring(i + 1);
            if (i != 0) {
                String s1 = p_342228_.substring(0, i);
                return createUntrusted(s1, s);
            } else {
                return withDefaultNamespace(s);
            }
        } else {
            return withDefaultNamespace(p_342228_);
        }
    }

    @Nullable
    public static ResourceLocation tryBySeparator(String p_344079_, char p_344067_) {
        int i = p_344079_.indexOf(p_344067_);
        if (i >= 0) {
            String s = p_344079_.substring(i + 1);
            if (!isValidPath(s)) {
                return null;
            } else if (i != 0) {
                String s1 = p_344079_.substring(0, i);
                return isValidNamespace(s1) ? new ResourceLocation(s1, s) : null;
            } else {
                return new ResourceLocation("minecraft", s);
            }
        } else {
            return isValidPath(p_344079_) ? new ResourceLocation("minecraft", p_344079_) : null;
        }
    }

    public static DataResult<ResourceLocation> read(String p_135838_) {
        try {
            return DataResult.success(parse(p_135838_));
        } catch (ResourceLocationException resourcelocationexception) {
            return DataResult.error(() -> "Not a valid resource location: " + p_135838_ + " " + resourcelocationexception.getMessage());
        }
    }

    public String getPath() {
        return this.path;
    }

    public String getNamespace() {
        return this.namespace;
    }

    public ResourceLocation withPath(String p_251088_) {
        return new ResourceLocation(this.namespace, assertValidPath(this.namespace, p_251088_));
    }

    public ResourceLocation withPath(UnaryOperator<String> p_250342_) {
        return this.withPath(p_250342_.apply(this.path));
    }

    public ResourceLocation withPrefix(String p_250620_) {
        return this.withPath(p_250620_ + this.path);
    }

    public ResourceLocation withSuffix(String p_266769_) {
        return this.withPath(this.path + p_266769_);
    }

    @Override
    public String toString() {
        return this.namespace + ":" + this.path;
    }

    @Override
    public boolean equals(Object p_135846_) {
        if (this == p_135846_) {
            return true;
        } else {
            return !(p_135846_ instanceof ResourceLocation resourcelocation)
                ? false
                : this.namespace.equals(resourcelocation.namespace) && this.path.equals(resourcelocation.path);
        }
    }

    @Override
    public int hashCode() {
        return 31 * this.namespace.hashCode() + this.path.hashCode();
    }

    public int compareTo(ResourceLocation p_135826_) {
        int i = this.path.compareTo(p_135826_.path);
        if (i == 0) {
            i = this.namespace.compareTo(p_135826_.namespace);
        }

        return i;
    }

    public String toDebugFileName() {
        return this.toString().replace('/', '_').replace(':', '_');
    }

    public String toLanguageKey() {
        return this.namespace + "." + this.path;
    }

    public String toShortLanguageKey() {
        return this.namespace.equals("minecraft") ? this.path : this.toLanguageKey();
    }

    public String toLanguageKey(String p_214297_) {
        return p_214297_ + "." + this.toLanguageKey();
    }

    public String toLanguageKey(String p_270871_, String p_270199_) {
        return p_270871_ + "." + this.toLanguageKey() + "." + p_270199_;
    }

    private static String readGreedy(StringReader p_330450_) {
        int i = p_330450_.getCursor();

        while (p_330450_.canRead() && isAllowedInResourceLocation(p_330450_.peek())) {
            p_330450_.skip();
        }

        return p_330450_.getString().substring(i, p_330450_.getCursor());
    }

    public static ResourceLocation read(StringReader p_135819_) throws CommandSyntaxException {
        int i = p_135819_.getCursor();
        String s = readGreedy(p_135819_);

        try {
            return parse(s);
        } catch (ResourceLocationException resourcelocationexception) {
            p_135819_.setCursor(i);
            throw ERROR_INVALID.createWithContext(p_135819_);
        }
    }

    public static ResourceLocation readNonEmpty(StringReader p_330926_) throws CommandSyntaxException {
        int i = p_330926_.getCursor();
        String s = readGreedy(p_330926_);
        if (s.isEmpty()) {
            throw ERROR_INVALID.createWithContext(p_330926_);
        } else {
            try {
                return parse(s);
            } catch (ResourceLocationException resourcelocationexception) {
                p_330926_.setCursor(i);
                throw ERROR_INVALID.createWithContext(p_330926_);
            }
        }
    }

    public static boolean isAllowedInResourceLocation(char p_135817_) {
        return p_135817_ >= '0' && p_135817_ <= '9'
            || p_135817_ >= 'a' && p_135817_ <= 'z'
            || p_135817_ == '_'
            || p_135817_ == ':'
            || p_135817_ == '/'
            || p_135817_ == '.'
            || p_135817_ == '-';
    }

    public static boolean isValidPath(String p_135842_) {
        for (int i = 0; i < p_135842_.length(); i++) {
            if (!validPathChar(p_135842_.charAt(i))) {
                return false;
            }
        }

        return true;
    }

    public static boolean isValidNamespace(String p_135844_) {
        for (int i = 0; i < p_135844_.length(); i++) {
            if (!validNamespaceChar(p_135844_.charAt(i))) {
                return false;
            }
        }

        return true;
    }

    private static String assertValidNamespace(String p_250769_, String p_249616_) {
        if (!isValidNamespace(p_250769_)) {
            throw new ResourceLocationException("Non [a-z0-9_.-] character in namespace of location: " + p_250769_ + ":" + p_249616_);
        } else {
            return p_250769_;
        }
    }

    public static boolean validPathChar(char p_135829_) {
        return p_135829_ == '_'
            || p_135829_ == '-'
            || p_135829_ >= 'a' && p_135829_ <= 'z'
            || p_135829_ >= '0' && p_135829_ <= '9'
            || p_135829_ == '/'
            || p_135829_ == '.';
    }

    private static boolean validNamespaceChar(char p_135836_) {
        return p_135836_ == '_' || p_135836_ == '-' || p_135836_ >= 'a' && p_135836_ <= 'z' || p_135836_ >= '0' && p_135836_ <= '9' || p_135836_ == '.';
    }

    private static String assertValidPath(String p_251418_, String p_248828_) {
        if (!isValidPath(p_248828_)) {
            throw new ResourceLocationException("Non [a-z0-9/._-] character in path of location: " + p_251418_ + ":" + p_248828_);
        } else {
            return p_248828_;
        }
    }

    public static class Serializer implements JsonDeserializer<ResourceLocation>, JsonSerializer<ResourceLocation> {
        public ResourceLocation deserialize(JsonElement p_135851_, Type p_135852_, JsonDeserializationContext p_135853_) throws JsonParseException {
            return ResourceLocation.parse(GsonHelper.convertToString(p_135851_, "location"));
        }

        public JsonElement serialize(ResourceLocation p_135855_, Type p_135856_, JsonSerializationContext p_135857_) {
            return new JsonPrimitive(p_135855_.toString());
        }
    }
}