From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Racci Date: Fri, 3 Dec 2021 23:48:26 +1100 Subject: [PATCH] Potion NamespacedKey diff --git a/src/main/java/org/bukkit/potion/PotionEffect.java b/src/main/java/org/bukkit/potion/PotionEffect.java index 24e36cdf580da885ac64002673a786b9c5a3f787..d20cc4d4f5b37a3de9cb3cf47af7a908e9dbc2fc 100644 --- a/src/main/java/org/bukkit/potion/PotionEffect.java +++ b/src/main/java/org/bukkit/potion/PotionEffect.java @@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableMap; import java.util.Map; import java.util.NoSuchElementException; import org.bukkit.Color; +import org.bukkit.NamespacedKey; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; import org.bukkit.entity.LivingEntity; @@ -26,12 +27,14 @@ public class PotionEffect implements ConfigurationSerializable { private static final String AMBIENT = "ambient"; private static final String PARTICLES = "has-particles"; private static final String ICON = "has-icon"; + private static final String KEY = "namespacedKey"; // Purpur private final int amplifier; private final int duration; private final PotionEffectType type; private final boolean ambient; private final boolean particles; private final boolean icon; + @Nullable private final NamespacedKey key; // Purpur /** * Creates a potion effect. @@ -44,6 +47,36 @@ public class PotionEffect implements ConfigurationSerializable { * @param icon the icon status, see {@link PotionEffect#hasIcon()} */ public PotionEffect(@NotNull PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles, boolean icon) { + // Purpur start + this(type, duration, amplifier, ambient, particles, icon, null); + } + + /** + * Create a potion effect. + * @param duration measured in ticks, see {@link + * PotionEffect#getDuration()} + * @param amplifier the amplifier, see {@link PotionEffect#getAmplifier()} + * @param ambient the ambient status, see {@link PotionEffect#isAmbient()} + * @param particles the particle status, see {@link PotionEffect#hasParticles()} + * @param key the namespacedKey, see {@link PotionEffect#getKey()} + */ + public PotionEffect(@NotNull PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles, @Nullable NamespacedKey key) { + this(type, duration, amplifier, ambient, particles, particles, key); + } + + /** + * Creates a potion effect. + * @param type effect type + * @param duration measured in ticks, see {@link + * PotionEffect#getDuration()} + * @param amplifier the amplifier, see {@link PotionEffect#getAmplifier()} + * @param ambient the ambient status, see {@link PotionEffect#isAmbient()} + * @param particles the particle status, see {@link PotionEffect#hasParticles()} + * @param icon the icon status, see {@link PotionEffect#hasIcon()} + * @param key the namespacedKey, see {@link PotionEffect#getKey()} + */ + public PotionEffect(@NotNull PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles, boolean icon, @Nullable NamespacedKey key) { + // Purpur end Preconditions.checkArgument(type != null, "effect type cannot be null"); this.type = type; this.duration = duration; @@ -51,6 +84,7 @@ public class PotionEffect implements ConfigurationSerializable { this.ambient = ambient; this.particles = particles; this.icon = icon; + this.key = key; // Purpur - add key } /** @@ -98,36 +132,43 @@ public class PotionEffect implements ConfigurationSerializable { * @param map the map to deserialize from */ public PotionEffect(@NotNull Map map) { - this(getEffectType(map), getInt(map, DURATION), getInt(map, AMPLIFIER), getBool(map, AMBIENT, false), getBool(map, PARTICLES, true), getBool(map, ICON, getBool(map, PARTICLES, true))); + this(getEffectType(map), getInt(map, DURATION), getInt(map, AMPLIFIER), getBool(map, AMBIENT, false), getBool(map, PARTICLES, true), getBool(map, ICON, getBool(map, PARTICLES, true)), getKey(map)); // Purpur - getKey } // Paper start @NotNull public PotionEffect withType(@NotNull PotionEffectType type) { - return new PotionEffect(type, duration, amplifier, ambient, particles, icon); + return new PotionEffect(type, duration, amplifier, ambient, particles, icon, key); // Purpur - add key } @NotNull public PotionEffect withDuration(int duration) { - return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon); + return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon, key); // Purpur - add key } @NotNull public PotionEffect withAmplifier(int amplifier) { - return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon); + return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon, key); // Purpur - add key } @NotNull public PotionEffect withAmbient(boolean ambient) { - return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon); + return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon, key); // Purpur - add key } @NotNull public PotionEffect withParticles(boolean particles) { - return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon); + return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon, key); // Purpur - add key } @NotNull public PotionEffect withIcon(boolean icon) { - return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon); + return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon, key); // Purpur - add key } // Paper end + // Purpur start + @NotNull + public PotionEffect withKey(@Nullable NamespacedKey key) { + return new PotionEffect(this.type, duration, amplifier, ambient, particles, icon, key); + } + // Purpur end + @NotNull private static PotionEffectType getEffectType(@NotNull Map map) { int type = getInt(map, TYPE); @@ -154,17 +195,33 @@ public class PotionEffect implements ConfigurationSerializable { return def; } + // Purpur start + @Nullable + private static NamespacedKey getKey(@NotNull Map map) { + Object key = map.get(KEY); + if (key instanceof String stringKey) { + return NamespacedKey.fromString(stringKey); + } + return null; + } + // Purpur end + @Override @NotNull public Map serialize() { - return ImmutableMap.builder() - .put(TYPE, type.getId()) - .put(DURATION, duration) - .put(AMPLIFIER, amplifier) - .put(AMBIENT, ambient) - .put(PARTICLES, particles) - .put(ICON, icon) - .build(); + // Purpur start - add key, don't serialize if null. + ImmutableMap.Builder builder = ImmutableMap.builder() + .put(TYPE, type.getId()) + .put(DURATION, duration) + .put(AMPLIFIER, amplifier) + .put(AMBIENT, ambient) + .put(PARTICLES, particles) + .put(ICON, icon); + if(key != null) { + builder.put(KEY, key.toString()); + } + return builder.build(); + // Purpur end } /** @@ -188,7 +245,7 @@ public class PotionEffect implements ConfigurationSerializable { return false; } PotionEffect that = (PotionEffect) obj; - return this.type.equals(that.type) && this.ambient == that.ambient && this.amplifier == that.amplifier && this.duration == that.duration && this.particles == that.particles && this.icon == that.icon; + return this.type.equals(that.type) && this.ambient == that.ambient && this.amplifier == that.amplifier && this.duration == that.duration && this.particles == that.particles && this.icon == that.icon && this.key == that.key; // Purpur - add key } /** @@ -256,6 +313,24 @@ public class PotionEffect implements ConfigurationSerializable { return icon; } + + // Purpur start + /** + * @return if the key isn't the default namespacedKey + */ + public boolean hasKey() { + return key != null; + } + + /** + * @return the key attached to the potion + */ + @Nullable + public NamespacedKey getKey() { + return key; + } + // Purpur end + @Override public int hashCode() { int hash = 1; @@ -270,6 +345,6 @@ public class PotionEffect implements ConfigurationSerializable { @Override public String toString() { - return type.getName() + (ambient ? ":(" : ":") + duration + "t-x" + amplifier + (ambient ? ")" : ""); + return type.getName() + (ambient ? ":(" : ":") + duration + "t-x" + amplifier + (ambient ? ")" : "") + (hasKey() ? "(" + key + ")" : ""); // Purpur - add key if not null } }