mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-17 16:37:43 +01:00
311 lines
16 KiB
Diff
311 lines
16 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Racci <tangentmoons@gmail.com>
|
|
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 f5c9be3fde2654bd5a6b3ee737afe96a9393e836..3aecf016ce182391e20c8d31b9399a9855ceb3e6 100644
|
|
--- a/src/main/java/net/minecraft/world/effect/MobEffectInstance.java
|
|
+++ b/src/main/java/net/minecraft/world/effect/MobEffectInstance.java
|
|
@@ -13,6 +13,7 @@ import net.minecraft.util.ExtraCodecs;
|
|
import net.minecraft.util.Mth;
|
|
import net.minecraft.world.entity.LivingEntity;
|
|
import org.slf4j.Logger;
|
|
+import org.bukkit.NamespacedKey;
|
|
|
|
public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
private static final Logger LOGGER = LogUtils.getLogger();
|
|
@@ -24,6 +25,7 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
private boolean visible;
|
|
private boolean showIcon;
|
|
@Nullable
|
|
+ private NamespacedKey key; // Purpur - add key
|
|
private MobEffectInstance hiddenEffect;
|
|
private Optional<MobEffectInstance.FactorData> factorData;
|
|
|
|
@@ -43,17 +45,36 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
this(type, duration, amplifier, ambient, visible, visible);
|
|
}
|
|
|
|
+ // Purpur start
|
|
+ public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean visible, @Nullable 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(), (NamespacedKey)null);
|
|
+ }
|
|
+
|
|
+ public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean showParticles, boolean showIcon, @Nullable 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<MobEffectInstance.FactorData> factorCalculationData) {
|
|
+ // Purpur start
|
|
+ this(type, duration, amplifier, ambient, showParticles, showIcon, hiddenEffect, factorCalculationData, (NamespacedKey) null);
|
|
+ }
|
|
+
|
|
+ public MobEffectInstance(MobEffect type, int duration, int amplifier, boolean ambient, boolean showParticles, boolean showIcon, @Nullable MobEffectInstance hiddenEffect, Optional<MobEffectInstance.FactorData> factorCalculationData, @Nullable 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;
|
|
}
|
|
@@ -74,6 +95,7 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
this.ambient = that.ambient;
|
|
this.visible = that.visible;
|
|
this.showIcon = that.showIcon;
|
|
+ this.key = that.key; // Purpur - add key
|
|
}
|
|
|
|
public boolean update(MobEffectInstance that) {
|
|
@@ -126,6 +148,13 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
bl = true;
|
|
}
|
|
|
|
+ // Purpur start
|
|
+ if (that.key != this.key) {
|
|
+ this.key = that.key;
|
|
+ bl = true;
|
|
+ }
|
|
+ // Purpur end
|
|
+
|
|
return bl;
|
|
}
|
|
|
|
@@ -153,6 +182,17 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
return this.showIcon;
|
|
}
|
|
|
|
+ // Purpur start
|
|
+ public boolean hasKey() {
|
|
+ return this.key != null;
|
|
+ }
|
|
+
|
|
+ @Nullable
|
|
+ public NamespacedKey getKey() {
|
|
+ return this.key;
|
|
+ }
|
|
+ // Purpur end
|
|
+
|
|
public boolean tick(LivingEntity entity, Runnable overwriteCallback) {
|
|
if (this.duration > 0) {
|
|
if (this.effect.isDurationEffectTick(this.duration, this.amplifier)) {
|
|
@@ -209,6 +249,12 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
string = string + ", Show Icon: false";
|
|
}
|
|
|
|
+ // Purpur start
|
|
+ if (this.hasKey()) {
|
|
+ string = string + ", Key: " + this.key;
|
|
+ }
|
|
+ // Purpur end
|
|
+
|
|
return string;
|
|
}
|
|
|
|
@@ -220,7 +266,7 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
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
|
|
}
|
|
}
|
|
|
|
@@ -244,6 +290,11 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
nbt.putBoolean("Ambient", this.isAmbient());
|
|
nbt.putBoolean("ShowParticles", this.isVisible());
|
|
nbt.putBoolean("ShowIcon", 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);
|
|
@@ -278,6 +329,13 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
bl3 = nbt.getBoolean("ShowIcon");
|
|
}
|
|
|
|
+ // Purpur start
|
|
+ NamespacedKey key = null;
|
|
+ if (nbt.contains("Key")) {
|
|
+ key = NamespacedKey.fromString(nbt.getString("Key"));
|
|
+ }
|
|
+ // Purpur end
|
|
+
|
|
MobEffectInstance mobEffectInstance = null;
|
|
if (nbt.contains("HiddenEffect", 10)) {
|
|
mobEffectInstance = loadSpecifiedEffect(type, nbt.getCompound("HiddenEffect"));
|
|
@@ -290,7 +348,7 @@ public class MobEffectInstance implements Comparable<MobEffectInstance> {
|
|
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
|
|
}
|
|
|
|
public void setNoCounter(boolean permanent) {
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
|
index f2d17cb06fbcd7370ab8465c5c4830f8ba31169a..3a3476000ee8c13e1c1a565b0ef38a5ac69bdfb3 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
|
|
@@ -438,7 +438,7 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
|
|
|
|
@Override
|
|
public boolean addPotionEffect(PotionEffect effect, boolean force) {
|
|
- this.getHandle().addEffect(new MobEffectInstance(MobEffect.byId(effect.getType().getId()), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles(), effect.hasIcon()), EntityPotionEffectEvent.Cause.PLUGIN); // Paper - Don't ignore icon
|
|
+ this.getHandle().addEffect(new MobEffectInstance(MobEffect.byId(effect.getType().getId()), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles(), effect.hasIcon(), effect.getKey()), EntityPotionEffectEvent.Cause.PLUGIN); // Purpur - add key // Paper - Don't ignore icon
|
|
return true;
|
|
}
|
|
|
|
@@ -459,7 +459,7 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
|
|
@Override
|
|
public PotionEffect getPotionEffect(PotionEffectType type) {
|
|
MobEffectInstance handle = this.getHandle().getEffect(MobEffect.byId(type.getId()));
|
|
- return (handle == null) ? null : new PotionEffect(PotionEffectType.getById(MobEffect.getId(handle.getEffect())), handle.getDuration(), handle.getAmplifier(), handle.isAmbient(), handle.isVisible());
|
|
+ return (handle == null) ? null : new PotionEffect(PotionEffectType.getById(MobEffect.getId(handle.getEffect())), handle.getDuration(), handle.getAmplifier(), handle.isAmbient(), handle.isVisible(), handle.getKey()); // Purpur - add key
|
|
}
|
|
|
|
@Override
|
|
@@ -471,7 +471,7 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
|
|
public Collection<PotionEffect> getActivePotionEffects() {
|
|
List<PotionEffect> effects = new ArrayList<PotionEffect>();
|
|
for (MobEffectInstance handle : this.getHandle().activeEffects.values()) {
|
|
- effects.add(new PotionEffect(PotionEffectType.getById(MobEffect.getId(handle.getEffect())), handle.getDuration(), handle.getAmplifier(), handle.isAmbient(), handle.isVisible()));
|
|
+ effects.add(new PotionEffect(PotionEffectType.getById(MobEffect.getId(handle.getEffect())), handle.getDuration(), handle.getAmplifier(), handle.isAmbient(), handle.isVisible(), handle.getKey())); // Purpur - add key
|
|
}
|
|
return effects;
|
|
}
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
|
|
index 298326115f048bb79e3c949861c187134eb5efb8..9b060070ddbbade4c1b6830b9b861f462d60b138 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
|
|
@@ -13,6 +13,7 @@ import net.minecraft.nbt.ListTag;
|
|
import org.apache.commons.lang.Validate;
|
|
import org.bukkit.Color;
|
|
import org.bukkit.Material;
|
|
+import org.bukkit.NamespacedKey;
|
|
import org.bukkit.configuration.serialization.DelegateDeserialization;
|
|
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
|
|
import org.bukkit.craftbukkit.potion.CraftPotionUtil;
|
|
@@ -42,6 +43,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
|
|
@@ -92,7 +94,13 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
|
|
boolean ambient = effect.getBoolean(AMBIENT.NBT);
|
|
boolean particles = tag.contains(SHOW_PARTICLES.NBT, CraftMagicNumbers.NBT.TAG_BYTE) ? effect.getBoolean(SHOW_PARTICLES.NBT) : true;
|
|
boolean icon = tag.contains(SHOW_ICON.NBT, CraftMagicNumbers.NBT.TAG_BYTE) ? effect.getBoolean(SHOW_ICON.NBT) : particles;
|
|
- this.customEffects.add(new PotionEffect(type, duration, amp, ambient, particles, icon));
|
|
+ // Purpur start
|
|
+ NamespacedKey key = null;
|
|
+ if (tag.contains(KEY.NBT)) {
|
|
+ key = NamespacedKey.fromString(effect.getString(KEY.NBT));
|
|
+ }
|
|
+ this.customEffects.add(new PotionEffect(type, duration, amp, ambient, particles, icon, key));
|
|
+ // Purpur end
|
|
}
|
|
}
|
|
}
|
|
@@ -141,6 +149,11 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
|
|
effectData.putBoolean(AMBIENT.NBT, effect.isAmbient());
|
|
effectData.putBoolean(SHOW_PARTICLES.NBT, effect.hasParticles());
|
|
effectData.putBoolean(SHOW_ICON.NBT, effect.hasIcon());
|
|
+ // Purpur start
|
|
+ if (effect.hasKey()) {
|
|
+ effectData.putString(KEY.NBT, effect.getKey().toString());
|
|
+ }
|
|
+ // Purpur end
|
|
effectList.add(effectData);
|
|
}
|
|
}
|
|
@@ -202,7 +215,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 acb69821a99aa69bce6d127e10976089c85be223..c5abd73981c5f4b41605eba0d44e6573dfd2a77a 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionUtil.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionUtil.java
|
|
@@ -101,7 +101,7 @@ public class CraftPotionUtil {
|
|
|
|
public static MobEffectInstance fromBukkit(PotionEffect effect) {
|
|
MobEffect type = MobEffect.byId(effect.getType().getId());
|
|
- return new MobEffectInstance(type, effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles());
|
|
+ return new MobEffectInstance(type, effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles(), effect.getKey()); // Purpur - add key
|
|
}
|
|
|
|
public static PotionEffect toBukkit(MobEffectInstance effect) {
|
|
@@ -110,7 +110,7 @@ public class CraftPotionUtil {
|
|
int duration = effect.getDuration();
|
|
boolean ambient = effect.isAmbient();
|
|
boolean particles = effect.isVisible();
|
|
- return new PotionEffect(type, duration, amp, ambient, particles);
|
|
+ return new PotionEffect(type, duration, amp, ambient, particles, effect.getKey()); // 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 7ea3cc4f8e35b61b3eba717ed58ee98cf835168c..48aa077ab2e2b708fb3d3287e636b4dc1c969925 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.support.AbstractTestingBase;
|
|
import org.junit.Test;
|
|
|
|
@@ -47,4 +48,27 @@ public class PotionTest extends AbstractTestingBase {
|
|
assertEquals("Same type not returned by name " + key, bukkit, byName);
|
|
}
|
|
}
|
|
+
|
|
+ // 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<String, Object> s1 = namedSpacedEffect.serialize();
|
|
+ Map<String, Object> s2 = effect.serialize();
|
|
+ assertTrue(s1.containsKey("namespacedKey"));
|
|
+ assertFalse(s2.containsKey("namespacedKey"));
|
|
+ assertNotNull(new PotionEffect(s1).getKey());
|
|
+ assertNull(new PotionEffect(s2).getKey());
|
|
+ }
|
|
+ // Purpur end
|
|
}
|