#matchMainHand / check item or armor for mobs LootJS

212 messages · Page 1 of 1 (latest)

bright holly
#

Hello, essentially I'm looking for a way to check if a mob has an item equipped give them a chance to drop it. I've tried using matchMainHand, but as per the wiki, that's only for players. Thank you

vale sandBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

bright holly
#

bumperoo

analog plume
#

I'd also love to know!

analog plume
#

just a bump!

fleet dragon
#

If it only should work for specific items you can try this

.matchEntity(entity => {
  entity.matchSlot(EquipmentSlot.MAINHAND, "item")
})

If you want to do this for any item you could take a look at the apply callback and at the function it provides https://github.com/AlmostReliable/lootjs/wiki/1.20.1#lootcontextjs
if you get the entity you can probably check for the equipped items

GitHub

A Minecraft mod for packdevs to easily modify the loot system with KubeJS. - AlmostReliable/lootjs

bright holly
fleet dragon
#

not like that no, You always need the .addEntityLootModifier first. You also had your brackets incorrectly for the .matchEntity callback, otherwise I've structure the code a little better

LootJS.modifiers((event) => {
    event
        .addEntityLootModifier("minecraft:piglin_brute")
        .matchEntity(entity => {
            entity.matchSlot(EquipmentSlot.MAINHAND, "simplyswords:soulstealer")
        })
        .addLoot(LootEntry.of("simplyswords:soulstealer").when((c) => c.randomChance(1.0)))
})
bright holly
#

ahh I see okay makes sense ill give it a shot

bright holly
fleet dragon
#
const $EquipmentSlot = Java.loadClass("net.minecraft.world.entity.EquipmentSlot")
LootJS.modifiers((event) => {
    event
        .addEntityLootModifier("minecraft:piglin_brute")
        .matchEntity(entity => {
            entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")
        })
        .addLoot(LootEntry.of("simplyswords:soulstealer").when((c) => c.randomChance(1.0)))
})

try this

the thing that confuses me is that the code I had before was documented like this https://github.com/AlmostReliable/lootjs/wiki/1.18.2#equipment-slots in 1.18.2, but basically nothing changed that shouldn't have made this work (or it just didn't work in 1.18.2 aswell)

bright holly
#

I suspected that it may be because this applied to a previous version, I'm this checking now

fleet dragon
#

Yeah I know, but I couldn't find any code in base KubeJS or LootJS that added this EquipmentSlot

bright holly
#
const $EquipmentSlot = Java.loadClass("net.minecraft.world.entity.EquipmentSlot")
LootJS.modifiers((event) => {
    event.addEntityLootModifier("minecraft:piglin_brute")
        .matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")})
        .addLoot(LootEntry.of("simplyswords:soulstealer").when((c) => c.randomChance(0.5)))})```
#

is the random chance value messing it up?

#

okay so what I think is happening, is that the script already causes the sword to drop, but the addloot causes an additional one to drop

#

actually no that isn't whats happening

fleet dragon
#

Generally mobs have a chance to drop equipment

bright holly
#

I gotta do something for like 45 mins just so you think I didnt disappear, thank you for the help so far brb

bright holly
analog plume
#

I would really love to

#

Couls you pls dm me later with a short explanation?

bright holly
analog plume
analog plume
#

just a bump

analog plume
#

LMFAO

#

why is it takin so longgg

bright holly
#

just a waiting game

analog plume
#

regretful

honest breach
#

so where did u stuck, what is a problem

bright holly
honest breach
#

u just wanna change dropping chance or what?

bright holly
honest breach
#

just remove drop before adding loot with different chance

bright holly
honest breach
#

i mean, if u remove weapon drop before adding new one, u will get only one weapon

#

if it works as i think hmmm

bright holly
#

Would you be able to provide an example of this?

honest breach
#
const $EquipmentSlot = Java.loadClass("net.minecraft.world.entity.EquipmentSlot")
LootJS.modifiers((event) => {
    event.addEntityLootModifier("minecraft:piglin_brute")
        .matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")})
        .removeLoot("simplyswords:soulstealer")
        .addLoot(LootEntry.of("simplyswords:soulstealer").when((c) => c.randomChance(0.5)))})

try this

bright holly
#

okay, ill have to check it out tomorrow, ill get back to you

bright holly
honest breach
#

no idea hmmm

bright holly
#

welp

#

thank you though 🙂

analog plume
#

Actually lemme think

#

Nah idk

vague spindle
#

idk how to do it with lootjs, but i have a regular kubejs server script in which i changed item drops from skeletons and pigmans. guess you can modify it to suit your needs

EntityEvents.spawned(event => {
    const entity = event.entity

    if (entity.type === "minecraft:skeleton" || entity.type === "minecraft:zombified_piglin") {

        const equipment = entity.getEquipment()
        if (equipment) {
            equipment.mainHandDropChance = 0
            equipment.offHandDropChance = 0
        }
    }
})
bright holly
#

Thank you I'm looking into it now

bright holly
# vague spindle idk how to do it with lootjs, but i have a regular kubejs server script in which...
EntityEvents.spawned(event => {
let entity = event.entity
if (entity.type === "minecraft:piglin_brute" || entity.type === "takesapillage:legioner" || entity.type === "paradise_lost:envoy" || entity.type === "minecraft:wither_skeleton" || entity.type === "monsterplus:spectral_skeleton") 
{
const equipment = entity.getEquipment()
if (equipment === "simplyswords:soulstealer" || equipment === "simplyswords:arcanethyst" || equipment === "simplyswords:dormant_relic" || equipment === "simplyswords:emberslash" || equipment === "simplyswords:soulpyre") {
      equipment.mainHandDropChance = 1
    }
  }
})``` Unfortunately the following code does nothing for me, and the mobs don't drop their items. Is this a 1.20.1 script?
bright holly
# vague spindle yep

