Jump to content

Recommended Posts

Posted (edited)

Hi all,

I have an item where the color of every pixel is determined by what is used to craft it (I use a custom crafting table with a 16x16 grid). The item has an array of hexadecimal values, 256 values long, for a 16x16 texture. These values will be determined at the time of crafting, and will stay the same for the remainder of the item's lifetime. I've looked at the MapItemRenderer class, and DynamicTexture seems like the way to go about this, but I can't seem to figure out how the puzzle pieces fit together. Do I have to write my own ItemRenderer for this, or is there a way to apply the texture to the item after the mod's initialization? If I need to write a renderer, how do I tell forge to bind it to this item and provide it with the data it needs to generate the DynamicTexture? Any help would be appreciated. If you need code samples let me know, but I'm not sure that they'd help any more than the info I've given.

Edited by chaseoqueso
Posted

Ok so progress report:
 

I've started going in the direction of a ICustomModelLoader based on ModelDynBucket, but I'm still struggling to put the pieces together. It's clear that this model loader is registered properly, since I'm no longer getting errors about missing blockstate or item model .json files. I can also confirm that it accepts exactly 1 resource location (which I assume with relative certainty to be the item in question), and that it gets loadModel called, constructs a new model, and that the model then gets bake called, which constructs a new baked model

All that is well and good, but my custom ItemOverrideList doesn't seem to be getting utilized; neither getOverrides nor handleItemState get called. I don't see any place to register it, and I don't see anything different between my class and the ModelDynBucket class. If anyone has any insight on how to fix this, it would be greatly appreciated.


