aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/mixin
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/mixin')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java7
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java53
2 files changed, 60 insertions, 0 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java
index 84a0f43..70a3815 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/JsonUnbakedModelAccessor.java
@@ -3,6 +3,7 @@ package shcm.shsupercm.fabric.citresewn.mixin;
import com.mojang.datafixers.util.Either;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.util.SpriteIdentifier;
+import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@@ -12,4 +13,10 @@ import java.util.Map;
public interface JsonUnbakedModelAccessor {
@Accessor
Map<String, Either<SpriteIdentifier, String>> getTextureMap();
+
+ @Accessor
+ Identifier getParentId();
+
+ @Accessor
+ void setParentId(Identifier parentId);
}
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java
index a5e6a30..07f08fd 100644
--- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java
+++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java
@@ -1,17 +1,22 @@
package shcm.shsupercm.fabric.citresewn.mixin;
+import com.mojang.datafixers.util.Either;
import net.minecraft.client.render.model.*;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.client.util.ModelIdentifier;
+import net.minecraft.client.util.SpriteIdentifier;
+import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;
+import org.apache.commons.io.IOUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import shcm.shsupercm.fabric.citresewn.ActiveCITs;
@@ -19,9 +24,13 @@ import shcm.shsupercm.fabric.citresewn.CITResewn;
import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig;
import shcm.shsupercm.fabric.citresewn.pack.CITParser;
import shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier;
+import shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier;
import shcm.shsupercm.fabric.citresewn.pack.cits.CIT;
import shcm.shsupercm.fabric.citresewn.pack.cits.CITItem;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
@@ -32,6 +41,7 @@ public abstract class ModelLoaderMixin {
@Shadow @Final private Map<Identifier, UnbakedModel> unbakedModels;
@Shadow @Final private Map<Identifier, UnbakedModel> modelsToBake;
@Shadow @Final private Map<Identifier, BakedModel> bakedModels;
+ @Shadow protected abstract JsonUnbakedModel loadModelFromJson(Identifier id) throws IOException;
private Map<Identifier, BakedModel> citOverrideCacheMap = new HashMap<>();
@@ -111,4 +121,47 @@ public abstract class ModelLoaderMixin {
profiler.pop();
}
+
+
+ @Inject(method = "loadModelFromJson", cancellable = true, at = @At("HEAD"))
+ public void forceLiteralResewnModelIdentifier(Identifier id, CallbackInfoReturnable<JsonUnbakedModel> cir) {
+ if (id instanceof ResewnItemModelIdentifier) {
+ InputStream is = null;
+ Resource resource = null;
+ try {
+ JsonUnbakedModel json = JsonUnbakedModel.deserialize(IOUtils.toString(is = (resource = resourceManager.getResource(id)).getInputStream(), StandardCharsets.UTF_8));
+ json.id = id.toString();
+ json.id = json.id.substring(0, json.id.length() - 5);
+
+ ((JsonUnbakedModelAccessor) json).getTextureMap().replaceAll((layer, original) -> {
+ Optional<SpriteIdentifier> left = original.left();
+ if (left.isPresent() && left.get().getTextureId().getPath().startsWith("./")) {
+ Identifier resolvedIdentifier = CIT.resolvePath(id, left.get().getTextureId().getPath(), ".png", null);
+ if (resolvedIdentifier != null)
+ return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier)));
+ }
+ return original;
+ });
+
+ Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId();
+ if (parentId.getPath().startsWith("./")) {
+ parentId = CIT.resolvePath(id, parentId.getPath(), ".json", null);
+ if (parentId != null)
+ ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId));
+ }
+
+ cir.setReturnValue(json);
+ } catch (Exception ignored) {
+ } finally {
+ IOUtils.closeQuietly(is, resource);
+ }
+ }
+ }
+
+ @Redirect(method = "loadModelFromJson", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;"))
+ public Resource getResource(ResourceManager resourceManager, Identifier id) throws IOException {
+ if (id.getPath().endsWith(".json.json") && id.getPath().contains("cit"))
+ return resourceManager.getResource(new Identifier(id.getNamespace(), id.getPath().substring(7, id.getPath().length() - 5)));
+ return resourceManager.getResource(id);
+ }
}