Jump to content

[1.12.2] [SOLVED] How to load your own resource pack manually


FireController1847

Recommended Posts

I've been poking around the ResourcePackRepository files and a bunch similar to it, and I have yet to find a way to insert my own resource pack manually into the list. This is what I'm attempting to do: I'll add a button to the resource pack screen that says "Add External Pack...", and when the user clicks this button it will load a ResourcePack from wherever the file was. This provides temporary or permanent ways to not have to find the .resourcepacks folder. For example, if you don't want to copy the same resource pack over and over and over again. I know how to get the file path and location, but I'm having issues adding it manually. I've tested by creating a new ResourcePackRepository$Entry, and due to the fact that the constructors are private, I cannot do this. Am I looking through the wrong files? Is there something else I should be doing entirely? How do I do this? xD

Here's what I've got so far (ignore the fact the button is in the Multiplayer screen, I was messing around):
 

	@SubscribeEvent
	public void onActionPerformed(ActionPerformedEvent.Pre event) {
		if (event.getGui() instanceof GuiMultiplayer && event.getButton().id == 9) {
			// JFileChooser fc = new JFileChooser();
			// fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			// fc.getActionMap().get("viewTypeDetails").actionPerformed(null);
			// fc.setPreferredSize(new Dimension(1200, 900));
			// try {
			// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			// } catch (Exception e) {
			// }
			// if (fc.showOpenDialog(fc) == JFileChooser.APPROVE_OPTION) {
			// System.out.println(fc.getSelectedFile().getAbsolutePath());
			// }
			ResourcePackRepository rpr = Minecraft.getMinecraft().getResourcePackRepository();
			List<Entry> repos = rpr.getRepositoryEntriesAll();
			System.out.println(repos);
			for (Entry repo : repos) {
				System.out.println(repo);
			}
			Entry e = new Entry(new File("E:\\Users\\xxxx\\Desktop\\xxxx\\xxxx\\run\\resourcepacks\\Faithful 1.12.2-rv4.zip"));
			System.out.println(e);
			// repos.add(e);

			// rpr.setRepositories(repos);
		}
	}

 

Edited by FireController1847
Added an example piece that does not work.

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

The only resource packs that show up in that menu are those located at the minecraft profile/resourcepacks. My implimentation will open a popup menu where you could have resourcepacks in your downloads folder, your desktop, wherever you want. This means that you can select resourcepacks manually from anywhere on your PC instead of just located to your profile/resourcepacks.

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

Think of it this way: You have some modpacks on technic, ftb, some custom mods on the main thing, and ATLauncher, but you want all of them to access the same resourcepack file. This file would have to be literally copied (on windows) to each resourcepack file, and if it's a large resourcepack this could take quite a bit of space. This is supposed to allow you to load only one file without the need to copy it at all.

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

To be honest, my first issue comes into place where I'm not exactly sure if I'm even using the right components. As of right now, I'm attempting to create a new "Entry" (ResourcePackRepository$Entry) and insert it into Minecraft.getMinecraft().getResourcePackRepository(). But due to the fact the constructor of Entry is private, I can't create a new Entry therefore I can't test by adding a new element into the ResourcePackRepository

