-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathStackUtils.java
More file actions
110 lines (97 loc) · 3.74 KB
/
StackUtils.java
File metadata and controls
110 lines (97 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package io.github.mooy1.infinitylib.common;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import lombok.experimental.UtilityClass;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
@UtilityClass
@ParametersAreNonnullByDefault
public final class StackUtils {
private static final NamespacedKey ID_KEY = Slimefun.getItemDataService().getKey();
@Nullable
public static String getId(ItemStack item) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
if (sfItem != null) {
return sfItem.getId();
} else if (item.hasItemMeta()) {
return getId(item.getItemMeta());
} else {
return null;
}
}
@Nullable
public static String getId(ItemMeta meta) {
return meta.getPersistentDataContainer().get(ID_KEY, PersistentDataType.STRING);
}
@Nonnull
public static String getIdOrType(ItemStack item) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
if (sfItem != null) {
return sfItem.getId();
} else if (item.hasItemMeta()) {
String id = getId(item.getItemMeta());
return id == null ? item.getType().name() : id;
} else {
return item.getType().name();
}
}
@Nullable
public static ItemStack itemById(String id) {
SlimefunItem item = SlimefunItem.getById(id);
return item == null ? null : item.getItem().clone();
}
@Nonnull
public static ItemStack itemByIdOrType(String idOrType) {
SlimefunItem item = SlimefunItem.getById(idOrType);
return item == null ? new ItemStack(Material.valueOf(idOrType)) : item.getItem().clone();
}
/**
* Returns true when both items:
* - Are null or air
* - Have the same slimefun id
* - Have the same type and display name or lack thereof
*/
public static boolean isSimilar(@Nullable ItemStack first, @Nullable ItemStack second) {
if (first == null || first.getType().isAir()) {
return second == null || second.getType().isAir();
} else if (second == null || second.getType().isAir()) {
return false;
} else if (first.hasItemMeta()) {
if (second.hasItemMeta()) {
ItemMeta firstMeta = first.getItemMeta();
ItemMeta secondMeta = second.getItemMeta();
String firstId = getId(firstMeta);
if (firstId == null) {
if (getId(secondMeta) == null) {
if (first.getType() == second.getType()) {
if (firstMeta.hasDisplayName()) {
return secondMeta.hasDisplayName()
&& firstMeta.getDisplayName().equals(secondMeta.getDisplayName());
} else {
return !secondMeta.hasDisplayName();
}
} else {
return false;
}
} else {
return false;
}
} else {
return firstId.equals(getId(secondMeta));
}
} else {
return false;
}
} else if (second.hasItemMeta()) {
return false;
} else {
return first.getType() == second.getType();
}
}
}