#How to make villagers run away from the player if they are wearing or holding specific items
115 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
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)}
)```
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
what are your logs?
there may be errors you're not seeing
also, entity AI only applies on world restart or entity spawn
Paste version of client.log, server.log, startup.log from @plain plank
still doesnt work
have you reloaded your world or tried spawning a new one?
yes
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
and here you're using event.player which there is no event.player in the goal event
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) })
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)
})```
also maybe unrelated but does anyone know what might cause the villagers to go so fast?
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)
its the speed modifier you have set to 2 which is usually 0.25, i suggest getting probejs and hovering over the method as it provides this information
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)
})```
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)
})```
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) }
yup that fixed it
neat
what do the 3 numbers here represent?
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)```
^
the last 1 does change the speed but the first one i didnt see changes
did you do /probejs dump command?
yea
nope
like this right?
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.
that catch block isnt right
and theres a trailing comma here
yeah just send the whole document tbh
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)
}```
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
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
show code
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.
this one but with .25, .25, instead of the 0, 0,
also tried 0.25, 0.25,
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
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);
}
});```
^
oh yea forgot
Paste version of server.log from @plain plank
are you summoning a new entity every time to test?
yea
hmm
also reloading the world every time
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
it breaks the goal
hmm
0.25 villager and 1 villager
wait so try 0.8 instead
i think its slowing them down to the point they cant run away with their conflicting goals
this is at 0.7
yea it works
glad we got this figured out
i am noticing that its both trying to run away and stay with the 0.25 villager
how do you make it so that it ignores other goals if the avoid goal is active?
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
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.
Just read through that entire ticket to see it was solved at the end...
Please close the ticket like lio said @plain plank
Ticket re-opened!
ok so- thats not right
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
);```
ok that fixes it but the villager still tries to go to sleep instead of running away
i think it needs to both panic and avoid
cuz if i punch him it does work
whatever thats a minor mistake i think its fine even without fixing it