(Sidenote: I acknowledge that my current code for creating and storing the DynamicTexture in the process method is garbage, but one step at a time y'know)
 

package com.chaseoqueso.bitcrafting.rendering;

import com.chaseoqueso.bitcrafting.BitCraftingMod;
import com.chaseoqueso.bitcrafting.items.ItemBitSword;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.*;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.model.*;
import net.minecraftforge.common.model.IModelState;
import net.minecraftforge.common.model.TRSRTransformation;

import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Function;

public class ModelBitSword implements IModel
{
    public static final ModelResourceLocation LOCATION = new ModelResourceLocation(new ResourceLocation(BitCraftingMod.MODID, "itembitsword"), "inventory");

    private DynamicTexture tempTexture;
    @Nullable
    private ResourceLocation textureLocation;

    public ModelBitSword()
    {
        this(null);
    }

    public ModelBitSword(@Nullable ResourceLocation textureLocation)
    {
        this.textureLocation = textureLocation;
    }

    @Override
    public Collection<ResourceLocation> getTextures()
    {
        ImmutableSet.Builder<ResourceLocation> builder = ImmutableSet.builder();

        if (textureLocation != null)
            builder.add(textureLocation);

        return builder.build();
    }

    @Override
    public IBakedModel bake(IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter)
    {
        ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> transformMap = PerspectiveMapWrapper.getTransforms(state);

        TRSRTransformation transform = state.apply(Optional.empty()).orElse(TRSRTransformation.identity());
        TextureAtlasSprite fluidSprite = null;
        TextureAtlasSprite particleSprite = null;
        ImmutableList.Builder<BakedQuad> builder = ImmutableList.builder();
        
        if (textureLocation != null)
        {
            IBakedModel model = (new ItemLayerModel(ImmutableList.of(textureLocation))).bake(state, format, bakedTextureGetter);
            builder.addAll(model.getQuads(null, null, 0));
            particleSprite = model.getParticleTexture();
        }

        return new ModelBitSword.BakedBitSword(this, builder.build(), particleSprite, format, Maps.immutableEnumMap(transformMap), Maps.newHashMap(), transform.isIdentity());
    }

    @Override
    public ModelBitSword process(ImmutableMap<String, String> customData)
    {
        String colorArrayString = customData.get("colorArrayString");
        String[] items = colorArrayString.split(",");

        tempTexture = new DynamicTexture(16, 16);
        int[] textureArray = tempTexture.getTextureData();

        for (int i = 0; i < items.length; i++) {
            try {
                textureArray[i] = Integer.parseInt(items[i]);
            } catch (NumberFormatException nfe) {
                System.out.println("ERROR: Something went wrong with converting the sword's color data from string to int array");
            };
        }

        tempTexture.updateDynamicTexture();

        textureLocation = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation("itembitsword/" + colorArrayString, tempTexture);

        return new ModelBitSword(textureLocation);
    }
                                         
    @Override
    public ModelBitSword retexture(ImmutableMap<String, String> textures)
    {
        ResourceLocation base = textureLocation;

        if (textures.containsKey("base"))
            base = new ResourceLocation(textures.get("base"));

        return new ModelBitSword(base);
    }

    public enum LoaderBitSword implements ICustomModelLoader
    {
        INSTANCE;

        @Override
        public boolean accepts(ResourceLocation modelLocation)
        {
            if(modelLocation.getResourceDomain().equals(BitCraftingMod.MODID) && modelLocation.getResourcePath().contains("itembitsword")) System.out.println("Bit Sword Accepts");
            return modelLocation.getResourceDomain().equals(BitCraftingMod.MODID) && modelLocation.getResourcePath().contains("itembitsword");
        }

        @Override
        public IModel loadModel(ResourceLocation modelLocation)
        {
            return new ModelBitSword();
        }

        @Override
        public void onResourceManagerReload(IResourceManager resourceManager)
        {
            //I think I need to clear the cache here but idk what that means or how to do it (probably something to do with BakedBitSword.cache but that's private so idk)
        }
    }

    private static final class BakedBitSwordOverrideHandler extends ItemOverrideList
    {
        public static final ModelBitSword.BakedBitSwordOverrideHandler INSTANCE = new ModelBitSword.BakedBitSwordOverrideHandler();
        private BakedBitSwordOverrideHandler()
        {
            super(ImmutableList.of());
        }

        @Override
        public IBakedModel handleItemState(IBakedModel originalModel, ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity)
        {

            if (!(stack.getItem() instanceof ItemBitSword))
            {
                return originalModel;
            }

            ModelBitSword.BakedBitSword model = (ModelBitSword.BakedBitSword)originalModel;

            String arrayString = ItemBitSword.getColorArrayAsString(stack);

            if (!model.cache.containsKey(arrayString))
            {
                ModelBitSword parent = model.parent.process(ImmutableMap.of("colorArrayString", arrayString));
                Function<ResourceLocation, TextureAtlasSprite> textureGetter;
                textureGetter = location -> Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(location.toString());

                IBakedModel bakedModel = parent.bake(new SimpleModelState(model.transforms), model.format, textureGetter);
                model.cache.put(arrayString, bakedModel);
                return bakedModel;
            }

            return model.cache.get(arrayString);
        }
    }

    private static final class BakedBitSword extends BakedItemModel
    {
        private final ModelBitSword parent;
        private final Map<String, IBakedModel> cache;
        private final VertexFormat format;
        private final ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> transforms;

        BakedBitSword(ModelBitSword parent,
                       ImmutableList<BakedQuad> quads,
                       TextureAtlasSprite particle,
                       VertexFormat format,
                       ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> transforms,
                       Map<String, IBakedModel> cache,
                       boolean untransformed)
        {
            super(quads, particle, transforms, BakedBitSwordOverrideHandler.INSTANCE, untransformed);
            this.transforms = transforms;
            this.format = format;
            this.parent = parent;
            this.cache = cache;
        }
    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Unlock an Instant $100 OFF with the Exclusive Temu Coupon Code ALF401700! Whether you're a first-time shopper or a loyal returning customer, this verified Temu promo code ALF401700 is your gateway to incredible savings on thousands of products. By applying code ALF401700 at checkout, you’ll receive a guaranteed $100 discount on your purchase, plus enjoy additional savings of up to 50% OFF on selected items. This special coupon is designed to maximize your discounts, making your shopping experience at Temu more affordable than ever. Why Choose Temu Coupon Code ALF401700 in 2025? First-Time Shoppers: Score a massive 50% off your first order plus a flat $100 OFF using promo code ALF401700. Returning Customers: Don’t miss out! Use ALF401700 to claim a generous $100 OFF on your next purchase. Massive Clearance Sales: With Temu’s ongoing 2025 clearance events, this code unlocks up to 90% OFF on select deals. Global Reach: Whether you shop from the USA, Canada, Europe, Asia, or beyond, Temu coupon ALF401700 works worldwide. 2025’s Top Temu Discounts Powered by ALF401700: Temu $100 OFF New User Promo — ALF401700 Temu Exclusive Discount for Returning Shoppers — ALF401700 Memorial Day Special: $100 OFF Using ALF401700 Country-Specific Offers: USA, Japan, Mexico, Chile, Colombia, Malaysia, Philippines, South Korea, Saudi Arabia, Qatar, Germany, France, Israel — all accept code ALF401700 Free Gift Unlocks — Apply ALF401700 Without Referrals Stackable Bundles: Combine ALF401700 for $100 OFF with up to 50% sitewide discounts 100% OFF Flash Deals during Temu Events with ALF401700 How to Redeem Your Temu Coupon Code ALF401700: Visit the official Temu website or open the Temu app. Select your favorite products and add them to your cart. Enter the promo code ALF401700 in the discount code field at checkout. Watch your total instantly drop by $100 and enjoy any additional percentage discounts automatically applied. Complete your order and enjoy huge savings! Key Benefits of Using ALF401700 Temu Promo Code: Verified and Tested: This code is active and guaranteed to work for 2025. Applicable for All Users: New or existing customers get to enjoy the perks. Works Across Multiple Countries: Perfect for international shoppers. No Minimum Purchase Required: Use it anytime to save $100. Perfect for Big and Small Orders: Whether buying essentials or splurging, save big with ALF401700. Temu Coupon Code ALF401700 by Region: 🇺🇸 United States — Save $100 OFF 🇨🇦 Canada — Instant $100 Discount 🇬🇧 United Kingdom — Exclusive $100 OFF 🇯🇵 Japan — Hot $100 OFF Deal 🇲🇽 Mexico — Get $100 OFF 🇨🇱 Chile — 2025 Special Discount 🇰🇷 South Korea — Massive Savings 🇵🇭 Philippines — Extra $100 OFF 🇸🇦 Saudi Arabia — Verified Promo 🇶🇦 Qatar — Top Discount 🇩🇪 Germany — Exclusive Savings 🇫🇷 France — Coupon Works 🇮🇱 Israel — Huge Discount Final Words: Make 2025 your year of unbeatable savings with the Temu coupon code ALF401700. Act now to claim your $100 OFF plus unlock additional discounts on thousands of products. Whether shopping for fashion, electronics, home essentials, or gifts, this is the best Temu promo code to maximize your budget. Download the Temu app today, enter ALF401700 at checkout, and watch the savings roll in!
    • Verified users can now unlock a $100 OFF Temu Coupon Code using the verified promo code [ALF401700]. This Temu $100 OFF code works for both new and existing customers and can be used to redeem up to 50% off your next order. Our exclusive Temu coupon code [ALF401700] delivers a flat $100 OFF on top of existing deals. First-time customers using code ALF401700 can save an extra 100% off select items. Returning users also qualify for an automatic $100 OFF discount just by applying this code at checkout. But wait—there’s more. With our Temu coupon codes for 2025, users can score up to 90% OFF on clearance items. Whether you’re shopping in the USA, Canada, UK, or elsewhere, Temu promo code ALF401700 unlocks extra discounts tailored to your account. Some users are saving 100% on items using this 2025 Temu promo code. 🔥 Temu Coupon Highlights Using Code [ALF401700]: Temu new user code – ALF401700: Save 50% off your first order + $100 OFF. Temu promo for existing customers – ALF401700: Enjoy flat $100 OFF instantly. Global availability: Works in the USA, UK, Canada, Germany, France, Japan, Chile, Colombia, Malaysia, Mexico, South Korea, Philippines, Saudi Arabia, Qatar, Pakistan, and more. Top 2025 Coupon Deal: Get $200 OFF plus 100% bonus discounts using code ALF401700. Top-Ranked Temu Deals for 2025 (Coupon Code: ALF401700): ✅ Temu $100 OFF Memorial Day Sale — Use ALF401700 ✅ Temu First Order Coupon — Use ALF401700 for 50% + $100 OFF ✅ Temu USA Coupon Code — Save $100 instantly with ALF401700 ✅ Temu Japan, Germany, Chile Codes — All support ALF401700 ✅ Temu Reddit Discount – $100 OFF: Valid for both new and old users ✅ Temu Coupon Bundle 2025 — $100 OFF + up to 50% slash ✅ 100% OFF Free Gift Code — Use ALF401700, no invite needed ✅ Temu Sign-Up Bonus Promo — Get a welcome $100 OFF instantly ✅ Free Temu Code for New Users — Use ALF401700, no referral required  Temu Clearance Codes 2025 — Use ALF401700 for 85–100% discounts Why ALF401700 is the Best Temu Code in 2025 Using Temu code ALF401700 is your ticket to massive savings, free shipping, first-order discounts, and stackable coupon bundles. Whether you're browsing electronics, fashion, home goods, or beauty products, this verified Temu discount code offers real savings—up to 90% OFF + $100 OFF on qualified orders. 💡 Pro Tip: Apply ALF401700 during checkout in the Temu app or website to activate your instant $100 discount, even if you’re not a new user. Temu $100 OFF Code by Country (All Use ALF401700): 🇺🇸 Temu USA – ALF401700 🇯🇵 Temu Japan – ALF401700 🇲🇽 Temu Mexico – ALF401700 🇨🇱 Temu Chile – ALF401700 🇨🇴 Temu Colombia – ALF401700 🇲🇾 Temu Malaysia – ALF401700 🇵🇭 Temu Philippines – ALF401700 🇰🇷 Temu Korea – ALF401700 🇵🇰 Temu Pakistan – ALF401700 🇫🇮 Temu Finland – ALF401700 🇸🇦 Temu Saudi Arabia – ALF401700 🇶🇦 Temu Qatar – ALF401700 🇫🇷 Temu France – ALF401700 🇩🇪 Temu Germany – ALF401700 🇮🇱 Temu Israel – ALF401700 Temu Coupon Code [ALF401700] Summary for SEO: Temu $100 OFF Code  Temu First Order Discount Code 2025  Temu Verified Promo Code ALF401700  Temu 50% OFF + $100 Bonus  Temu 100% OFF Code 2025  Temu App Promo ALF401700  Temu Working Discount Code 2025 
    • Hey there, nothing to do with the code, I am just suggesting you use Intelij IDEA. Trust me, it is the best.
    • Hey there, nothing to do with the code, I am just suggesting you use Intelij IDEA. Trust me, it is the best.
    • You can check the config of Geckolib and Mowzie's Mobs - maybe there is a way to disable some render features
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.