Make ResourcePackHook.disabled thread "safe"

This commit is contained in:
JFronny 2021-10-21 21:03:12 +02:00
parent a79571fa92
commit 9f60822395
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
2 changed files with 12 additions and 10 deletions

View File

@ -6,7 +6,7 @@ minecraft_version=1.17.1
yarn_mappings=build.61
loader_version=0.12.1
# Mod Properties
mod_version=2.1.1
mod_version=2.1.2
maven_group=io.gitlab.jfronny
archives_base_name=libjf

View File

@ -10,32 +10,34 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
@SuppressWarnings("unused")
public class ResourcePackHook {
private static boolean disabled = false;
private static final Map<Long, Boolean> disabled = new HashMap<>();
public static void setDisabled(boolean disabled) {
ResourcePackHook.disabled = disabled;
ResourcePackHook.disabled.put(Thread.currentThread().getId(), disabled);
}
public static boolean isDisabled() {
return disabled;
return disabled.getOrDefault(Thread.currentThread().getId(), false);
}
public static boolean hookContains(boolean value, ResourcePack pack, ResourceType type, Identifier id) {
return disabled ? value : UserResourceEvents.CONTAINS.invoker().contains(type, id, value, pack);
return isDisabled() ? value : UserResourceEvents.CONTAINS.invoker().contains(type, id, value, pack);
}
public static InputStream hookOpen(InputStream value, ResourcePack pack, ResourceType type, Identifier id) throws IOException {
if (disabled) return value;
if (isDisabled()) return value;
InputStream is = UserResourceEvents.OPEN.invoker().open(type, id, value, pack);
if (is == null)
throw new FileNotFoundException(new ResourcePath(type, id).getName() + "CN");
return is;
}
public static InputStream hookOpenEx(IOException ex, ResourcePack pack, ResourceType type, Identifier id) throws IOException {
if (disabled) throw ex;
if (isDisabled()) throw ex;
try {
return hookOpen(null, pack, type, id);
}
@ -44,10 +46,10 @@ public class ResourcePackHook {
}
}
public static Collection<Identifier> hookFindResources(Collection<Identifier> value, ResourcePack pack, ResourceType type, String namespace, String prefix, int maxDepth, Predicate<String> pathFilter) {
return disabled ? value : UserResourceEvents.FIND_RESOURCE.invoker().findResources(type, namespace, prefix, maxDepth, pathFilter, value, pack);
return isDisabled() ? value : UserResourceEvents.FIND_RESOURCE.invoker().findResources(type, namespace, prefix, maxDepth, pathFilter, value, pack);
}
public static InputStream hookOpenRoot(InputStream value, ResourcePack pack, String fileName) throws IOException {
if (disabled) return value;
if (isDisabled()) return value;
InputStream is = value;
is = UserResourceEvents.OPEN_ROOT.invoker().openRoot(fileName, is, pack);
if (is == null)
@ -55,7 +57,7 @@ public class ResourcePackHook {
return is;
}
public static InputStream hookOpenRootEx(IOException ex, ResourcePack pack, String fileName) throws IOException {
if (disabled) throw ex;
if (isDisabled()) throw ex;
try {
return hookOpenRoot(null, pack, fileName);
}