Just a quick note this tutorial is being shown in examples, if you don't understand what part of this means please don't hesitate to message me or comment down below (Commenting below is preferable as it may mean more people that might find it helpful to see it will see it.)
Bits that you can change and not change the functionality too much are in red. (Or well they would be if the silly forum let you do that in code blocks... sorry but I guess you will just have to guess)
This tutorial was made using java 1.8.0_102 and I make no assurances that it will work with other versions.
Backstory to this post:
Okay, so I had a small issue today when I couldn't find a way to add a file into a directory that my mod needed to add as a default file to load if the user had not provided an alternative.
I tried to add it as a resource but that didn't work (probably due to my lack of proficiency using java so this method I am suggesting is probably not the best but I shall continue to say anyway in case it is of use to anyone )... I was tempted to get the game to download it but then I thought of a solution... what if the file was hardcoded in...
Okay now the method.
First, convert your small file to base64.
I used this website which accepts files up to 10 mb in size. (Personally I downloaded the file and then opened it in Notepad++.)
Then if your converted base64 is split up into separate lines make it all into one line now, by putting the code in a text editor going to the beginning of each line and pressing the back space key once. Make sure you are not deleting any actual characters (beware of word wrapping).
Then open up your code and make a space of a few lines where you want to start the file generation. (I put it in the PreInitializationEvent because I need it to be generated as soon as possible but you should be able to do it wherever you want. For example from a gui button or from a player event)
Now onto the actual coding part of the tutorial:
First you will want to add a line defining the folder that you want to make it in.
For example this sets a String called path to a directory that is located in the main minecraft/run folder. (Replace the path with whatever you want it to be). ('File.separator' is another way of doing '/' but does not depend on operating system.)
Then you will want to make the folder you are putting this file in if it doesn't already exist. To do this we will use:
new File(path).mkdirs();
Then we will want to define the file we want to make. To do this we will make a new file using:
File fileInCode = new File(path+File.separator+"smallFile.txt");
Now it's time to use that base64 we converted earlier. What we want to do is convert that base64 we converted earlier into a byte array:
(My base64 string is probably much smaller than yours as it is only two words, do not be surprised if it is extremely long.) (You may want to put it in a string variable somewhere else but this is up to you. You would do that by doing something like 'String myBase64 = "RXhlbXBsYXIgVGV4dA==";' and replacing the insides of the brackets below with 'myBase64')
Okay, so now there is one more step before we are done. This is actually the most important one, making the file. We are going to use FileUtils to write the bytes to the file. (We are also surrounding this with a try catch block):
try {
FileUtils.writeByteArrayToFile(fileInCode, ourByteArray);
} catch (IOException e) {
// Replace this comment with better error handling if you want
e.printStackTrace();
}
And it's finally over! Good luck!
Code in full:
String path = (Loader.instance().getConfigDir().getParentFile().getAbsolutePath()+File.separator+"Our File Directory");
new File(path).mkdirs();
File fileInCode = new File(path+File.separator+"smallFile.txt");
byte[] ourByteArray = Base64.getDecoder().decode("RXhlbXBsYXIgVGV4dA==");
try {
FileUtils.writeByteArrayToFile(fileInCode, ourByteArray);
} catch (IOException e) {
// Replace this with better error handling if you want
e.printStackTrace();
}
A full class built off the forge example mod class for example purposes:
package com.example.examplemod;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import org.apache.commons.io.FileUtils;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@Mod(modid = ExampleMod.MODID, version = ExampleMod.VERSION)
public class ExampleMod
{
public static final String MODID = "examplemod";
public static final String VERSION = "1.0";
@EventHandler
public void init(FMLInitializationEvent event)
{
String path = (Loader.instance().getConfigDir().getParentFile().getAbsolutePath()+File.separator+"Our File Directory");
new File(path).mkdirs();
File fileInCode = new File(path+File.separator+"smallFile.txt");
byte[] ourByteArray = Base64.getDecoder().decode("RXhlbXBsYXIgVGV4dA==");
try {
FileUtils.writeByteArrayToFile(fileInCode, ourByteArray);
} catch (IOException e) {
// Replace this with better error handling if you want
e.printStackTrace();
}
}
}
Just a quick note this tutorial is being shown in examples, if you don't understand what part of this means please don't hesitate to message me or comment down below (Commenting below is preferable as it may mean more people that might find it helpful to see it will see it.)
Bits that you can change and not change the functionality too much are in red. (Or well they would be if the silly forum let you do that in code blocks... sorry but I guess you will just have to guess)
This tutorial was made using java 1.8.0_102 and I make no assurances that it will work with other versions.
Backstory to this post:
Okay, so I had a small issue today when I couldn't find a way to add a file into a directory that my mod needed to add as a default file to load if the user had not provided an alternative.
I tried to add it as a resource but that didn't work (probably due to my lack of proficiency using java so this method I am suggesting is probably not the best but I shall continue to say anyway in case it is of use to anyone )... I was tempted to get the game to download it but then I thought of a solution... what if the file was hardcoded in...
Okay now the method.
Now onto the actual coding part of the tutorial:
First you will want to add a line defining the folder that you want to make it in.
For example this sets a String called path to a directory that is located in the main minecraft/run folder. (Replace the path with whatever you want it to be). ('File.separator' is another way of doing '/' but does not depend on operating system.)
Then you will want to make the folder you are putting this file in if it doesn't already exist. To do this we will use:
Then we will want to define the file we want to make. To do this we will make a new file using:
Now it's time to use that base64 we converted earlier. What we want to do is convert that base64 we converted earlier into a byte array:
(My base64 string is probably much smaller than yours as it is only two words, do not be surprised if it is extremely long.) (You may want to put it in a string variable somewhere else but this is up to you. You would do that by doing something like 'String myBase64 = "RXhlbXBsYXIgVGV4dA==";' and replacing the insides of the brackets below with 'myBase64')
Okay, so now there is one more step before we are done. This is actually the most important one, making the file. We are going to use FileUtils to write the bytes to the file. (We are also surrounding this with a try catch block):
And it's finally over! Good luck!
Code in full:
A full class built off the forge example mod class for example purposes:
I hope this helps!