Skip to content

Commit dbc3c52

Browse files
committed
Fixed potion wrapper work better no mater Minecraft version
Also fixed some missing javadoc and improve some with better grammar.
1 parent 8c3592c commit dbc3c52

File tree

12 files changed

+185
-65
lines changed

12 files changed

+185
-65
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/BottleEffectMeta.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.broken.arrow.library.itemcreator.meta;
22

3-
import org.broken.arrow.library.itemcreator.meta.potion.PotionData;
3+
import org.broken.arrow.library.itemcreator.meta.potion.PotionTypeWrapper;
44
import org.broken.arrow.library.itemcreator.meta.potion.PotionModifier;
55
import org.broken.arrow.library.itemcreator.meta.potion.PotionsUtility;
6-
import org.bukkit.Color;
76
import org.bukkit.inventory.meta.ItemMeta;
87
import org.bukkit.inventory.meta.PotionMeta;
98
import org.bukkit.potion.PotionEffect;
@@ -31,7 +30,7 @@ public class BottleEffectMeta {
3130
private boolean waterBottle;
3231
private boolean override = true;
3332
private ColorMeta colorMeta;
34-
private PotionData potionData;
33+
private PotionTypeWrapper potionTypeWrapper;
3534
private boolean extended;
3635
private boolean upgraded;
3736

@@ -136,11 +135,11 @@ public BottleEffectMeta setWaterBottle(final boolean waterBottle) {
136135
*/
137136
@Nullable
138137
public PotionType getPotionType() {
139-
return (this.potionData == null ? null : this.potionData.getPotionType());
138+
return (this.potionTypeWrapper == null ? null : this.potionTypeWrapper.getPotionType());
140139
}
141140

142141
/**
143-
* Sets the predefined {@link PotionData} to apply to this potion item.
142+
* Sets the predefined {@link PotionTypeWrapper} to apply to this potion item.
144143
* <p>
145144
* This automatically handles version compatibility. You can specify the desired
146145
* potion variant (e.g., long, strong, or base version), and it will use the correct
@@ -162,11 +161,11 @@ public PotionType getPotionType() {
162161
* a potion type (or set it to {@code null}) and use the appropriate setters instead.
163162
* </p>
164163
*
165-
* @param potionData the potion type to apply; set {@code null} to allow custom effects
164+
* @param potionTypeWrapper the potion type to apply; set {@code null} to allow custom effects
166165
* @return this instance for method chaining.
167166
*/
168-
public BottleEffectMeta setPotionData(@Nullable final PotionData potionData) {
169-
this.potionData = potionData;
167+
public BottleEffectMeta setPotionData(@Nullable final PotionTypeWrapper potionTypeWrapper) {
168+
this.potionTypeWrapper = potionTypeWrapper;
170169
return this;
171170
}
172171

@@ -245,7 +244,7 @@ public void applyBottleEffects(@Nonnull final ItemMeta itemMeta) {
245244
return;
246245
}
247246

248-
if (this.potionData != null && this.setPotionType(potionMeta)) {
247+
if (this.potionTypeWrapper != null && this.setPotionType(potionMeta)) {
249248
return;
250249
}
251250

@@ -265,7 +264,7 @@ private boolean setPotionType(@Nonnull final PotionMeta potionMeta) {
265264
final PotionType potionType = this.getPotionType();
266265
if (potionType != null) {
267266
PotionsUtility potionsUtility = new PotionsUtility(potionMeta);
268-
PotionModifier modifier = this.potionData.getModifier();
267+
PotionModifier modifier = this.potionTypeWrapper.getModifier();
269268
potionsUtility.setPotion(potionType, modifier);
270269
return true;
271270
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/ColorMeta.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
import org.bukkit.Color;
66

77
import javax.annotation.Nonnull;
8+
import javax.annotation.Nullable;
89
import java.util.logging.Level;
910

1011
public class ColorMeta {
1112
private static final Logging logger = new Logging(ColorMeta.class);
12-
private String rgb;
13-
private Color color = Color.fromRGB(0,0,0);
13+
private String rgb = "";
14+
@Nullable
15+
private Color color;
1416

1517
/**
1618
* Get red color.
1719
*
1820
* @return red component, from 0 to 255.
1921
*/
2022
public int getRed() {
21-
return color.getRed();
23+
return this.getColor().getRed();
2224
}
2325

2426
/**
@@ -27,7 +29,7 @@ public int getRed() {
2729
* @return green component, from 0 to 255.
2830
*/
2931
public int getGreen() {
30-
return color.getGreen();
32+
return this.getColor().getGreen();
3133
}
3234

3335
/**
@@ -36,7 +38,7 @@ public int getGreen() {
3638
* @return blue component, from 0 to 255.
3739
*/
3840
public int getBlue() {
39-
return color.getBlue();
41+
return this.getColor().getBlue();
4042
}
4143

4244
/**
@@ -45,7 +47,7 @@ public int getBlue() {
4547
* @return alpha component, from 0 to 255.
4648
*/
4749
public int getAlpha() {
48-
return color.getAlpha();
50+
return this.getColor().getAlpha();
4951
}
5052

5153
/**
@@ -63,7 +65,7 @@ public String getRgb() {
6365
* @return An integer representation of this color, as 0xRRGGBB
6466
*/
6567
public int toRgb() {
66-
return color.asRGB();
68+
return this.getColor().asRGB();
6769
}
6870

6971
/**
@@ -72,15 +74,17 @@ public int toRgb() {
7274
* @return An integer representation of this color, as 0xAARRGGBB
7375
*/
7476
public int toArgb() {
75-
return color.asARGB();
77+
return this.getColor().asARGB();
7678
}
7779

7880
/**
7981
* Retrieve the color set.
8082
*
81-
* @return the color set.
83+
* @return the color set or if not set it will return default black color.
8284
*/
8385
public Color getColor() {
86+
if (this.color == null)
87+
return Color.fromRGB(0, 0, 0);
8488
return this.color;
8589
}
8690

@@ -133,24 +137,37 @@ public void setRgb(final String rgb) {
133137
final int colorGreen = Integer.parseInt(colors[2]);
134138
final int colorBlue = Integer.parseInt(colors[1]);
135139

136-
this.setRgb(255,colorRed, colorGreen, colorBlue);
140+
this.setRgb(255, colorRed, colorGreen, colorBlue);
137141
} catch (final NumberFormatException exception) {
138142
logger.log(Level.WARNING, exception, () -> "you don´t use numbers inside this string. Your input: " + rgb);
139143
}
140144
}
141145

146+
/**
147+
* Creates a new Color object from a red, green, and blue
148+
*
149+
* @param red integer from 0-255
150+
* @param green integer from 0-255
151+
* @param blue integer from 0-255
152+
* @throws Validate.ValidateExceptions if any value is strictly {@literal >255 or <0}
153+
*/
154+
public void setRgb(final int red, final int green, final int blue) {
155+
Validate.checkBoolean(red < 0 || green < 0 || blue < 0, "You can't use negative numbers for the rbg color.");
156+
this.setRgb(255, red, green, blue);
157+
}
158+
142159
/**
143160
* Creates a new Color object from an alpha, red, green, and blue
144161
*
145162
* @param alpha integer from 0-255
146-
* @param red integer from 0-255
163+
* @param red integer from 0-255
147164
* @param green integer from 0-255
148-
* @param blue integer from 0-255
165+
* @param blue integer from 0-255
149166
* @throws Validate.ValidateExceptions if any value is strictly {@literal >255 or <0}
150167
*/
151-
public void setRgb(final int alpha,final int red, final int green, final int blue) {
152-
Validate.checkBoolean(alpha < 0 ||red < 0 || green < 0 || blue < 0, "You can't use negative numbers for the arbg color.");
153-
final Color color = Color.fromARGB(alpha,red, green, blue);
168+
public void setRgb(final int alpha, final int red, final int green, final int blue) {
169+
Validate.checkBoolean(alpha < 0 || red < 0 || green < 0 || blue < 0, "You can't use negative numbers for the arbg color.");
170+
final Color color = Color.fromARGB(alpha, red, green, blue);
154171
this.setColor(color);
155172
}
156173

@@ -159,7 +176,7 @@ public void setRgb(final int alpha,final int red, final int green, final int blu
159176
*
160177
* @param color The color object to set.
161178
*/
162-
private void setColor(final Color color) {
179+
private void setColor(@Nonnull final Color color) {
163180
Validate.checkBoolean(color == null, "You can't set color to null.");
164181

165182
final int colorRed = color.getRed();

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/BuildMapView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public void setLocked(final boolean locked) {
274274
}
275275

276276
/**
277-
* Build the {@link MapView)} with your settings set.
277+
* Build the {@link MapView} with your settings set.
278278
*
279279
* @return a new MapView instance with your settings.
280280
*/

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/potion/PotionData.java renamed to Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/potion/PotionTypeWrapper.java

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import javax.annotation.Nonnull;
77
import javax.annotation.Nullable;
8+
import java.util.HashMap;
9+
import java.util.Map;
810

911
/**
1012
* Represents a mapping of potion data types to Minecraft's {@link PotionType},
@@ -14,7 +16,7 @@
1416
* <p>
1517
* Designed to follow the modern style used in Spigot and Paper APIs as of Minecraft 1.20 and above.
1618
*/
17-
public enum PotionData {
19+
public enum PotionTypeWrapper {
1820
/**
1921
* Uncraftable potion (special case).
2022
*/
@@ -186,24 +188,31 @@ public enum PotionData {
186188
/**
187189
* Slow Falling potion, long duration.
188190
*/
189-
LONG_SLOW_FALLING(PotionType.SLOW_FALLING, PotionModifier.LONG);;
191+
LONG_SLOW_FALLING(PotionType.SLOW_FALLING, PotionModifier.LONG);
190192

193+
private static final Map<String, PotionTypeWrapper> POTION_TYPE_NAME = new HashMap<>();
191194
private final PotionType potionType;
192195
private final PotionModifier potionModifier;
193196
private final float serverVersion = ItemCreator.getServerVersion();
194197

195198
/**
196199
* Constructs a new {@code PotionData} entry.
197200
*
198-
* @param potionType The base {@link PotionType} associated with this potion data,
199-
* or {@code null} for special cases like MUNDANE or WATER.
200-
* @param potionModifier The {@link PotionModifier} modifier for the potion, e.g., NORMAL, LONG, or STRONG.
201+
* @param potionType The base {@link PotionType} associated with this potion data,
202+
* or {@code null} for special cases like MUNDANE or WATER.
203+
* @param potionModifier The {@link PotionModifier} modifier for the potion, e.g., NORMAL, LONG, or STRONG.
201204
*/
202-
PotionData(@Nonnull final PotionType potionType, @Nonnull final PotionModifier potionModifier) {
205+
PotionTypeWrapper(@Nonnull final PotionType potionType, @Nonnull final PotionModifier potionModifier) {
203206
this.potionType = potionType;
204207
this.potionModifier = potionModifier;
205208
}
206209

210+
static {
211+
for (PotionTypeWrapper data : values()) {
212+
POTION_TYPE_NAME.put(data.getPotionType().name(), data);
213+
}
214+
}
215+
207216
/**
208217
* Retrieves the associated {@link PotionType} for this enum constant. Handles version differences
209218
* and special cases like uncraftable or water potions.
@@ -220,35 +229,72 @@ public PotionType getPotionType() {
220229
}
221230

222231
/**
223-
* Find the portion mapping from the bukkit potion type.
232+
* Finds the {@link PotionTypeWrapper} corresponding to a Bukkit {@link PotionType}.
233+
* <p>
234+
* This method provides a fast lookup for modern potion mappings and is the recommended
235+
* way to resolve wrappers when working with Bukkit's potion system on 1.20.2 and newer.
236+
* Older Minecraft versions do not offer the same variety of potion types, if you need
237+
* compatibility from 1.8.8 and up, use {@link #findPotionByName(String)} instead.
238+
* </p>
224239
*
225-
* @param bukkitPortionType the type you want to find.
226-
* @return the PotionData instance or null if it could not find the PotionType.
240+
* @param bukkitPotionType the Bukkit {@link PotionType} to find.
241+
* @return the matching {@link PotionTypeWrapper}, or {@code null} if no mapping exists.
227242
*/
228243
@Nullable
229-
public static PotionData findPotionByType(PotionType bukkitPortionType) {
230-
PotionData[] potionTypes = values();
231-
for (PotionData potion : potionTypes) {
232-
if (potion.getPotionType() == bukkitPortionType)
233-
return potion;
234-
}
235-
return null;
244+
public static PotionTypeWrapper findPotionByType(final PotionType bukkitPotionType) {
245+
return POTION_TYPE_NAME.get(bukkitPotionType.name());
236246
}
237247

238248
/**
239-
* Find the portion from the bukkit potion type.
249+
* Finds the {@link PotionTypeWrapper} associated with the given potion name.
250+
* <p>
251+
* This method supports both modern and legacy potion naming. If the provided name matches
252+
* an entry in {@code POTION_TYPE_NAME}, that entry will be returned first.
253+
* Otherwise, it attempts to resolve the name using {@link PotionTypeWrapper#valueOf(String)}.
254+
* </p>
255+
*
256+
* <p><strong>Note:</strong> Using legacy potion names is discouraged, as it prevents
257+
* easy retrieval of potion effect types. Prefer modern {@link PotionType} values
258+
* when possible (see {@link PotionTypeWrapper}), and then use {@link #getModifier()}
259+
* to obtain the correct potion modifier.</p>
240260
*
241-
* @param bukkitPortionType the type you want to find.
242-
* @return the PotionData instance or null if it could not find the PotionType.
261+
* @param name the potion name to find (case-insensitive).
262+
* @return the corresponding {@link PotionTypeWrapper}, or {@code null} if no match is found.
243263
*/
244264
@Nullable
245-
public static PotionType findPotionByName(String bukkitPortionType) {
246-
PotionType[] potionTypes = PotionType.values();
247-
String bukkitPortion = bukkitPortionType.toUpperCase();
248-
for (PotionType potion : potionTypes) {
249-
if (potion.name().equals(bukkitPortion))
250-
return potion;
265+
public static PotionTypeWrapper findPotionByName(final String name) {
266+
String bukkitPortion = name.toUpperCase();
267+
PotionTypeWrapper potionTypeWrapper = POTION_TYPE_NAME.get(bukkitPortion);
268+
if (potionTypeWrapper != null)
269+
return potionTypeWrapper;
270+
try {
271+
return PotionTypeWrapper.valueOf(bukkitPortion);
272+
} catch (IllegalArgumentException e) {
273+
return null;
251274
}
275+
}
276+
277+
/**
278+
* Resolves the Bukkit {@link PotionType} from its string name.
279+
* <p>
280+
* This method internally uses {@link #findPotionByName(String)} to obtain the
281+
* corresponding {@link PotionTypeWrapper}, and then returns its underlying
282+
* {@link PotionType}.
283+
* </p>
284+
*
285+
* <p><strong>Note:</strong> On Minecraft versions below 1.20.2, many potions
286+
* do not have distinct {@link PotionType}s. This method may therefore return a
287+
* less specific type or {@code null} if the enum name is not valid.</p>
288+
*
289+
* @param bukkitPotionType the name of the Bukkit potion type (case-insensitive).
290+
* @return the corresponding {@link PotionType}, or {@code null} if not found.
291+
*/
292+
@Nullable
293+
public static PotionType findPotionTypeByName(String bukkitPotionType) {
294+
String bukkitPotion = bukkitPotionType.toUpperCase();
295+
PotionTypeWrapper potionByName = findPotionByName(bukkitPotion);
296+
if (potionByName != null)
297+
return potionByName.getPotionType();
252298
return null;
253299
}
254300

@@ -324,12 +370,12 @@ private PotionType getPotionMapping() {
324370
* Attempting to get the uncraftable type, this default back
325371
* to mundane on newer Minecraft versions like 1.21 and beyond.
326372
*
327-
* @return A {@link PotionType#UNCRAFTABLE} if it exist
373+
* @return A {@link PotionType#UNCRAFTABLE} if it exists
328374
* other cases {@link PotionType#MUNDANE}
329375
*/
330376
@Nonnull
331377
private static PotionType getUncraftable() {
332-
PotionType potion = findPotionByName("UNCRAFTABLE");
378+
PotionType potion = findPotionTypeByName("UNCRAFTABLE");
333379
return potion != null ? potion : PotionType.MUNDANE;
334380
}
335381

0 commit comments

Comments
 (0)