#How to make villagers run away from the player if they are wearing or holding specific items

115 messages · Page 1 of 1 (latest)

plain plank
#

basically trying to recreate the scary player mod in kubejs

dark waspBOT
#

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

plain plank
#

really tried to understand entityjs

#

no clue what im doing but this aint it

EntityJSEvents.addGoalSelectors("minecraft:villager", event => {
    const {player} = event 
    if (player.mainHandItem.id == 'minecraft:stick') {
    event.avoidEntity(1, Player, () => true, 6, 2, 2, () => true)}
    else
    event.avoidEntity(1, Player, () => true, 6, 2, 2, () => false)}
)```
plain plank
#

How is this not right

EntityJSEvents.addGoals('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player')
    event.avoidEntity(1, Player, () => true, 6, 2, 2, () => false)
})```
#

im failing to understand the basics of how this works

round sentinel
#

what are your logs?

#

there may be errors you're not seeing

#

also, entity AI only applies on world restart or entity spawn

spiral reefBOT
#

Paste version of client.log, server.log, startup.log from @plain plank

plain plank
#

still doesnt work

round sentinel
#

have you reloaded your world or tried spawning a new one?

plain plank
#

yes

round sentinel
#

@molten verge this one's for ya because i don't know entityjs as much as you raha

molten verge
#

they were close, @plain plank you're using the wrong goal event in that second code snippet, that ones for targetSelectors, the one for goalSelectors is js EntityJSEvents.addGoalSelectors

molten verge
#

instead youll need to use the predicate to detect if the entity/player is holding a stick js EntityJSEvents.addGoalSelectors('minecraft:villager', event => { let Player = Java.loadClass('net.minecraft.world.entity.player.Player') event.avoidEntity(1, Player, player => player.mainHandItem.id == "minecraft:stick", 10, 2, 2, e => true) })

plain plank
#

yup that works, is there a way to make the code a bit more polished tho?

EntityJSEvents.addGoalSelectors('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player')
    event.avoidEntity(1, Player, player => player.mainHandItem.id == "minecraft:stick", 10, 2, 2, e => true)
    event.avoidEntity(1, Player, player => player.mainHandItem.id == "minecraft:carrot", 10, 2, 2, e => true)
    event.avoidEntity(1, Player, player => player.headArmorItem == "minecraft:diamond_helmet", 10, 2, 2, e => true)
    event.avoidEntity(1, Player, player => player.chestArmorItem == "minecraft:diamond_chestplate", 10, 2, 2, e => true)
    event.avoidEntity(1, Player, player => player.legsArmorItem == "minecraft:diamond_leggings", 10, 2, 2, e => true)
    event.avoidEntity(1, Player, player => player.feetArmorItem == "minecraft:diamond_boots", 10, 2, 2, e => true)
})```
molten verge
#

you can combine multiple of the boolean values with the or operand js event.avoidEntity(1, Player, player => player.mainHandItem.id == "minecraft:stick" || player.mainHandItem.id == "minecraft:carrot" || player.headArmorItem == "minecraft:diamond_helmet" , 10, 2, 2, e => true)

molten verge
plain plank
#

if i use decimal it doesnt work

#

i do think speed 1 is normal running tho

#

This is what i have rn, ill see tomorrow

EntityJSEvents.addGoalSelectors('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player')
    event.avoidEntity(1, Player, player => 
        player.mainHandItem.id == "minecraft:stick" ||
        player.headArmorItem == "minecraft:diamond_helmet" ||
        player.chestArmorItem == "minecraft:diamond_chestplate" ||
        player.legsArmorItem == "minecraft:diamond_leggings" ||
        player.feetArmorItem == "minecraft:diamond_boots",
        10, 1, 1, e => true)
})```
plain plank
#

why is this crashing?

