Don't add an effect multiple times

This commit is contained in:
Mrbysco 2024-10-22 23:01:57 +02:00
parent 0d27a4a304
commit 90cecc8ed8

View file

@ -109,18 +109,32 @@ public class MethodHandler implements IMethodHandler {
@Override @Override
public void addEffectProperties(ItemStack stack, MobEffectInstance effect, boolean addDur, boolean addAmp) { public void addEffectProperties(ItemStack stack, MobEffectInstance effect, boolean addDur, boolean addAmp) {
MobEffectInstance[] effects = this.getEffectsFromStack(stack); MobEffectInstance[] effects = this.getEffectsFromStack(stack);
boolean effectUpdated = false;
for (int i = 0; i < effects.length; i++) { for (int i = 0; i < effects.length; i++) {
if (effects[i].getEffect() == effect.getEffect()) { if (effects[i].getEffect() == effect.getEffect()) {
effects[i] = new MobEffectInstance(effects[i].getEffect(), effects[i].getDuration() + (addDur effects[i] = new MobEffectInstance(effects[i].getEffect(),
? effect.getDuration() effects[i].getDuration() + (addDur ? effect.getDuration() : 0),
: 0), effects[i].getAmplifier() + (addAmp effects[i].getAmplifier() + (addAmp ? effect.getAmplifier() > 0 ? effect.getAmplifier() : 1 : 0));
? effect.getAmplifier() > 0 effectUpdated = true;
? effect.getAmplifier() break;
: 1
: 0));
} }
this.addEffectToStack(stack, effects[i]);
} }
if (!effectUpdated) {
this.addEffectToStack(stack, effect);
} else {
// Clear existing effects from the stack
this.clearEffectsFromStack(stack);
// Add updated effects to the stack
for (MobEffectInstance updatedEffect : effects) {
this.addEffectToStack(stack, updatedEffect);
}
}
}
private void clearEffectsFromStack(ItemStack stack) {
CompoundTag tag = stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag();
tag.remove("Counter");
stack.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
} }
@Override @Override