Skip to content

Commit

Permalink
Fix crash when vengeance enchantment is active on bow
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Aug 11, 2024
1 parent a901924 commit 041803c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.enchantment.EnchantedItemInUse;
import net.minecraft.world.item.enchantment.effects.EnchantmentEntityEffect;
import net.minecraft.world.phys.Vec3;
Expand All @@ -30,6 +31,9 @@ public void apply(ServerLevel level, int enchantmentLevel, EnchantedItemInUse en
if (enchantmentLevel > 0) {
int chance = Math.max(1, chance() / enchantmentLevel);
if (chance > 0 && level.random.nextInt(chance) == 0) {
if (entity instanceof Projectile projectile) {
entity = projectile.getEffectSource();
}
ItemVengeanceRing.toggleVengeanceArea(level, entity, area * enchantmentLevel, true, true, true);
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/org/cyclops/evilcraft/item/ItemVengeanceRing.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -102,24 +101,28 @@ public boolean apply(Entity entity) {

// Vengeance all the spirits in the neighbourhood
for(EntityVengeanceSpirit spirit : spirits) {
spirit.setEnabledVengeance((Player) entity, enableVengeance);
if(enableVengeance) {
spirit.setTarget((LivingEntity) entity);
} else if(spirit.getTarget() == entity) {
spirit.setTarget(null);
if (entity instanceof Player player) {
spirit.setEnabledVengeance(player, enableVengeance);
if (enableVengeance) {
spirit.setTarget(player);
} else if (spirit.getTarget() == player) {
spirit.setTarget(null);
}
}
}

// If no spirits were found in an area, we spawn a new one and make him angry.
if(spirits.size() == 0 && enableVengeance) {
EntityVengeanceSpirit spirit = EntityVengeanceSpirit.spawnRandom(world, blockPos, area / 4);
if(spirit != null) {
if(forceGlobal) {
spirit.setGlobalVengeance(true);
} else {
spirit.setEnabledVengeance((Player) entity, true);
if (entity instanceof Player player) {
if (forceGlobal) {
spirit.setGlobalVengeance(true);
} else {
spirit.setEnabledVengeance(player, true);
}
spirit.setTarget(player);
}
spirit.setTarget((LivingEntity) entity);
int chance = EntityVengeanceSpiritConfig.nonDegradedSpawnChance;
spirit.setSwarm(chance <= 0 || world.random.nextInt(chance) > 0);
}
Expand Down

0 comments on commit 041803c

Please sign in to comment.