So this should work? And the listed enemies should drop the listed items %100 of the time if they're holding it?

vague spindle
bright holly
ornate blaze
#

For the script you said worked, except they dropped 2, did you try adding a count?

.addLoot(LootEntry.of("simplyswords:soulstealer", 1).when((c) => c.randomChance(1.0)))

#

I suspect it might not fix it, but worth a shot

bright holly
#

How simple was that

ornate blaze
#

Happy it worked!

bright holly
#

It didn't lol

#

Just got lucky

ornate blaze
#

Haha, okay, I suspected it may not

bright holly
#

I don't even understand what could be going wrong

#

Is it possible to write a script that deletes an item if you pick up 2 withing x amount of ticks?

ornate blaze
#

Can you post the entire LootJS.modifiers block?

bright holly
#

or makes it so 2 cant exist as drops

#

const $EquipmentSlot = Java.loadClass("net.minecraft.world.entity.EquipmentSlot")
LootJS.modifiers((event) => {
    event.addEntityLootModifier("minecraft:piglin_brute")
        .matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")})
        .removeLoot("simplyswords:soulstealer")
        .addLoot(LootEntry.of("simplyswords:soulstealer", 1).when((c) => c.randomChance(0.5)))})
#

it seems like it drops 1 sword %33 percent of the time, nothing %33 of the time and the remaining 2 swords

ornate blaze
#

give me a sec and I'll explain why, sorry, in a match lol

bright holly
#

s

ornate blaze
#

So basically it's running the script twice, so you have a few different possible outputs, right?

0, 0
0, 1,
1, 0,
1, 1

So it's roughly 50% chance to get 1, 25% to get 0, and 25% to get 1

ornate blaze
#

The thing is, I don't know why it's running it twice, and I've run into something like this in KubeJS, but not LootJS

bright holly
#

sounds like a bug

ornate blaze
#

simplyswords:soulstealer isn't already in its loot table, right?

#

Just asking because you have .removeLoot("simplysword:soulstealer")

bright holly
#

