Jump to content

Reloading a single texture/resource (forge 1.20.1)


brickyboy

Recommended Posts

For my mod I want to reload a single texture from the assets. (So all resource packs and such). I've succeeded in forcing a client to reload all assets, basically forcing an F3+T, but now I want to know if its possible to reload a single texture or resource. While it would be good to be able to reload models as well (When I say resource I mean sounds, textures, models, those things), I would settle for just being able to reload a texture.

 

I've reloaded all assets using Minecraft.getInstance().reloadResourcePacks(); on the client side.

 

My client is receiving a custom packet that contains a string resource location. This string is turned into an actual ResourceLocation, before being sent to my ResourceReloader.reloadSingleTexture utility function.

What I've tried in my utility function:

Spoiler

Releasing the texture and re-binding using TextureManager

public static void reloadSingleTexture(String resourcePath) {

  TextureManager textureManager = Minecraft.getInstance().getTextureManager();

  ResourceLocation resourceLocation = new ResourceLocation(resourcePath);

  textureManager.release(resourceLocation);

  // Rebind the texture, forcing it to be re-loaded?
  textureManager.bindForSetup(resourceLocation);
}
Spoiler

Make a new texture object and add that using TextureManager

public static void reloadSingleTexture(String resourcePath) {

    TextureManager textureManager = Minecraft.getInstance().getTextureManager();
  
    ResourceLocation resourceLocation = new ResourceLocation(resourcePath);

  	// Just some unimportant error prevention
    AbstractTexture texture = textureManager.getTexture(resourceLocation);
    if (texture == null) {
      return;
    }

    textureManager.release(resourceLocation);

  	// Make a new texture object?
    SimpleTexture newTexture = new SimpleTexture(resourceLocation);
  
  	// Re-add the texture?
    textureManager.register(resourceLocation, newTexture);
}

 

Resources I've looked at:

 

Reloading all textures, and outdated (somewhere around 1.9-1.12)

 

Maybe a "new way" with forge to reload textures? Seems mostly for testing and not for use as a part of your mod:

https://gist.github.com/Gegy/148be244454fe667ade6c09cc6d5145e

 

Havent looked much at this one, but it seems to be reloading groups/namespaces of textures somehow? Also outdated - 1.12

https://www.curseforge.com/minecraft/mc-mods/resource-reloader


Isn't super helpful, since fabric, but also seems to just be mixin the keyboard so that F3+T works in GUI's, nothing very useful:

https://modrinth.com/mod/f3-t-everywhere

Link to comment
Share on other sites

I think my issue may be that the block model needs to be re-baked when the texture gets changed, but I can't find any info on how to do that either (other than an entire reload of assets like F3+T). Any info someone can give on reloading a texture or re-baking a model would be helpful.

Link to comment
Share on other sites

Ok since I'm pretty sure reloading a single resource is not really possible, I'm now asking for more info on how to simply reload less resources. I found this mod:

https://www.curseforge.com/minecraft/mc-mods/resource-reloader

That seems to be using FMLClientHandler.instance().refreshResources(type); to reload a single registry/group of resources, but since the mod is 1.12 I'm wondering what the 1.20.1 equivalent of this is and how these groups/registries work. Like how to get a group/registry from a resource location maybe? Any info would be helpful.

Link to comment
Share on other sites

A friend found this code, but I don't know where. It seems to be very outdated, maybe from 1.12? and so uses TextureManager$loadTexture and TextureManager$deleteTexture which both don't seem to exist anymore. It also uses Minecraft.getMinecraft().mcDataDir.getCanonicalPath() which I replaced with the resource location of my texture .getPath()? Not sure if thats entirely correct.

String textureName = "entitytest.png";
File textureFile = null;
try {
  textureFile = new File(Minecraft.getMinecraft().mcDataDir.getCanonicalPath(), textureName);
} catch (Exception ex) {

}

if (textureFile != null && textureFile.exists()) {
  ResourceLocation MODEL_TEXTURE = Resources.OTHER_TESTMODEL_CUSTOM;

  TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager();
  texturemanager.deleteTexture(MODEL_TEXTURE);
  Object object = new ThreadDownloadImageData(textureFile, null, MODEL_TEXTURE, new ImageBufferDownload());
  texturemanager.loadTexture(MODEL_TEXTURE, (ITextureObject)object);

  return true;
} else {
  return false;
}

 

Then I've been trying to go through the source code of the reload resource packs from minecraft, to see if I can "cache" some data and simply reload some textures and swap them out, but I can't seem to figure out where exactly its "loading" the texture files and such.

Minecraft$reloadResourcePacks(bool) seems to be mainly controlling the loading screen, and using this.resourcePackRepository.reload(); which is PackRepository$reload(), but that function seems to be using this absolute confusion of a line List<String> list = this.selected.stream().map(Pack::getId).collect(ImmutableList.toImmutableList()); and then this.discoverAvailable() and this.rebuildSelected. The rebuild selected seemed promising, but it seems to just be going through each pack and doing this to them? pack.getDefaultPosition().insert(list, pack, Functions.identity(), false); e.g. putting them into a list of packs and returning that into this.selected? Where do the textures actually get baked/loaded/whatever?

Any info on how Minecraft reloads resource packs or how the texture manager works would be appreciated!

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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