Search in sources :

Example 1 with JsonUnbakedModel

use of net.minecraft.client.render.model.json.JsonUnbakedModel in project ThonkUtil by LimeAppleBoat.

the class ModelLoaderMixin method loadModelFromJson.

@Inject(method = "loadModelFromJson", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;"), cancellable = true)
public void loadModelFromJson(Identifier id, CallbackInfoReturnable<JsonUnbakedModel> cir) {
    // System.out.println(id.getNamespace() + ":" + id.getPath().split("/")[1]);
    if (!(Registry.ITEM.get(Identifier.tryParse(id.getNamespace() + ":" + id.getPath().split("/")[1])) instanceof CapeItem))
        return;
    String b = id.getNamespace() + ":" + "cape/" + id.getPath().split("/")[1];
    // Here, we can do different checks to see if the current item is a block-item, a tool, or other.
    // This can be done in a lot of different ways, like putting all our items in a Set or a List and checking if the current item is contained inside.
    // For this tutorial, we only have 1 item, so we will not be doing that, and we will be going with "generated" as default item type.
    String modelJson = ThonkUtilCapes.createItemModelJson(Identifier.tryParse(b), "cape");
    if ("".equals(modelJson))
        return;
    // We check if the json string we get is valid before continuing.
    JsonUnbakedModel model = JsonUnbakedModel.deserialize(modelJson);
    model.id = id.toString();
    cir.setReturnValue(model);
}
Also used : JsonUnbakedModel(net.minecraft.client.render.model.json.JsonUnbakedModel) CapeItem(com.jab125.thonkutil.api.CapeItem) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with JsonUnbakedModel

use of net.minecraft.client.render.model.json.JsonUnbakedModel in project CITResewn by SHsuperCM.

the class ModelLoaderMixin method linkBakedCITItemModels.

@Inject(method = "upload", at = @At("RETURN"))
public void linkBakedCITItemModels(TextureManager textureManager, Profiler profiler, CallbackInfoReturnable<SpriteAtlasManager> cir) {
    if (CITResewn.INSTANCE.activeCITs == null)
        return;
    profiler.push("citresewn_linking");
    info("Linking baked models to CITItems...");
    if (CITResewn.INSTANCE.activeCITs != null) {
        for (CITItem citItem : CITResewn.INSTANCE.activeCITs.citItems.values().stream().flatMap(Collection::stream).distinct().collect(Collectors.toList())) {
            for (Map.Entry<List<ModelOverride.Condition>, JsonUnbakedModel> citModelEntry : citItem.unbakedAssets.entrySet()) {
                if (citModelEntry.getKey() == null) {
                    citItem.bakedModel = this.bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id));
                } else {
                    BakedModel bakedModel = bakedModels.get(new ResewnItemModelIdentifier(citModelEntry.getValue().id));
                    if (bakedModel == null)
                        CITResewn.logWarnLoading("Skipping sub cit: Failed loading model for \"" + citModelEntry.getValue().id + "\" in " + citItem.pack.resourcePack.getName() + " -> " + citItem.propertiesIdentifier.getPath());
                    else
                        citItem.bakedSubModels.override(citModelEntry.getKey(), bakedModel);
                }
            }
            citItem.unbakedAssets = null;
        }
    }
    profiler.pop();
}
Also used : CITItem(shcm.shsupercm.fabric.citresewn.pack.cits.CITItem) BakedModel(net.minecraft.client.render.model.BakedModel) JsonUnbakedModel(net.minecraft.client.render.model.json.JsonUnbakedModel) ModelOverride(net.minecraft.client.render.model.json.ModelOverride) ResewnItemModelIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with JsonUnbakedModel

use of net.minecraft.client.render.model.json.JsonUnbakedModel in project CITResewn by SHsuperCM.

the class ModelLoaderMixin method addCITItemModels.

@Inject(method = "addModel", at = @At("TAIL"))
public void addCITItemModels(ModelIdentifier eventModelId, CallbackInfo ci) {
    if (eventModelId != ModelLoader.MISSING_ID)
        return;
    if (CITResewn.INSTANCE.activeCITs == null)
        return;
    info("Loading CITItem models...");
    CITResewn.INSTANCE.activeCITs.citItems.values().stream().flatMap(Collection::stream).distinct().forEach(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.logErrorLoading(e.getMessage());
        }
    });
    CITItem.GENERATED_SUB_CITS_SEEN.clear();
}
Also used : JsonUnbakedModel(net.minecraft.client.render.model.json.JsonUnbakedModel) ResewnItemModelIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with JsonUnbakedModel

use of net.minecraft.client.render.model.json.JsonUnbakedModel in project CITResewn by SHsuperCM.

the class ModelLoaderMixin method forceLiteralResewnModelIdentifier.

