#fix ugly code

25 messages · Page 1 of 1 (latest)

sinful condor
#

I think my code is ugly.
i am using kube js!!! why i make these with commends
i think there must be a way to do the same thing using only kube js
does it?
help fix my code plz

ItemEvents.entityInteracted('minecraft:wolf', event => {
    if (event.item.id != 'kubejs:test_food') return;
    event.item.count--;
    event.player.giveInHand('bone');
    event.target.playSound('entity.wolf.howl');
    event.server.runCommand(`execute as ${event.target.uuid} run say YUMMY!!!`);
    event.server.runCommand(`effect give ${event.target.uuid} minecraft:absorption 180 2`);
    event.server.runCommand(`effect give ${event.target.uuid} minecraft:strength 180 2`);
    event.server.runCommand(`effect give ${event.target.uuid} minecraft:regeneration 180 2`);
    event.server.runCommand(`effect give ${event.target.uuid} minecraft:speed 180 2`);
    event.server.runCommand(`effect give ${event.target.uuid} minecraft:health_boost 180 2`);
})
whole mantleBOT
#

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

violet sluice
#

In most cases, there is no need to use runCommand. You have other better options.

normal pond
#

try this:

const buffs = {
    'minecraft:absorption': {duration: 180,amplifier: 2},
    'minecraft:strength': { duration: 180, amplifier: 2},
    'minecraft:regeneration': { duration: 180, amplifier: 2},
    'minecraft:speed': { duration: 180, amplifier: 2},
    'minecraft:health_boost': { duration: 180, amplifier: 2}
}
ItemEvents.entityInteracted('minecraft:wolf', event => {
    const { player, target, item, server } = event;
    if (item.id != 'kubejs:test_food') return;
    item.count--;
    player.giveInHand('minecraft:bone');
    target.playSound('entity.wolf.howl');
    target.tell('Yummy!!!')
    
    Object.values(buffs).forEach(buff => {
        target.potionEffects.add(buff, buff.duration, buff.amplifier)
    })
    
})```
sinful condor
#

thanks

sinful condor
#

wait
it didn't work

normal pond
#

what didnt?

tepid kernelBOT
#

You can find your KubeJS server log in /minecraft/logs/kubejs/server.log.
If you are on 1.18 or below it will be called server.txt.
Please send it if asked, as it contains helpful information.

normal pond
#

i assume it didn't output the potioneffects

#
Object.entries(buffs).forEach(([key, buff]) => {
    target.potionEffects.add(key, buff.duration, buff.amplifier)
})```
violet sluice
#
const buffs = {
  "minecraft:absorption": { duration: 180, amplifier: 2 },
  "minecraft:strength": { duration: 180, amplifier: 2 },
  "minecraft:regeneration": { duration: 180, amplifier: 2 },
  "minecraft:speed": { duration: 180, amplifier: 2 },
  "minecraft:health_boost": { duration: 180, amplifier: 2 },
};
ItemEvents.entityInteracted("minecraft:wolf", (event) => {
  const { player, target, item, server } = event;
  if (item.id !== "kubejs:test_food") return;

  item.count--;
  player.giveInHand("minecraft:bone");
  target.playSound("entity.wolf.howl");
  
  server.runCommandSilent(`execute as ${target.stringUuid} say YUMMY!!!`)

  Object.entries(buffs).forEach(([buff, data]) => {
    target.potionEffects.add(buff, data.duration, data.amplifier);
  });
});
normal pond
#

oh; yea, entity.tell would only tell it to the entity eDerpy
wouldn't a player.tell suffice? as i assume you don't need to output the yummy to all players
if you do, you could also use
server.tell

public void tell(Component message) {
    getMinecraftServer().sendMessage(message, Util.NIL_UUID);

    for (var player : getMinecraftServer().getPlayerList().getPlayers()) {
        player.sendMessage(message, Util.NIL_UUID);
    }
}```
sinful condor
#

idk how
but i can feed this to creeper and even ender dragon

#
const buffs = {
    "minecraft:absorption": { duration: 180, amplifier: 2 },
    "minecraft:strength": { duration: 180, amplifier: 2 },
    "minecraft:regeneration": { duration: 180, amplifier: 2 },
    "minecraft:speed": { duration: 180, amplifier: 2 },
    "minecraft:health_boost": { duration: 180, amplifier: 2 },
};
ItemEvents.entityInteracted("minecraft:wolf", (event) => {
    const { player, target, item, server } = event;
    if (item.id !== "kubejs:test_food") return;
    console.log(target.name)
    item.count--;
    player.giveInHand("minecraft:bone");
    target.playSound("entity.wolf.howl");

    server.runCommandSilent(`execute as ${target.uuid} run say YUMMY!!!`)

    Object.entries(buffs).forEach(([buff, data]) => {
        target.potionEffects.add(buff, data.duration, data.amplifier);
    });
});
#

log:

tepid kernelBOT
#

Paste version of message.txt from @sinful condor

normal pond
#

just add an entity check in your script... seems that adding it as an event argument doesn't work

if(target.type != 'minecraft:wolf') return

frigid garnet
#

because its an item event that filter should be for items

#

so its weird that its still triggerivg

sinful condor
#

so what's the correct code?
tell me plz

sinful condor
#

.

#

there is still one problem