| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.HolderSet;
|
| 10 | import net.minecraft.core.registries.Registries;
|
| 11 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 12 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 13 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.sounds.SoundEvent;
|
| 17 | import net.minecraft.stats.Stats;
|
| 18 | import net.minecraft.util.ExtraCodecs;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.world.InteractionHand;
|
| 21 | import net.minecraft.world.damagesource.DamageSource;
|
| 22 | import net.minecraft.world.damagesource.DamageType;
|
| 23 | import net.minecraft.world.entity.LivingEntity;
|
| 24 | import net.minecraft.world.entity.player.Player;
|
| 25 | import net.minecraft.world.item.ItemStack;
|
| 26 | import net.minecraft.world.level.Level;
|
| 27 |
|
| 28 | public record BlocksAttacks(
|
| 29 | float blockDelaySeconds,
|
| 30 | float disableCooldownScale,
|
| 31 | List<BlocksAttacks.DamageReduction> damageReductions,
|
| 32 | BlocksAttacks.ItemDamageFunction itemDamage,
|
| 33 | Optional<HolderSet<DamageType>> bypassedBy,
|
| 34 | Optional<Holder<SoundEvent>> blockSound,
|
| 35 | Optional<Holder<SoundEvent>> disableSound
|
| 36 | ) {
|
| 37 | public static final Codec<BlocksAttacks> CODEC = RecordCodecBuilder.create(
|
| 38 | i -> i.group(
|
| 39 | ExtraCodecs.NON_NEGATIVE_FLOAT.optionalFieldOf("block_delay_seconds", 0.0F).forGetter(BlocksAttacks::blockDelaySeconds),
|
| 40 | ExtraCodecs.NON_NEGATIVE_FLOAT.optionalFieldOf("disable_cooldown_scale", 1.0F).forGetter(BlocksAttacks::disableCooldownScale),
|
| 41 | BlocksAttacks.DamageReduction.CODEC
|
| 42 | .listOf()
|
| 43 | .optionalFieldOf("damage_reductions", List.of(new BlocksAttacks.DamageReduction(90.0F, Optional.empty(), 0.0F, 1.0F)))
|
| 44 | .forGetter(BlocksAttacks::damageReductions),
|
| 45 | BlocksAttacks.ItemDamageFunction.CODEC.optionalFieldOf("item_damage", BlocksAttacks.ItemDamageFunction.DEFAULT).forGetter(BlocksAttacks::itemDamage),
|
| 46 | RegistryCodecs.holderSet(Registries.DAMAGE_TYPE).optionalFieldOf("bypassed_by").forGetter(BlocksAttacks::bypassedBy),
|
| 47 | SoundEvent.CODEC.optionalFieldOf("block_sound").forGetter(BlocksAttacks::blockSound),
|
| 48 | SoundEvent.CODEC.optionalFieldOf("disabled_sound").forGetter(BlocksAttacks::disableSound)
|
| 49 | )
|
| 50 | .apply(i, BlocksAttacks::new)
|
| 51 | );
|
| 52 | public static final StreamCodec<RegistryFriendlyByteBuf, BlocksAttacks> STREAM_CODEC = StreamCodec.composite(
|
| 53 | ByteBufCodecs.FLOAT,
|
| 54 | BlocksAttacks::blockDelaySeconds,
|
| 55 | ByteBufCodecs.FLOAT,
|
| 56 | BlocksAttacks::disableCooldownScale,
|
| 57 | BlocksAttacks.DamageReduction.STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 58 | BlocksAttacks::damageReductions,
|
| 59 | BlocksAttacks.ItemDamageFunction.STREAM_CODEC,
|
| 60 | BlocksAttacks::itemDamage,
|
| 61 | ByteBufCodecs.holderSet(Registries.DAMAGE_TYPE).apply(ByteBufCodecs::optional),
|
| 62 | BlocksAttacks::bypassedBy,
|
| 63 | SoundEvent.STREAM_CODEC.apply(ByteBufCodecs::optional),
|
| 64 | BlocksAttacks::blockSound,
|
| 65 | SoundEvent.STREAM_CODEC.apply(ByteBufCodecs::optional),
|
| 66 | BlocksAttacks::disableSound,
|
| 67 | BlocksAttacks::new
|
| 68 | );
|
| 69 |
|
| 70 | public void onBlocked(final ServerLevel level, final LivingEntity user) {
|
| 71 | this.blockSound
|
| 72 | .ifPresent(
|
| 73 | sound -> level.playSound(
|
| 74 | null, user.getX(), user.getY(), user.getZ(), (Holder<SoundEvent>)sound, user.getSoundSource(), 1.0F, 0.8F + level.getRandom().nextFloat() * 0.4F
|
| 75 | )
|
| 76 | );
|
| 77 | }
|
| 78 |
|
| 79 | public void disable(final ServerLevel level, final LivingEntity user, final float baseSeconds, final ItemStack blockingWith) {
|
| 80 | int cooldownTicks = this.disableBlockingForTicks(baseSeconds);
|
| 81 | if (cooldownTicks > 0) {
|
| 82 | if (user instanceof Player player) {
|
| 83 | player.getCooldowns().addCooldown(blockingWith, cooldownTicks);
|
| 84 | }
|
| 85 |
|
| 86 | user.stopUsingItem();
|
| 87 | this.disableSound
|
| 88 | .ifPresent(
|
| 89 | sound -> level.playSound(
|
| 90 | null, user.getX(), user.getY(), user.getZ(), (Holder<SoundEvent>)sound, user.getSoundSource(), 0.8F, 0.8F + level.getRandom().nextFloat() * 0.4F
|
| 91 | )
|
| 92 | );
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public void hurtBlockingItem(final Level level, final ItemStack item, final LivingEntity user, final InteractionHand hand, final float damage) {
|
| 97 | if (user instanceof Player player) {
|
| 98 | if (!level.isClientSide()) {
|
| 99 | player.awardStat(Stats.ITEM_USED.get(item.getItem()));
|
| 100 | }
|
| 101 |
|
| 102 | int itemDamage = this.itemDamage.apply(damage);
|
| 103 | if (itemDamage > 0) {
|
| 104 | item.hurtAndBreak(itemDamage, user, hand.asEquipmentSlot());
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | private int disableBlockingForTicks(final float baseSeconds) {
|
| 110 | float seconds = baseSeconds * this.disableCooldownScale;
|
| 111 | return seconds > 0.0F ? Math.round(seconds * 20.0F) : 0;
|
| 112 | }
|
| 113 |
|
| 114 | public int blockDelayTicks() {
|
| 115 | return Math.round(this.blockDelaySeconds * 20.0F);
|
| 116 | }
|
| 117 |
|
| 118 | public float resolveBlockedDamage(final DamageSource source, final float dealtDamage, final double angle) {
|
| 119 | float blockedDamage = 0.0F;
|
| 120 |
|
| 121 | for (BlocksAttacks.DamageReduction reduction : this.damageReductions) {
|
| 122 | blockedDamage += reduction.resolve(source, dealtDamage, angle);
|
| 123 | }
|
| 124 |
|
| 125 | return Mth.clamp(blockedDamage, 0.0F, dealtDamage);
|
| 126 | }
|
| 127 |
|
| 128 | public record DamageReduction(float horizontalBlockingAngle, Optional<HolderSet<DamageType>> type, float base, float factor) {
|
| 129 | public static final Codec<BlocksAttacks.DamageReduction> CODEC = RecordCodecBuilder.create(
|
| 130 | i -> i.group(
|
| 131 | ExtraCodecs.POSITIVE_FLOAT.optionalFieldOf("horizontal_blocking_angle", 90.0F).forGetter(BlocksAttacks.DamageReduction::horizontalBlockingAngle),
|
| 132 | RegistryCodecs.holderSet(Registries.DAMAGE_TYPE).optionalFieldOf("type").forGetter(BlocksAttacks.DamageReduction::type),
|
| 133 | Codec.FLOAT.fieldOf("base").forGetter(BlocksAttacks.DamageReduction::base),
|
| 134 | Codec.FLOAT.fieldOf("factor").forGetter(BlocksAttacks.DamageReduction::factor)
|
| 135 | )
|
| 136 | .apply(i, BlocksAttacks.DamageReduction::new)
|
| 137 | );
|
| 138 | public static final StreamCodec<RegistryFriendlyByteBuf, BlocksAttacks.DamageReduction> STREAM_CODEC = StreamCodec.composite(
|
| 139 | ByteBufCodecs.FLOAT,
|
| 140 | BlocksAttacks.DamageReduction::horizontalBlockingAngle,
|
| 141 | ByteBufCodecs.holderSet(Registries.DAMAGE_TYPE).apply(ByteBufCodecs::optional),
|
| 142 | BlocksAttacks.DamageReduction::type,
|
| 143 | ByteBufCodecs.FLOAT,
|
| 144 | BlocksAttacks.DamageReduction::base,
|
| 145 | ByteBufCodecs.FLOAT,
|
| 146 | BlocksAttacks.DamageReduction::factor,
|
| 147 | BlocksAttacks.DamageReduction::new
|
| 148 | );
|
| 149 |
|
| 150 | public float resolve(final DamageSource source, final float dealtDamage, final double angle) {
|
| 151 | if (angle > (float) (Math.PI / 180.0) * this.horizontalBlockingAngle) {
|
| 152 | return 0.0F;
|
| 153 | } else {
|
| 154 | return this.type.isPresent() && !this.type.get().contains(source.typeHolder()) ? 0.0F : Mth.clamp(this.base + this.factor * dealtDamage, 0.0F, dealtDamage);
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | public record ItemDamageFunction(float threshold, float base, float factor) {
|
| 160 | public static final Codec<BlocksAttacks.ItemDamageFunction> CODEC = RecordCodecBuilder.create(
|
| 161 | i -> i.group(
|
| 162 | ExtraCodecs.NON_NEGATIVE_FLOAT.fieldOf("threshold").forGetter(BlocksAttacks.ItemDamageFunction::threshold),
|
| 163 | Codec.FLOAT.fieldOf("base").forGetter(BlocksAttacks.ItemDamageFunction::base),
|
| 164 | Codec.FLOAT.fieldOf("factor").forGetter(BlocksAttacks.ItemDamageFunction::factor)
|
| 165 | )
|
| 166 | .apply(i, BlocksAttacks.ItemDamageFunction::new)
|
| 167 | );
|
| 168 | public static final StreamCodec<ByteBuf, BlocksAttacks.ItemDamageFunction> STREAM_CODEC = StreamCodec.composite(
|
| 169 | ByteBufCodecs.FLOAT,
|
| 170 | BlocksAttacks.ItemDamageFunction::threshold,
|
| 171 | ByteBufCodecs.FLOAT,
|
| 172 | BlocksAttacks.ItemDamageFunction::base,
|
| 173 | ByteBufCodecs.FLOAT,
|
| 174 | BlocksAttacks.ItemDamageFunction::factor,
|
| 175 | BlocksAttacks.ItemDamageFunction::new
|
| 176 | );
|
| 177 | public static final BlocksAttacks.ItemDamageFunction DEFAULT = new BlocksAttacks.ItemDamageFunction(1.0F, 0.0F, 1.0F);
|
| 178 |
|
| 179 | public int apply(final float dealtDamage) {
|
| 180 | return dealtDamage < this.threshold ? 0 : Mth.floor(this.base + this.factor * dealtDamage);
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|