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 diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java index 9cc81bcccbf1141f66fedada1359b7c0dfa8e22a..7491c7cf38d888b31a509613d237b55f9732400a 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftMushroomCow.java @@ -34,11 +34,19 @@ public class CraftMushroomCow extends CraftCow implements MushroomCow, io.paperm return ImmutableList.of(); } + // Paper start - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta @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())) { + return 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"); + MobEffect minecraftPotionEffect = CraftPotionEffectType.bukkitToMinecraft(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; } SuspiciousStewEffects stewEffects = this.getHandle().stewEffects; @@ -101,6 +109,43 @@ public class CraftMushroomCow extends CraftCow implements MushroomCow, io.paperm this.getHandle().setVariant(net.minecraft.world.entity.animal.MushroomCow.MushroomType.values()[variant.ordinal()]); } + // Paper start + @Override + public java.util.List getStewEffects() { + if (this.getHandle().stewEffects == null) { + return java.util.List.of(); + } + + java.util.List nmsPairs = new java.util.ArrayList<>(this.getHandle().stewEffects.size()); + for (final net.minecraft.world.level.block.SuspiciousEffectHolder.EffectEntry effect : this.getHandle().stewEffects) { + nmsPairs.add(io.papermc.paper.potion.SuspiciousEffectEntry.create( + org.bukkit.craftbukkit.potion.CraftPotionEffectType.minecraftToBukkit(effect.effect()), + effect.duration() + )); + } + + return java.util.Collections.unmodifiableList(nmsPairs); + } + + @Override + public void setStewEffects(final java.util.List effects) { + if (effects.isEmpty()) { + this.getHandle().stewEffects = null; + return; + } + + java.util.List nmsPairs = new java.util.ArrayList<>(effects.size()); + for (final io.papermc.paper.potion.SuspiciousEffectEntry effect : effects) { + nmsPairs.add(new net.minecraft.world.level.block.SuspiciousEffectHolder.EffectEntry( + org.bukkit.craftbukkit.potion.CraftPotionEffectType.bukkitToMinecraft(effect.effect()), + effect.duration() + )); + } + + this.getHandle().stewEffects = nmsPairs; + } + // 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 index 8fc3cd507d333d2bdea759d7c102a56e88ad5f5a..6b229ec0d4110fd0e758610bfacd9e8ec71ad2c5 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSuspiciousStew.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSuspiciousStew.java @@ -22,7 +22,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious static final ItemMetaKeyType EFFECTS = new ItemMetaKeyType<>(DataComponents.SUSPICIOUS_STEW_EFFECTS, "effects"); - private List customEffects; + private List customEffects; // Paper - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta CraftMetaSuspiciousStew(CraftMetaItem meta) { super(meta); @@ -74,8 +74,8 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious if (this.customEffects != null) { List effectList = new ArrayList<>(); - for (PotionEffect effect : this.customEffects) { - effectList.add(new net.minecraft.world.item.component.SuspiciousStewEffects.Entry(CraftPotionEffectType.bukkitToMinecraftHolder(effect.getType()), effect.getDuration())); + for (io.papermc.paper.potion.SuspiciousEffectEntry effect : this.customEffects) { + 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)); } @@ -112,7 +112,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious @Override public List 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(); } @@ -120,15 +120,21 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious @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()); + // 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"); + int index = this.indexOfEffect(suspiciousEffectEntry.effect()); if (index != -1) { if (overwrite) { - PotionEffect old = this.customEffects.get(index); - if (old.getDuration() == effect.getDuration()) { + io.papermc.paper.potion.SuspiciousEffectEntry old = this.customEffects.get(index); + if (old.duration() == suspiciousEffectEntry.duration()) { return false; } - this.customEffects.set(index, effect); + this.customEffects.set(index, suspiciousEffectEntry); return true; } else { return false; @@ -137,10 +143,11 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious if (this.customEffects == null) { this.customEffects = new ArrayList<>(); } - this.customEffects.add(effect); + this.customEffects.add(suspiciousEffectEntry); return true; } } + // Paper end - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta @Override public boolean removeCustomEffect(PotionEffectType type) { @@ -151,10 +158,12 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious } boolean changed = false; - Iterator iterator = this.customEffects.iterator(); + // Paper start - add overloads to use suspicious effect entry to mushroom cow and suspicious stew meta + Iterator 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; } @@ -177,7 +186,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious } 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; } } @@ -222,7 +231,7 @@ public class CraftMetaSuspiciousStew extends CraftMetaItem implements Suspicious 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;