I've been trying to mod a sword into PE for a while now by following Arjay's videos, and BlockLauncher keeps telling me the "the item icon [texture variable name] does not exist." Could someone look at the below code and see if there's anything wrong with it? I kinda expected the code not to work, because those videos were posted a number of years ago and are probably outdated, so... Anyways, thanks!
/*Master Sword*/
var texture = "//i.imgur.com/oJlYgpy.jpg"
ModPE.setItem(500, "texture", 0, "Master Sword");
function attackHook(attacker, victim){
if(Player.getCarriedItem() == 500){
var dmg = 15;
Entity.setHealth(victim, Entity.getHealth(victim) - dmg)
}
}
You can't use an image from the internet as if it would be an image stored on the device.
The image has to be downloaded before it can be used in your mod.
You can:
1) Tell the person who downloads your mod to download the images for it, too, and put them in a specific folder with a specific name so that your mod can use them. Not recommended, the user just wants to download the mod an run it without having to worry about images.
2) Create a variable that stores the file as a Byte-Array and write this to a file and then use this file.
3) Download the image from the internet and use this file.
A possible way to use solution 3):
First of all, the url to the image shouldn't start with // since // is a part of the url to specify a protocol but for this to work this part has to be complete.
So replace // with https://.
Next, you never used the texture-variable. You wrote "texture" when calling the ModPE.setItem()-method which would give this method a string containing texture and not the previous defined variable.
//Write data to the file using the url above:
var response = android.net.http.AndroidHttpClient.newInstance("").execute(new org.apache.http.client.methods.HttpGet(url)).getEntity().writeTo(fos);
//Close the Stream now:
fos.close();
}
}catch(err){
//optional error handling
}
//Now the image is downloaded an ready to use.
/*Master Sword*/
//change the texture variable to the path of the downloaded image:
The example solution is a pretty easy way to accomplish this but, if possible, I recommend to google how you should do this, but that's really not a topic for a beginner.
Thanks for helping! I tried copy-pasting what you said to do while adding the needed links and whatnot, but I still got the same error. I'm very confused.
Sorry that downloading didn't work, I forgot to add an important line:
A new file couldn't be created because the necessary directories don't exist, therefore they must first be created, which is done by this line:
file.getParentFile().mkdirs();
It first gets the parent file of the to be created jpg-image, which is simply the folder in which it should be. It then creates all the directories for this folder.
Another problem with the code was that ModPE.setItem() can use as a texture only a Minecraft texture that already exists. (In the code below I used the camera texture as it is used rarely.)
What you can do however is to override this texture. The function that overrides textures can't use the path to an image for the texture, it needs the path as an URI (https://en.wikipedia.org/wiki/URI), which is why I put File://localhost/ in front of the path.
This is how the code for that looks:
var filePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/games/com.mojang/ModScript/MasterSword/images/";
var fileName = "masterSword.jpg";
try{
if(!java.io.File(filePath + fileName).exists()){
var file = new java.io.File(filePath + fileName);
file.getParentFile().mkdirs();
file.createNewFile();
var fos = new java.io.FileOutputStream(file);
var url = "http://i.imgur.com/8cvUB5X.jpg";
var response = android.net.http.AndroidHttpClient.newInstance("").execute(new org.apache.http.client.methods.HttpGet(url)).getEntity().writeTo(fos);
fos.close();
}
}catch(err){
//optional error handling
}
var texture = "File://localhost/" + filePath + fileName;
ModPE.overrideTexture("camera", texture);
ModPE.setItem(500, "camera", null, "Master Sword", 1);
function attackHook(attacker, victim){
if(Player.getCarriedItem() == 500){
var dmg = 15;
Entity.setHealth(victim, Entity.getHealth(victim) - dmg);
}
}
In theory (if BlockLauncher supports this) you should be able to simplify this since an URL is just a type of URI:
function attackHook(attacker, victim){
if(Player.getCarriedItem() == 500){
var dmg = 15;
Entity.setHealth(victim, Entity.getHealth(victim) - dmg);
}
}
When I tested this code the ModPE.overrideTexture()-function didn't work, in both versions. I don't know whether this is a bug in the current BlockLauncher-App or if something else is wrong.
The best way to fix this is probably to use a texture pack with just the camera-texture replaced (or whichever texture you want to use).
Of course this situation as a whole is not good, after all there are only so many textures in MCPE. I'm sorry that I don't really now if you can just add a texture somehow.
The parameters of the setItem function are: ModPE.setItem(Id, texture, texture-version, name, max-stack-size);
If you want a list of all textures there are you can:
-Copy the MCPE-APK to some other directory. (On my device it is located at /data/app/com.mojang.minecraftpe-1/base.apk. If you want an easier way to locate it you can use for example FX File Explorer.)
-Rename the file from .apk to .zip (it really is just a zip archive).
-Open this zip-archive and go to assets/resource_packs/vanilla/textures/items.
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
Hey,
I've been trying to mod a sword into PE for a while now by following Arjay's videos, and BlockLauncher keeps telling me the "the item icon [texture variable name] does not exist." Could someone look at the below code and see if there's anything wrong with it? I kinda expected the code not to work, because those videos were posted a number of years ago and are probably outdated, so... Anyways, thanks!
/*Master Sword*/ var texture = "//i.imgur.com/oJlYgpy.jpg" ModPE.setItem(500, "texture", 0, "Master Sword"); function attackHook(attacker, victim){ if(Player.getCarriedItem() == 500){ var dmg = 15; Entity.setHealth(victim, Entity.getHealth(victim) - dmg) } }Bump for hope.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumYou can't use an image from the internet as if it would be an image stored on the device.
The image has to be downloaded before it can be used in your mod.
You can:
1) Tell the person who downloads your mod to download the images for it, too, and put them in a specific folder with a specific name so that your mod can use them. Not recommended, the user just wants to download the mod an run it without having to worry about images.
2) Create a variable that stores the file as a Byte-Array and write this to a file and then use this file.
3) Download the image from the internet and use this file.
A possible way to use solution 3):
First of all, the url to the image shouldn't start with // since // is a part of the url to specify a protocol but for this to work this part has to be complete.
So replace // with https://.
Next, you never used the texture-variable. You wrote "texture" when calling the ModPE.setItem()-method which would give this method a string containing texture and not the previous defined variable.
I also added some of these: ;
A little bit improved version:
/*Master Sword*/
var texture = "https://i.imgur.com/oJlYgpy.jpg";
ModPE.setItem(500, texture, 0, "Master Sword");
function attackHook(attacker, victim){
if(Player.getCarriedItem() == 500){
var dmg = 15;
Entity.setHealth(victim, Entity.getHealth(victim) - dmg);
}
}
This would still result in an error since the previous explained image-problem is still unsolved.
You can download the image and use it this way:
//specifiy where the file should be saved on the device and under which name. You can customize these two.
var filePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/games/com.mojang/ModScript/MasterSword/images/";
var fileName = "masterSword.jpg";
//Put the following parts in a try-catch block. Error can easily occur, for example when there is now internet connection.
try{
//check whether the image already exists. This should be the case when the mod is started for the second time.
//When the image already exists there is no need to download it again.
if(!java.io.File(filePath + fileName).exists()){
//specify a file with the filePath and fileName variables
var file = new java.io.File(filePath + fileName);
//create the file:
file.createNewFile();
//In programming in general you don't just put stuff into a file, you use a stream.
//This stream is usually buffered to speed up the overall process of using a file.
//Create a Stream to put things into the file:
var fos = new java.io.FileOutputStream(file);
//specify the url to download the image from:
//This url has to refer to only an image and not a whole website!
var url = "https://i.imgur.com/oJlYgpy.jpg";
//Write data to the file using the url above:
var response = android.net.http.AndroidHttpClient.newInstance("").execute(new org.apache.http.client.methods.HttpGet(url)).getEntity().writeTo(fos);
//Close the Stream now:
fos.close();
}
}catch(err){
//optional error handling
}
//Now the image is downloaded an ready to use.
/*Master Sword*/
//change the texture variable to the path of the downloaded image:
var texture = filePath + fileName;
ModPE.setItem(500, texture, 0, "Master Sword");
function attackHook(attacker, victim){
if(Player.getCarriedItem() == 500){
var dmg = 15;
Entity.setHealth(victim, Entity.getHealth(victim) - dmg);
}
}
This should work.
You should know that the method that is used in this example to get the image from the internet is deprecated: https://developer.android.com/sdk/api_diff/22/changes/android.net.http.AndroidHttpClient
You should use URL ( https://developer.android.com/reference/java/net/URL ) to get an input-stream from the internet ( https://developer.android.com/reference/java/net/URL#openConnection() ).
You should also do this in background using AsyncTask ( https://developer.android.com/reference/android/os/AsyncTask ).
As you see, this is not an easy thing to do.
The example solution is a pretty easy way to accomplish this but, if possible, I recommend to google how you should do this, but that's really not a topic for a beginner.
Thanks for helping! I tried copy-pasting what you said to do while adding the needed links and whatnot, but I still got the same error. I'm very confused.
-
View User Profile
-
View Posts
-
Send Message
Curse PremiumI don't know what's wrong with your code.
Could you post/pm it, so I can look what could be wrong? Also, do you get an error message?
Sorry, there's no notification for a forum reply...
Here's my code. I get the same error as outlined in the first post. Thanks for helping, btw!
var filePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/games/com.mojang/ModScript/MasterSword/images/"; var fileName = "masterSword.jpg"; try{ if(!java.io.File(filePath + fileName).exists()){ var file = new java.io.File(filePath + fileName); file.createNewFile(); var fos = new java.io.FileOutputStream(file); var url = "http://i.imgur.com/8cvUB5X.jpg"; var response = android.net.http.AndroidHttpClient.newInstance("").execute(new org.apache.http.client.methods.HttpGet(url)).getEntity().writeTo(fos); fos.close(); } }catch(err){ //optional error handling } var texture = filePath + fileName; ModPE.setItem(500, texture, "Master Sword"); function attackHook(attacker, victim){ if(Player.getCarriedItem() == 500){ var dmg = 15; Entity.setHealth(victim, Entity.getHealth(victim) - dmg); } }-
View User Profile
-
View Posts
-
Send Message
Curse PremiumSorry that downloading didn't work, I forgot to add an important line:
A new file couldn't be created because the necessary directories don't exist, therefore they must first be created, which is done by this line:
file.getParentFile().mkdirs();
It first gets the parent file of the to be created jpg-image, which is simply the folder in which it should be. It then creates all the directories for this folder.
Another problem with the code was that ModPE.setItem() can use as a texture only a Minecraft texture that already exists. (In the code below I used the camera texture as it is used rarely.)
What you can do however is to override this texture. The function that overrides textures can't use the path to an image for the texture, it needs the path as an URI (https://en.wikipedia.org/wiki/URI), which is why I put File://localhost/ in front of the path.
This is how the code for that looks:
In theory (if BlockLauncher supports this) you should be able to simplify this since an URL is just a type of URI:
When I tested this code the ModPE.overrideTexture()-function didn't work, in both versions. I don't know whether this is a bug in the current BlockLauncher-App or if something else is wrong.
The best way to fix this is probably to use a texture pack with just the camera-texture replaced (or whichever texture you want to use).
Of course this situation as a whole is not good, after all there are only so many textures in MCPE. I'm sorry that I don't really now if you can just add a texture somehow.
The parameters of the setItem function are: ModPE.setItem(Id, texture, texture-version, name, max-stack-size);
If you want a list of all textures there are you can:
-Copy the MCPE-APK to some other directory. (On my device it is located at /data/app/com.mojang.minecraftpe-1/base.apk. If you want an easier way to locate it you can use for example FX File Explorer.)
-Rename the file from .apk to .zip (it really is just a zip archive).
-Open this zip-archive and go to assets/resource_packs/vanilla/textures/items.