net.minecraft.client.resources.ResourcePackRepository$Entry

    @SideOnly(Side.CLIENT)
    public class Entry
    {
        private final IResourcePack reResourcePack;
        private PackMetadataSection rePackMetadataSection;
        private ResourceLocation locationTexturePackIcon;

        // Private
        private Entry(File resourcePackFileIn)
        {
            this(ResourcePackRepository.this.getResourcePack(resourcePackFileIn));
        }

        // Private
        private Entry(IResourcePack reResourcePackIn)
        {
            this.reResourcePack = reResourcePackIn;
        }

 

Edited by FireController1847
Add ResourcePackRepository$Entry

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

Wait, what wiki are we talking about? If we're talking about the Forge Documentation, where is it?

-- From what I've read online, how does this tie into what I'm doing?
-- Do you have any examples of people doing this? (I work best that way. I know, lame)

Edited by FireController1847

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

 

32 minutes ago, FireController1847 said:

But due to the fact the constructor of Entry is private, I can't create a new Entry therefore I can't test by adding a new element into the ResourcePackRepository

Reflection would allow you to make it not private so you could create a new entry.

I can't seem to find the tutorial on reflection but this is one on access transformers which also links to some useful tools and examples that will help you.

https://tutorials.darkhax.net/tutorials/access_transformers/

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

So I'm attempting the Reflection method, but it's saying it cannot find the constructor? (Sorry, this is my first time with relfections and AHHHHHHHHH)
 

Constructor entry = ReflectionHelper.findConstructor(Entry.class, File.class);
System.out.println(entry);


Error
 

[19:49:40] [main/ERROR] [FML]: Exception caught during firing event net.minecraftforge.client.event.GuiScreenEvent$ActionPerformedEvent$Pre@65b11833:
net.minecraftforge.fml.relauncher.ReflectionHelper$UnknownConstructorException: Could not find constructor 'Entry(java.io.File)' in class net.minecraft.client.resources.ResourcePackRepository$Entry
	at net.minecraftforge.fml.relauncher.ReflectionHelper.findConstructor(ReflectionHelper.java:270) ~[forgeSrc-1.12.2-14.23.0.2503.jar:?]
	at com.firecontrol.testmod.Handlers.BlockHandler.onActionPerformed(BlockHandler.java:92) ~[BlockHandler.class:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_9_BlockHandler_onActionPerformed_Pre.invoke(.dynamic) ~[?:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:179) [EventBus.class:?]
	at net.minecraft.client.gui.GuiScreen.mouseClicked(GuiScreen.java:489) [GuiScreen.class:?]
	at net.minecraft.client.gui.GuiMultiplayer.mouseClicked(GuiMultiplayer.java:441) [GuiMultiplayer.class:?]
	at net.minecraft.client.gui.GuiScreen.handleMouseInput(GuiScreen.java:611) [GuiScreen.class:?]
	at net.minecraft.client.gui.GuiMultiplayer.handleMouseInput(GuiMultiplayer.java:90) [GuiMultiplayer.class:?]
	at net.minecraft.client.gui.GuiScreen.handleInput(GuiScreen.java:576) [GuiScreen.class:?]
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:1884) [Minecraft.class:?]
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1186) [Minecraft.class:?]
	at net.minecraft.client.Minecraft.run(Minecraft.java:441) [Minecraft.class:?]
	at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_144]
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_144]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_144]
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
	at GradleStart.main(GradleStart.java:26) [start/:?]
[19:49:40] [main/ERROR] [FML]: Index: 1 Listeners:
[19:49:40] [main/ERROR] [FML]: 0: NORMAL
[19:49:40] [main/ERROR] [FML]: 1: ASM: com.firecontrol.testmod.Handlers.BlockHandler@16858bf6 onActionPerformed(Lnet/minecraftforge/client/event/GuiScreenEvent$ActionPerformedEvent$Pre;)V

 

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

I hope this helps you 

you also might want to look at that tutorial again because of the Searge name for the field which I believe also applies to reflection correct me if I'm wrong its been a while since I've done this.

 

net.minecraftforge.fml.relauncher.ReflectionHelper

/**
 * Finds a constructor in the specified class that has matching parameter types.
 *
 * @param klass The class to find the constructor in
 * @param parameterTypes The parameter types of the constructor.
 * @param <T> The type
 * @return The constructor
 * @throws NullPointerException if {@code klass} is null
 * @throws NullPointerException if {@code parameterTypes} is null
 * @throws UnknownConstructorException if the constructor could not be found
 */
@Nonnull
public static <T> Constructor<T> findConstructor(@Nonnull final Class<T> klass, @Nonnull final Class<?>... parameterTypes)
{
    Preconditions.checkNotNull(klass, "class");
    Preconditions.checkNotNull(parameterTypes, "parameter types");

    final Constructor<T> constructor;
    try
    {
        constructor = klass.getDeclaredConstructor(parameterTypes);
        constructor.setAccessible(true);
    }
    catch (final NoSuchMethodException e)
    {
        final StringBuilder desc = new StringBuilder();
        desc.append(klass.getSimpleName()).append('(');
        for (int i = 0, length = parameterTypes.length; i < length; i++)
        {
            desc.append(parameterTypes[i].getName());
            if (i > length)
            {
                desc.append(',').append(' ');
            }
        }
        desc.append(')');
        throw new UnknownConstructorException("Could not find constructor '" + desc.toString() + "' in " + klass);
    }
    return constructor;
}
Link to comment
Share on other sites