@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()) {
                    String originalPath = left.get().getTextureId().getPath();
                    String[] split = originalPath.split("/");
                    if (originalPath.startsWith("./") || (split.length > 2 && split[1].equals("cit"))) {
                        Identifier resolvedIdentifier = CIT.resolvePath(id, originalPath, ".png", identifier -> resourceManager.containsResource(identifier));
                        if (resolvedIdentifier != null)
                            return Either.left(new SpriteIdentifier(left.get().getAtlasId(), new ResewnTextureIdentifier(resolvedIdentifier)));
                    }
                }
                return original;
            });
            Identifier parentId = ((JsonUnbakedModelAccessor) json).getParentId();
            if (parentId != null) {
                String[] parentIdPathSplit = parentId.getPath().split("/");
                if (parentId.getPath().startsWith("./") || (parentIdPathSplit.length > 2 && parentIdPathSplit[1].equals("cit"))) {
                    parentId = CIT.resolvePath(id, parentId.getPath(), ".json", identifier -> resourceManager.containsResource(identifier));
                    if (parentId != null)
                        ((JsonUnbakedModelAccessor) json).setParentId(new ResewnItemModelIdentifier(parentId));
                }
            }
            json.getOverrides().replaceAll(override -> {
                String[] modelIdPathSplit = override.getModelId().getPath().split("/");
                if (override.getModelId().getPath().startsWith("./") || (modelIdPathSplit.length > 2 && modelIdPathSplit[1].equals("cit"))) {
                    Identifier resolvedOverridePath = CIT.resolvePath(id, override.getModelId().getPath(), ".json", identifier -> resourceManager.containsResource(identifier));
                    if (resolvedOverridePath != null)
                        return new ModelOverride(new ResewnItemModelIdentifier(resolvedOverridePath), override.streamConditions().collect(Collectors.toList()));
                }
                return override;
            });
            cir.setReturnValue(json);
        } catch (Exception ignored) {
        } finally {
            IOUtils.closeQuietly(is, resource);
        }
    }
}
Also used : Either(com.mojang.datafixers.util.Either) UnbakedModel(net.minecraft.client.render.model.UnbakedModel) java.util(java.util) SpriteIdentifier(net.minecraft.client.util.SpriteIdentifier) BakedModel(net.minecraft.client.render.model.BakedModel) Inject(org.spongepowered.asm.mixin.injection.Inject) ResewnTextureIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) Resource(net.minecraft.resource.Resource) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) Mixin(org.spongepowered.asm.mixin.Mixin) CIT(shcm.shsupercm.fabric.citresewn.pack.cits.CIT) TextureManager(net.minecraft.client.texture.TextureManager) At(org.spongepowered.asm.mixin.injection.At) CITResewn.info(shcm.shsupercm.fabric.citresewn.CITResewn.info) CITItem(shcm.shsupercm.fabric.citresewn.pack.cits.CITItem) CITResewn(shcm.shsupercm.fabric.citresewn.CITResewn) ModelLoader(net.minecraft.client.render.model.ModelLoader) ResourceManager(net.minecraft.resource.ResourceManager) Final(org.spongepowered.asm.mixin.Final) ModelOverride(net.minecraft.client.render.model.json.ModelOverride) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) SpriteAtlasManager(net.minecraft.client.render.model.SpriteAtlasManager) ModelIdentifier(net.minecraft.client.util.ModelIdentifier) JsonUnbakedModel(net.minecraft.client.render.model.json.JsonUnbakedModel) Profiler(net.minecraft.util.profiler.Profiler) Identifier(net.minecraft.util.Identifier) Shadow(org.spongepowered.asm.mixin.Shadow) ModifyArg(org.spongepowered.asm.mixin.injection.ModifyArg) ResewnItemModelIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier) InputStream(java.io.InputStream) ResewnTextureIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier) InputStream(java.io.InputStream) Resource(net.minecraft.resource.Resource) SpriteIdentifier(net.minecraft.client.util.SpriteIdentifier) SpriteIdentifier(net.minecraft.client.util.SpriteIdentifier) ResewnTextureIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnTextureIdentifier) ModelIdentifier(net.minecraft.client.util.ModelIdentifier) Identifier(net.minecraft.util.Identifier) ResewnItemModelIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier) JsonUnbakedModel(net.minecraft.client.render.model.json.JsonUnbakedModel) ResewnItemModelIdentifier(shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier) ModelOverride(net.minecraft.client.render.model.json.ModelOverride) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

JsonUnbakedModel (net.minecraft.client.render.model.json.JsonUnbakedModel)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 ResewnItemModelIdentifier (shcm.shsupercm.fabric.citresewn.pack.ResewnItemModelIdentifier)3 BakedModel (net.minecraft.client.render.model.BakedModel)2 ModelOverride (net.minecraft.client.render.model.json.ModelOverride)2 CITItem (shcm.shsupercm.fabric.citresewn.pack.cits.CITItem)2 CapeItem (com.jab125.thonkutil.api.CapeItem)1 Either (com.mojang.datafixers.util.Either)1 InputStream (java.io.InputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ModelLoader (net.minecraft.client.render.model.ModelLoader)1 SpriteAtlasManager (net.minecraft.client.render.model.SpriteAtlasManager)1 UnbakedModel (net.minecraft.client.render.model.UnbakedModel)1 TextureManager (net.minecraft.client.texture.TextureManager)1 ModelIdentifier (net.minecraft.client.util.ModelIdentifier)1 SpriteIdentifier (net.minecraft.client.util.SpriteIdentifier)1 Resource (net.minecraft.resource.Resource)1 ResourceManager (net.minecraft.resource.ResourceManager)1