I am trying to create a mod that will read your spawnpoint and then save it, but I am having trouble. I have tried the following for creating spawnpoint files withing new worlds:
function newLevel() {
sx=Player.getX();
sy=Player.getY();
sz=Player.getZ();
}
function saveFile (directory, filename) {
try {
directory = android.os.Environment.getExternalStorageDirectory ().getPath () + "/games/com.mojang/minecraftworlds" + getWorldDir () + "/" + directory;
var newFile = new java.io.File (directory,filename);
var directory = new java.io.File (directory);
var success = directory.mkdirs (); // creates the directory if not already created
if (!success){
throw new java.io.IOException("Directory "+directory+ "cannot be created");
}
newFile.createNewFile ();
var outWrite = new java.io.OutputStreamWriter (new java.io.FileOutputStream (newFile)); // creates the output writer
outWrite.append (sx + "," + sy + "," + sz);
outWrite.close(); // closes the writer; not necessary to close, but better do it
return false; // tells that it succeeds; not necessary to catch, but better do it
}
catch(thrown){ // catches the error in the try block
return thrown.toString(); // returns a human-readable description of the error. The most common one is java.io.IOException that there is no such file
}
}
But whenever I start a new world, no file is created. It looks like nothing happens, and I receive no error messages, but when i go into the world Directory there is no file created.
var path = android.os.Environment.getExternalStorageDirectory().getPath() + "/games/com.mojang/minecraftWorlds/"+Level.getWorldDir() + "/";
function Save() {
java.io.File(path).mkdirs();
var newFile = new java.io.File(path,"filename.txt");
newFile.createNewFile();
var outWrite = new java.io.OutputStreamWriter(new java.io.FileOutputStream(newFile));
outWrite.append(var1.toString());
outWrite.append("," + var2.toString()); //We need to separate all variables. We split them with ","
outWrite.append("," + var3.toString());
outWrite.close();
}
function Load(){
if(!java.io.File(path + "filename.txt").exists()) return;
var file = new java.io.File(path + "filename.txt");
var fos = new java.io.FileInputStream(file);
var str = new java.lang.StringBuilder();
var ch;
while((ch=fos.read())!=-1) str.append(java.lang.Character(ch));
var1=parseInt(str.toString().split(",")[0]); //Here we split text by ","
var2=parseInt(str.toString().split(",")[1]);
var3=parseInt(str.toString().split(",")[2]);
fos.close();
}
if you don't know much Javascript yet, then suggest you learn it or else you would be wasting your time. trust me, its worth it to read about code and write up examples of what you just learned about, it helps you learn.
I am trying to create a mod that will read your spawnpoint and then save it, but I am having trouble. I have tried the following for creating spawnpoint files withing new worlds:
function newLevel() {
sx=Player.getX();
sy=Player.getY();
sz=Player.getZ();
}
function saveFile (directory, filename) {
try {
directory = android.os.Environment.getExternalStorageDirectory ().getPath () + "/games/com.mojang/minecraftworlds" + getWorldDir () + "/" + directory;
var newFile = new java.io.File (directory,filename);
var directory = new java.io.File (directory);
var success = directory.mkdirs (); // creates the directory if not already created
if (!success){
throw new java.io.IOException("Directory "+directory+ "cannot be created");
}
newFile.createNewFile ();
var outWrite = new java.io.OutputStreamWriter (new java.io.FileOutputStream (newFile)); // creates the output writer
outWrite.append (sx + "," + sy + "," + sz);
outWrite.close(); // closes the writer; not necessary to close, but better do it
return false; // tells that it succeeds; not necessary to catch, but better do it
}
catch(thrown){ // catches the error in the try block
return thrown.toString(); // returns a human-readable description of the error. The most common one is java.io.IOException that there is no such file
}
}
But whenever I start a new world, no file is created. It looks like nothing happens, and I receive no error messages, but when i go into the world Directory there is no file created.
The WriteFile code is taken from https://github.com/Connor4898/ModPE-Scripts/wiki/ModPE-Script-Templates
Thanks in advance
I have nothing to say...
var path = android.os.Environment.getExternalStorageDirectory().getPath() + "/games/com.mojang/minecraftWorlds/"+Level.getWorldDir() + "/";
function Save() {
java.io.File(path).mkdirs();
var newFile = new java.io.File(path,"filename.txt");
newFile.createNewFile();
var outWrite = new java.io.OutputStreamWriter(new java.io.FileOutputStream(newFile));
outWrite.append(var1.toString());
outWrite.append("," + var2.toString()); //We need to separate all variables. We split them with ","
outWrite.append("," + var3.toString());
outWrite.close();
}
function Load(){
if(!java.io.File(path + "filename.txt").exists()) return;
var file = new java.io.File(path + "filename.txt");
var fos = new java.io.FileInputStream(file);
var str = new java.lang.StringBuilder();
var ch;
while((ch=fos.read())!=-1) str.append(java.lang.Character(ch));
var1=parseInt(str.toString().split(",")[0]); //Here we split text by ","
var2=parseInt(str.toString().split(",")[1]);
var3=parseInt(str.toString().split(",")[2]);
fos.close();
}
Would be nice if we could import over packages like java.util.properties. would make things easier for setting these kinds of things.
Here is what I would try,
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; } function newLevel(){ var content = load( Level.getWorldDir(), 'spawn.txt' ); if( content == ""){ save( Level.getWorldDir(), 'spawn.txt', 'spawn: ' + Player.getX() + ', ' + Player.getY() + ', ' + Player.getZ() ); } else { //this is what I suppose you would use it for... var s1 = content.split(':')[1].split(','); Entity.setPosition( Player.getEntity(), parseFloat(s1[0]), parseFloat(s1[1]), parseFloat(s1[2]) ); } }Want Custom ModPE functions? Look here -> WolfyPE ModPE functions
Lol i have no idea what most of those things even mean but I'll give it a whirl
I have nothing to say...
if you don't know much Javascript yet, then suggest you learn it or else you would be wasting your time. trust me, its worth it to read about code and write up examples of what you just learned about, it helps you learn.
Want Custom ModPE functions? Look here -> WolfyPE ModPE functions