Skip to content

Commit b19cdb7

Browse files
authored
Crl fixes and more (#220)
* fix classes not finding bindings * clean up some lsp stuff * test builds for this branch * fix issue with traits & auto package declaration * delete script cache when java version changed * explicitly declare lcl has parent loader * some convenience methods for logging * close #215 * dont test build this branch
1 parent 5f87d4b commit b19cdb7

File tree

13 files changed

+78
-32
lines changed

13 files changed

+78
-32
lines changed

src/main/java/com/cleanroommc/groovyscript/GroovyScriptConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ public static class Compat {
1919

2020
@Config.Comment("Enables DE energy core compat. Config is mainly for other mods compat.")
2121
public boolean draconicEvolutionEnergyCore = true;
22+
23+
@Config.Name("ExtendedCrafting recipe maker makes grs recipes")
24+
@Config.Comment("If this is true, the recipe maker from ExtendedCrafting will produce a script for GroovyScript instead of CraftTweaker.")
25+
public boolean extendedCraftingRecipeMakerMakesGrsRecipes = true;
2226
}
2327
}

src/main/java/com/cleanroommc/groovyscript/api/GroovyLog.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.function.Supplier;
1515

1616
/**
17-
* A interface for the GroovyScript logger. The log is separate to Minecraft's normal and debug log.
17+
* An interface for the GroovyScript logger. The log is separate to Minecraft's normal and debug log.
1818
* The generated log file can be found at "[Minecraft instance]/groovy.log".
1919
* All logging methods format its content similarly to how C does it.
2020
* Curly braces in the msg parameter get replaced with the given arguments.
@@ -269,6 +269,19 @@ interface Msg {
269269
*/
270270
Msg add(boolean condition, String msg, Object... args);
271271

