Started by
Goldorion
on
Topic category: Plugins and third-party tools
Plugin's page: https://mcreator.net/plugin/64638/file-creator
File Manager is a plugin adding new procedure blocks allowing you to create and read your files!
File Types
- JSON
- TXT (simple file reader/writer)
- ZIP (5.7+)
Important Information
- This version supports Forge 1.19.4 and 1.20.1 and Fabric .20.1
The 2 following examples require File Manager 5.0 or after.
Create a JSON file
Read a JSON file
I made a tutorial covering this here.
Credits/License
Tutorial on sub-JSON objects by NorthWestTrees
Icon by Tobi-Wan#0482
Licensed under the GNU Lesser General Public License, version 2.1
- Appropriate credit must be provided to the creators and maintainers of this plugin.
- Forked versions of this plugin must be distributed under the same license as this with attribution if distributed.
- Changes must be stated if any modified works are to be distributed.
- Under no circumstances you can state that the original creator endorses modified works.
Changelog
To see a complete changelog of the plugin, check this file.
5.8
* Removed support for Forge 1.18.2, 1.19.2
* Removed support for Fabric 1.19.2
* Re-organized File Manager's categories to use the new parent/children category system
* [#51] Add a Remove JSON property
* [#54] Add Get client's directory
* [#58] Add Get JSON property name at index
* [Bugfix #50] Is JSON object empty caused a build error
* [Bugfix #52] Copy url to file didn't work correctly
Edited by Goldorion on Thu, 02/08/2024 - 02:49
Like I want to access the data if the value is true or false in another procedure but I can't since it's a local one and here is my procedure: https://user-images.githubusercontent.com/38841486/133566148-a51cba6e-b…
This does lead me to think that maybe the Does "file" exist block might be broken too
When I try tocreate a JSON file (for a config), the compilation fails!
My console output is (NOTE: Some sensitive informationhas been removed):
Executing Gradle task: build
Build info: MCreator 2021.2.36710, forge-1.16.5, 64-bit, 16320 MB, Windows 10, JVM 11.0.11, JAVA_HOME: C:\Program Files\Pylo\MCreator\jdk
> Task :compileJava
LoadConfigProcedure.java:56: error: ';' expected FileWriter MoarModVariables.configFilefw = new FileWriter(MoarModVariables.configFile);
^
LoadConfigProcedure.java:64: error: ';' expected BufferedReader MoarModVariables.configFileReader = new BufferedReader(new FileReader(MoarModVariables.configFile));
^
2 errors
> Task :compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.1.1/userguide/command_line_interface.html#sec…
BUILD FAILED in 7s
1 actionable task: 1 executed
BUILD FAILED
Task completed in 57 seconds
My code is below:
package mrbradlerz.moar.procedures;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraft.world.IWorld;
import mrbradlerz.moar.MoarModVariables;
import mrbradlerz.moar.MoarMod;
import java.util.Map;
import java.util.Collections;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.File;
import java.io.BufferedReader;
import com.google.gson.JsonObject;
import com.google.gson.GsonBuilder;
import com.google.gson.Gson;
public class LoadConfigProcedure {
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
private static class GlobalTrigger {
@SubscribeEvent
public static void init(FMLCommonSetupEvent event) {
executeProcedure(Collections.emptyMap());
}
}
public static void executeProcedure(Map<String, Object> dependencies){
if(dependencies.get("world") == null) {
if(!dependencies.containsKey("world"))
MoarMod.LOGGER.warn("Failed to load dependency world for procedure LoadConfig!");
return;
}
IWorld world = (IWorld) dependencies.get("world");
if ((!MoarModVariables.configFile.exists())) {MoarModVariables.configFile = new File(((FMLPaths.GAMEDIR.get().toString())+""+("config")), File.separator + "moar.json");
if (!MoarModVariables.configFile.exists()) {
try {
MoarModVariables.configFile.createNewFile();
} catch (IOException exception) {
exception.printStackTrace();
}
}
{
Gson mainGSONBuilderVariable = new GsonBuilder().setPrettyPrinting().create();
JsonObject config = new JsonObject();
JsonObject general = new JsonObject();
general.addProperty("giveBurnedPaper", (true));
config.add("general", general);
try {
FileWriter MoarModVariables.configFilefw = new FileWriter(MoarModVariables.configFile);
MoarModVariables.configFilefw.write(mainGSONBuilderVariable.toJson(config));
MoarModVariables.configFilefw.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}{
try {
BufferedReader MoarModVariables.configFileReader = new BufferedReader(new FileReader(MoarModVariables.configFile));
StringBuilder jsonstringbuilder = new StringBuilder();
String line;
while((line = MoarModVariables.configFileReader.readLine()) != null) {
jsonstringbuilder.append(line);
}
MoarModVariables.configFileReader.close();
JsonObject config = new Gson().fromJson(jsonstringbuilder.toString(), JsonObject.class);
MoarModVariables.MapVariables.get(world).giveBurnedPaper = (boolean) config.general
.get("giveBurnedPaper").getAsBoolean();
MoarModVariables.MapVariables.get(world).syncData(world);
} catch (IOException e) {
e.printStackTrace();
}
}}
}
}
It would be really great if we had a "does JSON property exist" block
Cause if we try to read a JSON property value that doesn't exist and try to use it for a variable, Java will throw a NullPointerException at game start, which could happen for example if you update a mod and the existing config file doesn't cover all options
So checking whether a JSON property exists in the file would be really helpful
File Manager 5.0 is now released to support MCreator 2021.3, Forge 1.17.1 and make easier the creation of JSON files! https://mcreator.net/plugin/64638/file-creator
Changelog:
Whenever I load my mod, the config file gets reset to default. anyone know how to fix this?
nevermind
File Manager 5.2 is available.
https://mcreator.net/plugin/64638/file-creator
is there a way you can copy a file from a local directory?
Such as .minecraft/saves/world/randomfile
Is it possible with File Manager to make 'data-driven' Mods?
I want to add into a Mod a Plague System where you can create Plagues, Cures etc. but you have also the Ability to create your own Viruses via json File.
With some work, it would be totally doable.
Just a question about plugin security, this plugin *can't* overwrite system files, right?
It's technically possible to alter the system's files if you have proper permissions (or even simply without doing anything). My real option to limit the reach of this plugin would be to limit writing files to the current game's directory and the temp directory. However, this can still be bypassed by using the custom code snippet blocks. :/ This is the problem with files in Java and we have the same with Java plugins for MCreator. This is the reason users need to manually enable Java plugins from Preferences inside MCreator.
This is also to avoid as much as possible problems on other players' computers that I still don't (and won't) have a procedure block to delete files. I don't want a user to accidentally (or not) delete files from the computers of other players.
Is it also possible to have non-existent JSON properties return their default values? It would make procedure-making a lot easier instead of having it break the procedure.