Suspicious Effect Entry API
Exposes a new suspicious effect entry type that properly represents storable effects in the context of suspicious effects as they only define the potion effect type and duration. This differentiates them from the existing PotionEffect API found in bukkit and hence clarifies that storable values in the parts of the API in which it replaces PotionEffect. Co-authored-by: Yannick Lamprecht <yannicklamprecht@live.de>
This commit is contained in:
@@ -37,16 +37,24 @@ public class CraftMushroomCow extends CraftCow implements MushroomCow, io.paperm
|
|||||||
@Override
|
@Override
|
||||||
public boolean addEffectToNextStew(PotionEffect potionEffect, boolean overwrite) {
|
public boolean addEffectToNextStew(PotionEffect potionEffect, boolean overwrite) {
|
||||||
Preconditions.checkArgument(potionEffect != null, "PotionEffect cannot be null");
|
Preconditions.checkArgument(potionEffect != null, "PotionEffect cannot be null");
|
||||||
MobEffectInstance minecraftPotionEffect = CraftPotionUtil.fromBukkit(potionEffect);
|
// Paper start - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
if (!overwrite && this.hasEffectForNextStew(potionEffect.getType())) {
|
return this.addEffectToNextStew(io.papermc.paper.potion.SuspiciousEffectEntry.create(potionEffect.getType(), potionEffect.getDuration()), overwrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addEffectToNextStew(io.papermc.paper.potion.SuspiciousEffectEntry suspiciousEffectEntry, boolean overwrite) {
|
||||||
|
Preconditions.checkArgument(suspiciousEffectEntry != null, "SuspiciousEffectEntry cannot be null");
|
||||||
|
Holder<MobEffect> minecraftPotionEffect = CraftPotionEffectType.bukkitToMinecraftHolder(suspiciousEffectEntry.effect());
|
||||||
|
// Paper end - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
|
if (!overwrite && this.hasEffectForNextStew(suspiciousEffectEntry.effect())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
SuspiciousStewEffects stewEffects = this.getHandle().stewEffects;
|
SuspiciousStewEffects stewEffects = this.getHandle().stewEffects;
|
||||||
if (stewEffects == null) {
|
if (stewEffects == null) {
|
||||||
stewEffects = SuspiciousStewEffects.EMPTY;
|
stewEffects = SuspiciousStewEffects.EMPTY;
|
||||||
}
|
}
|
||||||
SuspiciousStewEffects.Entry recordSuspiciousEffect = new SuspiciousStewEffects.Entry(minecraftPotionEffect.getEffect(), minecraftPotionEffect.getDuration());
|
SuspiciousStewEffects.Entry recordSuspiciousEffect = new SuspiciousStewEffects.Entry(minecraftPotionEffect, suspiciousEffectEntry.duration()); // Paper - sus effect entry API
|
||||||
this.removeEffectFromNextStew(potionEffect.getType()); // Avoid duplicates of effects
|
this.removeEffectFromNextStew(suspiciousEffectEntry.effect()); // Avoid duplicates of effects // Paper - sus effect entry API
|
||||||
this.getHandle().stewEffects = stewEffects.withEffectAdded(recordSuspiciousEffect);
|
this.getHandle().stewEffects = stewEffects.withEffectAdded(recordSuspiciousEffect);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -101,6 +109,43 @@ public class CraftMushroomCow extends CraftCow implements MushroomCow, io.paperm
|
|||||||
this.getHandle().setVariant(net.minecraft.world.entity.animal.MushroomCow.Variant.values()[variant.ordinal()]);
|
this.getHandle().setVariant(net.minecraft.world.entity.animal.MushroomCow.Variant.values()[variant.ordinal()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Paper start
|
||||||
|
@Override
|
||||||
|
public List<io.papermc.paper.potion.SuspiciousEffectEntry> getStewEffects() {
|
||||||
|
if (this.getHandle().stewEffects == null) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<io.papermc.paper.potion.SuspiciousEffectEntry> effectEntries = new java.util.ArrayList<>(this.getHandle().stewEffects.effects().size());
|
||||||
|
for (final SuspiciousStewEffects.Entry effect : this.getHandle().stewEffects.effects()) {
|
||||||
|
effectEntries.add(io.papermc.paper.potion.SuspiciousEffectEntry.create(
|
||||||
|
org.bukkit.craftbukkit.potion.CraftPotionEffectType.minecraftHolderToBukkit(effect.effect()),
|
||||||
|
effect.duration()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return java.util.Collections.unmodifiableList(effectEntries);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStewEffects(final List<io.papermc.paper.potion.SuspiciousEffectEntry> effects) {
|
||||||
|
if (effects.isEmpty()) {
|
||||||
|
this.getHandle().stewEffects = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<SuspiciousStewEffects.Entry> nmsPairs = new java.util.ArrayList<>(effects.size());
|
||||||
|
for (final io.papermc.paper.potion.SuspiciousEffectEntry effect : effects) {
|
||||||
|
nmsPairs.add(new SuspiciousStewEffects.Entry(
|
||||||
|
org.bukkit.craftbukkit.potion.CraftPotionEffectType.bukkitToMinecraftHolder(effect.effect()),
|
||||||
|
effect.duration()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.getHandle().stewEffects = new SuspiciousStewEffects(nmsPairs);
|
||||||
|
}
|
||||||
|
// Paper end
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "CraftMushroomCow";
|
return "CraftMushroomCow";
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
|
|
||||||
static final ItemMetaKeyType<SuspiciousStewEffects> EFFECTS = new ItemMetaKeyType<>(DataComponents.SUSPICIOUS_STEW_EFFECTS, "effects");
|
static final ItemMetaKeyType<SuspiciousStewEffects> EFFECTS = new ItemMetaKeyType<>(DataComponents.SUSPICIOUS_STEW_EFFECTS, "effects");
|
||||||
|
|
||||||
private List<PotionEffect> customEffects;
|
private List<io.papermc.paper.potion.SuspiciousEffectEntry> customEffects; // Paper - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
|
|
||||||
CraftMetaSuspiciousStew(CraftMetaItem meta) {
|
CraftMetaSuspiciousStew(CraftMetaItem meta) {
|
||||||
super(meta);
|
super(meta);
|
||||||
@@ -47,7 +47,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int duration = effect.duration();
|
int duration = effect.duration();
|
||||||
this.customEffects.add(new PotionEffect(type, duration, 0));
|
this.customEffects.add(io.papermc.paper.potion.SuspiciousEffectEntry.create(type, duration)); // Paper - use suspicious effect entry for suspicious stew meta
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -73,8 +73,8 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
if (this.customEffects != null) {
|
if (this.customEffects != null) {
|
||||||
List<SuspiciousStewEffects.Entry> effectList = new ArrayList<>();
|
List<SuspiciousStewEffects.Entry> effectList = new ArrayList<>();
|
||||||
|
|
||||||
for (PotionEffect effect : this.customEffects) {
|
for (io.papermc.paper.potion.SuspiciousEffectEntry effect : this.customEffects) {
|
||||||
effectList.add(new net.minecraft.world.item.component.SuspiciousStewEffects.Entry(CraftPotionEffectType.bukkitToMinecraftHolder(effect.getType()), effect.getDuration()));
|
effectList.add(new net.minecraft.world.item.component.SuspiciousStewEffects.Entry(CraftPotionEffectType.bukkitToMinecraftHolder(effect.effect()), effect.duration())); // Paper - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
}
|
}
|
||||||
tag.put(CraftMetaSuspiciousStew.EFFECTS, new SuspiciousStewEffects(effectList));
|
tag.put(CraftMetaSuspiciousStew.EFFECTS, new SuspiciousStewEffects(effectList));
|
||||||
}
|
}
|
||||||
@@ -106,7 +106,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
@Override
|
@Override
|
||||||
public List<PotionEffect> getCustomEffects() {
|
public List<PotionEffect> getCustomEffects() {
|
||||||
if (this.hasCustomEffects()) {
|
if (this.hasCustomEffects()) {
|
||||||
return ImmutableList.copyOf(this.customEffects);
|
return this.customEffects.stream().map(suspiciousEffectEntry -> suspiciousEffectEntry.effect().createEffect(suspiciousEffectEntry.duration(), 0)).toList(); // Paper - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
}
|
}
|
||||||
return ImmutableList.of();
|
return ImmutableList.of();
|
||||||
}
|
}
|
||||||
@@ -114,27 +114,47 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
@Override
|
@Override
|
||||||
public boolean addCustomEffect(PotionEffect effect, boolean overwrite) {
|
public boolean addCustomEffect(PotionEffect effect, boolean overwrite) {
|
||||||
Preconditions.checkArgument(effect != null, "Potion effect cannot be null");
|
Preconditions.checkArgument(effect != null, "Potion effect cannot be null");
|
||||||
|
return addCustomEffect(io.papermc.paper.potion.SuspiciousEffectEntry.create(effect.getType(), effect.getDuration()), overwrite); // Paper - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
int index = this.indexOfEffect(effect.getType());
|
}
|
||||||
if (index != -1) {
|
|
||||||
if (overwrite) {
|
// Paper start - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
PotionEffect old = this.customEffects.get(index);
|
@Override
|
||||||
if (old.getDuration() == effect.getDuration()) {
|
public boolean addCustomEffect(final io.papermc.paper.potion.SuspiciousEffectEntry suspiciousEffectEntry, final boolean overwrite) {
|
||||||
return false;
|
Preconditions.checkArgument(suspiciousEffectEntry != null, "Suspicious effect entry cannot be null");
|
||||||
|
if (this.hasCustomEffects()) {
|
||||||
|
final List<io.papermc.paper.potion.SuspiciousEffectEntry> matchingEffects = this.customEffects.stream().filter(
|
||||||
|
entry -> entry.effect() == suspiciousEffectEntry.effect()
|
||||||
|
).toList();
|
||||||
|
if (!matchingEffects.isEmpty()) {
|
||||||
|
if (overwrite) {
|
||||||
|
boolean foundMatchingDuration = false;
|
||||||
|
boolean mutated = false;
|
||||||
|
for (final io.papermc.paper.potion.SuspiciousEffectEntry matchingEffect : matchingEffects) {
|
||||||
|
if (matchingEffect.duration() != suspiciousEffectEntry.duration()) {
|
||||||
|
this.customEffects.remove(suspiciousEffectEntry);
|
||||||
|
mutated = true;
|
||||||
|
} else {
|
||||||
|
foundMatchingDuration = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (foundMatchingDuration && !mutated) {
|
||||||
|
return false;
|
||||||
|
} else if (!foundMatchingDuration) {
|
||||||
|
this.customEffects.add(suspiciousEffectEntry);
|
||||||
}
|
}
|
||||||
this.customEffects.set(index, effect);
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
}
|
||||||
if (this.customEffects == null) {
|
if (this.customEffects == null) {
|
||||||
this.customEffects = new ArrayList<>();
|
this.customEffects = new ArrayList<>();
|
||||||
}
|
}
|
||||||
this.customEffects.add(effect);
|
this.customEffects.add(suspiciousEffectEntry);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
// Paper end - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeCustomEffect(PotionEffectType type) {
|
public boolean removeCustomEffect(PotionEffectType type) {
|
||||||
@@ -145,10 +165,12 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean changed = false;
|
boolean changed = false;
|
||||||
Iterator<PotionEffect> iterator = this.customEffects.iterator();
|
// Paper start - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
|
Iterator<io.papermc.paper.potion.SuspiciousEffectEntry> iterator = this.customEffects.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
PotionEffect effect = iterator.next();
|
io.papermc.paper.potion.SuspiciousEffectEntry effect = iterator.next();
|
||||||
if (type.equals(effect.getType())) {
|
if (type.equals(effect.effect())) {
|
||||||
|
// Paper end - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
@@ -171,7 +193,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < this.customEffects.size(); i++) {
|
for (int i = 0; i < this.customEffects.size(); i++) {
|
||||||
if (this.customEffects.get(i).getType().equals(type)) {
|
if (this.customEffects.get(i).effect().equals(type)) { // Paper - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -216,7 +238,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
|
|||||||
super.serialize(builder);
|
super.serialize(builder);
|
||||||
|
|
||||||
if (this.hasCustomEffects()) {
|
if (this.hasCustomEffects()) {
|
||||||
builder.put(CraftMetaSuspiciousStew.EFFECTS.BUKKIT, ImmutableList.copyOf(this.customEffects));
|
builder.put(CraftMetaSuspiciousStew.EFFECTS.BUKKIT, ImmutableList.copyOf(com.google.common.collect.Lists.transform(this.customEffects, s -> new PotionEffect(s.effect(), s.duration(), 0)))); // Paper - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta - convert back to potion effect for bukkit legacy item serialisation to maintain backwards compatibility for the written format.
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|||||||
Reference in New Issue
Block a user