|
1 | 1 | package com.cleanroommc.groovyscript.api; |
2 | 2 |
|
| 3 | +import com.cleanroommc.groovyscript.mapper.ObjectMappers; |
| 4 | +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
| 5 | +import net.minecraft.util.ResourceLocation; |
3 | 6 | import net.minecraftforge.registries.IForgeRegistry; |
4 | 7 | import net.minecraftforge.registries.IForgeRegistryEntry; |
5 | 8 | import org.jetbrains.annotations.ApiStatus; |
6 | 9 | import org.jetbrains.annotations.NotNull; |
7 | 10 |
|
| 11 | +import java.util.Locale; |
| 12 | +import java.util.Map; |
8 | 13 | import java.util.function.Function; |
9 | 14 |
|
10 | 15 | /** |
|
15 | 20 | @FunctionalInterface |
16 | 21 | public interface IGameObjectParser<T> extends IObjectParser<T> { |
17 | 22 |
|
18 | | - static <T extends IForgeRegistryEntry<T>> IObjectParser<T> wrapForgeRegistry(IForgeRegistry<T> forgeRegistry) { |
19 | | - return IObjectParser.wrapForgeRegistry(forgeRegistry); |
| 23 | + static <T extends IForgeRegistryEntry<T>> IGameObjectParser<T> wrapForgeRegistry(IForgeRegistry<T> forgeRegistry) { |
| 24 | + return (s, args) -> { |
| 25 | + Result<ResourceLocation> rl = ObjectMappers.parseResourceLocation(s, args); |
| 26 | + if (rl.hasError()) return Result.error(rl.getError()); |
| 27 | + T value = forgeRegistry.getValue(rl.getValue()); |
| 28 | + return value == null ? Result.error() : Result.some(value); |
| 29 | + }; |
20 | 30 | } |
21 | 31 |
|
22 | | - static <T extends Enum<T>> IObjectParser<T> wrapEnum(Class<T> enumClass, boolean caseSensitive) { |
23 | | - return IObjectParser.wrapEnum(enumClass, caseSensitive); |
| 32 | + static <T extends Enum<T>> IGameObjectParser<T> wrapEnum(Class<T> enumClass, boolean caseSensitive) { |
| 33 | + Map<String, T> map = new Object2ObjectOpenHashMap<>(); |
| 34 | + for (T t : enumClass.getEnumConstants()) { |
| 35 | + map.put(caseSensitive ? t.name() : t.name().toUpperCase(Locale.ROOT), t); |
| 36 | + } |
| 37 | + return (s, args) -> { |
| 38 | + T t = map.get(caseSensitive ? s : s.toUpperCase(Locale.ROOT)); |
| 39 | + return t == null ? Result.error() : Result.some(t); |
| 40 | + }; |
24 | 41 | } |
25 | 42 |
|
26 | | - static <T> IObjectParser<T> wrapStringGetter(Function<String, T> getter) { |
27 | | - return IObjectParser.wrapStringGetter(getter); |
| 43 | + static <T> IGameObjectParser<T> wrapStringGetter(Function<String, T> getter) { |
| 44 | + return wrapStringGetter(getter, false); |
28 | 45 | } |
29 | 46 |
|
30 | | - static <T> IObjectParser<T> wrapStringGetter(Function<String, T> getter, boolean isUpperCase) { |
31 | | - return IObjectParser.wrapStringGetter(getter, isUpperCase); |
| 47 | + static <T> IGameObjectParser<T> wrapStringGetter(Function<String, T> getter, boolean isUpperCase) { |
| 48 | + return (s, args) -> { |
| 49 | + if (args.length > 0) { |
| 50 | + return Result.error("extra arguments are not allowed"); |
| 51 | + } |
| 52 | + T t = getter.apply(isUpperCase ? s.toUpperCase(Locale.ROOT) : s); |
| 53 | + return t == null ? Result.error() : Result.some(t); |
| 54 | + }; |
32 | 55 | } |
33 | 56 |
|
34 | | - static <T, V> IObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction) { |
35 | | - return IObjectParser.wrapStringGetter(getter, trueTypeFunction); |
| 57 | + static <T, V> IGameObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction) { |
| 58 | + return wrapStringGetter(getter, trueTypeFunction, false); |
36 | 59 | } |
37 | 60 |
|
38 | | - static <T, V> IObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction, boolean isUpperCase) { |
39 | | - return IObjectParser.wrapStringGetter(getter, trueTypeFunction, isUpperCase); |
| 61 | + static <T, V> IGameObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction, boolean isUpperCase) { |
| 62 | + return (s, args) -> { |
| 63 | + if (args.length > 0) { |
| 64 | + return Result.error("extra arguments are not allowed"); |
| 65 | + } |
| 66 | + V v = getter.apply(isUpperCase ? s.toUpperCase(Locale.ROOT) : s); |
| 67 | + return v == null ? Result.error() : Result.some(trueTypeFunction.apply(v)); |
| 68 | + }; |
40 | 69 | } |
41 | 70 | } |
0 commit comments