Skip to content

Commit

Permalink
Fix potion effect removing on death (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
IzzelAliz committed Jun 12, 2024
1 parent 8b98000 commit 972207b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import io.izzel.arclight.common.bridge.core.entity.LivingEntityBridge;
import io.izzel.arclight.common.bridge.core.entity.player.PlayerEntityBridge;
import io.izzel.arclight.common.bridge.core.entity.player.ServerPlayerEntityBridge;
import io.izzel.arclight.common.util.IteratorUtil;
import io.izzel.arclight.mixin.Decorate;
import io.izzel.arclight.mixin.DecorationOps;
import io.izzel.arclight.mixin.Eject;
import io.izzel.tools.collection.XmapList;
import net.minecraft.advancements.CriteriaTriggers;
Expand Down Expand Up @@ -94,7 +97,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

@SuppressWarnings({"ConstantConditions", "Guava"})
@Mixin(LivingEntity.class)
Expand Down Expand Up @@ -456,16 +458,14 @@ public int getExpReward() {
}
}

@Redirect(method = "removeAllEffects", at = @At(value = "INVOKE", target = "Ljava/util/Map;values()Ljava/util/Collection;"))
private Collection<MobEffectInstance> arclight$clearReason(Map<MobEffect, MobEffectInstance> instance) {
@SuppressWarnings("unchecked")
@Decorate(method = "removeAllEffects", at = @At(value = "INVOKE", target = "Ljava/util/Collection;iterator()Ljava/util/Iterator;"))
private Iterator<MobEffectInstance> arclight$clearReason(Collection<MobEffectInstance> instance) throws Throwable {
var cause = bridge$getEffectCause().orElse(EntityPotionEffectEvent.Cause.UNKNOWN);
return instance.values().stream().filter(effect -> {
return IteratorUtil.filter((Iterator<MobEffectInstance>) DecorationOps.callsite().invoke(instance), effect -> {
EntityPotionEffectEvent event = CraftEventFactory.callEntityPotionEffectChangeEvent((LivingEntity) (Object) this, effect, null, cause, EntityPotionEffectEvent.Action.CLEARED);
if (event.isCancelled()) {
return false;
}
return true;
}).collect(Collectors.toList());
return !event.isCancelled();
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.izzel.arclight.common.util;

import java.util.Iterator;
import java.util.function.Predicate;

public class IteratorUtil {

public static <T> Iterator<T> filter(Iterator<T> iterator, Predicate<T> predicate) {
return new FilterIterator<>(iterator, predicate);
}

private static class FilterIterator<T> implements Iterator<T> {

private final Iterator<T> iterator;
private final Predicate<T> predicate;

private boolean hasNext = false;
private T next;

private FilterIterator(Iterator<T> iterator, Predicate<T> predicate) {
this.iterator = iterator;
this.predicate = predicate;
this.computeNext();
}

@Override
public boolean hasNext() {
return hasNext;
}

@Override
public T next() {
try {
return this.next;
} finally {
computeNext();
}
}

private void computeNext() {
while (iterator.hasNext()) {
T next = iterator.next();
if (predicate.test(next)) {
hasNext = true;
this.next = next;
return;
}
}
hasNext = false;
this.next = null;
}

@Override
public void remove() {
iterator.remove();
}
}
}

0 comments on commit 972207b

Please sign in to comment.