Skip to content

Commit f4f1b6b

Browse files
committed
Improve support for 1.8.8
1 parent 3903d4a commit f4f1b6b

File tree

5 files changed

+168
-32
lines changed

5 files changed

+168
-32
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.broken.arrow.library.itemcreator.meta.BottleEffectMeta;
77
import org.broken.arrow.library.itemcreator.meta.enhancement.EnhancementMeta;
88
import org.broken.arrow.library.itemcreator.meta.MetaHandler;
9+
import org.broken.arrow.library.itemcreator.meta.potion.PotionTypeWrapper;
910
import org.broken.arrow.library.itemcreator.utility.ConvertToItemStack;
1011
import org.broken.arrow.library.itemcreator.utility.Tuple;
1112
import org.broken.arrow.library.itemcreator.utility.builders.ItemBuilder;
@@ -22,6 +23,7 @@
2223
import org.bukkit.inventory.ItemStack;
2324
import org.bukkit.inventory.meta.Damageable;
2425
import org.bukkit.inventory.meta.ItemMeta;
26+
import org.bukkit.inventory.meta.PotionMeta;
2527
import org.bukkit.potion.PotionEffect;
2628

2729
import javax.annotation.Nonnull;
@@ -919,7 +921,7 @@ private ItemStack checkTypeOfItem() {
919921
}
920922
ConvertToItemStack convertToItemStack = this.getConvertItems();
921923
if (builder.getMaterial() != null) {
922-
if (serverVersion > 1.12) {
924+
if (serverVersion > 12.2f) {
923925
result = new ItemStack(builder.getMaterial());
924926
} else {
925927
result = convertToItemStack.checkItem(builder.getMaterial(), this.getDamage(), this.color, this.getData());
@@ -937,7 +939,7 @@ private ItemStack checkTypeOfItem(final Object object) {
937939
return getConvertItems().checkItem(object);
938940
}
939941

940-
private void addItemMeta(ItemStack itemStack, final ItemMeta itemMeta) {
942+
private void addItemMeta(@Nonnull final ItemStack itemStack,@Nonnull final ItemMeta itemMeta) {
941943
this.setDamageMeta(itemStack, itemMeta);
942944
if (this.serverVersion > 10.0F)
943945
setUnbreakableMeta(itemMeta);
@@ -947,15 +949,30 @@ private void addItemMeta(ItemStack itemStack, final ItemMeta itemMeta) {
947949
hideEnchantments(itemMeta);
948950
}
949951

950-
private void setDamageMeta(ItemStack itemStack, ItemMeta itemMeta) {
951-
short dmg = this.getDamage();
952+
private void setDamageMeta(final ItemStack itemStack,final ItemMeta itemMeta) {
953+
short dmg = getDmg(itemMeta);
952954
if (dmg > 0) {
953-
if (serverVersion < 1.13) {
954-
itemStack.setDurability(dmg);
955-
} else {
955+
if (serverVersion > 12.2F) {
956956
((Damageable) itemMeta).setDamage(dmg);
957+
} else {
958+
itemStack.setDurability(dmg);
959+
}
960+
}
961+
}
962+
963+
private short getDmg(@Nonnull final ItemMeta itemMeta) {
964+
final MetaHandler handler = getMetaHandler();
965+
if (itemMeta instanceof PotionMeta && handler != null) {
966+
final BottleEffectMeta bottleEffect = handler.getBottleEffect();
967+
968+
if (bottleEffect != null && bottleEffect.getPotionEffects().isEmpty()) {
969+
final PotionTypeWrapper potionTypeWrapper = bottleEffect.getPotionTypeWrapper();
970+
if (potionTypeWrapper != null) {
971+
return potionTypeWrapper.toLegacyDamage();
972+
}
957973
}
958974
}
975+
return this.getDamage();
959976
}
960977

961978
private void hideEnchantments(final ItemMeta itemMeta) {

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public static void blockWithUrl(@Nonnull final Block block, @Nonnull final UUID
282282
final BlockState skullState = block.getState();
283283
if (!(skullState instanceof Skull)) return;
284284

285-
if (checks.isDoesHavePlayerProfile()) {
285+
if (checks.isDoesHaveOwnerProfile()) {
286286
PlayerProfile profile = Bukkit.createPlayerProfile(id);
287287
if (url != null) {
288288
try {
@@ -681,7 +681,6 @@ public static class CheckClassesExist {
681681
private boolean legacy;
682682
private boolean warningPosted = false;
683683
private boolean doesHaveOwnerProfile = true;
684-
private boolean doesHavePlayerProfile = true;
685684

686685
/**
687686
* When instance is created it will check what classes exists for your minecraft version.
@@ -694,12 +693,6 @@ public CheckClassesExist() {
694693
} catch (ClassNotFoundException | NoSuchMethodException e) {
695694
legacy = true;
696695
}
697-
try {
698-
final UUID id = UUID.randomUUID();
699-
PlayerProfile profile = Bukkit.createPlayerProfile(id);
700-
} catch (NoClassDefFoundError | NoSuchMethodError e) {
701-
doesHavePlayerProfile = false;
702-
}
703696
final ItemStack skull = createSkull();
704697
if (skull != null) {
705698
final ItemMeta skullMeta = Bukkit.getItemFactory().getItemMeta(skull.getType());
@@ -728,23 +721,14 @@ public boolean isWarningPosted() {
728721
}
729722

730723
/**
731-
* Checks if it has OwnerProfile setter in the metadata class.
724+
* Checks if it has OwnerProfile setter in the metadata class and PlayerProfile class.
732725
*
733726
* @return Returns true if the profile exists.
734727
*/
735728
public boolean isDoesHaveOwnerProfile() {
736729
return doesHaveOwnerProfile;
737730
}
738731

739-
/**
740-
* If it modern version where PlayerProfile class exists.
741-
*
742-
* @return Returns true if PlayerProfile class exists.
743-
*/
744-
public boolean isDoesHavePlayerProfile() {
745-
return doesHavePlayerProfile;
746-
}
747-
748732
/**
749733
* Checks if running on legacy Bukkit API and logs a warning if using legacy API on modern server.
750734
*/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ public PotionType getPotionType() {
139139
return (this.potionTypeWrapper == null ? null : this.potionTypeWrapper.getPotionType());
140140
}
141141

142+
/**
143+
* Returns the predefined {@link PotionTypeWrapper} associated with this potion, if available.
144+
*
145+
* <p>This wrapper can provide additional metadata or configuration not exposed by
146+
* the standard {@link PotionType} from the Bukkit/Spigot API, useful when legacy
147+
* values, extended information, or custom behavior is required.</p>
148+
*
149+
* @return the associated {@code PotionTypeWrapper}, or {@code null} if none is set.
150+
*/
151+
@Nullable
152+
public PotionTypeWrapper getPotionTypeWrapper () {
153+
return this.potionTypeWrapper;
154+
}
155+
142156
/**
143157
* Gets the predefined {@link PotionType} to apply to the potion, if any.
144158
* <p>

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

Lines changed: 123 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public enum PotionTypeWrapper {
2828
/**
2929
* Mundane potion (no special effects).
3030
*/
31-
MUNDANE(PotionType.MUNDANE, PotionModifier.NORMAL),
31+
MUNDANE(getMundane(), PotionModifier.NORMAL),
3232
/**
3333
* Thick potion (usually for brewing).
3434
*/
35-
THICK(PotionType.THICK, PotionModifier.NORMAL),
35+
THICK(getPotionType("THICK"), PotionModifier.NORMAL),
3636
/**
3737
* Awkward potion (base potion for many effects).
3838
*/
39-
AWKWARD(PotionType.AWKWARD, PotionModifier.NORMAL),
39+
AWKWARD(getPotionType("AWKWARD"), PotionModifier.NORMAL),
4040
/**
4141
* Night Vision potion, normal duration.
4242
*/
@@ -168,7 +168,7 @@ public enum PotionTypeWrapper {
168168
/**
169169
* Luck potion, normal duration (if applicable).
170170
*/
171-
LUCK(PotionType.LUCK, PotionModifier.NORMAL),
171+
LUCK(getPotionType("LUCK"), PotionModifier.NORMAL),
172172
/**
173173
* Turtle Master potion, normal duration.
174174
*/
@@ -213,7 +213,7 @@ private static Map<String, PotionTypeWrapper> loadPotions() {
213213
for (PotionTypeWrapper data : values()) {
214214
typeWrapperHashMap.put(data.getPotionType().name(), data);
215215
}
216-
return typeWrapperHashMap ;
216+
return typeWrapperHashMap;
217217
}
218218

219219
/**
@@ -245,7 +245,7 @@ public PotionType getPotionType() {
245245
*/
246246
@Nullable
247247
public static PotionTypeWrapper findPotionByType(final PotionType bukkitPotionType) {
248-
if(bukkitPotionType == null)
248+
if (bukkitPotionType == null)
249249
return null;
250250
return POTION_TYPE_NAME.get(bukkitPotionType.name());
251251
}
@@ -372,6 +372,109 @@ private PotionType getPotionMapping() {
372372
}
373373
}
374374

375+
/**
376+
* Converts this {@code PotionTypeWrapper} to the legacy pre-1.12 potion
377+
* "damage" (durability) value.
378+
*
379+
* <p>In Minecraft versions prior to 1.12 (including 1.8.x), potion types are
380+
* encoded in the item damage value of a potion ItemStack. This method returns
381+
* the corresponding legacy short value that can be written via
382+
* {@link org.bukkit.inventory.ItemStack#setDurability} or
383+
* {@link org.bukkit.inventory.meta.Damageable#setDamage}.</p>
384+
*
385+
* <p>If this potion type cannot be represented in legacy (e.g., newer potion
386+
* types or special variants), the method returns {@code 0}, which corresponds
387+
* to a water bottle, acting as a safe fallback.</p>
388+
*
389+
* <p><b>Note:</b> If the ItemStack has custom potion effects, you generally
390+
* should not overwrite its damage value, unless you specifically need a
391+
* legacy-compatible representation.</p>
392+
*
393+
* @return the legacy potion damage value usable on pre-1.12 servers/clients.
394+
*/
395+
public short toLegacyDamage() {
396+
switch (this) {
397+
case NIGHT_VISION:
398+
return 8230;
399+
case LONG_NIGHT_VISION:
400+
return 8262;
401+
402+
case INVISIBILITY:
403+
return 8238;
404+
case LONG_INVISIBILITY:
405+
return 8270;
406+
407+
case JUMP:
408+
return 8203;
409+
case LONG_LEAPING:
410+
return 8267;
411+
case STRONG_LEAPING:
412+
return 8235;
413+
414+
case FIRE_RESISTANCE:
415+
return 8227;
416+
case LONG_FIRE_RESISTANCE:
417+
return 8259;
418+
419+
case SPEED:
420+
return 8194;
421+
case LONG_SWIFTNESS:
422+
return 8258;
423+
case STRONG_SWIFTNESS:
424+
return 8226;
425+
426+
case SLOWNESS:
427+
return 8234;
428+
case LONG_SLOWNESS:
429+
return 8266;
430+
case STRONG_SLOWNESS:
431+
return 8202;
432+
433+
case WATER_BREATHING:
434+
return 8237;
435+
case LONG_WATER_BREATHING:
436+
return 8269;
437+
438+
case INSTANT_HEAL:
439+
return 8261;
440+
case STRONG_HEALING:
441+
return 8229;
442+
443+
case INSTANT_DAMAGE:
444+
return 8268;
445+
case STRONG_HARMING:
446+
return 8236;
447+
448+
case POISON:
449+
return 8196;
450+
case LONG_POISON:
451+
return 8260;
452+
case STRONG_POISON:
453+
return 8228;
454+
455+
case REGEN:
456+
return 8193;
457+
case LONG_REGENERATION:
458+
return 8257;
459+
case STRONG_REGENERATION:
460+
return 8225;
461+
462+
case STRENGTH:
463+
return 8201;
464+
case LONG_STRENGTH:
465+
return 8265;
466+
case STRONG_STRENGTH:
467+
return 8233;
468+
469+
case LONG_WEAKNESS:
470+
return 8264;
471+
case WEAKNESS:
472+
return 8232;
473+
default:
474+
return 0;
475+
}
476+
}
477+
375478
/**
376479
* Attempting to get the uncraftable type, this default back
377480
* to mundane on newer Minecraft versions like 1.21 and beyond.
@@ -384,7 +487,20 @@ private static PotionType getUncraftable() {
384487
try {
385488
return PotionType.valueOf("UNCRAFTABLE");
386489
} catch (IllegalArgumentException | NoSuchFieldError ex) {
387-
return PotionType.MUNDANE;
490+
try {
491+
return PotionType.MUNDANE;
492+
} catch (NoSuchFieldError exception) {
493+
return PotionType.WATER;
494+
}
495+
}
496+
}
497+
498+
@Nonnull
499+
private static PotionType getMundane() {
500+
try {
501+
return PotionType.valueOf("MUNDANE");
502+
} catch (IllegalArgumentException | NoSuchFieldError ex) {
503+
return PotionType.WATER;
388504
}
389505
}
390506

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.broken.arrow.library.itemcreator.ItemCreator;
44
import org.broken.arrow.library.logging.Logging;
55
import org.bukkit.inventory.meta.PotionMeta;
6+
import org.bukkit.potion.Potion;
67
import org.bukkit.potion.PotionData;
78
import org.bukkit.potion.PotionType;
89

@@ -79,6 +80,10 @@ public void setPotion(@Nonnull final PotionType potion, @Nullable final PotionMo
7980
return;
8081
}
8182

83+
if (serverVersion < 9.0F) {
84+
return;
85+
}
86+
8287
try {
8388
boolean couldUpgrade = potionModifier == PotionModifier.STRONG;
8489
boolean couldExtended = potionModifier == PotionModifier.LONG;

0 commit comments

Comments
 (0)