#How can I use EntityEvents.hurt to do additional damage to entities attacked by the player?

26 messages · Page 1 of 1 (latest)

whole magnet
#

I would like to give random extra damage when a player attacks with a certain weapon, but my code doesn't work.
It does damage but no experience drop and no knockback. How can I make the attack be by the player?

EntityEvents.hurt(event => {
    if (event.source.player && event.source.player.mainHandItem.id === 'custom_weapon'){
        event.entity.attack(event.damage + Math.floor(Math.random() * 5))
    }
})
analog sandalBOT
#

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

rustic quest
#

.attack(event.player, event.damage)

whole magnet
#

I tried to modify the code, but it seems that the attack() method for event.entity, which is a LivingEntity, only accepts one argument. The attack() method for Entity seems to accept DamageSource as the first argument.

Is there any way to solve this? Thank you.

event.entity.attack(event.source.player, event.damage + Math.floor(Math.random() * 5))
[22:09:16] [Server thread/INFO] [KubeJS Server/]: weapon.js#1: Loaded Java class 'net.minecraft.world.damagesource.DamageSource'
[22:09:16] [Server thread/ERROR] [KubeJS Server/]: weapon.js#2: Java constructor for "net.minecraft.world.damagesource.DamageSource" with arguments "dev.latvian.mods.rhino.Undefined" not found.
[22:09:16] [Server thread/ERROR] [KubeJS Server/]: …rhino.EvaluatorException: Java constructor for "net.minecraft.world.damagesource.DamageSource" with arguments "…rhino.Undefined" not found. (server_scripts:weapon.js#2)
mental sequoiaBOT
#

Paste version of message.txt from @whole magnet

rustic quest
#
const $DamageSource = Java.loadClass(`net.minecraft.world.damagesource.DamageSource`)
const addDamage = new $DamageSource('addDamage')
function extraDmg(attacker, target, amount) {
    target.attack(addDamage.mobAttack(attacker), amount);
}
let executed = false;
EntityEvents.hurt(e => {
    if(executed) return;
    executed = true;
    extraDmg(e.source.actual, e.entity, 100);
    executed = false;
})
whole magnet
#

Thank you for the quick response. I apologize for my lack of understanding as I am a beginner.
What exactly should I specify for new $DamageSource()?

const addDamage = new $DamageSource(addDamage)
rustic quest
#

nope, i already gave an example

#

extraDmg(e.source.actual, e.entity, 100);

#

attacker, target, damage amount

whole magnet
#

I was wondering about the line const addDamage = new $DamageSource(addDamage). I'm not quite sure what should go inside new $DamageSource(). Could you give me a bit more guidance on that?

[22:09:16] [Server thread/INFO] [KubeJS Server/]: weapon.js#1: Loaded Java class 'net.minecraft.world.damagesource.DamageSource'
[22:09:16] [Server thread/ERROR] [KubeJS Server/]: weapon.js#2: Java constructor for "net.minecraft.world.damagesource.DamageSource" with arguments "dev.latvian.mods.rhino.Undefined" not found.
[22:09:16] [Server thread/ERROR] [KubeJS Server/]: …rhino.EvaluatorException: Java constructor for "net.minecraft.world.damagesource.DamageSource" with arguments "…rhino.Undefined" not found. (server_scripts:weapon.js#2)
rustic quest
#

that's weird, how did i get these working

rustic quest
#

wait, dang, it should be a string

whole magnet
#

I tried using a string, but it seems like it doesn't accept a string as an argument. It looks like I need to use Internal.Holder_<Internal.DamageType>. Could you help me with how to specify this correctly?

mental sequoiaBOT
#

[➤](#1169390879308529784 message)
Startup Script:

const $DamageSource = Java.loadClass(`net.minecraft.world.damagesource.DamageSource`)

/** Register effects */
StartupEvents.registry('mob_effect', event => {
    // Register radiation effect
    event.create('radiation')
        // Change the name to be "Radiation", in green
        .displayName(Component.green("Radiation"))
        // Set a tick event to apply the action
        .effectTick((entity, lvl) => global.radiationEffect(entity, lvl))
        // Set the color of the effect
        .color(Color.GREEN)
        // Set whether the effect is harmful
        .harmful();
})

/**
 * Applies the radiation effect.
 * Damages the entity (likely player) with 2 HP (1 hearts) per 10 ticks.
 *
 * @param {Internal.Entity} entity The entity to apply the effect to
 * @param {number} lvl The level of the effect
 */
global.radiationEffect = (entity, lvl) => {
    // Create damage source
    let damagesource = new $DamageSource('radiation')
    // Check if the global is run on the client. If so, return
    if (entity.level.clientSide) return;
    // Damage based on level 
    entity.attack(damagesource, lvl + 1);
}```
Client Script:
```js
/** Add language entries */
ClientEvents.lang('en_us', (e) => {
    // Effect / Radiation
    e.add("death.attack.radiation", "%1$s died in a horrible way");
})```

You can add an icon to go along with the effect by just dropping an 18px by 18px image over in ``kubejs/assets/kubejs/mob_effect/radiation.png`` - words of wisdom by @quick zealot 
Credit to @sharp knoll and @mental crane

@median otter 's 1.18 version here in: #1170471805148999751 message

@midnight pine 's 1.20 Successful Port here:
#1183085911974608977 message
rustic quest
#

i think i got where the issue is

#

i'm using 1.19

mental sequoiaBOT
#

[➤](#1183085911974608977 message)
1.20 Radiation Port
Startup Script:js StartupEvents.registry('mob_effect', event => { // Register radiation effect event.create('radiation') // Change the name to be "Radiation", in green .displayName("Radiation") // Set a tick event to apply the action .effectTick((entity, lvl) => global.radiationEffect(entity, lvl)) // Set the color of the effect .color(Color.GREEN) // Set whether the effect is harmful .harmful(); }) const $ResourceKey = Java.loadClass("net.minecraft.resources.ResourceKey") const DAMAGE_TYPE = $ResourceKey.createRegistryKey("damage_type") global.radiationEffect = (entity, lvl) => { // Create damage source const damageSource = getDamageSource(entity.level(), "kubejs:radiation"); // Check if the global is run on the client. If so, return if (entity.level().clientSide) return; // Damage based on level entity.attack(damageSource, (lvl - 0.2) + 1); } function getDamageSource(/** @type {Internal.Level}*/ level, /** @type {Internal.DamageType_}*/ damageType) { const resourceKey = $ResourceKey.create(DAMAGE_TYPE, Utils.id(damageType)) const holder = level.registryAccess().registryOrThrow(DAMAGE_TYPE).getHolderOrThrow(resourceKey) return new DamageSource(holder) }
Server Script:js // Register Damage Type ServerEvents.highPriorityData(event => { event.addJson("kubejs:damage_type/radiation", { "effects": "hurt", "exhaustion": 0.5, "message_id": "radiationKill", "scaling": "when_caused_by_living_non_player" }) }) EntityEvents.hurt(event => { if (!(event.entity.age % 10 == 0)) return if (event.source.getType().toString() == 'radiationKill') { event.entity.invulnerableTime = 1 } })
Client Script:js /** Add language entries */ ClientEvents.lang('en_us', (e) => { // Effect / Radiation e.add("death.attack.radiationKill", "%1$s died in a horrible way"); })

rustic quest
#

this should be able to register a new damage type

mental sequoiaBOT
#

[➤](#1246003822510346290 message)
btw if you do end up switching to forge then this would be the forge event startup script ```js
ForgeEvents.onEvent('net.minecraftforge.event.entity.living.LivingHurtEvent', event => {
global.livinghurt(event)
})
/**
*

  • @param {Internal.LivingHurtEvent} event
    */
    global.livinghurt = event => {
    const { amount, source: { actual }, source, entity } = event
    try {
    if (!['arrow', 'trident', 'fireball', 'shulker_bullet'].includes(source.getType())) return;
    event.setAmount(amount - 3)
    } catch (error) {
    console.log(error)
    }
    }```
rustic quest
#

change the damage directly

whole magnet
#

I was able to solve the problem by the method you taught me!
I appreciate your kind help and support!
This is the final code. Thank you so much.

ForgeEvents.onEvent('net.minecraftforge.event.entity.living.LivingHurtEvent', event => {
    const { amount, source: { player } } = event
    if (!player) return
    if (player && player.mainHandItem.id === 'custom_weapon') {
        event.setAmount(amount + Math.floor(Math.random() * 10))
    }
})