0
votes

I'm trying to make my first modscript for Minecraft Pocket Edition. It's very simple and has been made many times. But I just want to try and make one anyway to practice on making mods.

Here is the mod:

var tb = 0;
function useItem(x,y,z,itemId,blockId,side)
{
    if(itemId==280&&tb==0)
    {
        ModPE.setItem(332, 6, 7, "Ender Pearl");
        Player.addItemInventory(332, 32);
        tb=1;
    }else if(itemId==280) {
        Player.addItemInventory(332,5);
}
function entityRemovedHook(entity) 
{ 
    if(Entity.getEntityTypeId(entity)==82) 
    { 
        setPosition(getPlayerEnt(), Entity.getX(entity), Entity.getY(entity)+4, Entity.getZ(entity)); 
}
}

But I recieve an error whenever I try to import it to Blocklauncher:

}
org.mozilla.javascript.EvaluatorException: missing } after function body (Enderpearl.js#17)

There are more stuff to it but I find those unimportant that has nothing to do with the mod.

1
Please fix your formatting. Select all the code, don't use back ticks, and hit the code button.Daedalus
I fixed it! Thank youVVMC23
Sorry but where would I put the closing bracket?VVMC23
Like this?: if(itemId==280&&tb==0) {VVMC23
You have an else if block, but you never close it. You open it then end the function. That is your error. I recommend proper formatting, or even an IDE to spot errors like this more easily. A thing to remember, is that all brackets must match. Right now, your else if is closed by what I assume you think ends the function. That is why there is an error being thrown.Daedalus

1 Answers

2
votes

All brackets must match, this is the rule I commonly follow when I'm not using an IDE(integrated development environment) to script.

So for example, here is your current code. I'll be highlighting your singular error here in the comments of that code

function useItem(x,y,z,itemId,blockId,side)
{
    if(itemId==280&&tb==0)
    {
        ModPE.setItem(332, 6, 7, "Ender Pearl");
        Player.addItemInventory(332, 32);
        tb=1;
    }else if(itemId==280) { // Here, the 'else if' is opened..
        Player.addItemInventory(332,5);
} // but its never closed.  The interpreter interprets this as the end of
  // the else if, but then, the function is never closed
function entityRemovedHook(entity) // So now, you have a function within a function
{  
    if(Entity.getEntityTypeId(entity)==82) 
    { 
        setPosition(getPlayerEnt(), Entity.getX(entity), Entity.getY(entity)+4, Entity.getZ(entity)); 
}
} // 1

This is, of course, assuming that the singular bracket above your error is not 'separate' from the above code.. or in other words is the bracket labeled 1 the last bracket, or is there another after it. If there is another after it, then to continue with this answer..

var tb = 0;
function useItem(x,y,z,itemId,blockId,side)
{
    if(itemId==280&&tb==0)
    {
        ModPE.setItem(332, 6, 7, "Ender Pearl");
        Player.addItemInventory(332, 32);
        tb=1;
    }else if(itemId==280) {
        Player.addItemInventory(332,5);
    } //this bracket should exist here to properly close the else block
}
function entityRemovedHook(entity) 
{ 
    if(Entity.getEntityTypeId(entity)==82) 
    { 
        setPosition(getPlayerEnt(), Entity.getX(entity), Entity.getY(entity)+4, Entity.getZ(entity)); 
    } // I indented this bracket for easier reading
}

Note the extra bracket, placed after your else if's method use(JS is an object-oriented language; in this case, this references the term 'method' which is a function of an object, in this case being 'Player'). In the code above, I've bolded each and every bracket.

In the case that that is not the last bracket, the code seems fine, aside from defining a function within a function, in which case the inner function is not accessible to the outside; it is only accessible from within the function. If this is the case, I'm not familiar with whatever API you're using to interpret this code, and can't offer any other idea as to why its throwing that error.