From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Racci Date: Sat, 4 Dec 2021 00:07:05 +1100 Subject: [PATCH] Potion NamespacedKey diff --git a/src/main/java/net/minecraft/world/effect/MobEffectInstance.java b/src/main/java/net/minecraft/world/effect/MobEffectInstance.java index bf304db1f9506d3e83d16cd632b9155c39346634..d94497083f3da89dbef6479ef8f70591e630e3e7 100644 --- a/src/main/java/net/minecraft/world/effect/MobEffectInstance.java +++ b/src/main/java/net/minecraft/world/effect/MobEffectInstance.java @@ -36,6 +36,7 @@ public class MobEffectInstance implements Comparable { private boolean showIcon; @Nullable public MobEffectInstance hiddenEffect; + private org.bukkit.NamespacedKey key; // Purpur - add key private final Optional factorData; public MobEffectInstance(MobEffect type) { @@ -54,17 +55,36 @@ public class MobEffectInstance implements Comparable { this(type, duration, amplifier, ambient, visible, visible); } + // Purpur start + public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean visible, @Nullable org.bukkit.NamespacedKey key) { + this(type, duration, amplifier, ambient, visible, visible, key); + } + // Purpur end + public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean showParticles, boolean showIcon) { - this(type, duration, amplifier, ambient, showParticles, showIcon, (MobEffectInstance)null, type.createFactorData()); + // Purpur start + this(type, duration, amplifier, ambient, showParticles, showIcon, (MobEffectInstance)null, type.createFactorData(), (org.bukkit.NamespacedKey)null); + } + + public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean showParticles, boolean showIcon, @Nullable org.bukkit.NamespacedKey key) { + this(type, duration, amplifier, ambient, showParticles, showIcon, (MobEffectInstance)null, type.createFactorData(), key); + // Purpur end } public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean showParticles, boolean showIcon, @Nullable MobEffectInstance hiddenEffect, Optional factorCalculationData) { + // Purpur start + this(type, duration, amplifier, ambient, showParticles, showIcon, hiddenEffect, factorCalculationData, (org.bukkit.NamespacedKey) null); + } + + public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean showParticles, boolean showIcon, @Nullable MobEffectInstance hiddenEffect, Optional factorCalculationData, @Nullable org.bukkit.NamespacedKey key) { + // Purpur end this.effect = type; this.duration = duration; this.amplifier = amplifier; this.ambient = ambient; this.visible = showParticles; this.showIcon = showIcon; + this.key = key; // Purpur - add key this.hiddenEffect = hiddenEffect; this.factorData = factorCalculationData; } @@ -85,6 +105,7 @@ public class MobEffectInstance implements Comparable { this.ambient = that.ambient; this.visible = that.visible; this.showIcon = that.showIcon; + this.key = that.key; // Purpur - add key } public boolean update(MobEffectInstance that) { @@ -129,6 +150,13 @@ public class MobEffectInstance implements Comparable { bl = true; } + // Purpur start + if (that.key != this.key) { + this.key = that.key; + bl = true; + } + // Purpur end + return bl; } @@ -172,6 +200,17 @@ public class MobEffectInstance implements Comparable { return this.showIcon; } + // Purpur start + public boolean hasKey() { + return this.key != null; + } + + @Nullable + public org.bukkit.NamespacedKey getKey() { + return this.key; + } + // Purpur end + public boolean tick(LivingEntity entity, Runnable overwriteCallback) { if (this.hasRemainingDuration()) { int i = this.isInfiniteDuration() ? entity.tickCount : this.duration; @@ -232,6 +271,12 @@ public class MobEffectInstance implements Comparable { string = string + ", Show Icon: false"; } + // Purpur start + if (this.hasKey()) { + string = string + ", Key: " + this.key; + } + // Purpur end + return string; } @@ -247,7 +292,7 @@ public class MobEffectInstance implements Comparable { return false; } else { MobEffectInstance mobEffectInstance = (MobEffectInstance)object; - return this.duration == mobEffectInstance.duration && this.amplifier == mobEffectInstance.amplifier && this.ambient == mobEffectInstance.ambient && this.effect.equals(mobEffectInstance.effect); + return this.duration == mobEffectInstance.duration && this.amplifier == mobEffectInstance.amplifier && this.ambient == mobEffectInstance.ambient && this.effect.equals(mobEffectInstance.effect) && this.key == mobEffectInstance.key; // Purpur - add key } } @@ -272,6 +317,11 @@ public class MobEffectInstance implements Comparable { nbt.putBoolean("ambient", this.isAmbient()); nbt.putBoolean("show_particles", this.isVisible()); nbt.putBoolean("show_icon", this.showIcon()); + // Purpur start + if (this.key != null) { + nbt.putString("key", this.key.toString()); + } + // Purpur end if (this.hiddenEffect != null) { CompoundTag compoundTag = new CompoundTag(); this.hiddenEffect.save(compoundTag); @@ -306,6 +356,13 @@ public class MobEffectInstance implements Comparable { bl3 = nbt.getBoolean("show_icon"); } + // Purpur start + org.bukkit.NamespacedKey key = null; + if (nbt.contains("key")) { + key = org.bukkit.NamespacedKey.fromString(nbt.getString("key")); + } + // Purpur end + MobEffectInstance mobEffectInstance = null; if (nbt.contains("hidden_effect", 10)) { mobEffectInstance = loadSpecifiedEffect(type, nbt.getCompound("hidden_effect")); @@ -318,7 +375,7 @@ public class MobEffectInstance implements Comparable { optional = Optional.empty(); } - return new MobEffectInstance(type, j, Math.max(i, 0), bl, bl2, bl3, mobEffectInstance, optional); + return new MobEffectInstance(type, j, Math.max(i, 0), bl, bl2, bl3, mobEffectInstance, optional, key); // Purpur - add key } @Override diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java index bac3a5c378054481e1a5abaec1f83afde5d64ac1..f1050bf2b9efc54a894426b08989d44566acd875 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java @@ -45,6 +45,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta { static final ItemMetaKey POTION_COLOR = new ItemMetaKey("CustomPotionColor", "custom-color"); static final ItemMetaKey ID = new ItemMetaKey("id", "potion-id"); static final ItemMetaKey DEFAULT_POTION = new ItemMetaKey("Potion", "potion-type"); + static final ItemMetaKey KEY = new ItemMetaKey("key", "namespacedkey"); // Purpur - add key // Having an initial "state" in ItemMeta seems bit dirty but the UNCRAFTABLE potion type // is treated as the empty form of the meta because it represents an empty potion with no effect @@ -97,7 +98,13 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta { boolean ambient = effect.getBoolean(CraftMetaPotion.AMBIENT.NBT); boolean particles = effect.contains(CraftMetaPotion.SHOW_PARTICLES.NBT, CraftMagicNumbers.NBT.TAG_BYTE) ? effect.getBoolean(CraftMetaPotion.SHOW_PARTICLES.NBT) : true; boolean icon = effect.contains(CraftMetaPotion.SHOW_ICON.NBT, CraftMagicNumbers.NBT.TAG_BYTE) ? effect.getBoolean(CraftMetaPotion.SHOW_ICON.NBT) : particles; - this.customEffects.add(new PotionEffect(type, duration, amp, ambient, particles, icon)); + // Purpur start + NamespacedKey key = null; + if (tag.contains(CraftMetaPotion.KEY.NBT)) { + key = NamespacedKey.fromString(effect.getString(CraftMetaPotion.KEY.NBT)); + } + this.customEffects.add(new PotionEffect(type, duration, amp, ambient, particles, icon, key)); + // Purpur end } } } @@ -150,6 +157,11 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta { effectData.putBoolean(CraftMetaPotion.AMBIENT.NBT, effect.isAmbient()); effectData.putBoolean(CraftMetaPotion.SHOW_PARTICLES.NBT, effect.hasParticles()); effectData.putBoolean(CraftMetaPotion.SHOW_ICON.NBT, effect.hasIcon()); + // Purpur start + if (effect.hasKey()) { + effectData.putString(CraftMetaPotion.KEY.NBT, effect.getKey().toString()); + } + // Purpur end effectList.add(effectData); } } @@ -225,7 +237,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta { if (index != -1) { if (overwrite) { PotionEffect old = this.customEffects.get(index); - if (old.getAmplifier() == effect.getAmplifier() && old.getDuration() == effect.getDuration() && old.isAmbient() == effect.isAmbient()) { + if (old.getAmplifier() == effect.getAmplifier() && old.getDuration() == effect.getDuration() && old.isAmbient() == effect.isAmbient() && old.getKey() == effect.getKey()) { // Purpur - add key return false; } this.customEffects.set(index, effect); diff --git a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionUtil.java b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionUtil.java index e938255fcc5db0c289d3e132175a541187e4a748..f7a747ea73a80c97d863e0fd3772a0c333aef3c8 100644 --- a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionUtil.java +++ b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionUtil.java @@ -74,7 +74,7 @@ public class CraftPotionUtil { public static MobEffectInstance fromBukkit(PotionEffect effect) { MobEffect type = CraftPotionEffectType.bukkitToMinecraft(effect.getType()); // Paper - Note: do not copy over the hidden effect, as this method is only used for applying to entities which we do not want to convert over. - return new MobEffectInstance(type, effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles(), effect.hasIcon()); // Paper + return new MobEffectInstance(type, effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles(), effect.hasIcon(), effect.getKey()); // Paper // Purpur - add key } public static PotionEffect toBukkit(MobEffectInstance effect) { @@ -83,7 +83,7 @@ public class CraftPotionUtil { int duration = effect.getDuration(); boolean ambient = effect.isAmbient(); boolean particles = effect.isVisible(); - return new PotionEffect(type, duration, amp, ambient, particles, effect.showIcon(), effect.hiddenEffect == null ? null : toBukkit(effect.hiddenEffect)); // Paper + return new PotionEffect(type, duration, amp, ambient, particles, effect.showIcon(), effect.hiddenEffect == null ? null : toBukkit(effect.hiddenEffect), effect.getKey()); // Paper // Purpur - add key } public static boolean equals(MobEffect mobEffect, PotionEffectType type) { diff --git a/src/test/java/org/bukkit/potion/PotionTest.java b/src/test/java/org/bukkit/potion/PotionTest.java index 8963d93e99bdaf719fa160c11dd5af6a1d86f9a4..d852d8b14f5000415cbb4f06601059b3934b7efc 100644 --- a/src/test/java/org/bukkit/potion/PotionTest.java +++ b/src/test/java/org/bukkit/potion/PotionTest.java @@ -9,6 +9,7 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.item.alchemy.Potion; +import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.potion.CraftPotionEffectType; import org.bukkit.support.AbstractTestingBase; import org.junit.jupiter.api.Test; @@ -46,4 +47,27 @@ public class PotionTest extends AbstractTestingBase { assertEquals(bukkit, byName, "Same type not returned by name " + key); } } + + // Purpur start + @Test + public void testNamespacedKey() { + NamespacedKey key = new NamespacedKey("testnamespace", "testkey"); + PotionEffect namedSpacedEffect = new PotionEffect(PotionEffectType.DOLPHINS_GRACE, 20, 0, true, true, true, key); + assertNotNull(namedSpacedEffect.getKey()); + assertTrue(namedSpacedEffect.hasKey()); + assertFalse(namedSpacedEffect.withKey(null).hasKey()); + + PotionEffect effect = new PotionEffect(PotionEffectType.DOLPHINS_GRACE, 20, 0, true, true, true); + assertNull(effect.getKey()); + assertFalse(effect.hasKey()); + assertTrue(namedSpacedEffect.withKey(key).hasKey()); + + Map s1 = namedSpacedEffect.serialize(); + Map s2 = effect.serialize(); + assertTrue(s1.containsKey("namespacedKey")); + assertFalse(s2.containsKey("namespacedKey")); + assertNotNull(new PotionEffect(s1).getKey()); + assertNull(new PotionEffect(s2).getKey()); + } + // Purpur end }