| 1 | package net.minecraft.world.item.enchantment;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectArraySet;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.HashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.Set;
|
| 14 | import net.minecraft.ChatFormatting;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.core.HolderSet;
|
| 17 | import net.minecraft.core.component.DataComponentMap;
|
| 18 | import net.minecraft.core.component.DataComponentType;
|
| 19 | import net.minecraft.core.registries.Registries;
|
| 20 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 21 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 22 | import net.minecraft.network.chat.CommonComponents;
|
| 23 | import net.minecraft.network.chat.Component;
|
| 24 | import net.minecraft.network.chat.ComponentSerialization;
|
| 25 | import net.minecraft.network.chat.ComponentUtils;
|
| 26 | import net.minecraft.network.chat.MutableComponent;
|
| 27 | import net.minecraft.network.chat.Style;
|
| 28 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 29 | import net.minecraft.network.codec.StreamCodec;
|
| 30 | import net.minecraft.resources.Identifier;
|
| 31 | import net.minecraft.server.level.ServerLevel;
|
| 32 | import net.minecraft.tags.EnchantmentTags;
|
| 33 | import net.minecraft.util.ExtraCodecs;
|
| 34 | import net.minecraft.util.RandomSource;
|
| 35 | import net.minecraft.util.Unit;
|
| 36 | import net.minecraft.util.Util;
|
| 37 | import net.minecraft.world.damagesource.DamageSource;
|
| 38 | import net.minecraft.world.entity.Entity;
|
| 39 | import net.minecraft.world.entity.EquipmentSlot;
|
| 40 | import net.minecraft.world.entity.EquipmentSlotGroup;
|
| 41 | import net.minecraft.world.entity.LivingEntity;
|
| 42 | import net.minecraft.world.item.Item;
|
| 43 | import net.minecraft.world.item.ItemInstance;
|
| 44 | import net.minecraft.world.item.ItemStack;
|
| 45 | import net.minecraft.world.item.enchantment.effects.DamageImmunity;
|
| 46 | import net.minecraft.world.item.enchantment.effects.EnchantmentAttributeEffect;
|
| 47 | import net.minecraft.world.item.enchantment.effects.EnchantmentEntityEffect;
|
| 48 | import net.minecraft.world.item.enchantment.effects.EnchantmentLocationBasedEffect;
|
| 49 | import net.minecraft.world.item.enchantment.effects.EnchantmentValueEffect;
|
| 50 | import net.minecraft.world.level.block.state.BlockState;
|
| 51 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 52 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 53 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 54 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 55 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 56 | import net.minecraft.world.phys.Vec3;
|
| 57 | import org.apache.commons.lang3.mutable.MutableFloat;
|
| 58 |
|
| 59 | /**
|
| 60 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 61 | */
|
| 62 | public record Enchantment(Component description, Enchantment.EnchantmentDefinition definition, HolderSet<Enchantment> exclusiveSet, DataComponentMap effects) {
|
| 63 | public static final int MAX_LEVEL = 255;
|
| 64 | public static final Codec<Enchantment> DIRECT_CODEC = RecordCodecBuilder.create(
|
| 65 | i -> i.group(
|
| 66 | ComponentSerialization.CODEC.fieldOf("description").forGetter(Enchantment::description),
|
| 67 | Enchantment.EnchantmentDefinition.CODEC.forGetter(Enchantment::definition),
|
| 68 | RegistryCodecs.holderSet(Registries.ENCHANTMENT).optionalFieldOf("exclusive_set", HolderSet.empty()).forGetter(Enchantment::exclusiveSet),
|
| 69 | EnchantmentEffectComponents.CODEC.optionalFieldOf("effects", DataComponentMap.EMPTY).forGetter(Enchantment::effects)
|
| 70 | )
|
| 71 | .apply(i, Enchantment::new)
|
| 72 | );
|
| 73 | public static final Codec<Holder<Enchantment>> CODEC = RegistryCodecs.holder(Registries.ENCHANTMENT);
|
| 74 | public static final StreamCodec<RegistryFriendlyByteBuf, Holder<Enchantment>> STREAM_CODEC = ByteBufCodecs.holderRegistry(Registries.ENCHANTMENT);
|
| 75 |
|
| 76 | public static Enchantment.Cost constantCost(final int base) {
|
| 77 | return new Enchantment.Cost(base, 0);
|
| 78 | }
|
| 79 |
|
| 80 | public static Enchantment.Cost dynamicCost(final int base, final int perLevel) {
|
| 81 | return new Enchantment.Cost(base, perLevel);
|
| 82 | }
|
| 83 |
|
| 84 | public static Enchantment.EnchantmentDefinition definition(
|
| 85 | final HolderSet<Item> supportedItems,
|
| 86 | final HolderSet<Item> primaryItems,
|
| 87 | final int weight,
|
| 88 | final int maxLevel,
|
| 89 | final Enchantment.Cost minCost,
|
| 90 | final Enchantment.Cost maxCost,
|
| 91 | final int anvilCost,
|
| 92 | final EquipmentSlotGroup... slots
|
| 93 | ) {
|
| 94 | return new Enchantment.EnchantmentDefinition(supportedItems, Optional.of(primaryItems), weight, maxLevel, minCost, maxCost, anvilCost, List.of(slots));
|
| 95 | }
|
| 96 |
|
| 97 | public static Enchantment.EnchantmentDefinition definition(
|
| 98 | final HolderSet<Item> supportedItems,
|
| 99 | final int weight,
|
| 100 | final int maxLevel,
|
| 101 | final Enchantment.Cost minCost,
|
| 102 | final Enchantment.Cost maxCost,
|
| 103 | final int anvilCost,
|
| 104 | final EquipmentSlotGroup... slots
|
| 105 | ) {
|
| 106 | return new Enchantment.EnchantmentDefinition(supportedItems, Optional.empty(), weight, maxLevel, minCost, maxCost, anvilCost, List.of(slots));
|
| 107 | }
|
| 108 |
|
| 109 | public Map<EquipmentSlot, ItemStack> getSlotItems(final LivingEntity entity) {
|
| 110 | Map<EquipmentSlot, ItemStack> itemStacks = Maps.newEnumMap(EquipmentSlot.class);
|
| 111 |
|
| 112 | for (EquipmentSlot slot : EquipmentSlot.VALUES) {
|
| 113 | if (this.matchingSlot(slot)) {
|
| 114 | ItemStack itemStack = entity.getItemBySlot(slot);
|
| 115 | if (!itemStack.isEmpty()) {
|
| 116 | itemStacks.put(slot, itemStack);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | return itemStacks;
|
| 122 | }
|
| 123 |
|
| 124 | public HolderSet<Item> getSupportedItems() {
|
| 125 | return this.definition.supportedItems();
|
| 126 | }
|
| 127 |
|
| 128 | public boolean matchingSlot(final EquipmentSlot slot) {
|
| 129 | return this.definition.slots().stream().anyMatch(group -> group.test(slot));
|
| 130 | }
|
| 131 |
|
| 132 | public boolean isPrimaryItem(final ItemStack item) {
|
| 133 | return this.isSupportedItem(item) && (this.definition.primaryItems.isEmpty() || item.is(this.definition.primaryItems.get()));
|
| 134 | }
|
| 135 |
|
| 136 | public boolean isSupportedItem(final ItemStack item) {
|
| 137 | return item.is(this.definition.supportedItems);
|
| 138 | }
|
| 139 |
|
| 140 | public int getWeight() {
|
| 141 | return this.definition.weight();
|
| 142 | }
|
| 143 |
|
| 144 | public int getAnvilCost() {
|
| 145 | return this.definition.anvilCost();
|
| 146 | }
|
| 147 |
|
| 148 | public int getMinLevel() {
|
| 149 | return 1;
|
| 150 | }
|
| 151 |
|
| 152 | public int getMaxLevel() {
|
| 153 | return this.definition.maxLevel();
|
| 154 | }
|
| 155 |
|
| 156 | public int getMinCost(final int level) {
|
| 157 | return this.definition.minCost().calculate(level);
|
| 158 | }
|
| 159 |
|
| 160 | public int getMaxCost(final int level) {
|
| 161 | return this.definition.maxCost().calculate(level);
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public String toString() {
|
| 166 | return "Enchantment " + this.description.getString();
|
| 167 | }
|
| 168 |
|
| 169 | public static boolean areCompatible(final Holder<Enchantment> enchantment, final Holder<Enchantment> other) {
|
| 170 | return !enchantment.equals(other) && !enchantment.value().exclusiveSet.contains(other) && !other.value().exclusiveSet.contains(enchantment);
|
| 171 | }
|
| 172 |
|
| 173 | public static Component getFullname(final Holder<Enchantment> enchantment, final int level) {
|
| 174 | MutableComponent result = enchantment.value().description.copy();
|
| 175 | if (enchantment.is(EnchantmentTags.CURSE)) {
|
| 176 | result = ComponentUtils.mergeStyles(result, Style.EMPTY.withColor(ChatFormatting.RED));
|
| 177 | } else {
|
| 178 | result = ComponentUtils.mergeStyles(result, Style.EMPTY.withColor(ChatFormatting.GRAY));
|
| 179 | }
|
| 180 |
|
| 181 | if (level != 1 || enchantment.value().getMaxLevel() != 1) {
|
| 182 | result.append(CommonComponents.SPACE).append(Component.translatable("enchantment.level." + level));
|
| 183 | }
|
| 184 |
|
| 185 | return result;
|
| 186 | }
|
| 187 |
|
| 188 | public boolean canEnchant(final ItemStack itemStack) {
|
| 189 | return this.definition.supportedItems().contains(itemStack.typeHolder());
|
| 190 | }
|
| 191 |
|
| 192 | public <T> List<T> getEffects(final DataComponentType<List<T>> type) {
|
| 193 | return this.effects.getOrDefault(type, List.of());
|
| 194 | }
|
| 195 |
|
| 196 | public boolean isImmuneToDamage(final ServerLevel serverLevel, final int enchantmentLevel, final Entity victim, final DamageSource source) {
|
| 197 | LootContext context = damageContext(serverLevel, enchantmentLevel, victim, source);
|
| 198 |
|
| 199 | for (ConditionalEffect<DamageImmunity> filteredEffect : this.getEffects(EnchantmentEffectComponents.DAMAGE_IMMUNITY)) {
|
| 200 | if (filteredEffect.matches(context)) {
|
| 201 | return true;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | return false;
|
| 206 | }
|
| 207 |
|
| 208 | public void modifyDamageProtection(
|
| 209 | final ServerLevel serverLevel,
|
| 210 | final int enchantmentLevel,
|
| 211 | final ItemStack item,
|
| 212 | final Entity victim,
|
| 213 | final DamageSource source,
|
| 214 | final MutableFloat protection
|
| 215 | ) {
|
| 216 | applyEffects(
|
| 217 | this.getEffects(EnchantmentEffectComponents.DAMAGE_PROTECTION),
|
| 218 | damageContext(serverLevel, enchantmentLevel, victim, source),
|
| 219 | protection,
|
| 220 | (e, v) -> e.process(enchantmentLevel, victim.getRandom(), v)
|
| 221 | );
|
| 222 | }
|
| 223 |
|
| 224 | public void modifyDurabilityChange(final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final MutableFloat change) {
|
| 225 | this.modifyItemFilteredCount(EnchantmentEffectComponents.ITEM_DAMAGE, serverLevel, enchantmentLevel, itemStack, change);
|
| 226 | }
|
| 227 |
|
| 228 | public void modifyAmmoCount(final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final MutableFloat change) {
|
| 229 | this.modifyItemFilteredCount(EnchantmentEffectComponents.AMMO_USE, serverLevel, enchantmentLevel, itemStack, change);
|
| 230 | }
|
| 231 |
|
| 232 | public void modifyPiercingCount(final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final MutableFloat count) {
|
| 233 | this.modifyItemFilteredCount(EnchantmentEffectComponents.PROJECTILE_PIERCING, serverLevel, enchantmentLevel, itemStack, count);
|
| 234 | }
|
| 235 |
|
| 236 | public void modifyBlockExperience(final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final MutableFloat count) {
|
| 237 | this.modifyItemFilteredCount(EnchantmentEffectComponents.BLOCK_EXPERIENCE, serverLevel, enchantmentLevel, itemStack, count);
|
| 238 | }
|
| 239 |
|
| 240 | public void modifyMobExperience(
|
| 241 | final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final Entity killer, final MutableFloat experience
|
| 242 | ) {
|
| 243 | this.modifyEntityFilteredValue(EnchantmentEffectComponents.MOB_EXPERIENCE, serverLevel, enchantmentLevel, itemStack, killer, experience);
|
| 244 | }
|
| 245 |
|
| 246 | public void modifyDurabilityToRepairFromXp(final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final MutableFloat change) {
|
| 247 | this.modifyItemFilteredCount(EnchantmentEffectComponents.REPAIR_WITH_XP, serverLevel, enchantmentLevel, itemStack, change);
|
| 248 | }
|
| 249 |
|
| 250 | public void modifyTridentReturnToOwnerAcceleration(
|
| 251 | final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final Entity trident, final MutableFloat count
|
| 252 | ) {
|
| 253 | this.modifyEntityFilteredValue(EnchantmentEffectComponents.TRIDENT_RETURN_ACCELERATION, serverLevel, enchantmentLevel, itemStack, trident, count);
|
| 254 | }
|
| 255 |
|
| 256 | public void modifyTridentSpinAttackStrength(final RandomSource random, final int enchantmentLevel, final MutableFloat strength) {
|
| 257 | this.modifyUnfilteredValue(EnchantmentEffectComponents.TRIDENT_SPIN_ATTACK_STRENGTH, random, enchantmentLevel, strength);
|
| 258 | }
|
| 259 |
|
| 260 | public void modifyFishingTimeReduction(
|
| 261 | final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final Entity fisher, final MutableFloat timeReduction
|
| 262 | ) {
|
| 263 | this.modifyEntityFilteredValue(EnchantmentEffectComponents.FISHING_TIME_REDUCTION, serverLevel, enchantmentLevel, itemStack, fisher, timeReduction);
|
| 264 | }
|
| 265 |
|
| 266 | public void modifyFishingLuckBonus(
|
| 267 | final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack itemStack, final Entity fisher, final MutableFloat luck
|
| 268 | ) {
|
| 269 | this.modifyEntityFilteredValue(EnchantmentEffectComponents.FISHING_LUCK_BONUS, serverLevel, enchantmentLevel, itemStack, fisher, luck);
|
| 270 | }
|
| 271 |
|
| 272 | public void modifyDamage(
|
| 273 | final ServerLevel serverLevel,
|
| 274 | final int enchantmentLevel,
|
| 275 | final ItemStack itemStack,
|
| 276 | final Entity victim,
|
| 277 | final DamageSource damageSource,
|
| 278 | final MutableFloat amount
|
| 279 | ) {
|
| 280 | this.modifyDamageFilteredValue(EnchantmentEffectComponents.DAMAGE, serverLevel, enchantmentLevel, itemStack, victim, damageSource, amount);
|
| 281 | }
|
| 282 |
|
| 283 | public void modifyFallBasedDamage(
|
| 284 | final ServerLevel serverLevel,
|
| 285 | final int enchantmentLevel,
|
| 286 | final ItemStack itemStack,
|
| 287 | final Entity victim,
|
| 288 | final DamageSource damageSource,
|
| 289 | final MutableFloat amount
|
| 290 | ) {
|
| 291 | this.modifyDamageFilteredValue(
|
| 292 | EnchantmentEffectComponents.SMASH_DAMAGE_PER_FALLEN_BLOCK, serverLevel, enchantmentLevel, itemStack, victim, damageSource, amount
|
| 293 | );
|
| 294 | }
|
| 295 |
|
| 296 | public void modifyKnockback(
|
| 297 | final ServerLevel serverLevel,
|
| 298 | final int enchantmentLevel,
|
| 299 | final ItemStack itemStack,
|
| 300 | final Entity victim,
|
| 301 | final DamageSource damageSource,
|
| 302 | final MutableFloat amount
|
| 303 | ) {
|
| 304 | this.modifyDamageFilteredValue(EnchantmentEffectComponents.KNOCKBACK, serverLevel, enchantmentLevel, itemStack, victim, damageSource, amount);
|
| 305 | }
|
| 306 |
|
| 307 | public void modifyArmorEffectivness(
|
| 308 | final ServerLevel serverLevel,
|
| 309 | final int enchantmentLevel,
|
| 310 | final ItemStack itemStack,
|
| 311 | final Entity victim,
|
| 312 | final DamageSource damageSource,
|
| 313 | final MutableFloat amount
|
| 314 | ) {
|
| 315 | this.modifyDamageFilteredValue(EnchantmentEffectComponents.ARMOR_EFFECTIVENESS, serverLevel, enchantmentLevel, itemStack, victim, damageSource, amount);
|
| 316 | }
|
| 317 |
|
| 318 | public void doPostAttack(
|
| 319 | final ServerLevel serverLevel,
|
| 320 | final int enchantmentLevel,
|
| 321 | final EnchantedItemInUse item,
|
| 322 | final EnchantmentTarget forTarget,
|
| 323 | final Entity victim,
|
| 324 | final DamageSource damageSource
|
| 325 | ) {
|
| 326 | for (TargetedConditionalEffect<EnchantmentEntityEffect> effect : this.getEffects(EnchantmentEffectComponents.POST_ATTACK)) {
|
| 327 | if (forTarget == effect.enchanted()) {
|
| 328 | doPostAttack(effect, serverLevel, enchantmentLevel, item, victim, damageSource);
|
| 329 | }
|
| 330 | }
|
| 331 | }
|
| 332 |
|
| 333 | public static void doPostAttack(
|
| 334 | final TargetedConditionalEffect<EnchantmentEntityEffect> effect,
|
| 335 | final ServerLevel serverLevel,
|
| 336 | final int enchantmentLevel,
|
| 337 | final EnchantedItemInUse item,
|
| 338 | final Entity victim,
|
| 339 | final DamageSource damageSource
|
| 340 | ) {
|
| 341 | if (effect.matches(damageContext(serverLevel, enchantmentLevel, victim, damageSource))) {
|
| 342 | Entity target = switch (effect.affected()) {
|
| 343 | case ATTACKER -> damageSource.getEntity();
|
| 344 | case DAMAGING_ENTITY -> damageSource.getDirectEntity();
|
| 345 | case VICTIM -> victim;
|
| 346 | };
|
| 347 | if (target != null) {
|
| 348 | effect.effect().apply(serverLevel, enchantmentLevel, item, target, target.position());
|
| 349 | }
|
| 350 | }
|
| 351 | }
|
| 352 |
|
| 353 | public void doPostPiercingAttack(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity user) {
|
| 354 | applyEffects(
|
| 355 | this.getEffects(EnchantmentEffectComponents.POST_PIERCING_ATTACK),
|
| 356 | entityContext(serverLevel, enchantmentLevel, user, user.position()),
|
| 357 | e -> e.apply(serverLevel, enchantmentLevel, item, user, user.position())
|
| 358 | );
|
| 359 | }
|
| 360 |
|
| 361 | public void modifyProjectileCount(
|
| 362 | final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack weapon, final Entity shooter, final MutableFloat count
|
| 363 | ) {
|
| 364 | this.modifyEntityFilteredValue(EnchantmentEffectComponents.PROJECTILE_COUNT, serverLevel, enchantmentLevel, weapon, shooter, count);
|
| 365 | }
|
| 366 |
|
| 367 | public void modifyProjectileSpread(
|
| 368 | final ServerLevel serverLevel, final int enchantmentLevel, final ItemStack weapon, final Entity shooter, final MutableFloat angle
|
| 369 | ) {
|
| 370 | this.modifyEntityFilteredValue(EnchantmentEffectComponents.PROJECTILE_SPREAD, serverLevel, enchantmentLevel, weapon, shooter, angle);
|
| 371 | }
|
| 372 |
|
| 373 | public void modifyCrossbowChargeTime(final RandomSource random, final int enchantmentLevel, final MutableFloat time) {
|
| 374 | this.modifyUnfilteredValue(EnchantmentEffectComponents.CROSSBOW_CHARGE_TIME, random, enchantmentLevel, time);
|
| 375 | }
|
| 376 |
|
| 377 | public void modifyUnfilteredValue(
|
| 378 | final DataComponentType<EnchantmentValueEffect> component, final RandomSource random, final int enchantmentLevel, final MutableFloat value
|
| 379 | ) {
|
| 380 | EnchantmentValueEffect effect = this.effects.get(component);
|
| 381 | if (effect != null) {
|
| 382 | value.setValue(effect.process(enchantmentLevel, random, value.floatValue()));
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | public void tick(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final Entity entity) {
|
| 387 | applyEffects(
|
| 388 | this.getEffects(EnchantmentEffectComponents.TICK),
|
| 389 | entityContext(serverLevel, enchantmentLevel, entity, entity.position()),
|
| 390 | e -> e.apply(serverLevel, enchantmentLevel, item, entity, entity.position())
|
| 391 | );
|
| 392 | }
|
| 393 |
|
| 394 | public void onProjectileSpawned(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse weapon, final Entity projectile) {
|
| 395 | applyEffects(
|
| 396 | this.getEffects(EnchantmentEffectComponents.PROJECTILE_SPAWNED),
|
| 397 | entityContext(serverLevel, enchantmentLevel, projectile, projectile.position()),
|
| 398 | e -> e.apply(serverLevel, enchantmentLevel, weapon, projectile, projectile.position())
|
| 399 | );
|
| 400 | }
|
| 401 |
|
| 402 | public void onHitBlock(
|
| 403 | final ServerLevel serverLevel,
|
| 404 | final int enchantmentLevel,
|
| 405 | final EnchantedItemInUse weapon,
|
| 406 | final Entity projectile,
|
| 407 | final Vec3 position,
|
| 408 | final BlockState hitBlock
|
| 409 | ) {
|
| 410 | applyEffects(
|
| 411 | this.getEffects(EnchantmentEffectComponents.HIT_BLOCK),
|
| 412 | blockHitContext(serverLevel, enchantmentLevel, projectile, position, hitBlock),
|
| 413 | e -> e.apply(serverLevel, enchantmentLevel, weapon, projectile, position)
|
| 414 | );
|
| 415 | }
|
| 416 |
|
| 417 | /**
|
| 418 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 419 | */
|
| 420 | public final void modifyItemFilteredCount(
|
| 421 | final DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> effectType,
|
| 422 | final ServerLevel serverLevel,
|
| 423 | final int enchantmentLevel,
|
| 424 | final ItemInstance itemStack,
|
| 425 | final MutableFloat value
|
| 426 | ) {
|
| 427 | applyEffects(
|
| 428 | this.getEffects(effectType), itemContext(serverLevel, enchantmentLevel, itemStack), value, (e, v) -> e.process(enchantmentLevel, serverLevel.getRandom(), v)
|
| 429 | );
|
| 430 | }
|
| 431 |
|
| 432 | /**
|
| 433 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 434 | */
|
| 435 | public final void modifyEntityFilteredValue(
|
| 436 | final DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> effectType,
|
| 437 | final ServerLevel serverLevel,
|
| 438 | final int enchantmentLevel,
|
| 439 | final ItemStack itemStack,
|
| 440 | final Entity entity,
|
| 441 | final MutableFloat value
|
| 442 | ) {
|
| 443 | applyEffects(
|
| 444 | this.getEffects(effectType),
|
| 445 | entityContext(serverLevel, enchantmentLevel, entity, entity.position()),
|
| 446 | value,
|
| 447 | (e, v) -> e.process(enchantmentLevel, entity.getRandom(), v)
|
| 448 | );
|
| 449 | }
|
| 450 |
|
| 451 | /**
|
| 452 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 453 | */
|
| 454 | public final void modifyDamageFilteredValue(
|
| 455 | final DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>> effectType,
|
| 456 | final ServerLevel serverLevel,
|
| 457 | final int enchantmentLevel,
|
| 458 | final ItemStack itemStack,
|
| 459 | final Entity victim,
|
| 460 | final DamageSource damageSource,
|
| 461 | final MutableFloat value
|
| 462 | ) {
|
| 463 | applyEffects(
|
| 464 | this.getEffects(effectType),
|
| 465 | damageContext(serverLevel, enchantmentLevel, victim, damageSource),
|
| 466 | value,
|
| 467 | (e, v) -> e.process(enchantmentLevel, victim.getRandom(), v)
|
| 468 | );
|
| 469 | }
|
| 470 |
|
| 471 | public static LootContext damageContext(final ServerLevel serverLevel, final int enchantmentLevel, final Entity victim, final DamageSource source) {
|
| 472 | LootParams params = new LootParams.Builder(serverLevel)
|
| 473 | .withParameter(LootContextParams.THIS_ENTITY, victim)
|
| 474 | .withParameter(LootContextParams.ENCHANTMENT_LEVEL, enchantmentLevel)
|
| 475 | .withParameter(LootContextParams.ORIGIN, victim.position())
|
| 476 | .withParameter(LootContextParams.DAMAGE_SOURCE, source)
|
| 477 | .withOptionalParameter(LootContextParams.ATTACKING_ENTITY, source.getEntity())
|
| 478 | .withOptionalParameter(LootContextParams.DIRECT_ATTACKING_ENTITY, source.getDirectEntity())
|
| 479 | .create(LootContextParamSets.ENCHANTED_DAMAGE);
|
| 480 | return new LootContext.Builder(params).create(Optional.empty());
|
| 481 | }
|
| 482 |
|
| 483 | /**
|
| 484 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 485 | */
|
| 486 | public static LootContext itemContext(final ServerLevel serverLevel, final int enchantmentLevel, final ItemInstance itemStack) {
|
| 487 | LootParams params = new LootParams.Builder(serverLevel)
|
| 488 | .withParameter(LootContextParams.TOOL, itemStack)
|
| 489 | .withParameter(LootContextParams.ENCHANTMENT_LEVEL, enchantmentLevel)
|
| 490 | .create(LootContextParamSets.ENCHANTED_ITEM);
|
| 491 | return new LootContext.Builder(params).create(Optional.empty());
|
| 492 | }
|
| 493 |
|
| 494 | /**
|
| 495 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 496 | */
|
| 497 | public static LootContext locationContext(final ServerLevel serverLevel, final int enchantmentLevel, final Entity entity, final boolean active) {
|
| 498 | LootParams params = new LootParams.Builder(serverLevel)
|
| 499 | .withParameter(LootContextParams.THIS_ENTITY, entity)
|
| 500 | .withParameter(LootContextParams.ENCHANTMENT_LEVEL, enchantmentLevel)
|
| 501 | .withParameter(LootContextParams.ORIGIN, entity.position())
|
| 502 | .withParameter(LootContextParams.ENCHANTMENT_ACTIVE, active)
|
| 503 | .create(LootContextParamSets.ENCHANTED_LOCATION);
|
| 504 | return new LootContext.Builder(params).create(Optional.empty());
|
| 505 | }
|
| 506 |
|
| 507 | /**
|
| 508 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 509 | */
|
| 510 | public static LootContext entityContext(final ServerLevel serverLevel, final int enchantmentLevel, final Entity entity, final Vec3 position) {
|
| 511 | LootParams params = new LootParams.Builder(serverLevel)
|
| 512 | .withParameter(LootContextParams.THIS_ENTITY, entity)
|
| 513 | .withParameter(LootContextParams.ENCHANTMENT_LEVEL, enchantmentLevel)
|
| 514 | .withParameter(LootContextParams.ORIGIN, position)
|
| 515 | .create(LootContextParamSets.ENCHANTED_ENTITY);
|
| 516 | return new LootContext.Builder(params).create(Optional.empty());
|
| 517 | }
|
| 518 |
|
| 519 | /**
|
| 520 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 521 | */
|
| 522 | public static LootContext blockHitContext(
|
| 523 | final ServerLevel serverLevel, final int enchantmentLevel, final Entity entity, final Vec3 position, final BlockState hitBlock
|
| 524 | ) {
|
| 525 | LootParams params = new LootParams.Builder(serverLevel)
|
| 526 | .withParameter(LootContextParams.THIS_ENTITY, entity)
|
| 527 | .withParameter(LootContextParams.ENCHANTMENT_LEVEL, enchantmentLevel)
|
| 528 | .withParameter(LootContextParams.ORIGIN, position)
|
| 529 | .withParameter(LootContextParams.BLOCK_STATE, hitBlock)
|
| 530 | .create(LootContextParamSets.HIT_BLOCK);
|
| 531 | return new LootContext.Builder(params).create(Optional.empty());
|
| 532 | }
|
| 533 |
|
| 534 | /**
|
| 535 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 536 | */
|
| 537 | public static <T> void applyEffects(final List<ConditionalEffect<T>> effects, final LootContext filterData, final Enchantment.GenericAction<T> action) {
|
| 538 | for (ConditionalEffect<T> conditionalEffect : effects) {
|
| 539 | if (conditionalEffect.matches(filterData)) {
|
| 540 | action.apply(conditionalEffect.effect());
|
| 541 | }
|
| 542 | }
|
| 543 | }
|
| 544 |
|
| 545 | /**
|
| 546 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 547 | */
|
| 548 | public static <T> void applyEffects(
|
| 549 | final List<ConditionalEffect<T>> effects, final LootContext filterData, final MutableFloat value, final Enchantment.FloatAction<T> action
|
| 550 | ) {
|
| 551 | applyEffects(effects, filterData, action.asGeneric(value));
|
| 552 | }
|
| 553 |
|
| 554 | public void runLocationChangedEffects(final ServerLevel serverLevel, final int enchantmentLevel, final EnchantedItemInUse item, final LivingEntity entity) {
|
| 555 | EquipmentSlot slot = item.inSlot();
|
| 556 | if (slot != null) {
|
| 557 | Map<Enchantment, Set<EnchantmentLocationBasedEffect>> activeLocationDependentEffects = entity.activeLocationDependentEnchantments(slot);
|
| 558 | if (!this.matchingSlot(slot)) {
|
| 559 | Set<EnchantmentLocationBasedEffect> activeEffects = activeLocationDependentEffects.remove(this);
|
| 560 | if (activeEffects != null) {
|
| 561 | activeEffects.forEach(effectx -> effectx.onDeactivated(item, entity, entity.position(), enchantmentLevel));
|
| 562 | }
|
| 563 | } else {
|
| 564 | Set<EnchantmentLocationBasedEffect> activeEffects = activeLocationDependentEffects.get(this);
|
| 565 |
|
| 566 | for (ConditionalEffect<EnchantmentLocationBasedEffect> filteredEffect : this.getEffects(EnchantmentEffectComponents.LOCATION_CHANGED)) {
|
| 567 | EnchantmentLocationBasedEffect effect = filteredEffect.effect();
|
| 568 | boolean wasActive = activeEffects != null && activeEffects.contains(effect);
|
| 569 | if (filteredEffect.matches(locationContext(serverLevel, enchantmentLevel, entity, wasActive))) {
|
| 570 | if (!wasActive) {
|
| 571 | if (activeEffects == null) {
|
| 572 | activeEffects = new ObjectArraySet<>();
|
| 573 | activeLocationDependentEffects.put(this, activeEffects);
|
| 574 | }
|
| 575 |
|
| 576 | activeEffects.add(effect);
|
| 577 | }
|
| 578 |
|
| 579 | effect.onChangedBlock(serverLevel, enchantmentLevel, item, entity, entity.position(), !wasActive);
|
| 580 | } else if (activeEffects != null && activeEffects.remove(effect)) {
|
| 581 | effect.onDeactivated(item, entity, entity.position(), enchantmentLevel);
|
| 582 | }
|
| 583 | }
|
| 584 |
|
| 585 | if (activeEffects != null && activeEffects.isEmpty()) {
|
| 586 | activeLocationDependentEffects.remove(this);
|
| 587 | }
|
| 588 | }
|
| 589 | }
|
| 590 | }
|
| 591 |
|
| 592 | public void stopLocationBasedEffects(final int enchantmentLevel, final EnchantedItemInUse item, final LivingEntity entity) {
|
| 593 | EquipmentSlot slot = item.inSlot();
|
| 594 | if (slot != null) {
|
| 595 | Set<EnchantmentLocationBasedEffect> activeEffects = entity.activeLocationDependentEnchantments(slot).remove(this);
|
| 596 | if (activeEffects != null) {
|
| 597 | for (EnchantmentLocationBasedEffect effect : activeEffects) {
|
| 598 | effect.onDeactivated(item, entity, entity.position(), enchantmentLevel);
|
| 599 | }
|
| 600 | }
|
| 601 | }
|
| 602 | }
|
| 603 |
|
| 604 | public static Enchantment.Builder enchantment(final Enchantment.EnchantmentDefinition definition) {
|
| 605 | return new Enchantment.Builder(definition);
|
| 606 | }
|
| 607 |
|
| 608 | public static class Builder {
|
| 609 | private final Enchantment.EnchantmentDefinition definition;
|
| 610 | private HolderSet<Enchantment> exclusiveSet = HolderSet.empty();
|
| 611 | private final Map<DataComponentType<?>, List<?>> effectLists = new HashMap<>();
|
| 612 | private final DataComponentMap.Builder effectMapBuilder = DataComponentMap.builder();
|
| 613 |
|
| 614 | public Builder(final Enchantment.EnchantmentDefinition definition) {
|
| 615 | this.definition = definition;
|
| 616 | }
|
| 617 |
|
| 618 | public Enchantment.Builder exclusiveWith(final HolderSet<Enchantment> set) {
|
| 619 | this.exclusiveSet = set;
|
| 620 | return this;
|
| 621 | }
|
| 622 |
|
| 623 | public <E> Enchantment.Builder withEffect(final DataComponentType<List<ConditionalEffect<E>>> type, final E effect, final LootItemCondition.Builder condition) {
|
| 624 | this.getEffectsList(type).add(new ConditionalEffect<>(effect, Optional.of(Holder.direct(condition.build()))));
|
| 625 | return this;
|
| 626 | }
|
| 627 |
|
| 628 | public <E> Enchantment.Builder withEffect(final DataComponentType<List<ConditionalEffect<E>>> type, final E effect) {
|
| 629 | this.getEffectsList(type).add(new ConditionalEffect<>(effect, Optional.empty()));
|
| 630 | return this;
|
| 631 | }
|
| 632 |
|
| 633 | public <E> Enchantment.Builder withEffect(
|
| 634 | final DataComponentType<List<TargetedConditionalEffect<E>>> type,
|
| 635 | final EnchantmentTarget enchanted,
|
| 636 | final EnchantmentTarget affected,
|
| 637 | final E effect,
|
| 638 | final LootItemCondition.Builder condition
|
| 639 | ) {
|
| 640 | this.getEffectsList(type).add(new TargetedConditionalEffect<>(enchanted, affected, effect, Optional.of(Holder.direct(condition.build()))));
|
| 641 | return this;
|
| 642 | }
|
| 643 |
|
| 644 | public <E> Enchantment.Builder withEffect(
|
| 645 | final DataComponentType<List<TargetedConditionalEffect<E>>> type, final EnchantmentTarget enchanted, final EnchantmentTarget affected, final E effect
|
| 646 | ) {
|
| 647 | this.getEffectsList(type).add(new TargetedConditionalEffect<>(enchanted, affected, effect, Optional.empty()));
|
| 648 | return this;
|
| 649 | }
|
| 650 |
|
| 651 | public Enchantment.Builder withEffect(final DataComponentType<List<EnchantmentAttributeEffect>> type, final EnchantmentAttributeEffect effect) {
|
| 652 | this.getEffectsList(type).add(effect);
|
| 653 | return this;
|
| 654 | }
|
| 655 |
|
| 656 | public <E> Enchantment.Builder withSpecialEffect(final DataComponentType<E> type, final E effect) {
|
| 657 | this.effectMapBuilder.set(type, effect);
|
| 658 | return this;
|
| 659 | }
|
| 660 |
|
| 661 | public Enchantment.Builder withEffect(final DataComponentType<Unit> type) {
|
| 662 | this.effectMapBuilder.set(type, Unit.INSTANCE);
|
| 663 | return this;
|
| 664 | }
|
| 665 |
|
| 666 | private <E> List<E> getEffectsList(final DataComponentType<List<E>> type) {
|
| 667 | return (List<E>)this.effectLists.computeIfAbsent(type, k -> {
|
| 668 | ArrayList<E> newList = new ArrayList<>();
|
| 669 | this.effectMapBuilder.set(type, newList);
|
| 670 | return newList;
|
| 671 | });
|
| 672 | }
|
| 673 |
|
| 674 | public Enchantment build(final Identifier descriptionKey) {
|
| 675 | return new Enchantment(
|
| 676 | Component.translatable(Util.makeDescriptionId("enchantment", descriptionKey)), this.definition, this.exclusiveSet, this.effectMapBuilder.build()
|
| 677 | );
|
| 678 | }
|
| 679 | }
|
| 680 |
|
| 681 | public record Cost(int base, int perLevelAboveFirst) {
|
| 682 | public static final Codec<Enchantment.Cost> CODEC = RecordCodecBuilder.create(
|
| 683 | i -> i.group(
|
| 684 | Codec.INT.fieldOf("base").forGetter(Enchantment.Cost::base), Codec.INT.fieldOf("per_level_above_first").forGetter(Enchantment.Cost::perLevelAboveFirst)
|
| 685 | )
|
| 686 | .apply(i, Enchantment.Cost::new)
|
| 687 | );
|
| 688 |
|
| 689 | public int calculate(final int level) {
|
| 690 | return this.base + this.perLevelAboveFirst * (level - 1);
|
| 691 | }
|
| 692 | }
|
| 693 |
|
| 694 | public record EnchantmentDefinition(
|
| 695 | HolderSet<Item> supportedItems,
|
| 696 | Optional<HolderSet<Item>> primaryItems,
|
| 697 | int weight,
|
| 698 | int maxLevel,
|
| 699 | Enchantment.Cost minCost,
|
| 700 | Enchantment.Cost maxCost,
|
| 701 | int anvilCost,
|
| 702 | List<EquipmentSlotGroup> slots
|
| 703 | ) {
|
| 704 | public static final MapCodec<Enchantment.EnchantmentDefinition> CODEC = RecordCodecBuilder.mapCodec(
|
| 705 | i -> i.group(
|
| 706 | RegistryCodecs.holderSet(Registries.ITEM).fieldOf("supported_items").forGetter(Enchantment.EnchantmentDefinition::supportedItems),
|
| 707 | RegistryCodecs.holderSet(Registries.ITEM).optionalFieldOf("primary_items").forGetter(Enchantment.EnchantmentDefinition::primaryItems),
|
| 708 | ExtraCodecs.intRange(1, 1024).fieldOf("weight").forGetter(Enchantment.EnchantmentDefinition::weight),
|
| 709 | ExtraCodecs.intRange(1, 255).fieldOf("max_level").forGetter(Enchantment.EnchantmentDefinition::maxLevel),
|
| 710 | Enchantment.Cost.CODEC.fieldOf("min_cost").forGetter(Enchantment.EnchantmentDefinition::minCost),
|
| 711 | Enchantment.Cost.CODEC.fieldOf("max_cost").forGetter(Enchantment.EnchantmentDefinition::maxCost),
|
| 712 | ExtraCodecs.NON_NEGATIVE_INT.fieldOf("anvil_cost").forGetter(Enchantment.EnchantmentDefinition::anvilCost),
|
| 713 | EquipmentSlotGroup.CODEC.listOf().fieldOf("slots").forGetter(Enchantment.EnchantmentDefinition::slots)
|
| 714 | )
|
| 715 | .apply(i, Enchantment.EnchantmentDefinition::new)
|
| 716 | );
|
| 717 | }
|
| 718 |
|
| 719 | /**
|
| 720 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 721 | */
|
| 722 | @FunctionalInterface
|
| 723 | public interface FloatAction<T> {
|
| 724 | float apply(T effect, float value);
|
| 725 |
|
| 726 | default Enchantment.GenericAction<T> asGeneric(final MutableFloat v) {
|
| 727 | return effect -> v.setValue(this.apply(effect, v.floatValue()));
|
| 728 | }
|
| 729 | }
|
| 730 |
|
| 731 | /**
|
| 732 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 733 | */
|
| 734 | @FunctionalInterface
|
| 735 | public interface GenericAction<T> {
|
| 736 | void apply(T effect);
|
| 737 | }
|
| 738 | }
|
| 739 |
|