[1.18.2] How to make special bonuses with physical damage events?[KJS] Kubejs Minecraft Game

Foreword

This instance is forge1.18.2, and such magic transformation scripts should be placed in Kubejs/Server_Scripts.

I have only been in contact with JS recently.

text

At first, it was just because I felt that TAC's firearms were particularly weak when they were deified, so I thought that the damage to the gun can also cause the bleeding effect of deity. Driven by such an idea, I wrote a set of effects to the BA halo.Essence

 Onevent ('Entity.HURT', Event => {

If (event.source.actual && event.Isliving () &&SOURCE.ACTual.isplayer () &&SOURCE.TYPE == 'Bullet.isplayer () RCE.Actual.Headarmoritem === 'yuushya: Wriggle_nightBug '&& Event.source.Actual.Headarmoritem.GetNbt ()) {

Let {Entity, Source} = Event;

Let action = source.actual;

Let health = math.floor (entity.gethealth ());

Let maxhealth = math.floor (entity.getmaxhealth ());

Let cmdata = actual.headarmoritem.getnbt (). CustomModeldata;

If (cmdata === 11821901) {// armor -breaking

Let sundrance = Entity.PotionEffects.getActive ('Apotheosis: Sundering');

If (Sundering == NULL) {

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Apotheosis: Sundering 15`);

} Else {let sua = Sundering.amplifier+1;

Let SUD = Sundering.duration/20;

Let Sutime = Math.round (SUD+1);

Let sulevel = math.min (sua, 250);

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Apotheosis: Sundering $ {Sutime} $ {Sulevel}`);

}

Let Stringth = Actual.PotionEffects.getActive ('Minecraft: Streangth');

If (Stringth! = Null) {

Let dx = entity.x-actual.x;

Let Dy = (Entity.y+Entity.eyehead)-(Actual.y+Actual.eyehead);

Let dz = Entity.z-actual.z;

Let yaw = math.atan2 (dx, dz);

Let yawdegrees =-(yaw*(180/3.14159)). Tofixed (2);

Let distance = math.sqrt (dx*dx+dz*dz); let pitch = math.atan2 (dy, distance);

Let pitchdegrees =-(pitch*(180/3.14159)). Tofixed (2);

Actual.Setrotation (yawdegrees, pitchdegrees);

}

}

If (cmdata === 11821902) {// Lightning

If (Health%5 === 0 || Health%7 === 0) {{

Event.server.scheduleInticks (1, () => {{

Event.server.runcommandsilent (`Execute in $ {Event.Level.dimension} Run Summon Minecraft: Lightning_bolt $ {Entity.x} $ {Entity.y} $ {Entity.z} `);

Entity.attack (Event.source, event.damage*1.5);

})

}

}

If (cmdata === 11821903) {// explosion

Event.server.runcommandsilent (`Execute in $ {Event.Level.dimension} Run Summon Minecraft: TNT $ {Entity.x} $ {Entity.y+1} $ {ENTI {ENTI ty.z} `);}

If (cmdata === 11821904) {// Take off

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Minecraft: Levity 1 100`);

}

If (cmdata === 11821905) {// shield

Let absorption = actual.PotionEffects.getActive ('Minecraft: Absorption');

If (absorption == null) {

Event.server.runcommandsilent (`Effect Give $ {Actual} Minecraft: Absorption 5`);

Event.server.runcommandsilent (`Effect Give $ {Actual} Minecraft: Resistance 5 1`);

} Else {

Let aba = absorption.amplifier+1;

Let abtime = aba*5-5;

Let farvel = math.min (aba, 4);

event.server.runcommandsilent (`Effect Give $ {Actual} Minecraft: Absorption $ {5+Abtime} $ {0+AbleVEL}`); Andsilent (`Effect Give $ {Actual} Minecraft: Resistance $ {5+Abtime} 1`);

}

}

If (cmdata === 11821906) {// Treatment

If (actual. crouching) {

Event.server.runcommandsilent (`Execute at $ {Actual} Run Effect Give @e [Type =! ITEM, Distance = .. 5] Minecraft: Instant_health`)

Event.server.scheduleInticks (1, () => {{

For (let i = 0; i <180; i ++) {

Let x = actual.x+5*math.cos (i);

Let Z = Actual.z+5*Math.sin (i);

Let Y = Actual.y;

Event.server.runcommandsilent (`Execute in $ {Event.Level.dimension} Run PARTICLE DUST 0 255 0 1 $ {x} $ {y+0.1} $ {z} 0 0 0.01 1`);}

})

} Else {

Actual.heal (event.damage/2);

}

}

If (cmdata === 11821907) {// exchange

Let x1 = actual.x;

Let y1 = actual.y;

Let Z1 = Actual.z;

Let yaw1 = actual.getyaw ();

Let pitch1 = actual.getpitch ();

Let x2 = Entity.x;

Let Y2 = Entity.y;

Let Z2 = Entity.z;

Let yaw2 = Entity.getyaw ();

Let pitch2 = Entity.getpitch ();

Event.server.runcommandsilent (`Effect Give $ {Actual} Minecraft: Resistance 1 4`) event.server.runcommandsilent (`Effect Give $ {Entity .id} Minecraft: Resistance 1 4`)

Event.server.scheduleInticks (1, () => {{

Entity.PlaySound ('Minecraft: Entity.enderman.teleport')

Entity.setpositionandrotation (X1, Y1, Z1, YAW1, Pitch1)

})

Event.server.scheduleInticks (2, () => {

Actual.PlaySound ('Minecraft: Entity.nderman.teleport')

Actual.Setpositionandrotation (X2, Y2, Z2, YAW2, Pitch2)

})

}

If (cmdata === 11821908) {// tetanus

Let damage = event.damage;

Let mindamage = math.min (damage*5, health-damage-1);

If (Health> = Maxhealth*0.9) {{

Event.server.scheduleInticks (1, () => {{

Entity.attack (Event.source, Mindamage);})

}

}

If (cmdata === 11821909) {// Kill

If (Health <= Maxhealth/3) {{

Event.server.scheduleInticks (1, () => {{

Entity.kill ();

})

}

}

If (cmdata === 11821910) {// range

Let EntityList = Event.Level.GetentIeswithin (aabb.of (

Entity.x-3,

Entity.y-1,

Entity.z-3,

Entity.x+3,

Entity.y+3,

Entity.z+3

))

For (Let Entities of EntityList) {

ENTITIES.ATTACK (event.source, event.damage/2);}

}

If (cmdata === 11821911) {// bleeding

Let bleeding = Entity.PotionEffects.getActive ('Apotheosis: Bleeding');

If (bleeding == null) {

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Apotheosis: Bleeding 15`);

} Else {

Let Bla = Bleeding.amplifier+1;

Let bld = bleeding.duration/20;

Let bltime = math.round (bld+1);

Let bllevel = math.min (BLA, 250);

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Apotheosis: Bleeding $ {bltime} $ {0+bllevel}`);

}

}

If (cmdata === 11821912) {// Gambler

Let random = () => Utils.random.nextint (6); // 0-5 Let Randomlist = [Random (), RANDOM ()]]

Event.server.scheduleInticks (1, () => {{

If (rightomlist [0] === 0) {{

If (rightomlist [1] === 0 || Randomlist [1] === 2 || Randomlist [1] === 4) {

Entity.attack (Event.source, event.damage*19);

} Else {

Actual.attack (Event.source, Event.damage*19);

}

}

})

}

If (cmdata === 11821913) {// weak

Let Weakness = Entity.PotionEffects.GetActive ('Minecraft: Weakness');

If (weakness == null) {

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Minecraft: Weakness 15`);} Else {

Let wea = weakness.amplifier+1;

Let Wed = Weakness.Duration/20;

Let wetime = math.round (wed+15);

Let welevel = math.min (wea, 4);

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Minecraft: Weakness $ {Wetime} $ {0+Welevel}`);

}

}

}

})

explain

Obviously, there must be a lot of people who can't understand all all at once, so I will explain one by one. First of all, if you want to run directly, you need to install KJS, TAC, Divine, leisurely, small squares, small squares, small cubes,Town, thermal series.

Among them, Divine Chemical and Youran used the potion effect. The town of the square used a hat model, and the thermal series used a nuclear bomb. You may want to ask, since it is written for the effect of BA halo, why is there no BA halo?That's because the method of obtaining the BA halo is hard -coded, and there is no magic change. If I want to modify the halo formula, I can only find another way, so!I used the original custom model DATA, that is, the data packet, which can also wear the item model on the head (that is, the hat model of the town of the block).The GEO model really has no way).

Determine the source of damage

In Entity.HURT events, using IF to determine whether the player has special conditions.

 if (event.source.actual && event.Isliving () &&SOURCE.ACTual.isplayer () &&Source.Typ e === 'Bullet' && Event.source.actual.headarmoritem === 'yuushya: wriggle_nightbug' " l Judging whether the source of damage is directly damaged (this must be added,Otherwise, the error will be reported), such as the skull archery hit the player, the source of the damage is the arrow, and the direct damage source is the skeleton.

Event.entity.isliving () determine whether the injured entity is a living thing.

Event.source.actual.isplayer () determine whether the direct damage source is a player.

Event.source.type === 'Bullet' determine whether the type of damage source is 'Bullet' (that is, bullet damage).

Event.source.Actual.Headarmoritem === 'yuushya: wriggle_nightbug' to determine whether the helmet that direct damage source is definitely equal to 'yuushya: wriggle_nightbug'.

EVENT.Source.Actual.Headarmoritem.getNbt () determine whether the helmet that direct damage source has NBT (this must be added, otherwise an error is reported).

definition

Some functions that are reused later can be defined in advance to define common functions to simplify writing.

 Let {Entity, Source} = Event;

Let action = source.actual;

Let health = math.floor (entity.gethealth ());

Let maxhealth = math.floor (entity.getmaxhealth ());

Let cmdata = actual.headarmoritem.getnbt (). CustomModeldata;

These are relatively easy to understand. Among them, CustomModeldata is a custom model ID. If you use it for specific use, please see CustomModeldata.

Here I have made a total of 15 custom models, the ID is 11821901-11821915, and different ID corresponds to different items models.

 if (cmdata === 11821901) 

Take the first as an example. If the CustomModeldata column in the NBT NBT is detected, it is definitely equal to this number, then True.

Get the effect of physical potion

 Let Sundering = Entity.PotionEffects.getActive ('APOTHEOSIS: Sundering'); // Determine whether the potion effect on the injured entity is' Apotheosis : Sundering '(armored).If (Sundering == NULL) {// If the injured entity does not have armor -breaking effects.

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Apotheosis: Sundering 15`); // Give a 15 -second effect of the injured entity.

} Else {// If you already have armor -breaking effects.

Let sua = Sundering.amplifier+1; // Get the effect level +1.

Let SUD = Sundering.duration/20; // Get the effect time (the potion is TICK value, the game tick is 20 per second, so it is divided by 20 here).

Let Sutime = Math.round (SUD+1); // The effect of extending 1 second of seconds.

Let sulevel = math.min (sua, 250); // Set a maximum value and cannot exceed 250 levels.

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Apotheosis: Sundering $ {Sutime} $ {Sulevel}); // The bleeding effect of the injured entity is extended for 1 second and increased by 1.

}