EntityEvents.spawned(event => {
  let entity = event.entity
  //minecraft:piglin_brute
    if (entity.type == "minecraft:piglin_brute" && Math.random() < 0.52) {
        entity.setItemSlot(0, "simplyswords:soulstealer")
        entity.setItemSlot(1, "shieldexp:netherite_shield")
        entity.setItemSlot(4, "minecraft:netherite_chestplate")
        entity.setItemSlot(2, "minecraft:netherite_boots")
    }``` The only other entry with it. I've tested this and unlike how spawns normally drop weapons, this is not the case
bright holly
ornate blaze
#

Kk, give me a couple minutes to think this through

#

Okay, I'm not familiar with loading classes, but can you try this? And then I have a couple more ideas

LootJS.modifiers((event) => {
    
    event.addEntityLootModifier("minecraft:piglin_brute")
    .matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")})
    .addWeightedLoot(
        [0,1], [
        Item.of("simplysword:soulstealer").withChance(100)]);
        
})```
#

0, 1 means it can drop 0 or 1, with a chance of 100%, and I removed the removeLoot, my hope is that will work

bright holly
#

Okay I'll give it a crack, thank you!

ornate blaze
#

I have a few more ideas of how to tackle this if that doesn't work

bright holly
ornate blaze
#

yes, haha

bright holly
#

withchance right

#

alr thanks

ornate blaze
#

yep

bright holly
# ornate blaze Okay, I'm not familiar with loading classes, but can you try this? And then I ha...

okay SO, the script doesn't do anything, however, I've noticed that it's dropping normally at the 1/8 chance. Unfortunately, the rest of the mobs equipment is dropping, which isn't that great. Even when using it like such:

LootJS.modifiers((event) => {
    
    event.addEntityLootModifier("minecraft:piglin_brute")
    .matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")})
    .removeLoot("minecraft:netherite_chestplate")
    .removeLoot("shieldexp:netherite_shield")
    .removeLoot("minecraft:netherite_boots")
    .addWeightedLoot(
        [0,1], [
        Item.of("simplysword:soulstealer").withChance(100)])});```
ornate blaze
#

no probs at all? hmmmm

#

drops*

bright holly
#

it drops all items though, which is not the goal

#

because the brute has armor on

#

so the removeloot doesn't work in that script

ornate blaze
#

But the one I gave you, it just doesn't do anything, or it still drops twice sometimes?

bright holly
#

Same thing as if I just completely deleted it

ornate blaze
#

Hmmm, okay re-add the .removeLoot and see what happens

bright holly
#

with js }) const $EquipmentSlot = Java.loadClass("net.minecraft.world.entity.EquipmentSlot") LootJS.modifiers((event) => { event.addEntityLootModifier("minecraft:piglin_brute") .removeLoot("minecraft:netherite_chestplate") .removeLoot("shieldexp:netherite_shield") .removeLoot("minecraft:netherite_boots")});

#

still drops armor

ornate blaze
#

No I mean to mine

bright holly
#

or just one item removed?

ornate blaze
#

Oops, okay, so first, I made a type on the second simplyswords

LootJS.modifiers((event) => {
    
    event.addEntityLootModifier("minecraft:piglin_brute")
    .matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")})
    .addWeightedLoot(
        [0,1], [
        Item.of("simplyswords:soulstealer").withChance(100)]);
        
})```
bright holly
ornate blaze
#

Kk, that's what I suspected, let's try one more thing, but I need a few minutes to type it out lol

bright holly
ornate blaze
#

Okay, this is a hail mary, but worth a shot, lol

LootJS.modifiers((event) => {
    
    const whenSword = LootEntry.of("simplyswords:soulstealer", 1).when((c) =>
        c.matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")})
    
    event.addEntityLootModifier("minecraft:piglin_brute")
    .addAlternativesLoot(whenSword);
        
})```
#

uhh

#

typo lol

#

Okay, there we go lmao

#

If that doesn't work, it basically confirms the issue, that the entire script is running two times

bright holly
ornate blaze
#

what line?

bright holly
#

second from the last

#

red underline

#

is it defined?

#

actually, it may be redundant

ornate blaze
#

event shouldn't be throwing an exception; the const whenSword is being defined, and then even.addEntityLootModifier is calling the standard loot table

#

what exactly does the error message say?

bright holly
#

got it

#

was missing a parenthesis

ornate blaze
#

Ope, you're right, for the .when()

bright holly
#

still giving me "error missing ) after list"

ornate blaze
#

oh, for that error, try adding a semicolon after the const, so like

LootJS.modifiers((event) => {
    
    const whenSword = LootEntry.of("simplyswords:soulstealer", 1).when((c) =>
        c.matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")}));
    
    event.addEntityLootModifier("minecraft:piglin_brute")
    .addAlternativesLoot(whenSword);
        
})```
bright holly
#

syntax is so lame

#

still dropped 2 : (

ornate blaze
#

Okay, that's still data, that means it is running two times regardless of how we try to add it

bright holly
#

crazy question but how did you learn so much of KubeJS despite being new on the Discord

ornate blaze
#
  1. I'm an autodidact and have taught myself VB.NET and C#.NET in the past, 2) I'm a natural troubleshooter, 3) I'm relying heavily on the documentation and use an LLM to help me analyze and understand code I'm not familiar with
bright holly
ornate blaze
#

yep, lol

bright holly
#

also whats llm lol

ornate blaze
#

chatgpt, deepseek

#

I don't have them write code for me, but I use them to understand if I don't recognize the code

bright holly
#

hmm okay fair enough

ornate blaze
#

So let's start extremely basic to see if it repeats at the base level; just try this and see if it still sometimes drops 2:

    
    event.addEntityLootModifier("minecraft:piglin_brute")
    .addLoot("simplyswords:soulstealer");
        
})```
bright holly
#

it does

#

even with the ones not holding the weapon

ornate blaze
#

Interesting... what if you just do "minecraft:diamond_sword"? Basically I'm trying to isolate the element that's causing the double drop

bright holly
#

srry doing a bit more testing

ornate blaze
#

all good

bright holly
#

yeah still drops two...

#

I might need to add to another script

ornate blaze
#

Okay, now try a different mob, so like

    
    event.addEntityLootModifier("minecraft:cow")
    .addLoot("simplyswords:soulstealer");
        
})```
bright holly
#

still dropping two??

ornate blaze
#

Okay, then last step would be trying no script whatsoever and seeing what happens

#

well no

#

wait, nevermind

#

We already know it's that the script is running twice, we don't need to do that lmao

#

ummmm

bright holly
#

Okay so its firing all my loot scripts twice for some reason

ornate blaze
#

are you able to throw your entire .js somewhere so I can see the entire doc?

bright holly
#

I wonder if thats the case for all the scripts

frigid pawnBOT
#

Paste version of loot.js from @bright holly

ornate blaze
#

Sorry, a lot to look through; this is in server_scripts, right?

bright holly
#

indeed

#

I'm going to delete all server scripts except for a single entry

ornate blaze
#

In general I would keep separate js files by function, but that's just my own organizational imperative, haha

bright holly
#

If I did that you have no idea how many files I'd have

ornate blaze
#

Okay, so let's test something really quick; can you test out limpets and see if they ever drop 3 or 4 soul dust?

bright holly
#

I'll take a look, I just deleted all scripts so I'll copy them back in after I check something

#

I'm going to relaunch, but I deleted all server scripts except for this single entry and it still drops double```js
LootJS.modifiers((event) => {
event.addEntityLootModifier([
"cataclysm:coralssus"
])
.addLoot(
LootEntry.of(Item.of('irons_spellbooks:scroll', '{ISB_Spells:{data:[{id:"traveloptics:coral_barrage",index:0,level:1,locked:1b}],maxSpells:1,mustEquip:0b,spellWheel:0b}}').weakNBT()).when((c) => c.randomChance(1.0))
);
});

ornate blaze
#

well, at least we know the issue isn't with your scripts themselves, lol

bright holly
#

still got client and startups tho

#

those are next

#

okay still happens when I relaunch

ornate blaze
#

So when you remove every script except for the single call, it still drops double?

bright holly
#

yep still drops 2

#

I have only a single script

bright holly
#

looks like there may be a mod config

#

that sucks

#

Well it's about time I go to bed so I'll give it a crack later

ornate blaze
#

alrighty, you might, unfortunately, have to start checking individual mods 🙁

bright holly
#

thank you for all your help tho

bright holly
#

good riddance lol

#

final script to prevent armor drops ```js

let patchedEntities = [
'minecraft:piglin_brute'
]

EntityEvents.spawned(event => {
let ent = event.entity
if (!ent || !ent.isLiving()) return
if (!patchedEntities.includes(ent.type)) return

let dim = ent.level.dimension
let x = ent.x, y = ent.y, z = ent.z

event.server.scheduleInTicks(1, () => {
event.server.runCommandSilent(execute in ${dim} positioned ${x} ${y} ${z} run data merge entity @e[type=${ent.type},distance=..0.5,limit=1,sort=nearest] {ArmorDropChances:[0.0f,0.0f,0.0f,0.0f],HandDropChances:[0.0f,0.0f]})
})
})

const $EquipmentSlot = Java.loadClass("net.minecraft.world.entity.EquipmentSlot")
LootJS.modifiers((event) => {
event.addEntityLootModifier("minecraft:piglin_brute").matchEntity(entity => {entity.matchSlot($EquipmentSlot.MAINHAND, "simplyswords:soulstealer")}).addLoot(LootEntry.of("simplyswords:soulstealer").when((c) => c.randomChance(1.0)));
```

ornate blaze
#

well, I'm glad you got it 😅