Improve HookImplementation.disable

This commit is contained in:
JFronny 2021-10-03 12:29:49 +02:00
parent bfb03f1286
commit e041ef890e
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
3 changed files with 24 additions and 6 deletions

View File

@ -1,5 +1,5 @@
package io.gitlab.jfronny.libjf.interfaces;
public interface ThrowingRunnable {
void run() throws Throwable;
public interface ThrowingRunnable<TEx extends Throwable> {
void run() throws TEx;
}

View File

@ -1,5 +1,5 @@
package io.gitlab.jfronny.libjf.interfaces;
public interface ThrowingSupplier<T> {
T get() throws Throwable;
public interface ThrowingSupplier<T, TEx extends Throwable> {
T get() throws TEx;
}

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.libjf.data.manipulation;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.ResourcePath;
import io.gitlab.jfronny.libjf.interfaces.ThrowingRunnable;
import io.gitlab.jfronny.libjf.interfaces.ThrowingSupplier;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
@ -15,7 +16,7 @@ import java.util.function.Supplier;
public class HookImplementation {
private static boolean disabled = false;
public static <T> T disable(Supplier<T> then) {
public static <TVal, TEx extends Throwable> TVal disable(ThrowingSupplier<TVal, TEx> then) throws TEx {
try {
disabled = true;
return then.get();
@ -25,6 +26,23 @@ public class HookImplementation {
}
}
public static <TEx extends Throwable> void disable(ThrowingRunnable<TEx> then) throws TEx {
try {
disabled = true;
then.run();
} finally {
disabled = false;
}
}
public static <T> T disable(Supplier<T> then) {
try {
disabled = true;
return then.get();
}
finally {
disabled = false;
}
}
public static void disable(Runnable then) {
try {
disabled = true;