diff options
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/mixin')
6 files changed, 126 insertions, 76 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java index 10b67f0..49a0eb9 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/IdentifierMixin.java @@ -8,10 +8,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(Identifier.class) public class IdentifierMixin { - + /*//todo remove @Inject(method = "isPathValid(Ljava/lang/String;)Z", cancellable = true, at = @At("HEAD")) private static void isPathValid(String path, CallbackInfoReturnable<Boolean> ci) { - if(path.startsWith("citresewn") || path.startsWith("optifine") || path.startsWith("mcpatcher")) + if(path.startsWith("textures/block") || path.startsWith("block") || path.startsWith("optifine")) ci.setReturnValue(true); return; - } + }*/ } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java index 1679188..fd593ee 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ItemRendererMixin.java @@ -18,7 +18,7 @@ public class ItemRendererMixin { if (CITResewn.INSTANCE.activeCITs == null) return; - BakedModel citModel = CITResewn.INSTANCE.activeCITs.getItemModel(stack, cir.getReturnValue()); + BakedModel citModel = CITResewn.INSTANCE.activeCITs.getItemModel(stack, cir.getReturnValue(), world, entity); if (citModel != null) cir.setReturnValue(citModel); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java new file mode 100644 index 0000000..295af4f --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelLoaderMixin.java @@ -0,0 +1,104 @@ +package shcm.shsupercm.fabric.citresewn.mixin; + +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.resource.ResourceManager; +import net.minecraft.util.Identifier; +import net.minecraft.util.profiler.Profiler; +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.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.ActiveCITs; +import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.pack.CITParser; +import shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier; +import shcm.shsupercm.fabric.citresewn.pack.cits.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cits.CITItem; + +import java.util.*; +import java.util.stream.Collectors; + +@Mixin(ModelLoader.class) +public abstract class ModelLoaderMixin { + @Shadow @Final private ResourceManager resourceManager; + @Shadow @Final private Set<Identifier> modelsToLoad; + @Shadow @Final private Map<Identifier, UnbakedModel> unbakedModels; + @Shadow @Final private Map<Identifier, UnbakedModel> modelsToBake; + @Shadow @Final private Map<Identifier, BakedModel> bakedModels; + + private Map<Identifier, BakedModel> citOverrideCacheMap = new HashMap<>(); + + @Inject(method = "addModel", at = @At("TAIL")) + public void addCITModels(ModelIdentifier eventModelId, CallbackInfo ci) { if (eventModelId != ModelLoader.MISSING_ID) return; + if (CITResewn.INSTANCE.activeCITs != null) { + CITResewn.INSTANCE.activeCITs.dispose(); + CITResewn.INSTANCE.activeCITs = null; + } + + Collection<CIT> parsed = CITParser.parse(resourceManager.streamResourcePacks().collect(Collectors.toCollection(ArrayList::new))); + + for (CIT cit : parsed) + if (cit instanceof CITItem citItem) { + try { + citItem.loadUnbakedAssets(resourceManager); + + for (JsonUnbakedModel unbakedModel : citItem.unbakedAssets.values()) { + ResewnItemModelIdentifier id = new ResewnItemModelIdentifier(unbakedModel.id); + this.unbakedModels.put(id, unbakedModel); + this.modelsToLoad.addAll(unbakedModel.getModelDependencies()); + this.modelsToBake.put(id, unbakedModel); + } + } catch (Exception e) { + CITResewn.LOG.error(e.getMessage()); + } + } + + if (parsed.size() > 0) + CITResewn.INSTANCE.activeCITs = new ActiveCITs(parsed); + } + + @Inject(method = "bake", at = @At("RETURN")) + public void onBake(Identifier id, ModelBakeSettings settings, CallbackInfoReturnable<BakedModel> cir) { + this.citOverrideCacheMap.put(id, cir.getReturnValue()); + } + + @Inject(method = "upload", at = @At("RETURN")) + public void linkBakedModel(TextureManager textureManager, Profiler profiler, CallbackInfoReturnable<SpriteAtlasManager> cir) { + profiler.push("citresewn_linking"); + + if (CITResewn.INSTANCE.activeCITs != null) { + for (CITItem citItem : CITResewn.INSTANCE.activeCITs.citItems.values().stream().flatMap(Collection::stream).distinct().collect(Collectors.toList())) { + for (Map.Entry<Identifier, JsonUnbakedModel> citModelEntry : citItem.unbakedAssets.entrySet()) { + if (citModelEntry.getKey() == null) { + citItem.bakedModel = this.bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id)); + } else { + if (citItem.subItems == null) + citItem.subItems = new HashMap<>(); + + BakedModel override = citOverrideCacheMap.get(citModelEntry.getKey()); + + if (override == null) + override = citOverrideCacheMap.get(new ModelIdentifier(citModelEntry.getKey().getNamespace(), citModelEntry.getKey().getPath().substring(5), "inventory")); + + if (override != null) + citItem.subItems.put(override, this.bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id))); + else { + CITResewn.LOG.error("Skipping sub cit: No such sub item model \"" + citModelEntry.getKey().getPath().substring(5) + "\" in " + citItem.pack.resourcePack.getName() + " -> " + citItem.propertiesIdentifier.getPath()); + } + } + } + citItem.unbakedAssets = null; + } + } + + this.citOverrideCacheMap = null; + + profiler.pop(); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelOverrideListMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelOverrideListMixin.java deleted file mode 100644 index 0bfaa4e..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ModelOverrideListMixin.java +++ /dev/null @@ -1,27 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.mixin; - -import net.minecraft.client.render.model.BakedModel; -import net.minecraft.client.render.model.ModelLoader; -import net.minecraft.client.render.model.UnbakedModel; -import net.minecraft.client.render.model.json.JsonUnbakedModel; -import net.minecraft.client.render.model.json.ModelOverride; -import net.minecraft.client.render.model.json.ModelOverrideList; -import net.minecraft.util.Identifier; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import shcm.shsupercm.fabric.citresewn.CITResewn; - -import java.util.function.Function; - -@Mixin(ModelOverrideList.class) -public class ModelOverrideListMixin { - @Inject(method = "bakeOverridingModel", at = @At("RETURN")) - public void onBakeOverridingModel(ModelLoader loader, JsonUnbakedModel parent, Function<Identifier, UnbakedModel> unbakedModelGetter, ModelOverride override, CallbackInfoReturnable<BakedModel> cir) { - if (cir.getReturnValue() != null) { - String[] split = override.getModelId().getPath().split("/"); - CITResewn.INSTANCE.bakedOverridesCache.put(cir.getReturnValue(), split[split.length - 1]); - } - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ReloadableResourceManagerImplMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ReloadableResourceManagerImplMixin.java deleted file mode 100644 index fb5a267..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/ReloadableResourceManagerImplMixin.java +++ /dev/null @@ -1,45 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.mixin; - -import net.minecraft.resource.*; -import net.minecraft.util.Identifier; -import net.minecraft.util.Unit; -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.callback.CallbackInfoReturnable; -import shcm.shsupercm.fabric.citresewn.ActiveCITs; -import shcm.shsupercm.fabric.citresewn.CITResewn; -import shcm.shsupercm.fabric.citresewn.pack.CITParser; -import shcm.shsupercm.fabric.citresewn.pack.cits.CIT; - -import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; -import java.util.function.Predicate; -import java.util.stream.Stream; - -@Mixin(ReloadableResourceManagerImpl.class) -public abstract class ReloadableResourceManagerImplMixin implements ReloadableResourceManager { - @Shadow @Final private ResourceType type; - @Shadow public abstract Stream<ResourcePack> streamResourcePacks(); - - @Shadow public abstract Collection<Identifier> findResources(String startingPath, Predicate<String> pathPredicate); - - @Inject(method = "reload", at = @At("HEAD")) - private void onReload(Executor prepareExecutor, Executor applyExecutor, CompletableFuture<Unit> initialStage, List<ResourcePack> packs, CallbackInfoReturnable<ResourceReload> cir) { - if (this.type != ResourceType.CLIENT_RESOURCES) - return; - - if (CITResewn.INSTANCE.activeCITs != null) { - CITResewn.INSTANCE.activeCITs.dispose(); - CITResewn.INSTANCE.activeCITs = null; - } - - Collection<CIT> cits = CITParser.parse(packs); - - if (cits.size() > 0) - CITResewn.INSTANCE.activeCITs = new ActiveCITs(cits); - } -}
\ No newline at end of file diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/SpriteAtlasTextureMixin.java b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/SpriteAtlasTextureMixin.java new file mode 100644 index 0000000..dc21ae1 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/mixin/SpriteAtlasTextureMixin.java @@ -0,0 +1,18 @@ +package shcm.shsupercm.fabric.citresewn.mixin; + +import net.minecraft.client.texture.SpriteAtlasTexture; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier; + +@Mixin(SpriteAtlasTexture.class) +public class SpriteAtlasTextureMixin { + @Inject(method = "getTexturePath", cancellable = true, at = @At("HEAD")) + public void forceLiteralResewnTextureIdentifier(Identifier id, CallbackInfoReturnable<Identifier> cir) { + if (id instanceof ResewnTextureIdentifier) + cir.setReturnValue(id); + } +} |