Skip to content

Commit 34aa46d

Browse files
committed
Now checking methods and classes instead of Minecraft server version for better future version compatibility
1 parent b789bca commit 34aa46d

5 files changed

Lines changed: 115 additions & 108 deletions

File tree

src/main/java/dev/despical/commons/item/ItemBuilder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.cryptomorin.xseries.XEnchantment;
2222
import com.cryptomorin.xseries.XItemFlag;
2323
import com.cryptomorin.xseries.XMaterial;
24-
import com.cryptomorin.xseries.reflection.XReflection;
2524
import dev.despical.commons.util.Collections;
2625
import dev.despical.commons.util.Strings;
2726
import org.bukkit.Material;
@@ -139,9 +138,9 @@ public ItemBuilder durability(short durability) {
139138
public ItemBuilder unbreakable(boolean unbreakable) {
140139
ItemMeta itemMeta = itemStack.getItemMeta();
141140

142-
if (XReflection.supports(9)) {
141+
try {
143142
itemMeta.setUnbreakable(unbreakable);
144-
} else {
143+
} catch (Throwable throwable) {
145144
try {
146145
Method instanceMethod = itemMeta.getClass().getMethod("spigot");
147146
instanceMethod.setAccessible(true);
@@ -153,6 +152,8 @@ public ItemBuilder unbreakable(boolean unbreakable) {
153152
} catch (Exception exception) {
154153
exception.printStackTrace();
155154
}
155+
156+
throwable.printStackTrace();
156157
}
157158

158159
itemStack.setItemMeta(itemMeta);
@@ -201,4 +202,4 @@ public ItemBuilder lore(List<String> loreList) {
201202
public ItemStack build() {
202203
return itemStack;
203204
}
204-
}
205+
}

src/main/java/dev/despical/commons/item/ItemUtils.java

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,70 +37,72 @@
3737
*/
3838
public class ItemUtils {
3939

40-
public static final ItemStack PLAYER_HEAD_ITEM;
41-
42-
static {
43-
PLAYER_HEAD_ITEM = XMaterial.PLAYER_HEAD.parseItem();
44-
}
45-
46-
private ItemUtils() {
47-
}
48-
49-
public static boolean isNamed(ItemStack stack) {
50-
return stack != null && stack.hasItemMeta() && stack.getItemMeta().hasDisplayName();
51-
}
52-
53-
/**
54-
* Checks if the given 2 items have the same display name, lore and amount.
55-
*
56-
* @param first the first item to check
57-
* @param second the second item to check
58-
* @return {@code true} if the given 2 items are equals and both are same amount,
59-
* otherwise {@code false}
60-
*/
61-
public static boolean isSameItems(ItemStack first, ItemStack second) {
62-
return isSameItemsWithoutAmount(first, second) && first.getAmount() == second.getAmount();
63-
}
64-
65-
/**
66-
* Checks if the given 2 items have the same display name and lore.
67-
*
68-
* @param first the first item to check
69-
* @param second the second item to check
70-
* @return {@code true} if the given 2 items are equals, may not be same amount
71-
*/
72-
public static boolean isSameItemsWithoutAmount(ItemStack first, ItemStack second) {
73-
if (first == null || second == null) {
74-
return false;
75-
}
76-
77-
if (first.getType() != second.getType()) {
78-
return false;
79-
}
80-
81-
ItemMeta firstMeta = first.getItemMeta();
82-
ItemMeta secondMeta = second.getItemMeta();
83-
84-
return Objects.equals(firstMeta.getDisplayName(), secondMeta.getDisplayName()) &&
85-
Objects.equals(firstMeta.getLore(), secondMeta.getLore());
86-
}
87-
88-
public static ItemStack getSkull(String url) {
89-
return XSkull.of(PLAYER_HEAD_ITEM.clone())
90-
.profile(Profileable.detect(url))
91-
.applyAsync()
92-
.join();
93-
}
94-
95-
public static SkullMeta setPlayerHead(Player player, SkullMeta meta) {
96-
if (XReflection.supports(12)) {
97-
meta.setOwningPlayer(player);
98-
} else if (Bukkit.getServer().getVersion().contains("Paper") && player.getPlayerProfile().hasTextures()) {
99-
meta.setPlayerProfile(player.getPlayerProfile());
100-
} else {
101-
meta.setOwner(player.getName());
102-
}
103-
104-
return meta;
105-
}
40+
public static final ItemStack PLAYER_HEAD_ITEM;
41+
42+
static {
43+
PLAYER_HEAD_ITEM = XMaterial.PLAYER_HEAD.parseItem();
44+
}
45+
46+
private ItemUtils() {
47+
}
48+
49+
public static boolean isNamed(ItemStack stack) {
50+
return stack != null && stack.hasItemMeta() && stack.getItemMeta().hasDisplayName();
51+
}
52+
53+
/**
54+
* Checks if the given 2 items have the same display name, lore and amount.
55+
*
56+
* @param first the first item to check
57+
* @param second the second item to check
58+
* @return {@code true} if the given 2 items are equals and both are same amount,
59+
* otherwise {@code false}
60+
*/
61+
public static boolean isSameItems(ItemStack first, ItemStack second) {
62+
return isSameItemsWithoutAmount(first, second) && first.getAmount() == second.getAmount();
63+
}
64+
65+
/**
66+
* Checks if the given 2 items have the same display name and lore.
67+
*
68+
* @param first the first item to check
69+
* @param second the second item to check
70+
* @return {@code true} if the given 2 items are equals, may not be same amount
71+
*/
72+
public static boolean isSameItemsWithoutAmount(ItemStack first, ItemStack second) {
73+
if (first == null || second == null) {
74+
return false;
75+
}
76+
77+
if (first.getType() != second.getType()) {
78+
return false;
79+
}
80+
81+
ItemMeta firstMeta = first.getItemMeta();
82+
ItemMeta secondMeta = second.getItemMeta();
83+
84+
return Objects.equals(firstMeta.getDisplayName(), secondMeta.getDisplayName()) &&
85+
Objects.equals(firstMeta.getLore(), secondMeta.getLore());
86+
}
87+
88+
public static ItemStack getSkull(String url) {
89+
return XSkull.of(PLAYER_HEAD_ITEM.clone())
90+
.profile(Profileable.detect(url))
91+
.applyAsync()
92+
.join();
93+
}
94+
95+
public static SkullMeta setPlayerHead(Player player, SkullMeta meta) {
96+
try {
97+
meta.setOwningPlayer(player);
98+
} catch (Throwable throwable) {
99+
if (Bukkit.getServer().getVersion().contains("Paper") && player.getPlayerProfile().hasTextures()) {
100+
meta.setPlayerProfile(player.getPlayerProfile());
101+
} else {
102+
meta.setOwner(player.getName());
103+
}
104+
}
105+
106+
return meta;
107+
}
106108
}

src/main/java/dev/despical/commons/miscellaneous/AttributeUtils.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package dev.despical.commons.miscellaneous;
2020

2121
import com.cryptomorin.xseries.XAttribute;
22-
import com.cryptomorin.xseries.reflection.XReflection;
22+
import dev.despical.commons.util.ReflectionUtils;
2323
import org.bukkit.attribute.Attribute;
2424
import org.bukkit.entity.Player;
2525

@@ -31,35 +31,34 @@
3131
*/
3232
public class AttributeUtils {
3333

34-
private static final boolean NOT_SUPPORTS_MC_1_9 = !XReflection.supports(9);
34+
private static final boolean NOT_SUPPORTS_ATTRIBUTE = ReflectionUtils.isClassExists("org.bukkit.Attribute");
3535

36-
public static void setAttackCooldown(Player player, double value) {
37-
if (NOT_SUPPORTS_MC_1_9) {
38-
return;
39-
}
36+
public static void setAttackCooldown(Player player, double value) {
37+
if (NOT_SUPPORTS_ATTRIBUTE) {
38+
return;
39+
}
4040

41-
Attribute attribute = XAttribute.ATTACK_SPEED.get();
41+
Attribute attribute = XAttribute.ATTACK_SPEED.get();
4242

43-
if (attribute != null) {
44-
player.getAttribute(attribute).setBaseValue(value);
45-
}
46-
}
43+
if (attribute != null) {
44+
player.getAttribute(attribute).setBaseValue(value);
45+
}
46+
}
4747

48-
public static void resetAttackCooldown(Player player) {
49-
setAttackCooldown(player, 4);
50-
}
48+
public static void resetAttackCooldown(Player player) {
49+
setAttackCooldown(player, 4);
50+
}
5151

52-
public static void healPlayer(Player player) {
53-
if (NOT_SUPPORTS_MC_1_9) {
54-
player.setHealth(player.getMaxHealth());
55-
return;
56-
}
52+
public static void healPlayer(Player player) {
53+
if (NOT_SUPPORTS_ATTRIBUTE) {
54+
player.setHealth(player.getMaxHealth());
55+
return;
56+
}
5757

58-
Attribute attribute = XAttribute.MAX_HEALTH.get();
58+
Attribute attribute = XAttribute.MAX_HEALTH.get();
5959

60-
if (attribute != null) {
61-
player.setHealth(player.getAttribute(attribute).getValue());
62-
}
63-
64-
}
65-
}
60+
if (attribute != null) {
61+
player.setHealth(player.getAttribute(attribute).getValue());
62+
}
63+
}
64+
}

src/main/java/dev/despical/commons/miscellaneous/PlayerUtils.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
package dev.despical.commons.miscellaneous;
2020

21-
import com.cryptomorin.xseries.reflection.XReflection;
21+
import dev.despical.commons.util.ReflectionUtils;
2222
import org.bukkit.entity.Player;
2323
import org.bukkit.plugin.java.JavaPlugin;
2424

@@ -30,6 +30,8 @@
3030
*/
3131
public class PlayerUtils {
3232

33+
private static final boolean IS_MODERN_API = ReflectionUtils.isMethodExists(Player.class, "hidePlayer", JavaPlugin.class, Player.class);
34+
3335
private PlayerUtils() {
3436
}
3537

@@ -46,11 +48,12 @@ public static void hidePlayer(Player to, Player p, JavaPlugin plugin) {
4648
return;
4749
}
4850

49-
if (XReflection.supports(13)) {
51+
if (IS_MODERN_API) {
5052
to.hidePlayer(plugin, p);
51-
} else {
52-
to.hidePlayer(p);
53+
return;
5354
}
55+
56+
to.hidePlayer(p);
5457
}
5558

5659
/**
@@ -66,10 +69,11 @@ public static void showPlayer(Player to, Player p, JavaPlugin plugin) {
6669
return;
6770
}
6871

69-
if (XReflection.supports(13)) {
70-
to.showPlayer(plugin, p);
71-
} else {
72-
to.showPlayer(p);
73-
}
72+
if (IS_MODERN_API) {
73+
to.showPlayer(plugin, p);
74+
return;
75+
}
76+
77+
to.showPlayer(p);
7478
}
7579
}

src/main/java/dev/despical/commons/util/Strings.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package dev.despical.commons.util;
2020

21-
import com.cryptomorin.xseries.reflection.XReflection;
2221
import dev.despical.commons.string.StringMatcher;
2322
import org.bukkit.ChatColor;
2423

@@ -29,6 +28,8 @@
2928
*/
3029
public final class Strings {
3130

31+
private static final boolean SUPPORTS_RGB = ReflectionUtils.isMethodExists(net.md_5.bungee.api.ChatColor.class, "of", String.class);
32+
3233
private Strings() {
3334
}
3435

@@ -37,7 +38,7 @@ public static String format(String string) {
3738
return "";
3839
}
3940

40-
if (XReflection.supports(16) && string.contains("#")) {
41+
if (SUPPORTS_RGB && string.contains("#")) {
4142
string = StringMatcher.matchColorRegex(string);
4243
}
4344

@@ -78,4 +79,4 @@ public static String repeat(String string, int count) {
7879
}
7980
}
8081
}
81-
}
82+
}

0 commit comments

Comments
 (0)