Lock effect

 let Streangth = Actual.PotionEffects.getActive ('Minecraft: Streangth'); ngth '(strength).

If (stringth! = Null) {// If the direct damage source has the effect of power.Let dx = Entity.x-actual.x; // Get the X difference between the injured entity and the direct injury source.

Let Dy = (Entity.y+Entity.eyehead)-(Actual.y+Actual.eyehead); // The height difference between the vision of the injured entity and the direct injury source.

Let dz = Entity.z-Actual.z; // Get the Z difference between the injured entity and the direct injury source.

// Calculate yaw value

Let yaw = math.atan2 (dx, dz);

Let yawdegrees =-(yaw*(180/3.14159)). Tofixed (2);

// Calculate the pitch value

Let distance = math.sqrt (dx*dx+dz*dz);

Let pitch = math.atan2 (dy, distance);

Let pitchdegrees =-(pitch*(180/3.14159)). Tofixed (2);

Actual.setRotation (yawdegrees, pitchdegrees); // Set the vision value of the direct damage source (yaw value and pitch value).

}

Fake random effect

 if (health%5 === 0 || health%7 === 0) {// Detect the blood volume of the injured entity in 5 or 7, if there is no more number ofIt is true.Event.server.scheDuleinticks (1, () => {// Delay 1Tick execution.

event.server.runcommandsilent (`Execute in $ {Event.Level.Dimension} Run Summon Minecraft: Lightning_bolt $ {Entity.x} $ {Entity.y} $ {E {E {e ntity.z} `); // generatelightning.

Entity.attack (event.source, event.damage*1.5); // Give additional 1.5 times damage to the injured physical, and the type of damage is the source of damage.

})

}