Actually, I've managed to successfully load and improve the class (the second you posted this lol). I do have a new issue now, though. Here's my current code:
 

			try {
				ResourcePackRepository rpr = Minecraft.getMinecraft().getResourcePackRepository();
				Class cls = ResourcePackRepository.class;
				Method md = cls.getDeclaredMethod("getResourcePack", File.class);
				md.setAccessible(true);
				IResourcePack myNewPack = (IResourcePack) md.invoke(rpr, new File(
						"E:\\\\Users\\\\FireController1847\\\\Desktop\\\\Forge\\\\Testing\\\\run\\\\resourcepacks\\\\Faithful1.12.2-rv4.zip"));

				Class cls2 = Entry.class;
				Constructor cn = cls2.getDeclaredConstructor(ResourcePackRepository.class, IResourcePack.class);
				cn.setAccessible(true);
				Entry entry = (Entry) cn.newInstance(rpr, myNewPack);
				System.out.println(entry);
				
				List<Entry> repos = rpr.getRepositoryEntriesAll();
				repos.add(entry);
				rpr.setRepositories(repos);
			} catch (Exception e) {
				LogManager.getLogger().error(e);
				StackTraceElement[] stack = e.getStackTrace();
				for (StackTraceElement trace : stack) {
					LogManager.getLogger().error(trace);
				}
			}

The error comes on the repos.add(entry) line.
 

[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: java.lang.UnsupportedOperationException
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:218)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: com.firecontrol.testmod.Handlers.BlockHandler.onActionPerformed(BlockHandler.java:106)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraftforge.fml.common.eventhandler.ASMEventHandler_11_BlockHandler_onActionPerformed_Pre.invoke(.dynamic)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:179)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.gui.GuiScreen.mouseClicked(GuiScreen.java:489)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.gui.GuiMultiplayer.mouseClicked(GuiMultiplayer.java:441)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.gui.GuiScreen.handleMouseInput(GuiScreen.java:611)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.gui.GuiMultiplayer.handleMouseInput(GuiMultiplayer.java:90)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.gui.GuiScreen.handleInput(GuiScreen.java:576)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.Minecraft.runTick(Minecraft.java:1884)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1186)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.Minecraft.run(Minecraft.java:441)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.client.main.Main.main(Main.java:118)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: java.lang.reflect.Method.invoke(Unknown Source)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraft.launchwrapper.Launch.main(Launch.java:28)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: java.lang.reflect.Method.invoke(Unknown Source)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
[21:21:14] [main/ERROR] [com.firecontrol.testmod.Handlers.BlockHandler]: GradleStart.main(GradleStart.java:26)

 

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

I'll explain the lines one by one.
 

				ResourcePackRepository rpr = Minecraft.getMinecraft().getResourcePackRepository();
				Class cls = ResourcePackRepository.class;
				Method md = cls.getDeclaredMethod("getResourcePack", File.class);
				md.setAccessible(true);
				IResourcePack myNewPack = (IResourcePack) md.invoke(rpr,
						new File("E:\\Users\\FireController1847\\Desktop\\Faithful 1.12.2-rv4.zip"));

This creates a new valid ResourcePack by getting the private method "getResourcePack" from ResourcePackRepository.
 

				Class cls2 = Entry.class;
				Constructor cn = cls2.getDeclaredConstructor(ResourcePackRepository.class, IResourcePack.class);
				cn.setAccessible(true);
				Entry entry = (Entry) cn.newInstance(rpr, myNewPack);
				System.out.println(entry);

This gets the private constructor of Entry and creates a new instance of it using the IResourcePack we made above.
 

				List<Entry> repos = rpr.getRepositoryEntriesAll();
				repos.add(entry);
				rpr.setRepositories(repos)

This is where I didn't really know where to go. I attempted to add the Entry into all of the entries (therefore enabling the resource pack), but it comes up with the error listed in the comment above.

I am on my journey of making a remake of matmos, as explained here.

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.

Announcements



×
×
  • Create New...

Important Information

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