272+
/**
273+
* Adds a sub message to this message with exactly one parameter, but only if the given condition is true. The arg {@link Supplier}
274+
* is invoked if the condition is true.
275+
*
276+
* @param condition sub message will only be added if this is true
277+
* @param msg sub message
278+
* @param arg sub message argument
279+
* @return this
280+
*/
281+
default Msg add(boolean condition, String msg, Supplier<Object> arg) {
282+
return add(condition, msg, (Object) arg);
283+
}
284+
272285
/**
273286
* Adds a sub message to this message, but only if the given condition is true.
274287
* For convenience.
@@ -290,8 +303,8 @@ interface Msg {
290303
Msg add(boolean condition, Consumer<Msg> msgBuilder);
291304

292305
/**
293-
* Adds an exception to the message. The exception will always be logged at last.
294-
* The exception counts as a sub message. This message can only have one message at a time.
306+
* Adds an exception to the message. The exception will always be logged at last. The exception counts as a sub message. This
307+
* message can only have one message at a time.
295308
*
296309
* @param throwable exception.
297310
* @return this

src/main/java/com/cleanroommc/groovyscript/core/mixin/extendedcrafting/ItemRecipeMakerMixin.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import com.blakebr0.extendedcrafting.item.ItemRecipeMaker;
55
import com.blakebr0.extendedcrafting.lib.IExtendedTable;
66
import com.blakebr0.extendedcrafting.tile.TileEnderCrafter;
7+
import com.cleanroommc.groovyscript.GroovyScriptConfig;
78
import com.cleanroommc.groovyscript.helper.ingredient.GroovyScriptCodeConverter;
89
import com.google.common.base.Joiner;
910
import net.minecraft.item.ItemStack;
1011
import net.minecraftforge.oredict.OreDictionary;
1112
import org.spongepowered.asm.mixin.Mixin;
1213
import org.spongepowered.asm.mixin.Shadow;
14+
import org.spongepowered.asm.mixin.Unique;
1315
import org.spongepowered.asm.mixin.injection.At;
1416
import org.spongepowered.asm.mixin.injection.Inject;
1517
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@@ -28,6 +30,7 @@ public abstract class ItemRecipeMakerMixin {
2830

2931
@Inject(method = "setClipboard", at = @At("HEAD"), cancellable = true)
3032
public void setClipboard(IExtendedTable table, ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
33+
if (!GroovyScriptConfig.compat.extendedCraftingRecipeMakerMakesGrsRecipes) return;
3134
if (Desktop.isDesktopSupported()) {
3235
boolean ender = table instanceof TileEnderCrafter;
3336
StringBuilder string = (new StringBuilder("mods.extendedcrafting.")).append(ender ? "EnderCrafting" : "TableCrafting");
@@ -54,20 +57,21 @@ public void setClipboard(IExtendedTable table, ItemStack stack, CallbackInfoRetu
5457
cir.setReturnValue(false);
5558
}
5659

60+
@Unique
5761
public String groovyscript$makeItemArrayShapeless(IExtendedTable table) {
5862
StringBuilder builder = new StringBuilder();
5963
boolean isEmpty = true;
6064
for (ItemStack stack : table.getMatrix()) {
6165
if (!stack.isEmpty()) {
62-
builder.append(groovyscript$makeItem(stack))
63-
.append(", ");
66+
builder.append(groovyscript$makeItem(stack)).append(", ");
6467
isEmpty = false;
6568
}
6669
}
6770
if (isEmpty) return null;
6871
return builder.delete(builder.length() - 2, builder.length()).toString();
6972
}
7073

74+
@Unique
7175
public String groovyscript$makeItemArrayShaped(IExtendedTable table, boolean removeEmpties) {
7276
List<List<String>> matrix = new ArrayList<>();
7377
int row = 0;

src/main/java/com/cleanroommc/groovyscript/core/mixin/groovy/MetaClassImplMixin.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.cleanroommc.groovyscript.core.mixin.groovy;
22

33
import com.cleanroommc.groovyscript.GroovyScript;
4-
import com.cleanroommc.groovyscript.api.GroovyLog;
54
import com.cleanroommc.groovyscript.api.IDynamicGroovyProperty;
65
import com.cleanroommc.groovyscript.sandbox.security.GroovySecurityManager;
76
import groovy.lang.*;
@@ -23,9 +22,6 @@ public abstract class MetaClassImplMixin {
2322
@Shadow
2423
protected abstract Object doInvokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass);
2524

26-
@Shadow
27-
protected MetaClassRegistry registry;
28-
2925
@Shadow
3026
protected abstract Object invokeMissingMethod(Object instance, String methodName, Object[] arguments, RuntimeException original, boolean isCallToSuper);
3127

@@ -48,6 +44,8 @@ public abstract class MetaClassImplMixin {
4844
@Final
4945
private MetaMethod[] additionalMetaMethods;
5046

47+
@Shadow protected MetaClassRegistry registry;
48+
5149
@Inject(method = "<init>(Ljava/lang/Class;[Lgroovy/lang/MetaMethod;)V", at = @At("TAIL"))
5250
public void removeBlacklistedAdditional(Class<?> theClass, MetaMethod[] add, CallbackInfo ci) {
5351
if (additionalMetaMethods.length > 0) {
@@ -132,7 +130,7 @@ private Object invokePropertyOrMissing(Object object, String methodName, Object[
132130
} else if (!isCallToSuper && object instanceof IDynamicGroovyProperty dynamicGroovyProperty) {
133131
// TODO remove in 1.2.0
134132
value = dynamicGroovyProperty.getProperty(methodName);
135-
} else if (object.getClass().getClassLoader() instanceof GroovyClassLoader) {
133+
} else if (object instanceof GroovyObject) {
136134
value = GroovyScript.getSandbox().getBindings().get(methodName);
137135
}
138136

src/main/java/com/cleanroommc/groovyscript/core/mixin/groovy/ModuleNodeMixin.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cleanroommc.groovyscript.GroovyScript;
44
import com.cleanroommc.groovyscript.api.GroovyLog;
55
import com.cleanroommc.groovyscript.sandbox.FileUtil;
6+
import com.cleanroommc.groovyscript.sandbox.RunConfig;
67
import org.codehaus.groovy.ast.ModuleNode;
78
import org.codehaus.groovy.ast.PackageNode;
89
import org.codehaus.groovy.control.SourceUnit;
@@ -25,6 +26,11 @@ public abstract class ModuleNodeMixin {
2526
public void init(SourceUnit context, CallbackInfo ci) {
2627
// auto set package name
2728
String script = context.getName();
29+
if (!RunConfig.isGroovyFile(script)) {
30+
// probably not a script file
31+
// can happen with traits
32+
return;
33+
}
2834
String rel = FileUtil.relativize(GroovyScript.getScriptPath(), script);
2935
int i = rel.lastIndexOf(File.separatorChar);
3036
if (i >= 0) {
@@ -37,6 +43,11 @@ public void init(SourceUnit context, CallbackInfo ci) {
3743
@Inject(method = "setPackage", at = @At("HEAD"), cancellable = true)
3844
public void setPackage(PackageNode packageNode, CallbackInfo ci) {
3945
if (this.packageNode == null || this.context == null) return;
46+
if (!RunConfig.isGroovyFile(this.context.getName())) {
47+
// probably not a script file
48+
// can happen with traits
49+
return;
50+
}
4051
// package name was already set -> only copy data of new node
4152
String cur = this.packageNode.getName();
4253
String newName = packageNode.getName();

src/main/java/com/cleanroommc/groovyscript/sandbox/CompiledClass.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cleanroommc.groovyscript.sandbox;
22

33
import com.cleanroommc.groovyscript.api.GroovyLog;
4+
import groovy.lang.GroovyClassLoader;
45
import org.apache.commons.lang3.builder.ToStringBuilder;
56

67
import java.io.File;
@@ -47,6 +48,12 @@ public void onCompile(Class<?> clazz, String basePath) {
4748
}
4849
}
4950

51+
protected void ensureLoaded(GroovyClassLoader classLoader, String basePath) {
52+
if (this.clazz == null) {
53+
this.clazz = classLoader.defineClass(this.name, this.data);
54+
}
55+
}
56+
5057
public boolean readData(String basePath) {
5158
if (this.data != null && GroovyScriptSandbox.ENABLE_CACHE) return true;
5259
File file = getDataFile(basePath);

src/main/java/com/cleanroommc/groovyscript/sandbox/CompiledScript.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,13 @@ public void ensureLoaded(GroovyClassLoader classLoader, String basePath) {
4747
for (CompiledClass comp : this.innerClasses) {
4848
if (comp.clazz == null) {
4949
if (comp.readData(basePath)) {
50-
comp.clazz = classLoader.defineClass(comp.name, comp.data);
50+
comp.ensureLoaded(classLoader, basePath);
5151
} else {
5252
GroovyLog.get().error("Error loading inner class {} for class {}", comp.name, this.name);
5353
}
5454
}
5555
}
56-
if (this.clazz == null) {
57-
this.clazz = classLoader.defineClass(this.name, this.data);
58-
}
56+
super.ensureLoaded(classLoader, basePath);
5957
}
6058

6159
@NotNull

src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyLogImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public Path getPath() {
138138
}
139139

140140
/**
141-
* Logs a info msg to the groovy log AND Minecraft's log
141+
* Logs an info msg to the groovy log AND Minecraft's log
142142
*
143143
* @param msg message
144144
* @param args arguments
@@ -347,6 +347,13 @@ public Msg add(String msg, Object... data) {
347347
@Override
348348
public Msg add(boolean condition, String msg, Object... args) {
349349
if (condition) {
350+
if (args != null && args.length > 0) {
351+
for (int i = 0; i < args.length; i++) {
352+
if (args[i] instanceof Supplier<?> s) {
353+
args[i] = s.get();
354+
}
355+
}
356+
}
350357
return add(msg, args);
351358
}
352359
return this;

src/main/java/com/cleanroommc/groovyscript/sandbox/GroovySandbox.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import groovy.util.ResourceException;
1212
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
1313
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
14+
import net.minecraft.launchwrapper.Launch;
1415
import org.codehaus.groovy.control.CompilerConfiguration;
1516
import org.codehaus.groovy.runtime.InvokerHelper;
1617
import org.jetbrains.annotations.ApiStatus;
@@ -82,7 +83,7 @@ protected void stopRunning() {
8283
}
8384

8485
protected GroovyScriptEngine createScriptEngine() {
85-
GroovyScriptEngine engine = new GroovyScriptEngine(this.scriptEnvironment);
86+
GroovyScriptEngine engine = new GroovyScriptEngine(this.scriptEnvironment, Launch.classLoader);
8687
CompilerConfiguration config = new CompilerConfiguration(CompilerConfiguration.DEFAULT);
8788
config.setSourceEncoding("UTF-8");
8889
engine.setConfig(config);
@@ -103,11 +104,11 @@ public void load() throws Exception {
103104
Binding binding = createBindings();
104105
Set<File> executedClasses = new ObjectOpenHashSet<>();
105106

106-
running.set(true);
107+
this.running.set(true);
107108
try {
108109
load(engine, binding, executedClasses, true);
109110
} finally {
110-
running.set(false);
111+
this.running.set(false);
111112
postRun();
112113
setCurrentScript(null);
113114
}

src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.codehaus.groovy.control.SourceUnit;
3030
import org.codehaus.groovy.control.customizers.ImportCustomizer;
3131
import org.codehaus.groovy.runtime.InvokerInvocationException;
32+
import org.codehaus.groovy.vmplugin.VMPlugin;
3233
import org.jetbrains.annotations.ApiStatus;
3334
import org.jetbrains.annotations.Nullable;
3435

@@ -115,7 +116,8 @@ private void readIndex() {
115116
if (jsonElement == null || !jsonElement.isJsonObject()) return;
116117
JsonObject json = jsonElement.getAsJsonObject();
117118
int cacheVersion = json.get("version").getAsInt();
118-
if (cacheVersion != CACHE_VERSION) {
119+
String java = json.has("java") ? json.get("java").getAsString() : "";
120+
if (cacheVersion != CACHE_VERSION || !java.equals(VMPlugin.getJavaVersion())) {
119121
// cache version changed -> force delete cache
120122
deleteScriptCache();
121123
return;
@@ -135,6 +137,7 @@ private void writeIndex() {
135137
JsonObject json = new JsonObject();
136138
json.addProperty("!DANGER!", "DO NOT EDIT THIS FILE!!!");
137139
json.addProperty("version", CACHE_VERSION);
140+
json.addProperty("java", VMPlugin.getJavaVersion());
138141
JsonArray index = new JsonArray();
139142
json.add("index", index);
140143
for (Map.Entry<String, CompiledScript> entry : this.index.entrySet()) {

src/main/java/net/prominic/groovyls/compiler/control/GroovyLSCompilationUnit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,4 @@ public ASTNodeVisitor recompileAndVisitASTIfContextChanged(@Nullable URI context
137137

138138
return compileAndVisitAST(context);
139139
}
140-
}
140+
}

src/main/java/net/prominic/groovyls/config/CompilationUnitFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@ protected void addDirectoryToCompilationUnit(Path dirPath, GroovyLSCompilationUn
140140
addOpenFileToCompilationUnit(uri, contents, compilationUnit);
141141
});
142142
}
143-
}
143+
}

src/main/java/net/prominic/groovyls/config/ICompilationUnitFactory.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
import java.util.List;
2828

2929
public interface ICompilationUnitFactory {
30-
/**
31-
* If this factory would normally reuse an existing compilation unit, forces
32-
* the creation of a new one.
33-
*/
34-
public void invalidateCompilationUnit();
3530

36-
public List<String> getAdditionalClasspathList();
31+
/**
32+
* If this factory would normally reuse an existing compilation unit, forces the creation of a new one.
33+
*/
34+
void invalidateCompilationUnit();
3735

38-
public void setAdditionalClasspathList(List<String> classpathList);
36+
List<String> getAdditionalClasspathList();
3937

40-
/**
41-
* Returns a compilation unit.
42-
*/
38+
void setAdditionalClasspathList(List<String> classpathList);
39+
40+
/**
41+
* Returns a compilation unit.
42+
*/
4343
GroovyLSCompilationUnit create(Path workspaceRoot, @Nullable URI context);
44-
}
44+
}

0 commit comments

Comments
 (0)