Draw a circle

 if (actual. crouching) {// Detect whether the direct damage source is squatting state

event.server.runcommandsilent (`Execute at $ {actual} run effect give @e [type =! itm, distance = .. 5] minecraft: instant_health`); All entities except items are obtainedInstant treatment effect.

Event.server.scheDuleinticks (1, () => {// Delay 1Tick execution.

For (let i = 0; i <180; i ++) {

// Draw a circular round with 180 particle effects.

Let x = actual.x+5*math.cos (i); let z = actual.z+5*math.sin (i);

Let Y = Actual.y;

Event.server.runcommandsilent (`Execute in $ {Event.Level.dimension} Run PARTICLE DUST 0 255 0 1 $ {x} $ {y+0.1} $ {z} 0 0 0.01 1`));

}

})

} Else {// If not squat.

Actual.head (event.damage/2); // Obtain half of the damage and use it to treat direct damage sources.

}

Position interchange

 // Define the X, Y, Z value and yaw, pitch value of the direct damage source.

Let x1 = actual.x;

Let y1 = actual.y;

Let Z1 = Actual.z;

Let yaw1 = actual.getyaw ();

Let pitch1 = actual.getpitch ();

// Define the X, Y, Z value and yaw, pitch value of the injured entity.Let x2 = Entity.x;

