| 1 | package net.minecraft.world.item.enchantment.effects;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.entity.LivingEntity;
|
| 10 | import net.minecraft.world.item.enchantment.EnchantedItemInUse;
|
| 11 | import net.minecraft.world.item.enchantment.LevelBasedValue;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.joml.Quaternionf;
|
| 14 | import org.joml.Vector3d;
|
| 15 |
|
| 16 | public record ApplyEntityImpulse(Vec3 direction, Vec3 coordinateScale, LevelBasedValue magnitude) implements EnchantmentEntityEffect {
|
| 17 | public static final MapCodec<ApplyEntityImpulse> CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | Vec3.CODEC.fieldOf("direction").forGetter(ApplyEntityImpulse::direction),
|
| 20 | Vec3.CODEC.fieldOf("coordinate_scale").forGetter(ApplyEntityImpulse::coordinateScale),
|
| 21 | LevelBasedValue.CODEC.fieldOf("magnitude").forGetter(ApplyEntityImpulse::magnitude)
|
| 22 | )
|
| 23 | .apply(i, ApplyEntityImpulse::new)
|
| 24 | );
|
| 25 | private static final int POST_IMPULSE_CONTEXT_RESET_GRACE_TIME_TICKS = 10;
|
| 26 |
|
| 27 | @Override
|
| 28 | public void apply(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity, final Vec3 position) {
|
| 29 | Quaternionf look = entity.getLookQuaternion();
|
| 30 | Vector3d direction = look.transform(this.direction.x, this.direction.y, this.direction.z, new Vector3d())
|
| 31 | .mul(this.coordinateScale.x, this.coordinateScale.y, this.coordinateScale.z)
|
| 32 | .mul(this.magnitude.calculate(enchantmentLevel));
|
| 33 | entity.addDeltaMovement(new Vec3(direction.x, direction.y, direction.z));
|
| 34 | if (entity instanceof ServerPlayer player) {
|
| 35 | player.connection.send(new ClientboundSetEntityMotionPacket(entity));
|
| 36 | entity.syncVelocity = false;
|
| 37 | } else {
|
| 38 | entity.syncVelocity = true;
|
| 39 | }
|
| 40 |
|
| 41 | if (entity instanceof LivingEntity livingEntity) {
|
| 42 | livingEntity.applyPostImpulseGraceTime(10);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public MapCodec<ApplyEntityImpulse> codec() {
|
| 48 | return CODEC;
|
| 49 | }
|
| 50 | }
|
| 51 |
|