#Help Creating a Custom Spear/Trident

1 messages · Page 1 of 1 (latest)

true anchor
#

I'm super new to scripting, and I was following the tutorial on wiki.bedrock.dev for Custom Spear. I was successfully able to replicate it, and it worked perfectly fine on 1.19.63. However, since the update I haven't been able to fix it, and the spear can't be picked up. This is my code, and I'm sure I messed something up.

import { world, ItemStack, ItemTypes } from "@minecraft/server"
import { system } from "@minecraft/server";
system.events.beforeWatchdogTerminate.subscribe(data => {
data.cancel = true;
});

world.events.itemReleaseCharge.subscribe(ev => {
for (const player of world.getPlayers()) {
let inv = player.getComponent('inventory').container
const itemStack = inv.getItem(player.selectedSlot);
if (itemStack?.typeId === 'tlc:iron_spear') {
var container = player.getComponent('inventory').container
var newItem = new ItemStack(ItemTypes?.get("tlc:iron_spear"));
var oldItem = container?.getItem(player.selectedSlot)
player.removeTag("iron_spear")
}
let e = system.runInterval(() => {
if (player.hasTag("iron_spear") && itemStack?.typeId === 'tlc:iron_spear' && itemStack?.getComponent("durability").damage <= 125) {
player.removeTag("iron_spear")
newItem.getComponent("durability").damage = oldItem.getComponent("durability").damage + 1;
container.setItem(player.selectedSlot, newItem);
if (!player.hasTag("iron_spear")) {
system.runTimeout(() =>
}
}
})
}
})

arctic thorn
#

Oh, my spears don't work?

true anchor
#

not anymore 😦 the tutorial was awesome tho and helped a ton

arctic thorn
#

Idk, the script works for me

true anchor
#

really?

arctic thorn
#

Yea

true anchor
#

ill check my manifest quick

#

{
"format_version": 2,
"metadata": {
"authors": [
"Tymeless"
]
},
"header": {
"name": "pack.name",
"description": "pack.description",
"min_engine_version": [
1,
19,
50
],
"uuid": "f76e4753-8d95-4792-bce6-efbb03cdc249",
"version": [
1,
0,
0
]
},
"modules": [
{
"language": "javascript",
"type": "script",
"uuid": "9708f314-6db5-453b-b375-688fb329c920",
"version": [
1,
0,
0
],
"entry": "scripts/main.js"
}
],
"dependencies": [
{
"version": "1.1.0-beta",
"module_name": "@minecraft/server"
},
{
"module_name": "@minecraft/server-ui",
"version": "1.0.0-beta"
},
{
"module_name": "@minecraft/server-gametest",
"version": "1.0.0-beta"
}
]
}

arctic thorn
#

?format

carmine thornBOT
#
Formatting JSON, JS

Please format your code as code-blocks! This makes the text monospaced, and has support for syntax highlighting.

The format looks like this:

for JSON
```json
{
"example": 123
}
```

for JavaScript
```js
console.log("hello world", 123) 
```

The character used here is the backtick (U+0060 ` GRAVE ACCENT). This symbol is usually at the top left of your keyboard, occupying the tilde key (~). On mobile, it will be on the second or third page of symbols.

arctic thorn
#

Wait

#
import { world, ItemStack, ItemTypes } from "@minecraft/server"
import { system } from "@minecraft/server";
system.events.beforeWatchdogTerminate.subscribe(data => {
  data.cancel = true;
});

world.events.itemReleaseCharge.subscribe(ev => {
  for (const player of world.getPlayers()) {
    let inv = player.getComponent('inventory').container
    const itemStack = inv.getItem(player.selectedSlot);
    if (itemStack?.typeId === 'tlc:iron_spear') {
      var container = player.getComponent('inventory').container
      var newItem = new ItemStack(ItemTypes?.get("tlc:iron_spear"));
      var oldItem = container?.getItem(player.selectedSlot)
      player.removeTag("iron_spear")
    }
    let e = system.runInterval(() => {
      if (player.hasTag("iron_spear") && itemStack?.typeId === 'tlc:iron_spear' && itemStack?.getComponent("durability").damage <= 125) {
        player.removeTag("iron_spear")
        newItem.getComponent("durability").damage = oldItem.getComponent("durability").damage + 1;
        container.setItem(player.selectedSlot, newItem);
        if (!player.hasTag("iron_spear")) {
          system.runTimeout(() => 
      }
      }
    })
  }
})```
novel drumBOT
#
Debug Result

There are errors in this [code](#1086747536599023746 message):

<repl>.js:24:7 - error TS1109: Expression expected.

24       }
         ~

arctic thorn
#

You're missing a bracket

true anchor
#

omg yeah sorry i definitely shoulda formatted it

#

oh my word lol

arctic thorn
#

At lime 24

true anchor
#

so there should be two instead of 1?

arctic thorn
#

That's what our bot says

#

Add it at lime 24

true anchor
#

ok cool

#

ill run it quick

#

tysm

arctic thorn
#

:))

