2024-03-03 22:40:18 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com>
Date: Sun, 3 Mar 2024 19:43:40 +0100
Subject: [PATCH] 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>
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java
2024-10-24 17:16:31 +02:00
index bfe39c7a9d2910bee9b40cf5ab4c154711d5cbb0..596146ad7899c21645df8834ce5f0afd6c1b0604 100644
2024-03-03 22:40:18 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java
2024-04-26 02:10:12 +02:00
@@ -37,16 +37,24 @@ public class CraftMushroomCow extends CraftCow implements MushroomCow, io.paperm
2024-03-03 22:40:18 +01:00
@Override
public boolean addEffectToNextStew(PotionEffect potionEffect, boolean overwrite) {
Preconditions.checkArgument(potionEffect != null, "PotionEffect cannot be null");
- MobEffectInstance minecraftPotionEffect = CraftPotionUtil.fromBukkit(potionEffect);
- if (!overwrite && this.hasEffectForNextStew(potionEffect.getType())) {
2024-04-26 02:10:12 +02:00
+ // Paper start - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
+ return this.addEffectToNextStew(io.papermc.paper.potion.SuspiciousEffectEntry.create(potionEffect.getType(), potionEffect.getDuration()), overwrite);
2024-03-03 22:40:18 +01:00
+ }
+
+ @Override
+ public boolean addEffectToNextStew(io.papermc.paper.potion.SuspiciousEffectEntry suspiciousEffectEntry, boolean overwrite) {
+ Preconditions.checkArgument(suspiciousEffectEntry != null, "SuspiciousEffectEntry cannot be null");
2024-04-26 02:10:12 +02:00
+ Holder<MobEffect> minecraftPotionEffect = CraftPotionEffectType.bukkitToMinecraftHolder(suspiciousEffectEntry.effect());
2024-04-25 12:27:57 +02:00
+ // Paper end - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
2024-03-03 22:40:18 +01:00
+ if (!overwrite && this.hasEffectForNextStew(suspiciousEffectEntry.effect())) {
return false;
}
2024-04-25 12:27:57 +02:00
SuspiciousStewEffects stewEffects = this.getHandle().stewEffects;
2024-04-26 02:10:12 +02:00
if (stewEffects == null) {
stewEffects = SuspiciousStewEffects.EMPTY;
}
- SuspiciousStewEffects.Entry recordSuspiciousEffect = new SuspiciousStewEffects.Entry(minecraftPotionEffect.getEffect(), minecraftPotionEffect.getDuration());
- this.removeEffectFromNextStew(potionEffect.getType()); // Avoid duplicates of effects
+ SuspiciousStewEffects.Entry recordSuspiciousEffect = new SuspiciousStewEffects.Entry(minecraftPotionEffect, suspiciousEffectEntry.duration()); // Paper - sus effect entry API
+ this.removeEffectFromNextStew(suspiciousEffectEntry.effect()); // Avoid duplicates of effects // Paper - sus effect entry API
this.getHandle().stewEffects = stewEffects.withEffectAdded(recordSuspiciousEffect);
return true;
}
2024-04-25 12:27:57 +02:00
@@ -101,6 +109,43 @@ public class CraftMushroomCow extends CraftCow implements MushroomCow, io.paperm
2024-10-24 17:16:31 +02:00
this.getHandle().setVariant(net.minecraft.world.entity.animal.MushroomCow.Variant.values()[variant.ordinal()]);
2024-03-03 22:40:18 +01:00
}
+ // Paper start
+ @Override
2024-04-26 02:10:12 +02:00
+ public List<io.papermc.paper.potion.SuspiciousEffectEntry> getStewEffects() {
2024-03-03 22:40:18 +01:00
+ if (this.getHandle().stewEffects == null) {
2024-04-26 02:10:12 +02:00
+ return List.of();
2024-03-03 22:40:18 +01:00
+ }
+
2024-04-26 02:10:12 +02:00
+ 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()),
2024-03-03 22:40:18 +01:00
+ effect.duration()
+ ));
+ }
+
2024-04-26 02:10:12 +02:00
+ return java.util.Collections.unmodifiableList(effectEntries);
2024-03-03 22:40:18 +01:00
+ }
+
+ @Override
2024-04-26 02:10:12 +02:00
+ public void setStewEffects(final List<io.papermc.paper.potion.SuspiciousEffectEntry> effects) {
2024-03-03 22:40:18 +01:00
+ if (effects.isEmpty()) {
+ this.getHandle().stewEffects = null;
+ return;
+ }
+
2024-04-26 02:10:12 +02:00
+ List<SuspiciousStewEffects.Entry> nmsPairs = new java.util.ArrayList<>(effects.size());
2024-03-03 22:40:18 +01:00
+ for (final io.papermc.paper.potion.SuspiciousEffectEntry effect : effects) {
2024-04-26 02:10:12 +02:00
+ nmsPairs.add(new SuspiciousStewEffects.Entry(
+ org.bukkit.craftbukkit.potion.CraftPotionEffectType.bukkitToMinecraftHolder(effect.effect()),
2024-03-03 22:40:18 +01:00
+ effect.duration()
+ ));
+ }
+
2024-04-26 02:10:12 +02:00
+ this.getHandle().stewEffects = new SuspiciousStewEffects(nmsPairs);
2024-03-03 22:40:18 +01:00
+ }
+ // Paper end
+
@Override
public String toString() {
return "CraftMushroomCow";
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSuspiciousStew.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSuspiciousStew.java
2024-06-15 18:28:18 +02:00
index 7a43e326e51300306b9c5c23a16ffae92030bd2b..192cd33f6276ea4928e29ac2cd4e45bf8bf82dcb 100644
2024-03-03 22:40:18 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSuspiciousStew.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSuspiciousStew.java
2024-06-15 18:28:18 +02:00
@@ -21,7 +21,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-04-25 12:27:57 +02:00
static final ItemMetaKeyType<SuspiciousStewEffects> EFFECTS = new ItemMetaKeyType<>(DataComponents.SUSPICIOUS_STEW_EFFECTS, "effects");
2024-03-03 22:40:18 +01:00
- 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) {
super(meta);
2024-06-15 18:28:18 +02:00
@@ -47,7 +47,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-04-26 02:10:12 +02:00
continue;
}
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
}
});
}
2024-06-15 18:28:18 +02:00
@@ -73,8 +73,8 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-04-25 12:27:57 +02:00
if (this.customEffects != null) {
List<SuspiciousStewEffects.Entry> effectList = new ArrayList<>();
2024-03-03 22:40:18 +01:00
- for (PotionEffect effect : this.customEffects) {
2024-04-25 12:27:57 +02:00
- effectList.add(new net.minecraft.world.item.component.SuspiciousStewEffects.Entry(CraftPotionEffectType.bukkitToMinecraftHolder(effect.getType()), effect.getDuration()));
2024-03-03 22:40:18 +01:00
+ for (io.papermc.paper.potion.SuspiciousEffectEntry effect : this.customEffects) {
2024-04-25 12:27:57 +02:00
+ 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
2024-03-03 22:40:18 +01:00
}
2024-04-25 12:27:57 +02:00
tag.put(CraftMetaSuspiciousStew.EFFECTS, new SuspiciousStewEffects(effectList));
2024-03-03 22:40:18 +01:00
}
2024-06-15 18:28:18 +02:00
@@ -106,7 +106,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-03-03 22:40:18 +01:00
@Override
public List<PotionEffect> getCustomEffects() {
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();
}
2024-06-15 18:28:18 +02:00
@@ -114,27 +114,47 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-03-03 22:40:18 +01:00
@Override
public boolean addCustomEffect(PotionEffect effect, boolean overwrite) {
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());
2024-04-26 02:10:12 +02:00
- if (index != -1) {
2024-04-26 23:29:16 +02:00
- if (overwrite) {
- PotionEffect old = this.customEffects.get(index);
- if (old.getDuration() == effect.getDuration()) {
2024-03-03 22:40:18 +01:00
+ // Paper start - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
+ @Override
+ public boolean addCustomEffect(final io.papermc.paper.potion.SuspiciousEffectEntry suspiciousEffectEntry, final boolean overwrite) {
+ Preconditions.checkArgument(suspiciousEffectEntry != null, "Suspicious effect entry cannot be null");
2024-04-26 23:29:16 +02:00
+ 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;
+ }
2024-04-26 02:10:12 +02:00
+ }
2024-04-26 23:29:16 +02:00
+ if (foundMatchingDuration && !mutated) {
+ return false;
+ } else if (!foundMatchingDuration) {
+ this.customEffects.add(suspiciousEffectEntry);
+ }
+ return true;
+ } else {
2024-03-03 22:40:18 +01:00
return false;
}
- this.customEffects.set(index, effect);
2024-04-26 23:29:16 +02:00
- return true;
2024-04-26 02:10:12 +02:00
- } else {
2024-04-26 23:29:16 +02:00
- return false;
2024-03-03 22:40:18 +01:00
}
2024-04-26 23:29:16 +02:00
- } else {
- if (this.customEffects == null) {
- this.customEffects = new ArrayList<>();
- }
2024-03-03 22:40:18 +01:00
- this.customEffects.add(effect);
2024-04-26 23:29:16 +02:00
- return true;
2024-03-03 22:40:18 +01:00
}
2024-04-26 23:29:16 +02:00
+ if (this.customEffects == null) {
+ this.customEffects = new ArrayList<>();
+ }
+ this.customEffects.add(suspiciousEffectEntry);
+ return true;
2024-03-03 22:40:18 +01:00
}
+ // Paper end - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
@Override
public boolean removeCustomEffect(PotionEffectType type) {
2024-06-15 18:28:18 +02:00
@@ -145,10 +165,12 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-03-03 22:40:18 +01:00
}
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()) {
- PotionEffect effect = iterator.next();
- if (type.equals(effect.getType())) {
+ io.papermc.paper.potion.SuspiciousEffectEntry effect = iterator.next();
+ if (type.equals(effect.effect())) {
+ // Paper end - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta
iterator.remove();
changed = true;
}
2024-06-15 18:28:18 +02:00
@@ -171,7 +193,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-03-03 22:40:18 +01:00
}
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;
}
}
2024-06-15 18:28:18 +02:00
@@ -216,7 +238,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious
2024-03-03 22:40:18 +01:00
super.serialize(builder);
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;