style(data-manipulation): Use ScopedValue instead of map from thread ID to value

This commit is contained in:
Johannes Frohnmeyer 2024-04-23 20:54:08 +02:00
parent 7a75c776e4
commit 799a49a065
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 12 additions and 19 deletions

View File

@ -1,6 +1,8 @@
package io.gitlab.jfronny.libjf.data.manipulation.api;
import io.gitlab.jfronny.commons.LazySupplier;
import io.gitlab.jfronny.commons.concurrent.ScopedValue;
import io.gitlab.jfronny.commons.throwable.ExceptionWrapper;
import io.gitlab.jfronny.commons.throwable.ThrowingRunnable;
import io.gitlab.jfronny.commons.throwable.ThrowingSupplier;
import io.gitlab.jfronny.libjf.LibJf;
@ -19,20 +21,17 @@ import java.util.function.Supplier;
public class UserResourceEvents {
public static <TVal, TEx extends Throwable> TVal disable(ThrowingSupplier<TVal, TEx> then) throws TEx {
try {
ResourcePackHook.setDisabled(true);
return then.get();
}
finally {
ResourcePackHook.setDisabled(false);
return ScopedValue.getWhere(ResourcePackHook.DISABLED, true, then.orThrow());
} catch (ExceptionWrapper ew) {
throw (TEx) ExceptionWrapper.unwrap(ew);
}
}
public static <TEx extends Throwable> void disable(ThrowingRunnable<TEx> then) throws TEx {
try {
ResourcePackHook.setDisabled(true);
then.run();
} finally {
ResourcePackHook.setDisabled(false);
ScopedValue.runWhere(ResourcePackHook.DISABLED, true, then.orThrow());
} catch (ExceptionWrapper ew) {
throw (TEx) ExceptionWrapper.unwrap(ew);
}
}

View File

@ -1,6 +1,7 @@
package io.gitlab.jfronny.libjf.data.manipulation.impl;
import io.gitlab.jfronny.commons.LazySupplier;
import io.gitlab.jfronny.commons.concurrent.ScopedValue;
import io.gitlab.jfronny.libjf.data.manipulation.api.UserResourceEvents;
import net.minecraft.resource.*;
import net.minecraft.resource.metadata.ResourceMetadataReader;
@ -9,22 +10,15 @@ import org.jetbrains.annotations.ApiStatus;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("unused")
@ApiStatus.Internal
public class ResourcePackHook {
private static final Map<Long, Boolean> disabled = new HashMap<>();
@ApiStatus.Internal
public static void setDisabled(boolean disabled) {
ResourcePackHook.disabled.put(Thread.currentThread().getId(), disabled);
}
public static final ScopedValue<Boolean> DISABLED = new ScopedValue<>();
@ApiStatus.Internal
public static boolean isDisabled() {
return disabled.getOrDefault(Thread.currentThread().getId(), false);
private static boolean isDisabled() {
return DISABLED.orElse(false);
}
public static InputSupplier<InputStream> hookOpenRoot(InputSupplier<InputStream> value, ResourcePack pack, String[] fileName) {