EntityJSEvents.addGoalSelectors('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player')
    event.avoidEntity(1, Player, player => 
        player.mainHandItem.id == "tacz:modern_kinetic_gun" ||
        player.mainHandItem.id == "dungeons_and_combat:saw_cleaver_glaive" ||
        player.mainHandItem.id == "dungeons_and_combat:saw_cleaver_sickle" ||
        player.mainHandItem.id == "cannibal:stone_saw" ||
        player.mainHandItem.hastag("butcher:organs") ||
        player.mainHandItem.hastag("nethersdelight:tools/machetes") ||
        player.headArmorItem == "cannibal:bone_mask" ||
        player.headArmorItem == "butcher:pillagerdisguise_helmet" ||
        player.headArmorItem == "bonezone:spinal_skull_deer" ||
        player.headArmorItem == "bonezone:spinal_skull_goat" ||
        player.headArmorItem == "bonezone:spinal_skull_bird" ||
        player.headArmorItem == "bonezone:spinal_skull_bull" ||
        player.headArmorItem == "minecraft:skeleton_skull" ||
        player.headArmorItem == "minecraft:wither_skeleton_skull" ||
        player.headArmorItem == "minecraft:zombie_head" ||
        player.headArmorItem == "minecraft:creeper_head" ||
        player.chestArmorItem == "butcher:pillagerdisguise_chestplate" ||
        player.legsArmorItem == "butcher:pillagerdisguise_leggings" ||
        player.feetArmorItem == "butcher:pillagerdisguise_boots",
        10, 1, 1, e => true)
})```
molten verge
#

it's hasTag with uppercase T

#

to prevent it from crashing you can surround it with a try-catch block js try { //your crashable code } catch(e) { console.log(e) }

plain plank
#

yup that fixed it

molten verge
#

neat

plain plank
molten verge
#

to make it shorter code you can do something like ```js
let headItems = [
"minecraft:skeleton_skull",
"bonezone:spinal_skull_deer"
]

headItems.includes(player.headArmorItem.id)```

plain plank
#

the last 1 does change the speed but the first one i didnt see changes

molten verge
#

no look at the screenshot

#

those are the params

plain plank
#

oh yea yea

#

btw i did install probejs but it doesnt show me the parameters

molten verge
#

did you do /probejs dump command?

plain plank
#

yea

#

the autocomplete works

molten verge
#

dump again

#

actually wait

#

did you open your minecraft instance folder with vsc?

plain plank
#

yea

molten verge
#

odd

#

yeah try dumping again maybe, not sure

plain plank
#

nope

plain plank
molten verge
#

uh

#

send your full code

spiral reefBOT
#

Send your code here instead of a screenshot

It makes it easier to diagnose your code and help you fix it or make the required changes/additions.

molten verge
#

that catch block isnt right

#

and theres a trailing comma here

#

yeah just send the whole document tbh

plain plank
#

i was trying another thing there

try {
    LootJS.modifiers((event) => {
        const corpseloot = LootEntry.of("butcher:villager_corpse_item").when(c =>
            c.matchMainHand("dungeons_and_combat:saw_cleaver_sickle") ||
            c.matchMainHand("dungeons_and_combat:saw_cleaver_glaive")
        )
        event
        .addEntityLootModifier("minecraft:villager")
        .addSequenceLoot(corpseloot)
      }),
EntityJSEvents.addGoalSelectors('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player')
    event.avoidEntity(1, Player, player => 
        player.mainHandItem.id == "tacz:modern_kinetic_gun" ||
        player.mainHandItem.id == "dungeons_and_combat:saw_cleaver_glaive" ||
        player.mainHandItem.id == "dungeons_and_combat:saw_cleaver_sickle" ||
        player.mainHandItem.id == "cannibal:stone_saw" ||
        player.mainHandItem.hasTag("butcher:organs") ||
        player.mainHandItem.hasTag("nethersdelight:tools/machetes") ||
        player.headArmorItem == "cannibal:bone_mask" ||
        player.headArmorItem == "butcher:pillagerdisguise_helmet" ||
        player.headArmorItem == "bonezone:spinal_skull_deer" ||
        player.headArmorItem == "bonezone:spinal_skull_goat" ||
        player.headArmorItem == "bonezone:spinal_skull_bird" ||
        player.headArmorItem == "bonezone:spinal_skull_bull" ||
        player.headArmorItem == "minecraft:skeleton_skull" ||
        player.headArmorItem == "minecraft:wither_skeleton_skull" ||
        player.headArmorItem == "minecraft:zombie_head" ||
        player.headArmorItem == "minecraft:creeper_head" ||
        player.chestArmorItem == "butcher:pillagerdisguise_chestplate" ||
        player.legsArmorItem == "butcher:pillagerdisguise_leggings" ||
        player.feetArmorItem == "butcher:pillagerdisguise_boots",
        10, 0, 0, e => true)
})
} catch(e) {
    console.log(e)
  }```
