Skip to content

Commit 7978121

Browse files
More PotionEffectType API (#5737)
1 parent f6c7d53 commit 7978121

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Jake Potrebic <jake.m.potrebic@gmail.com>
3+
Date: Thu, 27 May 2021 21:58:33 -0700
4+
Subject: [PATCH] More PotionEffectType API
5+
6+
7+
diff --git a/src/main/java/org/bukkit/Registry.java b/src/main/java/org/bukkit/Registry.java
8+
index a696fcaffa03af9e6c92e2ef3e12b38eb59e5db4..6242336de18fdd708cc3d7b745cbbace13140bc0 100644
9+
--- a/src/main/java/org/bukkit/Registry.java
10+
+++ b/src/main/java/org/bukkit/Registry.java
11+
@@ -189,6 +189,27 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
12+
return GameEvent.getByKey(key);
13+
}
14+
};
15+
+ // Paper start
16+
+ /**
17+
+ * Potion effect types.
18+
+ *
19+
+ * @see org.bukkit.potion.PotionEffectType
20+
+ */
21+
+ Registry<org.bukkit.potion.PotionEffectType> POTION_EFFECT_TYPE = new Registry<org.bukkit.potion.PotionEffectType>() {
22+
+
23+
+ @Nullable
24+
+ @Override
25+
+ public org.bukkit.potion.PotionEffectType get(@NotNull NamespacedKey key) {
26+
+ return org.bukkit.potion.PotionEffectType.getByKey(key);
27+
+ }
28+
+
29+
+ @NotNull
30+
+ @Override
31+
+ public Iterator<org.bukkit.potion.PotionEffectType> iterator() {
32+
+ return Arrays.stream(org.bukkit.potion.PotionEffectType.values()).iterator();
33+
+ }
34+
+ };
35+
+ // Paper end
36+
37+
/**
38+
* Get the object by its key.
39+
diff --git a/src/main/java/org/bukkit/potion/PotionEffectType.java b/src/main/java/org/bukkit/potion/PotionEffectType.java
40+
index 22c28a503732671bc84c51372262e909d035c1fa..06e0f13d658b63b1fa984abb515eb0de704f9215 100644
41+
--- a/src/main/java/org/bukkit/potion/PotionEffectType.java
42+
+++ b/src/main/java/org/bukkit/potion/PotionEffectType.java
43+
@@ -14,7 +14,7 @@ import org.jetbrains.annotations.Nullable;
44+
/**
45+
* Represents a type of potion and its effect on an entity.
46+
*/
47+
-public abstract class PotionEffectType implements Keyed {
48+
+public abstract class PotionEffectType implements Keyed, net.kyori.adventure.translation.Translatable { // Paper - implement Translatable
49+
/**
50+
* Increases movement speed.
51+
*/
52+
@@ -358,4 +358,56 @@ public abstract class PotionEffectType implements Keyed {
53+
public static PotionEffectType[] values() {
54+
return Arrays.copyOfRange(byId, 1, byId.length);
55+
}
56+
+ // Paper start
57+
+ /**
58+
+ * Gets the effect attributes in an immutable map.
59+
+ *
60+
+ * @return the attribute map
61+
+ */
62+
+ public abstract @NotNull Map<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> getEffectAttributes();
63+
+
64+
+ /**
65+
+ * Gets the true modifier amount based on the effect amplifier.
66+
+ *
67+
+ * @param attribute the attribute
68+
+ * @param effectAmplifier the effect amplifier (0 indexed)
69+
+ * @return the modifier amount
70+
+ * @throws IllegalArgumentException if the supplied attribute is not present in the map from {@link #getEffectAttributes()}
71+
+ */
72+
+ public abstract double getAttributeModifierAmount(@NotNull org.bukkit.attribute.Attribute attribute, int effectAmplifier);
73+
+
74+
+ /**
75+
+ * Gets the category of this effect
76+
+ *
77+
+ * @return the category
78+
+ */
79+
+ public abstract @NotNull PotionEffectType.Category getEffectCategory();
80+
+
81+
+ /**
82+
+ * Category of {@link PotionEffectType}s
83+
+ */
84+
+ public enum Category {
85+
+
86+
+ BENEFICIAL(net.kyori.adventure.text.format.NamedTextColor.BLUE),
87+
+ HARMFUL(net.kyori.adventure.text.format.NamedTextColor.RED),
88+
+ NEUTRAL(net.kyori.adventure.text.format.NamedTextColor.BLUE);
89+
+
90+
+ private final net.kyori.adventure.text.format.TextColor color;
91+
+
92+
+ Category(net.kyori.adventure.text.format.TextColor color) {
93+
+ this.color = color;
94+
+ }
95+
+
96+
+ /**
97+
+ * Gets the text color used when displaying potions
98+
+ * of this category.
99+
+ *
100+
+ * @return the text color
101+
+ */
102+
+ @NotNull
103+
+ public net.kyori.adventure.text.format.TextColor getColor() {
104+
+ return color;
105+
+ }
106+
+ }
107+
+ // Paper end
108+
}
109+
diff --git a/src/main/java/org/bukkit/potion/PotionEffectTypeWrapper.java b/src/main/java/org/bukkit/potion/PotionEffectTypeWrapper.java
110+
index c3a86bb1910158a8d13a675dfa7236dd6a3f397c..a7653806c0fa76f4b3342ea199fe892c514a4c27 100644
111+
--- a/src/main/java/org/bukkit/potion/PotionEffectTypeWrapper.java
112+
+++ b/src/main/java/org/bukkit/potion/PotionEffectTypeWrapper.java
113+
@@ -40,4 +40,30 @@ public class PotionEffectTypeWrapper extends PotionEffectType {
114+
public Color getColor() {
115+
return getType().getColor();
116+
}
117+
+ // Paper start
118+
+ @Override
119+
+ public @NotNull org.bukkit.NamespacedKey getKey() {
120+
+ return this.getType().getKey();
121+
+ }
122+
+
123+
+ @Override
124+
+ public @NotNull java.util.Map<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> getEffectAttributes() {
125+
+ return this.getType().getEffectAttributes();
126+
+ }
127+
+
128+
+ @Override
129+
+ public double getAttributeModifierAmount(@NotNull org.bukkit.attribute.Attribute attribute, int effectAmplifier) {
130+
+ return this.getType().getAttributeModifierAmount(attribute, effectAmplifier);
131+
+ }
132+
+
133+
+ @Override
134+
+ public @NotNull PotionEffectType.Category getEffectCategory() {
135+
+ return this.getType().getEffectCategory();
136+
+ }
137+
+
138+
+ @Override
139+
+ public @NotNull String translationKey() {
140+
+ return this.getType().translationKey();
141+
+ }
142+
+ // Paper end
143+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: Jake Potrebic <jake.m.potrebic@gmail.com>
3+
Date: Thu, 27 May 2021 21:58:24 -0700
4+
Subject: [PATCH] More PotionEffectType API
5+
6+
7+
diff --git a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
8+
index fdd8e353bcbd2faace59288810d1a5121f174b56..82a6702a2ddc35222f5a416364de8c397c85d5bb 100644
9+
--- a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
10+
+++ b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
11+
@@ -102,4 +102,46 @@ public class CraftPotionEffectType extends PotionEffectType {
12+
public Color getColor() {
13+
return Color.fromRGB(this.handle.getColor());
14+
}
15+
+ // Paper start
16+
+ @Override
17+
+ public org.bukkit.NamespacedKey getKey() {
18+
+ return org.bukkit.craftbukkit.util.CraftNamespacedKey.fromMinecraft(net.minecraft.core.Registry.MOB_EFFECT.getKey(this.handle));
19+
+ }
20+
+
21+
+ @Override
22+
+ public java.util.Map<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> getEffectAttributes() {
23+
+ // re-create map each time because a nms MobEffect can have its attributes modified
24+
+ final java.util.Map<org.bukkit.attribute.Attribute, org.bukkit.attribute.AttributeModifier> attributeMap = new java.util.HashMap<>();
25+
+ this.handle.getAttributeModifiers().forEach((attribute, attributeModifier) -> {
26+
+ attributeMap.put(org.bukkit.craftbukkit.attribute.CraftAttributeMap.fromMinecraft(attribute.toString()), org.bukkit.craftbukkit.attribute.CraftAttributeInstance.convert(attributeModifier));
27+
+ });
28+
+ return java.util.Map.copyOf(attributeMap);
29+
+ }
30+
+
31+
+ @Override
32+
+ public double getAttributeModifierAmount(org.bukkit.attribute.Attribute attribute, int effectAmplifier) {
33+
+ com.google.common.base.Preconditions.checkArgument(effectAmplifier >= 0, "effectAmplifier must be greater than or equal to 0");
34+
+ net.minecraft.world.entity.ai.attributes.Attribute nmsAttribute = org.bukkit.craftbukkit.attribute.CraftAttributeMap.toMinecraft(attribute);
35+
+ com.google.common.base.Preconditions.checkArgument(this.handle.getAttributeModifiers().containsKey(nmsAttribute), attribute + " is not present on " + this.getKey());
36+
+ return this.handle.getAttributeModifierValue(effectAmplifier, this.handle.getAttributeModifiers().get(nmsAttribute));
37+
+ }
38+
+
39+
+ @Override
40+
+ public PotionEffectType.Category getEffectCategory() {
41+
+ return fromNMS(handle.getCategory());
42+
+ }
43+
+
44+
+ @Override
45+
+ public String translationKey() {
46+
+ return this.handle.getDescriptionId();
47+
+ }
48+
+
49+
+ public static PotionEffectType.Category fromNMS(net.minecraft.world.effect.MobEffectCategory mobEffectInfo) {
50+
+ return switch (mobEffectInfo) {
51+
+ case BENEFICIAL -> PotionEffectType.Category.BENEFICIAL;
52+
+ case HARMFUL -> PotionEffectType.Category.HARMFUL;
53+
+ case NEUTRAL -> PotionEffectType.Category.NEUTRAL;
54+
+ };
55+
+ }
56+
+ // Paper end
57+
}
58+
diff --git a/src/test/java/io/papermc/paper/effects/EffectCategoryTest.java b/src/test/java/io/papermc/paper/effects/EffectCategoryTest.java
59+
new file mode 100644
60+
index 0000000000000000000000000000000000000000..a5012bc0469ba03cde66749a11f4e7d93206bfd7
61+
--- /dev/null
62+
+++ b/src/test/java/io/papermc/paper/effects/EffectCategoryTest.java
63+
@@ -0,0 +1,28 @@
64+
+package io.papermc.paper.effects;
65+
+
66+
+import io.papermc.paper.adventure.PaperAdventure;
67+
+import net.minecraft.world.effect.MobEffectCategory;
68+
+import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
69+
+import org.bukkit.potion.PotionEffectType;
70+
+import org.junit.Test;
71+
+
72+
+import static org.junit.Assert.assertEquals;
73+
+import static org.junit.Assert.assertNotNull;
74+
+
75+
+public class EffectCategoryTest {
76+
+
77+
+ @Test
78+
+ public void testEffectCategoriesExist() {
79+
+ for (MobEffectCategory mobEffectInfo : MobEffectCategory.values()) {
80+
+ assertNotNull(mobEffectInfo + " is missing a bukkit equivalent", CraftPotionEffectType.fromNMS(mobEffectInfo));
81+
+ }
82+
+ }
83+
+
84+
+ @Test
85+
+ public void testCategoryHasEquivalentColors() {
86+
+ for (MobEffectCategory mobEffectInfo : MobEffectCategory.values()) {
87+
+ PotionEffectType.Category bukkitEffectCategory = CraftPotionEffectType.fromNMS(mobEffectInfo);
88+
+ assertEquals(mobEffectInfo.getTooltipFormatting().name() + " doesn't equal " + bukkitEffectCategory.getColor(), bukkitEffectCategory.getColor(), PaperAdventure.asAdventure(mobEffectInfo.getTooltipFormatting()));
89+
+ }
90+
+ }
91+
+}

0 commit comments

Comments
 (0)