-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move helper functions to BracketHelper
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
- Loading branch information
1 parent
2ab08c3
commit 23ad6de
Showing
4 changed files
with
112 additions
and
23 deletions.
There are no files selected for viewing
4 changes: 0 additions & 4 deletions
4
Common/src/main/java/com/blamejared/contenttweaker/core/ContentTweakerPlugin.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
63 changes: 63 additions & 0 deletions
63
Common/src/main/java/com/blamejared/contenttweaker/core/api/zen/bracket/BracketHelper.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,63 @@ | ||
package com.blamejared.contenttweaker.core.api.zen.bracket; | ||
|
||
import com.blamejared.contenttweaker.core.zen.rt.ResourceLocationNative; | ||
import com.blamejared.crafttweaker.api.util.ParseUtil; | ||
import net.minecraft.ResourceLocationException; | ||
import net.minecraft.resources.ResourceLocation; | ||
import org.openzen.zencode.shared.CodePosition; | ||
import org.openzen.zencode.shared.CompileException; | ||
import org.openzen.zencode.shared.CompileExceptionCode; | ||
import org.openzen.zenscript.lexer.ParseException; | ||
import org.openzen.zenscript.parser.expression.ParsedCallArguments; | ||
import org.openzen.zenscript.parser.expression.ParsedExpression; | ||
import org.openzen.zenscript.parser.expression.ParsedExpressionCall; | ||
import org.openzen.zenscript.parser.expression.ParsedExpressionMember; | ||
import org.openzen.zenscript.parser.expression.ParsedExpressionString; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
public final class BracketHelper { | ||
@FunctionalInterface | ||
public interface ParseOperation<T> { | ||
T execute() throws ParseException; | ||
} | ||
|
||
private BracketHelper() {} | ||
|
||
public static ResourceLocation locationOrThrow(final CodePosition position, final String in) throws ParseException { | ||
return locationOrThrow(position, in, () -> "Unable to convert given string \"" + in + "\" to resource location"); | ||
} | ||
|
||
public static ResourceLocation locationOrThrow(final CodePosition position, final String in, final Supplier<String> message) throws ParseException { | ||
Objects.requireNonNull(position); | ||
Objects.requireNonNull(message); | ||
try { | ||
return new ResourceLocation(Objects.requireNonNull(in, "null")); | ||
} catch (final NullPointerException | ResourceLocationException e) { | ||
throw new ParseException(position, message.get(), e); | ||
} | ||
} | ||
|
||
public static ParsedExpression locationArgument(final CodePosition position, final ResourceLocation location) { | ||
Objects.requireNonNull(position); | ||
Objects.requireNonNull(location); | ||
final ParsedExpression receiver = ParseUtil.staticMemberExpression(position, ResourceLocationNative.CLASS_NAME); | ||
final ParsedExpression of = new ParsedExpressionMember(position, receiver, "of", null); | ||
final ParsedExpressionString namespace = new ParsedExpressionString(position, location.getNamespace(), false); | ||
final ParsedExpressionString path = new ParsedExpressionString(position, location.getPath(), false); | ||
final ParsedCallArguments arguments = new ParsedCallArguments(null, List.of(namespace, path)); | ||
return new ParsedExpressionCall(position, of, arguments); | ||
} | ||
|
||
public static <F> F parseToCompile(final CodePosition position, final ParseOperation<F> operation) throws CompileException { | ||
try { | ||
return operation.execute(); | ||
} catch (final ParseException e) { | ||
final CompileException exception = new CompileException(position, CompileExceptionCode.PARSE_ERROR, e.message); | ||
exception.initCause(e); | ||
throw exception; | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
Common/src/main/java/com/blamejared/contenttweaker/core/zen/rt/ResourceLocationNative.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,44 @@ | ||
package com.blamejared.contenttweaker.core.zen.rt; | ||
|
||
import com.blamejared.contenttweaker.core.api.ContentTweakerConstants; | ||
import com.blamejared.contenttweaker.core.zen.ContentTweakerZenConstants; | ||
import com.blamejared.crafttweaker.api.annotation.ZenRegister; | ||
import com.blamejared.crafttweaker_annotations.annotations.NativeTypeRegistration; | ||
import net.minecraft.resources.ResourceLocation; | ||
import org.openzen.zencode.java.ZenCodeType; | ||
|
||
import java.util.Objects; | ||
|
||
@NativeTypeRegistration(value = ResourceLocation.class, zenCodeName = ResourceLocationNative.CLASS_NAME) | ||
@SuppressWarnings("unused") | ||
@ZenRegister(loaders = ContentTweakerConstants.CONTENT_LOADER_ID) | ||
public final class ResourceLocationNative { | ||
public static final String CLASS_NAME = ContentTweakerZenConstants.MAIN_PACKAGE + ".resource.ResourceLocation"; | ||
|
||
private ResourceLocationNative() {} | ||
|
||
@ZenCodeType.StaticExpansionMethod("of") | ||
public static ResourceLocation of(final String namespace, final String path) { | ||
return new ResourceLocation(namespace, path); // TODO("Panic on ResLocException") | ||
} | ||
|
||
@ZenCodeType.Getter("namespace") | ||
public static String namespace(final ResourceLocation $this) { | ||
return $this.getNamespace(); | ||
} | ||
|
||
@ZenCodeType.Getter("path") | ||
public static String path(final ResourceLocation $this) { | ||
return $this.getPath(); | ||
} | ||
|
||
@ZenCodeType.Operator(ZenCodeType.OperatorType.EQUALS) | ||
public static boolean is(final ResourceLocation $this, final ResourceLocation other) { | ||
return Objects.equals($this, other); | ||
} | ||
|
||
@ZenCodeType.Caster(implicit = true) | ||
public static String asString(final ResourceLocation $this) { | ||
return $this.toString(); | ||
} | ||
} |