Let Y2 = Entity.y;

Let Z2 = Entity.z;

Let yaw2 = Entity.getyaw ();

Let pitch2 = Entity.getpitch ();

// Give a 5 -second resistance of direct injury and injury entity 5 (100%free injury).

Event.server.runcommandsilent (`Effect Give $ {Actual} Minecraft: Resistance 1 4`)

Event.server.runcommandsilent (`Effect Give $ {Entity.id} Minecraft: Resistance 1 4`)

// The injured entity delays 1Tick to the position where the source of the direct injury is transmitted, and the sound of the last filmmaker is made.

Event.server.scheduleInticks (1, () => {{

Entity.PlaySound ('Minecraft: Entity.enderman.teleport')

Entity.setpositionandrotation (X1, Y1, Z1, YAW1, Pitch1)

})

// Direct damage source delay 2tick transmission to the position of the injured entity and make the sound of the last filmmaker.

Event.server.scheduleInticks (2, () => {

Actual.PlaySound ('Minecraft: Entity.nderman.teleport')

Actual.Setpositionandrotation (X2, Y2, Z2, YAW2, Pitch2)})

Scope damage

 Let EntityList = EVENT.LEVEL.GEENTITIESWITHIN (AABB.OF (// detect all the entities within a range.

Entity.x-3,

Entity.y-1,

Entity.z-3,

Entity.x+3,

Entity.y+3,

Entity.z+3

))

For (Let Entities of EntityList) {// traversed all entities.

ENTITIES.ATTACK (event.source, event.damage/2); // The additional harm is caused, and the damage type is the source of damage.

}

Russian turntable

 let random = () => utils.random.nextint (6); // Define the random number between 0-5 (that is, 6 random numbers).

Let randomlist = [random (), random ()] // random twice random times

Event.server.scheduleInticks (1, () => {{

If (RANDOMLIST [0] === 0) {// If the first random number is definitely equal to 0 (theoretically one -sixth probability).

if (rightomlist [1] === 0 || Randomlist [1] === 2 || Randomlist [1] === 4) {// If the second random number is definitely equal to 0, 2 or 4 (theoreticallyOne -half probability).Entity.attack (event.source, event.damage*19); // add 19 times damage to the injured entity, and the damage type is the source of damage.

} Else {// If the second random number is not 0, 2 or 4.

Actual.attack (event.source, event.damage*19); // adds an additional 19x damage to the direct damage source, and the type of damage is the source of damage.

}

}

})

DLC

This is not the Entity.HURT incident, and some other interesting small functions.

Spit seed

 Onevent ('item.food_eaten', event => {// Food edible event

// Test whether the player has special NBT items.

If (event.player && event.player.headarmoritem === 'yuushya: wriggle_nightbug' &&player.headarmoritem.Getnbt () && Event. Head Armoritem.getnbt (). CustomModeldata === 11821914) {

Let spitting = event.player.potionEffects.getActive ('Atmospheric: spitting'); // test whether the player has vomiting seed effects.

If (spitting == null) {// If there is no vomiting seed effect.

Event.server.runcommandsilent (`Effect Give $ {Event.player} atmospheric: spitting 5`); // Give players 5 seconds to spit seed effect.} Else {// If there is already a vomiting seed effect.

Let spa = spitting.amplifier+1; // Get the effect level +1.

Let spd = spitting.duration/20; // The effective time is divided by 20.

Let sptime = math.round (spd+2); // The effect is+2 seconds.

Let splevel = math.min (spa, 250); // The maximum effect level does not exceed 250.

event.server.runCommandSilent(`effect give ${event.player} atmospheric:spitting ${spTime} ${0 + spLevel}`); // 玩家的吐籽效果延长2秒并且增加1级。

}

}

})

Void annihilation

 Onevent ('Entity.death', event => {// physical death event

// Detect whether the entity is a player, and whether the player wears special NBT items.

If (event.entity.isplayer () && event.entity.headarmoritem === 'yuushya: wriggle_nightbug' &&Headarmoritem.getnbt () && EEVENT. entity.headarmoritem.getnbt (). CustomModeldata === 11821915){{

Event.entity.headarmoritem.count- = 1; // Remove the currently available items.

Event.server.runcommandsilent (`Execute at $ {Event.entity} Run Kill @E [Type =! Item, Distance = .. 18]); // Kill drops all the entities except the item with a radius of 18 grids.Event.server.scheduleInticks (1, () => {{

Event.server.runcommandsilent (`Execute in $ {Event.Level.Dimension} Run Summon thermal: Nuke_tnt $ {event.entity.x} $ {event.entity.y} $ { event.entity.z} `);//Delayed 1Tick to generate a nuclear bomb.

})

}

})