Skip to content

Commit

Permalink
More enchantment refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Electro593 committed Dec 1, 2024
1 parent 7596df1 commit f216a7c
Show file tree
Hide file tree
Showing 28 changed files with 155 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ public static boolean isImmuneTo(ItemStack itemStack, DamageSource damageSource)
return false;
}

// does itemStack have Damage Proof enchantment?
if (EnchantmentHelper.getLevel(SpectrumEnchantments.STEADFAST, itemStack) > 0) {
if (EnchantmentHelper.hasAnyEnchantmentsIn(itemStack, SpectrumEnchantmentTags.PREVENTS_ITEM_DAMAGE)) {
return true;
// is item immune to this specific kind of damage?
}


// is item immune to this specific kind of damage?
Item item = itemStack.getItem();
if (damageSourceImmunities.containsKey(item)) {
for (TagKey<DamageType> type : damageSourceImmunities.get(item)) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/de/dafuqs/spectrum/config/SpectrumConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public class SpectrumConfig implements ConfigData {

@Comment("Flat additional damage dealt with each level of the First Strike enchantment")
public float FirstStrikeDamagePerLevel = 2.0F;


//TODO: Re-add the settings to disable certain enchants

@Comment("The percentile a mobs armor/hand stacks are being dropped when hit with a Disarming enchanted weapon per the enchantments level")
public float DisarmingChancePerLevelMobs = 0.01F;
@Comment("If > 0 the Disarming Enchantment is able to remove armor and hand tools from a hit player. Should be a far smaller chance than for mobs")
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected void onEntityHit(EntityHitResult entityHitResult) {
}

if (!attacked.isOnGround() && propelled) {
damage *= 3 + ImprovedCriticalEnchantment.getAddtionalCritDamageMultiplier(SpectrumEnchantmentHelper.getLevel(owner.getWorld().getRegistryManager(), SpectrumEnchantments.IMPROVED_CRITICAL, stack));
damage *= 3 + SpectrumEnchantmentHelper.getAddtionalCritDamageMultiplier(SpectrumEnchantmentHelper.getLevel(owner.getWorld().getRegistryManager(), SpectrumEnchantments.IMPROVED_CRITICAL, stack));
crit = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.entity.damage.*;
import net.minecraft.item.*;
import net.minecraft.particle.*;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.*;
import net.minecraft.util.*;
import net.minecraft.util.math.*;
Expand Down Expand Up @@ -71,7 +72,7 @@ public Optional<ParticleEffect> getParticleEffects() {
}

@ApiStatus.OverrideOnly
public void addEnchantments(ItemStack stack) {
public void addEnchantments(ServerWorld world, ItemStack stack) {
}

@ApiStatus.OverrideOnly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void explode(@NotNull ServerWorld world, BlockPos pos, @Nullable P
if (optionalExplosionShape.isPresent()) {
shape = optionalExplosionShape.get();
}
explosionEffect.addEnchantments(miningStack);
explosionEffect.addEnchantments(world, miningStack);
}

float blastDamage = baseDamage * damageMod;
Expand Down Expand Up @@ -114,7 +114,7 @@ private static void playVisualEffects(ServerWorld world, Vec3d pos, List<Explosi
Vec3d particle = orientation.toVector(r).add(pos);
Collections.shuffle(types);

world.spawnParticles(types.get(0), particle.getX(), particle.getY(), particle.getZ(), 1, 0, 0, 0, 0);
world.spawnParticles(types.getFirst(), particle.getX(), particle.getY(), particle.getZ(), 1, 0, 0, 0, 0);
}
}

Expand All @@ -133,7 +133,7 @@ private static List<BlockPos> processExplosion(@NotNull ServerWorld world, @Null
}
}

boolean hasInventoryInsertion = EnchantmentHelper.getLevel(SpectrumEnchantments.INVENTORY_INSERTION, miningStack) > 0;
boolean hasInventoryInsertion = EnchantmentHelper.hasAnyEnchantmentsIn(miningStack, SpectrumEnchantmentTags.INVENTORY_INSERTION_EFFECT);
for (Pair<ItemStack, BlockPos> stackPosPair : drops) {
if (owner != null && hasInventoryInsertion) {
owner.getInventory().offerOrDrop(stackPosPair.getFirst());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import net.minecraft.enchantment.*;
import net.minecraft.item.*;
import net.minecraft.particle.*;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;

public class EnchantmentAddingModifier extends ExplosionModifier {

private final Enchantment enchantment;
private final RegistryKey<Enchantment> enchantment;
private final int level;

public EnchantmentAddingModifier(ExplosionModifierType type, Enchantment enchantment, int level, ParticleEffect particleEffect, int displayColor) {
public EnchantmentAddingModifier(ExplosionModifierType type, RegistryKey<Enchantment> enchantment, int level, ParticleEffect particleEffect, int displayColor) {
super(type, displayColor);
this.enchantment = enchantment;
this.level = level;
}

@Override
public void addEnchantments(ItemStack stack) {
stack.addEnchantment(enchantment, level);
public void addEnchantments(ServerWorld world, ItemStack stack) {
world.getRegistryManager()
.getOptionalWrapper(RegistryKeys.ENCHANTMENT)
.flatMap(impl -> impl.getOptional(enchantment))
.ifPresent(e -> stack.addEnchantment(e, level));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.dafuqs.spectrum.helpers;

import de.dafuqs.spectrum.SpectrumCommon;
import de.dafuqs.spectrum.api.item.*;
import de.dafuqs.spectrum.enchantments.*;
import de.dafuqs.spectrum.mixin.accessors.EnchantmentAccessor;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.component.type.ItemEnchantmentsComponent;
Expand All @@ -12,6 +12,7 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.*;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.*;
Expand Down Expand Up @@ -238,4 +239,14 @@ public static void doTreasureHunterForPlayer(ServerPlayerEntity thisEntity, Dama
}
}

public static float getAddtionalCritDamageMultiplier(int improvedCriticalLevel) {
return SpectrumCommon.CONFIG.ImprovedCriticalExtraDamageMultiplierPerLevel * improvedCriticalLevel;
}

public static RegistryEntryList<Item> getBlacklist(Enchantment enchantment) {
return enchantment.effects().getOrDefault(
SpectrumEnchantmentEffectComponentTypes.BLACKLISTED_ITEMS,
RegistryEntryList.empty());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ public ItemStack finishUsing(ItemStack itemStack, World world, LivingEntity user
}

private static void decrementWithChance(ItemStack itemStack, World world, ServerPlayerEntity playerEntity) {
if (EnchantmentHelper.getLevel(SpectrumEnchantments.INDESTRUCTIBLE, itemStack) > 0) {
if (EnchantmentHelper.hasAnyEnchantmentsIn(itemStack, SpectrumEnchantmentTags.INDESTRUCTIBLE_EFFECT)) {
return;
}
if (!playerEntity.getAbilities().creativeMode) {
int unbreakingLevel = EnchantmentHelper.getLevel(Enchantments.UNBREAKING, itemStack);
int unbreakingLevel = SpectrumEnchantmentHelper.getLevel(world.getRegistryManager(), Enchantments.UNBREAKING, itemStack);
if (unbreakingLevel == 0) {
itemStack.decrement(1);
} else {
Expand Down Expand Up @@ -303,7 +303,7 @@ public boolean isEnchantable(ItemStack stack) {

@Override
public boolean acceptsEnchantment(Enchantment enchantment) {
return enchantment == SpectrumEnchantments.INDESTRUCTIBLE || enchantment == Enchantments.UNBREAKING;
return enchantment == Enchantments.UNBREAKING;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package de.dafuqs.spectrum.items.trinkets;

import de.dafuqs.spectrum.*;
import de.dafuqs.spectrum.api.item.*;
import de.dafuqs.spectrum.helpers.SpectrumEnchantmentHelper;
import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.particle.*;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.enchantment.*;
import net.minecraft.entity.*;
import net.minecraft.entity.effect.*;
import net.minecraft.entity.player.*;
Expand All @@ -20,7 +19,7 @@

import java.util.*;

public class GleamingPinItem extends SpectrumTrinketItem implements ExtendedEnchantable {
public class GleamingPinItem extends SpectrumTrinketItem {

public static final int BASE_RANGE = 12;
public static final int RANGE_BONUS_PER_LEVEL_OF_SNIPING = 4;
Expand All @@ -35,15 +34,15 @@ public static void doGleamingPinEffect(@NotNull PlayerEntity player, @NotNull Se
world.playSound(null, player.getX(), player.getY(), player.getZ(), SpectrumSoundEvents.RADIANCE_PIN_TRIGGER, SoundCategory.PLAYERS, 0.4F, 0.9F + world.getRandom().nextFloat() * 0.2F);
SpectrumS2CPacketSender.playParticleWithRandomOffsetAndVelocity(world, player.getPos().add(0, 0.75, 0), SpectrumParticleTypes.LIQUID_CRYSTAL_SPARKLE, 100, new Vec3d(0, 0.5, 0), new Vec3d(2.5, 0.1, 2.5));

world.getOtherEntities(player, player.getBoundingBox().expand(getEffectRange(gleamingPinStack)), EntityPredicates.VALID_LIVING_ENTITY).forEach((entity) -> {
world.getOtherEntities(player, player.getBoundingBox().expand(getEffectRange(world, gleamingPinStack)), EntityPredicates.VALID_LIVING_ENTITY).forEach((entity) -> {
if (entity instanceof LivingEntity livingEntity) {
livingEntity.addStatusEffect(new StatusEffectInstance(StatusEffects.GLOWING, EFFECT_DURATION, 0, true, true));
}
});
}

public static int getEffectRange(ItemStack stack) {
return BASE_RANGE + RANGE_BONUS_PER_LEVEL_OF_SNIPING * EnchantmentHelper.getLevel(SpectrumEnchantments.SNIPING, stack);
public static int getEffectRange(ServerWorld world, ItemStack stack) {
return BASE_RANGE + RANGE_BONUS_PER_LEVEL_OF_SNIPING * SpectrumEnchantmentHelper.getLevel(world.getRegistryManager(), SpectrumEnchantments.SNIPING, stack);
}

@Override
Expand All @@ -57,11 +56,6 @@ public boolean isEnchantable(ItemStack stack) {
return stack.getCount() == 1;
}

@Override
public boolean acceptsEnchantment(Enchantment enchantment) {
return enchantment == SpectrumEnchantments.SNIPING;
}

@Override
public int getEnchantability() {
return 16;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/de/dafuqs/spectrum/mixin/BlockMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public abstract class BlockMixin {
@ModifyReturnValue(method = "getDroppedStacks(Lnet/minecraft/block/BlockState;Lnet/minecraft/server/world/ServerWorld;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/entity/BlockEntity;Lnet/minecraft/entity/Entity;Lnet/minecraft/item/ItemStack;)Ljava/util/List;", at = @At("RETURN"))
private static List<ItemStack> spectrum$getDroppedStacks(List<ItemStack> original, BlockState state, ServerWorld world, BlockPos pos, BlockEntity blockEntity, Entity entity, ItemStack stack) {
List<ItemStack> droppedStacks = original;
var enchantmentMap = stack.getEnchantments();

// Voiding curse: no drops
if (EnchantmentHelper.hasAnyEnchantmentsIn(stack, SpectrumEnchantmentTags.NO_BLOCK_DROPS)) {
Expand All @@ -54,8 +53,8 @@ public abstract class BlockMixin {
droppedStacks = FoundryHelper.applyFoundry(world, droppedStacks);
}

// Inventory Insertion enchant? Add it to players inventory if there is room
if (enchantmentMap.containsKey(SpectrumEnchantments.INVENTORY_INSERTION) && SpectrumEnchantments.INVENTORY_INSERTION.canEntityUse(entity)) {
// Inventory insertion enchant: add it to the player's inventory if there is room
if (EnchantmentHelper.hasAnyEnchantmentsIn(stack, SpectrumEnchantmentTags.INVENTORY_INSERTION_EFFECT)) {
List<ItemStack> leftoverReturnStacks = new ArrayList<>();

if (entity instanceof PlayerEntity playerEntity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.dafuqs.spectrum.mixin;

import de.dafuqs.spectrum.helpers.SpectrumEnchantmentHelper;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.enchantment.*;
import net.minecraft.item.*;
Expand All @@ -12,7 +13,7 @@ public abstract class CrossbowItemMixin {

@Inject(method = "getSpeed(Lnet/minecraft/item/ItemStack;)F", at = @At("RETURN"), cancellable = true)
private static void getSpeed(ItemStack stack, CallbackInfoReturnable<Float> cir) {
int snipingLevel = EnchantmentHelper.getLevel(SpectrumEnchantments.SNIPING, stack);
int snipingLevel = SpectrumEnchantmentHelper.getLevel(drm, SpectrumEnchantments.SNIPING, stack);
if (snipingLevel > 0) {
cir.setReturnValue(cir.getReturnValue() + 1.0F * snipingLevel);
}
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/de/dafuqs/spectrum/mixin/EnchantmentMixin.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
package de.dafuqs.spectrum.mixin;

import de.dafuqs.spectrum.api.item.*;
import de.dafuqs.spectrum.helpers.SpectrumEnchantmentHelper;
import net.minecraft.enchantment.*;
import net.minecraft.item.*;
import net.minecraft.registry.entry.RegistryEntryList;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.*;

@Mixin(Enchantment.class)
public abstract class EnchantmentMixin {


@Inject(method = "getApplicableItems()Lnet/minecraft/registry/entry/RegistryEntryList;", at = @At("RETURN"), cancellable = true)
public void spectrum$getAcceptableItems(CallbackInfoReturnable<RegistryEntryList<Item>> cir) {
var enchantment = (Enchantment) (Object) this;
var items = cir.getReturnValue();
var blacklist = SpectrumEnchantmentHelper.getBlacklist(enchantment);
var withoutBlacklisted = blacklist.stream().filter(b -> !items.contains(b)).toList();
cir.setReturnValue(RegistryEntryList.of(withoutBlacklisted));
}

@Inject(method = "isSupportedItem(Lnet/minecraft/item/ItemStack;)Z", at = @At("RETURN"), cancellable = true)
public void spectrum$isSupportedItems(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(spectrum$modifyIsSupported((Enchantment) (Object) this, stack, cir.getReturnValue()));
}

@Inject(method = "isAcceptableItem(Lnet/minecraft/item/ItemStack;)Z", at = @At("RETURN"), cancellable = true)
public void isAcceptableItem(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
var accepted = cir.getReturnValue();

if (!accepted) {
var enchantment = (Enchantment) (Object) this;
if (stack.getItem() instanceof ExtendedEnchantable extendedEnchantable && extendedEnchantable.acceptsEnchantment(enchantment)) {
cir.setReturnValue(true);
}
}
public void spectrum$isAcceptableItem(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(spectrum$modifyIsSupported((Enchantment) (Object) this, stack, cir.getReturnValue()));
}

@Unique
private static boolean spectrum$modifyIsSupported(Enchantment enchantment, ItemStack stack, boolean original) {
var isExtendedEnchantable = stack.getItem() instanceof ExtendedEnchantable extendedEnchantable
&& extendedEnchantable.acceptsEnchantment(enchantment);
var isBlacklisted = stack.isIn(SpectrumEnchantmentHelper.getBlacklist(enchantment));
return (original || isExtendedEnchantable) && !isBlacklisted;
}

}
14 changes: 11 additions & 3 deletions src/main/java/de/dafuqs/spectrum/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.entity.effect.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.*;
import net.minecraft.sound.*;
import net.minecraft.stat.*;
Expand All @@ -36,15 +37,17 @@ public abstract class EntityMixin {

@ModifyVariable(method = "slowMovement", at = @At(value = "LOAD"), argsOnly = true)
private Vec3d spectrum$applyInexorableAntiBlockSlowdown(Vec3d multiplier) {
if ((Object) this instanceof LivingEntity livingEntity && InexorableEnchantment.isArmorActive(livingEntity)) {
var entity = (Entity) (Object) this;
if (entity instanceof LivingEntity livingEntity && InexorableEnchantment.isArmorActive(livingEntity)) {
return Vec3d.ZERO;
}
return multiplier;
}

@Inject(method = "getVelocityMultiplier", at = @At("RETURN"), cancellable = true)
private void spectrum$applyInexorableAntiSlowdown(CallbackInfoReturnable<Float> cir) {
if ((Object) this instanceof LivingEntity livingEntity && InexorableEnchantment.isArmorActive(livingEntity)) {
var entity = (Entity) (Object) this;
if (entity instanceof LivingEntity livingEntity && InexorableEnchantment.isArmorActive(livingEntity)) {
cir.setReturnValue(Math.max(cir.getReturnValue(), 1F));
}
}
Expand All @@ -53,7 +56,12 @@ public abstract class EntityMixin {
public void spectrum$dropStack(ItemStack stack, CallbackInfoReturnable<ItemEntity> cir) {
if ((Object) this instanceof LivingEntity thisLivingEntity) {
if (thisLivingEntity.isDead() && thisLivingEntity.getAttacker() instanceof PlayerEntity killer) {
if (EnchantmentHelper.getEquipmentLevel(SpectrumEnchantments.INVENTORY_INSERTION, killer) > 0) {
var hasInventoryInsertion = thisLivingEntity.getWorld().getRegistryManager()
.getOptionalWrapper(RegistryKeys.ENCHANTMENT)
.flatMap(impl -> impl.getOptional(SpectrumEnchantments.INVENTORY_INSERTION))
.map(e -> EnchantmentHelper.getEquipmentLevel(e, killer) > 0)
.orElse(false);
if (hasInventoryInsertion) {
Item item = stack.getItem();
int count = stack.getCount();

Expand Down
Loading

0 comments on commit f216a7c

Please sign in to comment.