molten verge
#
LootJS.modifiers((event) => {
    const corpseloot = LootEntry.of("butcher:villager_corpse_item").when(c =>
        c.matchMainHand("dungeons_and_combat:saw_cleaver_sickle") ||
        c.matchMainHand("dungeons_and_combat:saw_cleaver_glaive")
    )
    event
        .addEntityLootModifier("minecraft:villager")
        .addSequenceLoot(corpseloot)
})
let headItems = [
    "minecraft:skeleton_skull",
    "minecraft:wither_skeleton_skull",
    "minecraft:zombie_head",
    "minecraft:creeper_head",
    "bonezone:spinal_skull_deer",
    "bonezone:spinal_skull_goat",
    "bonezone:spinal_skull_bird",
    "bonezone:spinal_skull_bull",
    "cannibal:bone_mask",
    "butcher:pillagerdisguise_helmet"
];

let weaponItems = [
    "tacz:modern_kinetic_gun",
    "dungeons_and_combat:saw_cleaver_glaive",
    "dungeons_and_combat:saw_cleaver_sickle",
    "cannibal:stone_saw"
];

let armorItems = {
    chest: ["butcher:pillagerdisguise_chestplate"],
    legs: ["butcher:pillagerdisguise_leggings"],
    feet: ["butcher:pillagerdisguise_boots"]
};

EntityJSEvents.addGoalSelectors('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player');
    try {
        event.avoidEntity(1, Player, player =>
            weaponItems.includes(player.mainHandItem.id) ||
            player.mainHandItem.hasTag("butcher:organs") ||
            player.mainHandItem.hasTag("nethersdelight:tools/machetes") ||
            headItems.includes(player.headArmorItem.id) ||
            armorItems.chest.includes(player.chestArmorItem.id) ||
            armorItems.legs.includes(player.legsArmorItem.id) ||
            armorItems.feet.includes(player.feetArmorItem.id),
            10, 0, 0, e => true
        );
    } catch (error) {
        console.log(error);
    }
});```
#

heres a couple examples of how to use .includes

#

and i got rid of that comma after your lootjs event because its not needed

plain plank
#

damn nice

#

only thing is that i do not understand why its doesnt let me use decimals for the villager speed

#

1 is still too fast

molten verge
#

what kind of decimal are you using

#

.25 or 0.25

plain plank
#

i see my mistake

#

nope it still doesnt work

molten verge
#

show code

spiral reefBOT
#

Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full scripts and log/crash report!

#

Please send your KubeJS server log. It can be found at /minecraft/logs/kubejs/server.log.
If you are on 1.18 or 1.16 it will be called server.txt.
Please send the file directly, without links or snippets.

plain plank
#

also tried 0.25, 0.25,

molten verge
#

you cant do .25

#

only 0.25 would work in that situation

#

and yes decimals work for the avoid entity goal, example in minecraft source code

plain plank
#

its not working for me tho

LootJS.modifiers((event) => {
    const corpseloot = LootEntry.of("butcher:villager_corpse_item").when(c =>
        c.matchMainHand("dungeons_and_combat:saw_cleaver_sickle") ||
        c.matchMainHand("dungeons_and_combat:saw_cleaver_glaive")
    )
    event
        .addEntityLootModifier("minecraft:villager")
        .addSequenceLoot(corpseloot)
})
let headItems = [
    "minecraft:skeleton_skull",
    "minecraft:wither_skeleton_skull",
    "minecraft:zombie_head",
    "minecraft:creeper_head",
    "bonezone:spinal_skull_deer",
    "bonezone:spinal_skull_goat",
    "bonezone:spinal_skull_bird",
    "bonezone:spinal_skull_bull",
    "cannibal:bone_mask",
    "butcher:pillagerdisguise_helmet"
];

let weaponItems = [
    "tacz:modern_kinetic_gun",
    "dungeons_and_combat:saw_cleaver_glaive",
    "dungeons_and_combat:saw_cleaver_sickle",
    "cannibal:stone_saw"
];

let armorItems = {
    chest: ["butcher:pillagerdisguise_chestplate"],
    legs: ["butcher:pillagerdisguise_leggings"],
    feet: ["butcher:pillagerdisguise_boots"]
};

EntityJSEvents.addGoalSelectors('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player');
    try {
        event.avoidEntity(1, Player, player =>
            weaponItems.includes(player.mainHandItem.id) ||
            player.mainHandItem.hasTag("butcher:organs") ||
            player.mainHandItem.hasTag("nethersdelight:tools/machetes") ||
            headItems.includes(player.headArmorItem.id) ||
            armorItems.chest.includes(player.chestArmorItem.id) ||
            armorItems.legs.includes(player.legsArmorItem.id) ||
            armorItems.feet.includes(player.feetArmorItem.id),
            6, 0.25, 0.25, e => true
        );
    } catch (error) {
        console.log(error);
    }
});```
plain plank
spiral reefBOT
#

