style(data-manipulation): Use ScopedValue instead of map from thread ID to value
This commit is contained in:
parent
7a75c776e4
commit
799a49a065
|
@ -1,6 +1,8 @@
|
||||||
package io.gitlab.jfronny.libjf.data.manipulation.api;
|
package io.gitlab.jfronny.libjf.data.manipulation.api;
|
||||||
|
|
||||||
import io.gitlab.jfronny.commons.LazySupplier;
|
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.ThrowingRunnable;
|
||||||
import io.gitlab.jfronny.commons.throwable.ThrowingSupplier;
|
import io.gitlab.jfronny.commons.throwable.ThrowingSupplier;
|
||||||
import io.gitlab.jfronny.libjf.LibJf;
|
import io.gitlab.jfronny.libjf.LibJf;
|
||||||
|
@ -19,20 +21,17 @@ import java.util.function.Supplier;
|
||||||
public class UserResourceEvents {
|
public class UserResourceEvents {
|
||||||
public static <TVal, TEx extends Throwable> TVal disable(ThrowingSupplier<TVal, TEx> then) throws TEx {
|
public static <TVal, TEx extends Throwable> TVal disable(ThrowingSupplier<TVal, TEx> then) throws TEx {
|
||||||
try {
|
try {
|
||||||
ResourcePackHook.setDisabled(true);
|
return ScopedValue.getWhere(ResourcePackHook.DISABLED, true, then.orThrow());
|
||||||
return then.get();
|
} catch (ExceptionWrapper ew) {
|
||||||
}
|
throw (TEx) ExceptionWrapper.unwrap(ew);
|
||||||
finally {
|
|
||||||
ResourcePackHook.setDisabled(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <TEx extends Throwable> void disable(ThrowingRunnable<TEx> then) throws TEx {
|
public static <TEx extends Throwable> void disable(ThrowingRunnable<TEx> then) throws TEx {
|
||||||
try {
|
try {
|
||||||
ResourcePackHook.setDisabled(true);
|
ScopedValue.runWhere(ResourcePackHook.DISABLED, true, then.orThrow());
|
||||||
then.run();
|
} catch (ExceptionWrapper ew) {
|
||||||
} finally {
|
throw (TEx) ExceptionWrapper.unwrap(ew);
|
||||||
ResourcePackHook.setDisabled(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package io.gitlab.jfronny.libjf.data.manipulation.impl;
|
package io.gitlab.jfronny.libjf.data.manipulation.impl;
|
||||||
|
|
||||||
import io.gitlab.jfronny.commons.LazySupplier;
|
import io.gitlab.jfronny.commons.LazySupplier;
|
||||||
|
import io.gitlab.jfronny.commons.concurrent.ScopedValue;
|
||||||
import io.gitlab.jfronny.libjf.data.manipulation.api.UserResourceEvents;
|
import io.gitlab.jfronny.libjf.data.manipulation.api.UserResourceEvents;
|
||||||
import net.minecraft.resource.*;
|
import net.minecraft.resource.*;
|
||||||
import net.minecraft.resource.metadata.ResourceMetadataReader;
|
import net.minecraft.resource.metadata.ResourceMetadataReader;
|
||||||
|
@ -9,22 +10,15 @@ import org.jetbrains.annotations.ApiStatus;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public class ResourcePackHook {
|
public class ResourcePackHook {
|
||||||
private static final Map<Long, Boolean> disabled = new HashMap<>();
|
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public static void setDisabled(boolean disabled) {
|
public static final ScopedValue<Boolean> DISABLED = new ScopedValue<>();
|
||||||
ResourcePackHook.disabled.put(Thread.currentThread().getId(), disabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ApiStatus.Internal
|
private static boolean isDisabled() {
|
||||||
public static boolean isDisabled() {
|
return DISABLED.orElse(false);
|
||||||
return disabled.getOrDefault(Thread.currentThread().getId(), false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static InputSupplier<InputStream> hookOpenRoot(InputSupplier<InputStream> value, ResourcePack pack, String[] fileName) {
|
public static InputSupplier<InputStream> hookOpenRoot(InputSupplier<InputStream> value, ResourcePack pack, String[] fileName) {
|
||||||
|
|
Loading…
Reference in New Issue