Refactor libjf-data-manipulation-v0
This commit is contained in:
parent
a09305773d
commit
18e0d59a6a
|
@ -1,4 +1,4 @@
|
|||
package io.gitlab.jfronny.libjf.data.manipulation;
|
||||
package io.gitlab.jfronny.libjf.data.manipulation.api;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
@ -7,6 +7,7 @@ import net.minecraft.util.Identifier;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class RecipeUtil {
|
||||
private static final Set<Identifier> REMOVAL_BY_ID = new HashSet<>();
|
||||
private static final Set<ItemStack> RECIPES_FOR_REMOVAL = new HashSet<>();
|
|
@ -1,5 +1,8 @@
|
|||
package io.gitlab.jfronny.libjf.data.manipulation;
|
||||
package io.gitlab.jfronny.libjf.data.manipulation.api;
|
||||
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.impl.ResourcePackHook;
|
||||
import io.gitlab.jfronny.libjf.interfaces.ThrowingRunnable;
|
||||
import io.gitlab.jfronny.libjf.interfaces.ThrowingSupplier;
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import net.minecraft.resource.ResourcePack;
|
||||
|
@ -12,6 +15,25 @@ import java.util.Collection;
|
|||
import java.util.function.Predicate;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public static <TEx extends Throwable> void disable(ThrowingRunnable<TEx> then) throws TEx {
|
||||
try {
|
||||
ResourcePackHook.setDisabled(true);
|
||||
then.run();
|
||||
} finally {
|
||||
ResourcePackHook.setDisabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static final Event<Contains> CONTAINS = EventFactory.createArrayBacked(Contains.class,
|
||||
(listeners) -> (type, id, previous, pack) -> {
|
||||
for (Contains listener : listeners) {
|
|
@ -1,8 +1,7 @@
|
|||
package io.gitlab.jfronny.libjf.data.manipulation;
|
||||
package io.gitlab.jfronny.libjf.data.manipulation.impl;
|
||||
|
||||
import io.gitlab.jfronny.libjf.ResourcePath;
|
||||
import io.gitlab.jfronny.libjf.interfaces.ThrowingRunnable;
|
||||
import io.gitlab.jfronny.libjf.interfaces.ThrowingSupplier;
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.api.UserResourceEvents;
|
||||
import net.minecraft.resource.ResourcePack;
|
||||
import net.minecraft.resource.ResourceType;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
@ -13,37 +12,30 @@ import java.io.InputStream;
|
|||
import java.util.Collection;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class HookImplementation {
|
||||
@SuppressWarnings("unused")
|
||||
public class ResourcePackHook {
|
||||
private static boolean disabled = false;
|
||||
public static <TVal, TEx extends Throwable> TVal disable(ThrowingSupplier<TVal, TEx> then) throws TEx {
|
||||
try {
|
||||
disabled = true;
|
||||
return then.get();
|
||||
}
|
||||
finally {
|
||||
disabled = false;
|
||||
}
|
||||
|
||||
public static void setDisabled(boolean disabled) {
|
||||
ResourcePackHook.disabled = disabled;
|
||||
}
|
||||
|
||||
public static <TEx extends Throwable> void disable(ThrowingRunnable<TEx> then) throws TEx {
|
||||
try {
|
||||
disabled = true;
|
||||
then.run();
|
||||
} finally {
|
||||
disabled = false;
|
||||
}
|
||||
public static boolean isDisabled() {
|
||||
return disabled;
|
||||
}
|
||||
|
||||
public static boolean hookContains(boolean value, ResourcePack pack, ResourceType type, Identifier id) {
|
||||
return disabled ? 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;
|
||||
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;
|
||||
try {
|
||||
return hookOpen(null, pack, type, id);
|
||||
}
|
||||
|
@ -52,9 +44,10 @@ public class HookImplementation {
|
|||
}
|
||||
}
|
||||
public static Collection<Identifier> hookFindResources(Collection<Identifier> value, ResourcePack pack, ResourceType type, String namespace, String prefix, int maxDepth, Predicate<String> pathFilter) {
|
||||
return UserResourceEvents.FIND_RESOURCE.invoker().findResources(type, namespace, prefix, maxDepth, pathFilter, value, pack);
|
||||
return disabled ? 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;
|
||||
InputStream is = value;
|
||||
is = UserResourceEvents.OPEN_ROOT.invoker().openRoot(fileName, is, pack);
|
||||
if (is == null)
|
||||
|
@ -62,6 +55,7 @@ public class HookImplementation {
|
|||
return is;
|
||||
}
|
||||
public static InputStream hookOpenRootEx(IOException ex, ResourcePack pack, String fileName) throws IOException {
|
||||
if (disabled) throw ex;
|
||||
try {
|
||||
return hookOpenRoot(null, pack, fileName);
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package io.gitlab.jfronny.libjf.data.manipulation;
|
||||
package io.gitlab.jfronny.libjf.data.manipulation.impl;
|
||||
|
||||
import io.gitlab.jfronny.libjf.LibJf;
|
||||
import io.gitlab.jfronny.libjf.unsafe.asm.AsmConfig;
|
||||
import io.gitlab.jfronny.libjf.unsafe.asm.AsmTransformer;
|
||||
import io.gitlab.jfronny.libjf.unsafe.asm.patch.Patch;
|
||||
|
@ -13,9 +12,9 @@ import org.objectweb.asm.tree.*;
|
|||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
public class DataAsmConfig implements AsmConfig {
|
||||
public class ResourcePackHookPatch implements AsmConfig {
|
||||
private static final String TARGET_CLASS_INTERMEDIARY = "net.minecraft.class_3262";
|
||||
private static final String HOOK_IMPLEMENTATION = Type.getInternalName(HookImplementation.class);
|
||||
private static final String HOOK_IMPLEMENTATION = Type.getInternalName(ResourcePackHook.class);
|
||||
@Override
|
||||
public Set<String> skipClasses() {
|
||||
return Set.of(getClass(TARGET_CLASS_INTERMEDIARY));
|
|
@ -3,7 +3,7 @@ package io.gitlab.jfronny.libjf.data.manipulation.mixin;
|
|||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.gitlab.jfronny.libjf.LibJf;
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.RecipeUtil;
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.api.RecipeUtil;
|
||||
import net.minecraft.recipe.Recipe;
|
||||
import net.minecraft.recipe.RecipeManager;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
@ -18,7 +18,7 @@ import java.util.Map;
|
|||
|
||||
@Mixin(RecipeManager.class)
|
||||
public class RecipeManagerMixin {
|
||||
@ModifyVariable(method = "apply", at = @At(value = "INVOKE_ASSIGN", target = "Ljava/util/Set;iterator()Ljava/util/Iterator;", ordinal = 0, remap = false))
|
||||
@ModifyVariable(method = "apply(Ljava/util/Map;Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/util/profiler/Profiler;)V", at = @At(value = "INVOKE_ASSIGN", target = "Ljava/util/Set;iterator()Ljava/util/Iterator;", ordinal = 0, remap = false))
|
||||
private Iterator<Map.Entry<Identifier, JsonObject>> filterIterator(Iterator<Map.Entry<Identifier, JsonObject>> iterator) {
|
||||
ArrayList<Map.Entry<Identifier, JsonObject>> replacement = new ArrayList<>();
|
||||
while(iterator.hasNext()) {
|
||||
|
@ -35,7 +35,7 @@ public class RecipeManagerMixin {
|
|||
return replacement.iterator();
|
||||
}
|
||||
|
||||
@Redirect(method = "apply", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableMap$Builder;put(Ljava/lang/Object;Ljava/lang/Object;)Lcom/google/common/collect/ImmutableMap$Builder;", remap = false))
|
||||
@Redirect(method = "apply(Ljava/util/Map;Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/util/profiler/Profiler;)V", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableMap$Builder;put(Ljava/lang/Object;Ljava/lang/Object;)Lcom/google/common/collect/ImmutableMap$Builder;", remap = false))
|
||||
private ImmutableMap.Builder<Identifier, Recipe<?>> onPutRecipe(ImmutableMap.Builder<Identifier, Recipe<?>> builder, Object key, Object value) {
|
||||
Identifier id = (Identifier) key;
|
||||
Recipe<?> recipe = (Recipe<?>) value;
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
"mixins": ["libjf-data-manipulation-v0.mixins.json"],
|
||||
"entrypoints": {
|
||||
"libjf:asm": [
|
||||
"io.gitlab.jfronny.libjf.data.manipulation.DataAsmConfig"
|
||||
"io.gitlab.jfronny.libjf.data.manipulation.impl.ResourcePackHookPatch"
|
||||
]
|
||||
},
|
||||
"custom": {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package io.gitlab.jfronny.libjf.data.manipulation.test;
|
||||
|
||||
import io.gitlab.jfronny.libjf.LibJf;
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.RecipeUtil;
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.UserResourceEvents;
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.api.RecipeUtil;
|
||||
import io.gitlab.jfronny.libjf.data.manipulation.api.UserResourceEvents;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.resource.DirectoryResourcePack;
|
||||
|
|
Loading…
Reference in New Issue