Paste version of server.log from @plain plank

molten verge
#

are you summoning a new entity every time to test?

plain plank
#

yea

molten verge
#

hmm

plain plank
#

also reloading the world every time

molten verge
#

no need for that

#

just /kubejs reload server_scripts then summon new villager

#

also are you saying its not working as in it breaks the goal and they stop running or its just the same speed

plain plank
#

it breaks the goal

molten verge
#

hmm

plain plank
molten verge
#

wait so try 0.8 instead

#

i think its slowing them down to the point they cant run away with their conflicting goals

plain plank
#

yea it works

molten verge
#

glad we got this figured out

plain plank
#

how do you make it so that it ignores other goals if the avoid goal is active?

molten verge
#

make priority 0

#

instead of 1

plain plank
#

aight i think thats it

#
let headItems = [
    "minecraft:skeleton_skull",
    "minecraft:wither_skeleton_skull",
    "minecraft:zombie_head",
    "minecraft:creeper_head",
    "bonezone:spinal_skull_deer",
    "bonezone:spinal_skull_goat",
    "bonezone:spinal_skull_bird",
    "bonezone:spinal_skull_bull",
    "cannibal:bone_mask",
    "butcher:pillagerdisguise_helmet"
];

let weaponItems = [
    "tacz:modern_kinetic_gun",
    "dungeons_and_combat:saw_cleaver_glaive",
    "dungeons_and_combat:saw_cleaver_sickle",
    "cannibal:stone_saw"
];

let armorItems = {
    chest: ["butcher:pillagerdisguise_chestplate"],
    legs: ["butcher:pillagerdisguise_leggings"],
    feet: ["butcher:pillagerdisguise_boots"]
};

EntityJSEvents.addGoalSelectors('minecraft:villager', event => {
    let Player = Java.loadClass('net.minecraft.world.entity.player.Player');
    try {
        event.avoidEntity(0, Player, player =>
            weaponItems.includes(player.mainHandItem.id) ||
            player.mainHandItem.hasTag("butcher:organs") ||
            player.mainHandItem.hasTag("nethersdelight:tools/machetes") ||
            headItems.includes(player.headArmorItem.id) ||
            armorItems.chest.includes(player.chestArmorItem.id) ||
            armorItems.legs.includes(player.legsArmorItem.id) ||
            armorItems.feet.includes(player.feetArmorItem.id),
            6, 0.8, 0.8, e => true
        );
    } catch (error) {
        console.log(error);
    }
});```
#

thanks

spiral reefBOT
#

Please close your ticket (with </ticket close:1054771505520717835> or the button atop this thread) once you resolved your issue!
This also helps others that would like to help out, as they don't have to look into this thread to check if it has been resolved by now.

Do you have any other questions regarding your issue? Feel free to ask!
Note: You should create a new post for unrelated issues.

iron gale
#

Just read through that entire ticket to see it was solved at the end...

#

Please close the ticket like lio said @plain plank

molten verge
#

lol

#

add that to your predicate as well, if they're sleeping

#
event.avoidEntity(0, Player, player =>
            !event.entity.sleeping && (
                weaponItems.includes(player.mainHandItem.id) ||
                player.mainHandItem.hasTag("butcher:organs") ||
                player.mainHandItem.hasTag("nethersdelight:tools/machetes") ||
                headItems.includes(player.headArmorItem.id) ||
                armorItems.chest.includes(player.chestArmorItem.id) ||
                armorItems.legs.includes(player.legsArmorItem.id) ||
                armorItems.feet.includes(player.feetArmorItem.id)),
            6, 0.8, 0.8, e => true
        );```
plain plank
#

i think it needs to both panic and avoid

plain plank
#

whatever thats a minor mistake i think its fine even without fixing it