---already answered---
---You may look here if you have been searching also for my questions' answer---
I created a mod for teleporting to selfe-made points but it does not saves the setted points as I want. I want to save them in a .txt file, one for each world, and I looked at some templates already, but I doesn't work at me. In the moment I save it with ModPE.saveData and so on. The dates I want to save are variables (var). Of couse I also want to read these files. I would be happy over help! Thanks for your answers! ;-)
I use the following two functions to load or save a string as a text file:
function save(path, filename, content) {
try {
java.io.File(path).mkdirs();
var newFile = new java.io.File(path, filename);
newFile.createNewFile();
var outWrite = new java.io.OutputStreamWriter(new java.io.FileOutputStream(newFile));
outWrite.append(content);
outWrite.close();
} catch(err) {
clientMessage(err);
}
}
function load(path, filename) {
var content = "";
if (java.io.File(path + filename).exists()) {
var file = new java.io.File(path + filename),
fos = new java.io.FileInputStream(file),
str = new java.lang.StringBuilder(),
ch;
while ((ch = fos.read()) != -1) {str.append(java.lang.Character(ch)); }
content = String(str.toString());
fos.close();
}
return content;
}
the path of the current world is:
var path = android.os.Environment.getExternalStorageDirectory().getPath() + "/games/com.mojang/minecraftWorlds/" + Level.getWorldDir()
Thanks for the answer.
I tried it, but it didn't worked. I putted it in my script after a button and the path at start, and blocklauncher didn't said it couldn't inport, but I saw no file in my worlds. I changed content in "test".
Question 1: Where is the mistake or how would it work?
Question 2: How can I change the name of the file?
Excuse me I forgot to add a "/" at the end of the path and because of that the load function wouldn't find the file
to save a string
save(path, "filename.txt", "text to be saved in the text file")
to load a string
textStringFromFile = load(path, "filename.txt")
I made a smal example script to demonstrate the save and load function:
//define some variables
var X;
var Y;
var Z;
var path;
//save world path when opening a new world
function newLevel() {
path = android.os.Environment.getExternalStorageDirectory().getPath() + "/games/com.mojang/minecraftWorlds/" + Level.getWorldDir() + "/";
}
function procCmd(cmd) {
if (cmd === "save") {
//save some numbers in the variables
X = Math.floor(Player.getX());
Y = Math.floor(Player.getY());
Z = Math.floor(Player.getZ());
//save the variables in the text file "position.txt"
//note that the different variables are separated with semicolon
save(path, "position.txt", X + ";" + Y + ";" + Z);
clientMessage("position saved");
} else if (cmd === "load") {
//load the text file "position.txt" and save the string in the variable content
var content = load(path, "position.txt");
//split content by semicolon and save the loaded stuff to the variables
X = content.split(";")[0];
Y = content.split(";")[1];
Z = content.split(";")[2];
clientMessage("X " + X + " / Y " + Y + " / Z " + Z)
}
}
function save(path, filename, content) {
try {
java.io.File(path).mkdirs();
var newFile = new java.io.File(path, filename);
newFile.createNewFile();
var outWrite = new java.io.OutputStreamWriter(new java.io.FileOutputStream(newFile));
outWrite.append(content);
outWrite.close();
} catch(err) {
clientMessage(err);
}
}
function load(path, filename) {
var content = "";
if (java.io.File(path + filename).exists()) {
var file = new java.io.File(path + filename),
fos = new java.io.FileInputStream(file),
str = new java.lang.StringBuilder(),
ch;
while ((ch = fos.read()) != -1) {str.append(java.lang.Character(ch)); }
content = String(str.toString());
fos.close();
}
return content;
}
to test the example import it in BlockLauncher enter a world and use the command '/save' to save the current position in a text file. after that you can use the command '/load' to load the position saved previously
-
View User Profile
-
View Posts
-
Send Message
Curse Premium---already answered---
---You may look here if you have been searching also for my questions' answer---
I created a mod for teleporting to selfe-made points but it does not saves the setted points as I want. I want to save them in a .txt file, one for each world, and I looked at some templates already, but I doesn't work at me. In the moment I save it with ModPE.saveData and so on. The dates I want to save are variables (var). Of couse I also want to read these files. I would be happy over help! Thanks for your answers! ;-)
function save(path, filename, content) { try { java.io.File(path).mkdirs(); var newFile = new java.io.File(path, filename); newFile.createNewFile(); var outWrite = new java.io.OutputStreamWriter(new java.io.FileOutputStream(newFile)); outWrite.append(content); outWrite.close(); } catch(err) { clientMessage(err); } }function load(path, filename) { var content = ""; if (java.io.File(path + filename).exists()) { var file = new java.io.File(path + filename), fos = new java.io.FileInputStream(file), str = new java.lang.StringBuilder(), ch; while ((ch = fos.read()) != -1) {str.append(java.lang.Character(ch)); } content = String(str.toString()); fos.close(); } return content; }the path of the current world is:
-
View User Profile
-
View Posts
-
Send Message
Curse Premium-
View User Profile
-
View Posts
-
Send Message
Curse PremiumThanks for the answer.
I tried it, but it didn't worked. I putted it in my script after a button and the path at start, and blocklauncher didn't said it couldn't inport, but I saw no file in my worlds. I changed content in "test".
Question 1: Where is the mistake or how would it work?
Question 2: How can I change the name of the file?
to save a string to load a string I made a smal example script to demonstrate the save and load function:
//define some variables var X; var Y; var Z; var path; //save world path when opening a new world function newLevel() { path = android.os.Environment.getExternalStorageDirectory().getPath() + "/games/com.mojang/minecraftWorlds/" + Level.getWorldDir() + "/"; } function procCmd(cmd) { if (cmd === "save") { //save some numbers in the variables X = Math.floor(Player.getX()); Y = Math.floor(Player.getY()); Z = Math.floor(Player.getZ()); //save the variables in the text file "position.txt" //note that the different variables are separated with semicolon save(path, "position.txt", X + ";" + Y + ";" + Z); clientMessage("position saved"); } else if (cmd === "load") { //load the text file "position.txt" and save the string in the variable content var content = load(path, "position.txt"); //split content by semicolon and save the loaded stuff to the variables X = content.split(";")[0]; Y = content.split(";")[1]; Z = content.split(";")[2]; clientMessage("X " + X + " / Y " + Y + " / Z " + Z) } } function save(path, filename, content) { try { java.io.File(path).mkdirs(); var newFile = new java.io.File(path, filename); newFile.createNewFile(); var outWrite = new java.io.OutputStreamWriter(new java.io.FileOutputStream(newFile)); outWrite.append(content); outWrite.close(); } catch(err) { clientMessage(err); } } function load(path, filename) { var content = ""; if (java.io.File(path + filename).exists()) { var file = new java.io.File(path + filename), fos = new java.io.FileInputStream(file), str = new java.lang.StringBuilder(), ch; while ((ch = fos.read()) != -1) {str.append(java.lang.Character(ch)); } content = String(str.toString()); fos.close(); } return content; }to test the example import it in BlockLauncher enter a world and use the command '/save' to save the current position in a text file. after that you can use the command '/load' to load the position saved previouslyI hope this answers your question 1 and 2
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumThank you very much!
Thanks helped a lot <3
what If I want to save my mod menu and when you open BlockLuncher everthing that has been done is saved as it was when you closed blockluncher
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumWhat do you mean by mod menu? A graphical menu that your mod uses?