generated from CleanroomMC/TemplateDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GameObjectHandler enhancement (#156)
* tons of goh changes & other * deprecate IDynamicGroovyProperty * remove IDynamicGroovyProperty lsp support * fix mod compat * hide properties with alias name in completion * handle goh conflicts * fix crash when no element found * break out into GroovyPropertyContainer * some reviews * more reviews * goh -> ObjectMapper * some reviews * more reviews & fix merge * IGameObjectParser -> IObjectParser & fix comments * small fixes * objectMapper() -> objectMapperBuilder() * some java docs * fix errors
- Loading branch information
Showing
79 changed files
with
1,988 additions
and
1,488 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.cleanroommc.groovyscript.api; | ||
|
||
public interface Hidden { | ||
|
||
boolean isHidden(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 17 additions & 59 deletions
76
src/main/java/com/cleanroommc/groovyscript/api/IGameObjectParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,41 @@ | ||
package com.cleanroommc.groovyscript.api; | ||
|
||
import com.cleanroommc.groovyscript.gameobjects.GameObjectHandlers; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraftforge.registries.IForgeRegistry; | ||
import net.minecraftforge.registries.IForgeRegistryEntry; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A bracket handler returns a object based on its input arguments. | ||
* A bracket handler can be called from groovy lik this: | ||
* <p> | ||
* {@code bracket_handler_name(args)} | ||
* </p> | ||
* In the first way there is always only one argument which is a String. | ||
* In the second method the argument size is at least, but not limited to one. | ||
* The first argument is always a string. The other can be anything. | ||
* @deprecated use {@link IObjectParser} | ||
*/ | ||
@Deprecated | ||
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0") | ||
@FunctionalInterface | ||
public interface IGameObjectParser<T> { | ||
public interface IGameObjectParser<T> extends IObjectParser<T> { | ||
|
||
/** | ||
* Parses a object based on input arguments | ||
* | ||
* @param args arguments. length >= 1 && args[0] instanceof String | ||
* @return a parsed Object | ||
*/ | ||
@NotNull | ||
Result<T> parse(String mainArg, Object[] args); | ||
|
||
static <T extends IForgeRegistryEntry<T>> IGameObjectParser<T> wrapForgeRegistry(IForgeRegistry<T> forgeRegistry) { | ||
return (s, args) -> { | ||
Result<ResourceLocation> rl = GameObjectHandlers.parseResourceLocation(s, args); | ||
if (rl.hasError()) return Result.error(rl.getError()); | ||
T value = forgeRegistry.getValue(rl.getValue()); | ||
return value == null ? Result.error() : Result.some(value); | ||
}; | ||
static <T extends IForgeRegistryEntry<T>> IObjectParser<T> wrapForgeRegistry(IForgeRegistry<T> forgeRegistry) { | ||
return IObjectParser.wrapForgeRegistry(forgeRegistry); | ||
} | ||
|
||
static <T extends Enum<T>> IGameObjectParser<T> wrapEnum(Class<T> enumClass, boolean caseSensitive) { | ||
Map<String, T> map = new Object2ObjectOpenHashMap<>(); | ||
for (T t : enumClass.getEnumConstants()) { | ||
map.put(caseSensitive ? t.name() : t.name().toUpperCase(Locale.ROOT), t); | ||
} | ||
return (s, args) -> { | ||
T t = map.get(caseSensitive ? s : s.toUpperCase(Locale.ROOT)); | ||
return t == null ? Result.error() : Result.some(t); | ||
}; | ||
static <T extends Enum<T>> IObjectParser<T> wrapEnum(Class<T> enumClass, boolean caseSensitive) { | ||
return IObjectParser.wrapEnum(enumClass, caseSensitive); | ||
} | ||
|
||
static <T> IGameObjectParser<T> wrapStringGetter(Function<String, T> getter) { | ||
return wrapStringGetter(getter, false); | ||
static <T> IObjectParser<T> wrapStringGetter(Function<String, T> getter) { | ||
return IObjectParser.wrapStringGetter(getter); | ||
} | ||
|
||
static <T> IGameObjectParser<T> wrapStringGetter(Function<String, T> getter, boolean isUpperCase) { | ||
return (s, args) -> { | ||
if (args.length > 0) { | ||
return Result.error("extra arguments are not allowed"); | ||
} | ||
T t = getter.apply(isUpperCase ? s.toUpperCase(Locale.ROOT) : s); | ||
return t == null ? Result.error() : Result.some(t); | ||
}; | ||
static <T> IObjectParser<T> wrapStringGetter(Function<String, T> getter, boolean isUpperCase) { | ||
return IObjectParser.wrapStringGetter(getter, isUpperCase); | ||
} | ||
|
||
static <T, V> IGameObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction) { | ||
return wrapStringGetter(getter, trueTypeFunction, false); | ||
static <T, V> IObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction) { | ||
return IObjectParser.wrapStringGetter(getter, trueTypeFunction); | ||
} | ||
|
||
static <T, V> IGameObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction, boolean isUpperCase) { | ||
return (s, args) -> { | ||
if (args.length > 0) { | ||
return Result.error("extra arguments are not allowed"); | ||
} | ||
V v = getter.apply(isUpperCase ? s.toUpperCase(Locale.ROOT) : s); | ||
return v == null ? Result.error() : Result.some(trueTypeFunction.apply(v)); | ||
}; | ||
static <T, V> IObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction, boolean isUpperCase) { | ||
return IObjectParser.wrapStringGetter(getter, trueTypeFunction, isUpperCase); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/com/cleanroommc/groovyscript/api/IObjectParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.cleanroommc.groovyscript.api; | ||
|
||
import com.cleanroommc.groovyscript.mapper.ObjectMappers; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraftforge.registries.IForgeRegistry; | ||
import net.minecraftforge.registries.IForgeRegistryEntry; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A function to parse an object from a string and any amount of additional arguments of any type. | ||
* This is used for {@link com.cleanroommc.groovyscript.mapper.ObjectMapper object mappers}. | ||
* | ||
* @param <T> type of the parsed objects | ||
*/ | ||
@FunctionalInterface | ||
public interface IObjectParser<T> { | ||
|
||
/** | ||
* Parses an object based on input arguments | ||
* | ||
* @param mainArg the first argument. Usually an id to get an object from a map. | ||
* @param args additional arguments, like metadata for item stacks | ||
* @return a parsed object | ||
*/ | ||
@NotNull | ||
Result<T> parse(String mainArg, Object[] args); | ||
|
||
static <T extends IForgeRegistryEntry<T>> IObjectParser<T> wrapForgeRegistry(IForgeRegistry<T> forgeRegistry) { | ||
return (s, args) -> { | ||
Result<ResourceLocation> rl = ObjectMappers.parseResourceLocation(s, args); | ||
if (rl.hasError()) return Result.error(rl.getError()); | ||
T value = forgeRegistry.getValue(rl.getValue()); | ||
return value == null ? Result.error() : Result.some(value); | ||
}; | ||
} | ||
|
||
static <T extends Enum<T>> IObjectParser<T> wrapEnum(Class<T> enumClass, boolean caseSensitive) { | ||
Map<String, T> map = new Object2ObjectOpenHashMap<>(); | ||
for (T t : enumClass.getEnumConstants()) { | ||
map.put(caseSensitive ? t.name() : t.name().toUpperCase(Locale.ROOT), t); | ||
} | ||
return (s, args) -> { | ||
T t = map.get(caseSensitive ? s : s.toUpperCase(Locale.ROOT)); | ||
return t == null ? Result.error() : Result.some(t); | ||
}; | ||
} | ||
|
||
static <T> IObjectParser<T> wrapStringGetter(Function<String, T> getter) { | ||
return wrapStringGetter(getter, false); | ||
} | ||
|
||
static <T> IObjectParser<T> wrapStringGetter(Function<String, T> getter, boolean isUpperCase) { | ||
return (s, args) -> { | ||
if (args.length > 0) { | ||
return Result.error("extra arguments are not allowed"); | ||
} | ||
T t = getter.apply(isUpperCase ? s.toUpperCase(Locale.ROOT) : s); | ||
return t == null ? Result.error() : Result.some(t); | ||
}; | ||
} | ||
|
||
static <T, V> IObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction) { | ||
return wrapStringGetter(getter, trueTypeFunction, false); | ||
} | ||
|
||
static <T, V> IObjectParser<T> wrapStringGetter(Function<String, V> getter, Function<V, @NotNull T> trueTypeFunction, boolean isUpperCase) { | ||
return (s, args) -> { | ||
if (args.length > 0) { | ||
return Result.error("extra arguments are not allowed"); | ||
} | ||
V v = getter.apply(isUpperCase ? s.toUpperCase(Locale.ROOT) : s); | ||
return v == null ? Result.error() : Result.some(trueTypeFunction.apply(v)); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.