generated from CleanroomMC/ForgeDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 30
Mapper backend refactor and texture deco improvements #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
589a06a
mapper backend refactor & texturedeco improvements
brachy84 c6edb5e
change texture deco cache path
brachy84 d1c9f7c
fixes & optimisations
brachy84 ef6c98c
reviews
brachy84 8f5dda3
Merge branch 'master' into mapper
brachy84 30eebdd
fix
brachy84 b270570
fix exception
brachy84 7edd23d
remove reflection & javadocs
brachy84 f363c3c
completions can consider all params
brachy84 15ead42
fix issues with custom mapper classes
brachy84 e52b477
spotless
brachy84 09f7126
Merge branch 'master' into mapper
brachy84 d26979f
fix weird bwm crash
brachy84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
190 changes: 190 additions & 0 deletions
190
src/main/java/com/cleanroommc/groovyscript/mapper/AbstractObjectMapper.java
This file contains hidden or 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,190 @@ | ||
| package com.cleanroommc.groovyscript.mapper; | ||
|
|
||
| import com.cleanroommc.groovyscript.api.GroovyLog; | ||
| import com.cleanroommc.groovyscript.api.INamed; | ||
| import com.cleanroommc.groovyscript.api.IObjectParser; | ||
| import com.cleanroommc.groovyscript.api.Result; | ||
| import com.cleanroommc.groovyscript.compat.mods.GroovyContainer; | ||
| import com.cleanroommc.groovyscript.helper.ArrayUtils; | ||
| import com.cleanroommc.groovyscript.sandbox.expand.IDocumented; | ||
| import com.cleanroommc.groovyscript.server.CompletionParams; | ||
| import com.cleanroommc.groovyscript.server.Completions; | ||
| import groovy.lang.Closure; | ||
| import groovy.lang.groovydoc.Groovydoc; | ||
| import groovy.lang.groovydoc.GroovydocHolder; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.codehaus.groovy.ast.ClassHelper; | ||
| import org.codehaus.groovy.ast.MethodNode; | ||
| import org.codehaus.groovy.ast.Parameter; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.lang.reflect.Modifier; | ||
| import java.util.*; | ||
|
|
||
| public abstract class AbstractObjectMapper<T> extends Closure<T> implements INamed, IDocumented, IObjectParser<T>, TextureBinder<T> { | ||
|
|
||
| private final String name; | ||
| private final GroovyContainer<?> mod; | ||
| private final Class<T> returnType; | ||
| private final List<Class<?>[]> paramTypes; | ||
| protected String documentation = StringUtils.EMPTY; | ||
| private List<MethodNode> methodNodes; | ||
|
|
||
| protected AbstractObjectMapper(String name, GroovyContainer<?> mod, Class<T> returnType) { | ||
| super(null); | ||
| this.name = name; | ||
| this.mod = mod; | ||
| this.returnType = returnType; | ||
| this.paramTypes = new ArrayList<>(); | ||
| addSignature(String.class); | ||
| } | ||
|
|
||
| /** | ||
| * Call in ctor to configure signatures | ||
| */ | ||
| protected final void clearSignatures() { | ||
| this.paramTypes.clear(); | ||
| this.methodNodes = null; | ||
| } | ||
|
|
||
| /** | ||
| * Call in ctor to configure signatures. | ||
| * By default, only `name(String)` exists. | ||
| */ | ||
| protected final void addSignature(Class<?>... types) { | ||
| this.paramTypes.add(types); | ||
| this.methodNodes = null; | ||
| } | ||
|
|
||
| public final T doCall(String s, Object... args) { | ||
| return invokeWithDefault(false, s, args); | ||
| } | ||
|
|
||
| public final T doCall() { | ||
| return invokeDefault(); | ||
| } | ||
|
|
||
| @Nullable | ||
| public final T invoke(boolean silent, String s, Object... args) { | ||
| Result<T> t = Objects.requireNonNull(parse(s, args), "Object mapper must return a non null result!"); | ||
| if (t.hasError()) { | ||
| if (!silent) { | ||
| if (this.mod == null) { | ||
| GroovyLog.get().error("Can't find {} for name {}!", name, s); | ||
| } else { | ||
| GroovyLog.get().error("Can't find {} {} for name {}!", mod, name, s); | ||
| } | ||
| if (t.getError() != null && !t.getError().isEmpty()) { | ||
| GroovyLog.get().error(" - reason: {}", t.getError()); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| return Objects.requireNonNull(t.getValue(), "Object mapper result must contain a non-null value!"); | ||
| } | ||
|
|
||
| public final T invokeWithDefault(boolean silent, String s, Object... args) { | ||
| T t = invoke(silent, s, args); | ||
| return t != null ? t : invokeDefault(); | ||
| } | ||
|
|
||
| public final T invokeDefault() { | ||
| Result<T> t = getDefaultValue(); | ||
| return t == null || t.hasError() ? null : t.getValue(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a default value for this mapper. This is called every time the parser returns an errored result. | ||
| * | ||
| * @return default value of this mapper. May be null | ||
| */ | ||
| public abstract Result<T> getDefaultValue(); | ||
|
|
||
| /** | ||
| * Adds all possible values this mapper can have at a param position. | ||
| * For example the `item()` mapper adds all item registry names when the index is 0. | ||
| * | ||
| * @param index the index of the param to complete | ||
| * @param params the values of all current (constant) params of the mapper | ||
| * @param items a list of completion items | ||
| */ | ||
| public void provideCompletion(int index, CompletionParams params, Completions items) {} | ||
|
|
||
| /** | ||
| * Draws an image representation of the given object. This is used for lsp. | ||
| * The icon will show up in VSC or other code editors with compat. | ||
| * If this is implemented, {@link #hasTextureBinder()} must return true. Otherwise, this will not be used. | ||
| * | ||
| * @param t object for which a texture should be drawn. | ||
| */ | ||
| public void bindTexture(T t) {} | ||
|
|
||
| /** | ||
| * Determines if {@link #bindTexture(Object)} is implemented and should be used. | ||
| * | ||
| * @return true if this mapper can bind textures | ||
| */ | ||
| public abstract boolean hasTextureBinder(); | ||
|
|
||
| @NotNull | ||
| public List<String> getTooltip(T t) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| public List<MethodNode> getMethodNodes() { | ||
| if (methodNodes == null) { | ||
| this.methodNodes = new ArrayList<>(); | ||
| for (Class<?>[] paramType : this.paramTypes) { | ||
| Parameter[] params = ArrayUtils.map( | ||
| paramType, | ||
| c -> new Parameter(ClassHelper.makeCached(c), ""), | ||
| new Parameter[paramType.length]); | ||
| MethodNode node = new MethodNode( | ||
| this.name, | ||
| Modifier.PUBLIC | Modifier.FINAL, | ||
| ClassHelper.makeCached(this.returnType), | ||
| params, | ||
| null, | ||
| null); | ||
| node.setDeclaringClass( | ||
| this.mod != null ? ClassHelper.makeCached(this.mod.get().getClass()) : ClassHelper.makeCached(ObjectMapperManager.class)); | ||
| node.setNodeMetaData(GroovydocHolder.DOC_COMMENT, new Groovydoc(getDocumentation(), node)); | ||
| this.methodNodes.add(node); | ||
| } | ||
| } | ||
| return methodNodes; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> getAliases() { | ||
| return Collections.singleton(this.name); | ||
| } | ||
|
|
||
| public GroovyContainer<?> getMod() { | ||
| return mod; | ||
| } | ||
|
|
||
| public Class<T> getReturnType() { | ||
| return returnType; | ||
| } | ||
|
|
||
| public List<Class<?>[]> getParamTypes() { | ||
| return paramTypes; | ||
| } | ||
|
|
||
| @Override | ||
| public final String getDocumentation() { | ||
| return documentation; | ||
| } | ||
|
|
||
| protected final String docOfType(String type) { | ||
| String mod = this.mod == null ? StringUtils.EMPTY : this.mod.getContainerName() + ' '; | ||
| return "returns a " + mod + type; | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/cleanroommc/groovyscript/mapper/BlockStateMapper.java
This file contains hidden or 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,79 @@ | ||
| package com.cleanroommc.groovyscript.mapper; | ||
|
|
||
| import com.cleanroommc.groovyscript.api.Result; | ||
| import com.cleanroommc.groovyscript.compat.mods.GroovyContainer; | ||
| import com.cleanroommc.groovyscript.server.CompletionParams; | ||
| import com.cleanroommc.groovyscript.server.Completions; | ||
| import net.minecraft.block.Block; | ||
| import net.minecraft.block.properties.IProperty; | ||
| import net.minecraft.block.state.IBlockState; | ||
| import net.minecraft.init.Blocks; | ||
| import net.minecraft.item.ItemStack; | ||
| import net.minecraft.util.ResourceLocation; | ||
| import net.minecraftforge.fml.common.registry.ForgeRegistries; | ||
| import net.prominic.groovyls.util.CompletionItemFactory; | ||
| import org.eclipse.lsp4j.CompletionItem; | ||
| import org.eclipse.lsp4j.CompletionItemKind; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class BlockStateMapper extends AbstractObjectMapper<IBlockState> { | ||
|
|
||
| public static final BlockStateMapper INSTANCE = new BlockStateMapper("blockstate", null); | ||
|
|
||
| protected BlockStateMapper(String name, GroovyContainer<?> mod) { | ||
| super(name, mod, IBlockState.class); | ||
| addSignature(String.class, int.class); | ||
| addSignature(String.class, String[].class); | ||
| this.documentation = docOfType("block state"); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<IBlockState> getDefaultValue() { | ||
| return Result.some(Blocks.AIR.getDefaultState()); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Result<IBlockState> parse(String mainArg, Object[] args) { | ||
brachy84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return ObjectMappers.parseBlockState(mainArg, args); | ||
| } | ||
|
|
||
| @Override | ||
| public void provideCompletion(int index, CompletionParams params, Completions items) { | ||
| if (index == 0) items.addAllOfRegistry(ForgeRegistries.BLOCKS); | ||
brachy84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (index >= 1 && params.isParamType(0, String.class)) { | ||
| Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(params.getParamAsType(0, String.class))); | ||
| if (block != null) { | ||
| // TODO completions for ints doesnt work properly | ||
| /*items.addAll(block.getBlockState().getValidStates(), state -> { | ||
| return CompletionItemFactory.createCompletion(CompletionItemKind.Value, String.valueOf(state.getBlock().getMetaFromState(state))); | ||
| });*/ | ||
| for (IProperty property : block.getBlockState().getProperties()) { | ||
| items.addAll(property.getAllowedValues(), val -> { | ||
| CompletionItem item = CompletionItemFactory.createCompletion(CompletionItemKind.Constant, property.getName() + "=" + property.getName((Comparable) val)); | ||
| return item; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void bindTexture(IBlockState iBlockState) { | ||
| ItemStack itemStack = new ItemStack(iBlockState.getBlock(), 1, iBlockState.getBlock().getMetaFromState(iBlockState)); | ||
| TextureBinder.ofItem().bindTexture(itemStack); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull List<String> getTooltip(IBlockState iBlockState) { | ||
| ItemStack itemStack = new ItemStack(iBlockState.getBlock(), 1, iBlockState.getBlock().getMetaFromState(iBlockState)); | ||
| return Collections.singletonList(itemStack.getDisplayName()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasTextureBinder() { | ||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this is the only place you are going to init
BlockStateMapper, why passnullhere and not drop and argument and callsuper(name, null, IBlockState.class)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was doing that for item so that mods can extend that class for a template (for example gregtech metaitems). I guess its not very useful for blockstate