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 c8ab330ef171795d08fa201cf8320703c7f1c66b..93e2ea220dc03c122f82af65d5e9fda5b582290c 100644 --- a/src/main/java/org/bukkit/potion/PotionEffect.java +++ b/src/main/java/org/bukkit/potion/PotionEffect.java @@ -33,6 +33,7 @@ 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; @@ -40,6 +41,7 @@ public class PotionEffect implements ConfigurationSerializable { private final boolean particles; private final boolean icon; private final PotionEffect hiddenEffect; // Paper + @Nullable private final NamespacedKey key; // Purpur /** * Creates a potion effect. @@ -50,11 +52,12 @@ public class PotionEffect implements ConfigurationSerializable { * @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()} * @param hiddenEffect the hidden PotionEffect * @hidden Internal-- hidden effects are only shown internally */ @org.jetbrains.annotations.ApiStatus.Internal // Paper - public PotionEffect(@NotNull PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles, boolean icon, @Nullable PotionEffect hiddenEffect) { // Paper + public PotionEffect(@NotNull PotionEffectType type, int duration, int amplifier, boolean ambient, boolean particles, boolean icon, @Nullable PotionEffect hiddenEffect, @Nullable NamespacedKey key) { // Paper // Purpur Preconditions.checkArgument(type != null, "effect type cannot be null"); this.type = type; this.duration = duration; @@ -62,6 +65,7 @@ public class PotionEffect implements ConfigurationSerializable { this.ambient = ambient; this.particles = particles; this.icon = icon; + this.key = key; // Purpur // Paper start this.hiddenEffect = hiddenEffect; } @@ -77,10 +81,27 @@ 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) { - this(type, duration, amplifier, ambient, particles, icon, null); + this(type, duration, amplifier, ambient, particles, icon, null, null); // Purpur // Paper end } + // Purpur start + /** + * 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) { + this(type, duration, amplifier, ambient, particles, icon, null, key); + } + // Purpur end + /** * Creates a potion effect with no defined color. * @@ -126,33 +147,33 @@ 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)), (PotionEffect) map.get(HIDDEN_EFFECT)); // Paper + this(getEffectType(map), getInt(map, DURATION), getInt(map, AMPLIFIER), getBool(map, AMBIENT, false), getBool(map, PARTICLES, true), getBool(map, ICON, getBool(map, PARTICLES, true)), (PotionEffect) map.get(HIDDEN_EFFECT), getKey(map)); // Paper // 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 } /** @@ -169,6 +190,13 @@ public class PotionEffect implements ConfigurationSerializable { } // 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) { PotionEffectType effect; @@ -201,6 +229,17 @@ 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() { @@ -215,6 +254,11 @@ public class PotionEffect implements ConfigurationSerializable { if (this.hiddenEffect != null) { builder.put(HIDDEN_EFFECT, this.hiddenEffect); } + // Purpur start + if (key != null) { + builder.put(KEY, key.toString()); + } + // Purpur end return builder.build(); // Paper end } @@ -243,7 +287,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 && java.util.Objects.equals(this.hiddenEffect, that.hiddenEffect); // Paper + 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 && java.util.Objects.equals(this.hiddenEffect, that.hiddenEffect) && this.key == that.key; // Paper // Purpur - add key } /** @@ -339,6 +383,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; @@ -354,6 +416,6 @@ public class PotionEffect implements ConfigurationSerializable { @Override public String toString() { - return "PotionEffect{" + "amplifier=" + amplifier + ", duration=" + duration + ", type=" + type + ", ambient=" + ambient + ", particles=" + particles + ", icon=" + icon + ", hiddenEffect=" + hiddenEffect + '}'; // Paper + return "PotionEffect{" + "amplifier=" + amplifier + ", duration=" + duration + ", type=" + type + ", ambient=" + ambient + ", particles=" + particles + ", icon=" + icon + ", hiddenEffect=" + hiddenEffect + ", key=" + key + '}'; // Paper // Purpur - add key } }