#Buffing mobs' stats and equipment based on a random nearby player's stages
74 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
Mainly I need to know how check the stages of nearby players and how to set the mob's equipment/stats.
I already have an EntityEvents.spawned event set up.
accessing stages is pretty easy. its just in player.stages
all the functions in this are found here https://github.com/KubeJS-Mods/KubeJS/blob/main/src/main/java/dev/latvian/mods/kubejs/stages/Stages.java
you could jump into the Player.java source code and see where to access their armor from there
you could also use the same strategy to go into the server and find the day count
im not an expert but what I usually do is find the functions (looking at the source code) around the things I want to change and then access it through the relevant events
btw im pret sure you can run commands server.runCommandSilent(...args) to find players` names, then access their instance from the server playerlist to find their stages
thats atleast is the basic concept of where to start while you're waiting for help from the experts
alright, ill give it a shot
i cant seem go get commands working for some reason, this is my test code and i am not given raw chicken when zombies are spawned with or without the forward slash
EntityEvents.spawned('minecraft:zombie', event => {
server.runCommandSilent('/give @a raw_chicken')
})```
wait raw chikens id is just chicken let me retry
okay now its working
okay ive got it to execute the command at the spawning entities location but idk how to actually get the nearest player
EntityEvents.spawned('minecraft:zombie', event => {
var x = event.entity.x
var y = event.entity.y
var z = event.entity.z
event.server.runCommandSilent('execute positioned ' + x + ' ' + y + ' ' + z + ' run give @p chicken')
})```
I thought this would work but it doesn't, so idk what to do.
EntityEvents.spawned('minecraft:zombie', event => {
function findnearest() {
var players = event.server.getPlayers()
var dist = Infinity
var target = nil
for (let i = 0; i < players.size(); i++) {
var newdist = players.get(i).distanceToSqr(event.entity.x, event.entity.y, event.entity.z)
if (newdist < dist) {
dist = newdist
target = players.get(i)
target.tell('test')
}
}
return target
}
var player = findnearest()
player.tell('test')
})```
Buffing mobs stats and equipment based on the nearest player's stages
Buffing mobs' stats and equipment based on the nearest player's stages
I've simplified my code down to this but idk how to get the player itself
EntityEvents.spawned('minecraft:zombie', event => {
var x = event.entity.x
var y = event.entity.y
var z = event.entity.z
var test = (event.server.runCommand('/execute positioned ' + x + ' ' + y + ' ' + z + ' run data get entity @r[type = player, sort = random, distance=..128, limit = 1] <path>'))
event.server.getPlayers()[0].tell(test)
})```
Buffing mobs' stats and equipment based on a random nearby player's stages
I figured it would be the best for it to choose a random nearby player to check the stages of
Because that way it would make it so a group of players with different stages would have a blend of differently armoured enemies spawning around them instead of having the armour based on the closest player to where the enemy spawned
@past oasis try this in server scripts js EntityEvents.spawned('minecraft:zombie', event => { const { entity, level, server } = event let mobAABB = entity.boundingBox.inflate(150) let NearbyPlayers = [] level.getEntitiesWithin(mobAABB).forEach(e => { if (e == null || !e.player) return let playerName = e.username NearbyPlayers.push(playerName) }) if (NearbyPlayers.length != 0) { let randomPlayer = NearbyPlayers[Math.floor(Math.random() * NearbyPlayers.length)] if (randomPlayer != null) { let player = server.getPlayer(randomPlayer) if (player && player.stages.getAll().some(stage => stage == "someStage")) { entity.setItemSlot(5, 'minecraft:iron_helmet') entity.setItemSlot(4, 'minecraft:iron_chestplate') entity.setItemSlot(3, 'minecraft:iron_leggings') entity.setItemSlot(2, 'minecraft:iron_boots') } } } })
to test it out you can give yourself the stage with this command /kubejs stages add Dev someStage
replace Dev with your player name ofcourse
It doesn't appear to work, the zombies don't spawn with iron armour when I have the someStage stage.
I've been attempting to make my own code that uses the distance to the block the mobs spawn at but I can't get it to work, I saw something about a distanceToSqr code I can run but I can't seem to get it to work.
EntityEvents.spawned('minecraft:zombie', event => {
var x = Math.round(event.entity.x)
var y = Math.round(event.entity.y)
var z = Math.round(event.entity.z)
var players = event.server.getPlayers()
var maxdist = 128
var targets = []
for (let i = 0; i < players.length; i++) {
players[i].tell('test 1')
var dist = players[i].distanceToSqr(x, y, z)
players[i].tell('test 2')
if (dist < maxdist) {
targets.push(players[i])
}
}
for (let i = 0; i < targets.length; i++) {
targets[i].tell('test 3')
}
})```
The code never gets past the `distanceToSqr` section
Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full crash report, log, and scripts!
works on my machine ™️
also make sure you added the stage to yourself and not to "Dev"
I reloaded my scripts after adding your code to my server script and it isn't working.
Of course the video popped up as a download.
im pretty sure i could figure it out myself if i knew how to get player.distanceToSqr to work
send your kubejs logs
??kjslogs
You can find your KubeJS server log in /minecraft/logs/kubejs/server.log.
If you are on 1.18 or 1.16 it will be called server.txt.
Please send it if asked, as it contains helpful information.
also distanceToSqr requires a vec3 type so try js distanceToSqr([x, y, z])
either that or js .distanceToSqr(entity.position())
Paste version of message.txt from @past oasis
thats odd, it should be gating players only in the aabb function 
you also have this to use for example which might be the most straight forward approachjs player.distanceToEntity(entity) < 120
Wait so how would i go about adding this into the script? I'm pretty new to KubeJS so idrk what I'm doing.
if (player.distanceToEntity(entity) < 128) {
targets.push(players[i])
}```
Something like this should work, right?
EntityEvents.spawned('minecraft:zombie', event => {
var players = event.server.getPlayers()
var maxdist = 128
var targets = []
for (let i = 0; i < players.length; i++) {
var dist = players[i].distanceToEntity(entity)
if (dist < maxdist) {
targets.push(players[i])
}
}
players[0].tell(targets.stages.getAll())
let randomPlayer = targets[Math.floor(Math.random() * targets.length)]
if (randomPlayer != null) {
let player = server.getPlayer(randomPlayer)
if (player && player.stages.getAll().some(stage => stage == "overworld_done")) {
entity.setItemSlot(5, 'minecraft:iron_helmet')
entity.setItemSlot(4, 'minecraft:iron_chestplate')
entity.setItemSlot(3, 'minecraft:iron_leggings')
entity.setItemSlot(2, 'minecraft:iron_boots')
}
}
})```
oh wait what overworld_done is for?
basically once the player beats all of the bosses ive added to the overworld they start seeing buffed mobs
how do i check for a specific stage? because right now it isnt working
send your logs again
This is repeated over and over again and is the only relevant thing to the issue:
[14:32:59] [ERROR] ! mobarmor.js#13: Error in 'EntityEvents.spawned': TypeError: Cannot find function includes in object [overworld_done, guardian_slain, aether_done, undergarden_done, nether_done, twilight_forest_done].
And this is the actual code I'm using
EntityEvents.spawned('minecraft:zombie', event => {
var players = event.server.getPlayers()
var maxdist = 128
var targets = []
for (let i = 0; i < players.length; i++) {
var dist = players[i].distanceToEntity(event.entity)
if (dist < maxdist) {
targets.push(players[i])
}
}
players[0].tell(targets[0].getStages().getAll().includes('overworld_done'))
var randomPlayer = Math.floor(Math.random() * targets.length);
if (randomPlayer != null) {
if (randomPlayer && randomPlayer.getStages().getAll().includes('overworld_done')) {
event.entity.setItemSlot(5, 'minecraft:iron_helmet')
event.entity.setItemSlot(4, 'minecraft:iron_chestplate')
event.entity.setItemSlot(3, 'minecraft:iron_leggings')
event.entity.setItemSlot(2, 'minecraft:iron_boots')
}
}
})```
It stops at line thirteen (which is players[0].tell(targets[0].getStages().getAll().includes('overworld_done')))
Okay I found out that I needed to replace getStage().getAll().includes() with stages.has()
But it still isn't applying the armour
send log
okay I've gotten the entity.setItemSlot() to work with the entities main hand but not anything else
wait capitalization error
entity.setMainHandItem()
yes, but i've never used that mod
EntityEvents.spawned('minecraft:zombie', e => {
const {server, entity} = e;
const check = server.players.some(p =>
p.stages.has('overworld_done') &&
p.distanceToEntity(entity) < 128
);
if(!check) return;
entity.setItemSlot(5, 'minecraft:iron_helmet')
entity.setItemSlot(4, 'minecraft:iron_chestplate')
entity.setItemSlot(3, 'minecraft:iron_leggings')
entity.setItemSlot(2, 'minecraft:iron_boots')
})
your code can be simplified into this ↑
thanks for helping with this and also for the simplification of the code, ive been using kubejs and javascript for less than a month so i dont really now much about any of this