#

I knew my spears didn't break :]

true anchor
#

ok so its not that then

#

cuz it still didnt work

arctic thorn
#

Send code

#

🥲

true anchor
#
import { world, ItemStack, ItemTypes } from "@minecraft/server"
import { system } from "@minecraft/server";
system.events.beforeWatchdogTerminate.subscribe(data => {
  data.cancel = true;
});

world.events.itemReleaseCharge.subscribe(ev => {
  for (const player of world.getPlayers()) {
    let inv = player.getComponent('inventory').container
    const itemStack = inv.getItem(player.selectedSlot);
    if (itemStack?.typeId === 'tlc:iron_spear') {
      var container = player.getComponent('inventory').container
      var newItem = new ItemStack(ItemTypes?.get("tlc:iron_spear"));
      var oldItem = container?.getItem(player.selectedSlot)
      player.removeTag("iron_spear")
    }
    let e = system.runInterval(() => {
      if (player.hasTag("iron_spear") && itemStack?.typeId === 'tlc:iron_spear' && itemStack?.getComponent("durability").damage <= 125) {
        player.removeTag("iron_spear")
        newItem.getComponent("durability").damage = oldItem.getComponent("durability").damage + 1;
        container.setItem(player.selectedSlot, newItem);
        if (!player.hasTag("iron_spear")) {
          system.runTimeout(() => 
      }
      }
    }
    })
  }
})
novel drumBOT
#
Debug Result

There are errors in this [code](#1086747536599023746 message):

<repl>.ts:24:7 - error TS1109: Expression expected.

24       }
         ~
<repl>.ts:27:5 - error TS1005: ',' expected.

27     })
       ~
<repl>.ts:27:6 - error TS1128: Declaration or statement expected.

27     })
        ~
<repl>.ts:28:3 - error TS1128: Declaration or statement expected.

28   }
     ~
<repl>.ts:29:1 - error TS1128: Declaration or statement expected.

29 })
   ~
<repl>.ts:29:2 - error TS1128: Declaration or statement expected.

29 })
    ~

true anchor
#

damn

arctic thorn
#

Oh my

true anchor
#

thats a lotta errors

arctic thorn
#

Let me fix that for you :)

true anchor
#

hahaha thank you SOOO much

#

i really appreciate it

arctic thorn
#
import { world, ItemStack, ItemTypes } from "@minecraft/server"
import { system } from "@minecraft/server";
system.events.beforeWatchdogTerminate.subscribe(data => {
  data.cancel = true;
});

world.events.itemReleaseCharge.subscribe(ev => {
  for (const player of world.getPlayers()) {
    let inv = player.getComponent('inventory').container
    const itemStack = inv.getItem(player.selectedSlot);
    if (itemStack?.typeId === 'tlc:iron_spear') {
      var container = player.getComponent('inventory').container
      var newItem = new ItemStack(ItemTypes?.get("tlc:iron_spear"));
      var oldItem = container?.getItem(player.selectedSlot)
      player.removeTag("iron_spear")
    }
    let e = system.runInterval(() => {
      if (player.hasTag("iron_spear") && itemStack?.typeId === 'tlc:iron_spear' && itemStack?.getComponent("durability").damage <= 125) {
        player.removeTag("iron_spear")
        newItem.getComponent("durability").damage = oldItem.getComponent("durability").damage + 1;
        container.setItem(player.selectedSlot, newItem);
        if (!player.hasTag("iron_spear")) {
          system.clearRun(e)
      }
      }
    })
  }
})```
#

@true anchor

novel drumBOT
#
No Errors

No errors in [code](#1086747536599023746 message)

true anchor
#

now for the big test!

#

YESSSS

#

IT WORKS

#

you are an absolutel legend @arctic thorn

#

thank you so much

arctic thorn
#

ayy :p

true anchor
#

if i make more than one spear do i just copy the lower part of the module and replace iron_spear with diamond_spear or whatever?

arctic thorn
#

Yes, as the wiki suggests

true anchor
#

ok cool

#

thank you again

#

you have an amazing day!

arctic thorn
#

U too :)