Skip to content

Commit fb945a6

Browse files
committed
some convenience methods for logging
1 parent 768be0e commit fb945a6

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

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/groovy/MetaClassImplMixin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ public abstract class MetaClassImplMixin {
2222
@Shadow
2323
protected abstract Object doInvokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass);
2424

25-
@Shadow
26-
protected MetaClassRegistry registry;
27-
2825
@Shadow
2926
protected abstract Object invokeMissingMethod(Object instance, String methodName, Object[] arguments, RuntimeException original, boolean isCallToSuper);
3027

@@ -47,6 +44,8 @@ public abstract class MetaClassImplMixin {
4744
@Final
4845
private MetaMethod[] additionalMetaMethods;
4946

47+
@Shadow protected MetaClassRegistry registry;
48+
5049
@Inject(method = "<init>(Ljava/lang/Class;[Lgroovy/lang/MetaMethod;)V", at = @At("TAIL"))
5150
public void removeBlacklistedAdditional(Class<?> theClass, MetaMethod[] add, CallbackInfo ci) {
5251
if (additionalMetaMethods.length > 0) {

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;

0 commit comments

Comments
 (0)