This repository has been archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
CraftItemMeta.java
executable file
·203 lines (180 loc) · 5.23 KB
/
CraftItemMeta.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dev.wolveringer.bungeeutil.item.meta;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import dev.wolveringer.bungeeutil.BungeeUtil;
import dev.wolveringer.bungeeutil.UtilReflection;
import dev.wolveringer.bungeeutil.inventory.Inventory;
import dev.wolveringer.bungeeutil.item.Item;
import dev.wolveringer.bungeeutil.item.ItemStack;
import dev.wolveringer.bungeeutil.item.ItemStack.Click;
import dev.wolveringer.bungeeutil.item.SyncHandle;
import dev.wolveringer.nbt.NBTTagCompound;
import dev.wolveringer.nbt.NBTTagInt;
import dev.wolveringer.nbt.NBTTagList;
import dev.wolveringer.nbt.NBTTagString;
public class CraftItemMeta implements ItemMeta {
protected Item item;
protected ArrayList<MetaListener> listener = new ArrayList<MetaListener>();
private int sync = -1;
public CraftItemMeta(Item i) {
this.item = i;
}
public void addMetaListener(MetaListener listener) {
this.listener.add(listener);
}
protected void build() {}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
CraftItemMeta other = (CraftItemMeta) obj;
if (this.item == null) {
if (other.item != null) {
return false;
}
} else if (!this.item.getTag().equals(other.item.getTag())) {
return false;
}
return true;
}
protected void fireUpdate() {
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
if (e.getClassName().contains(Inventory.class.getCanonicalName() + "$2")
&& e.getMethodName().equalsIgnoreCase("onUpdate")) {
// in
// update–
return;
}
}
if (this.listener.size() != 0){
try{
for (MetaListener l : new ArrayList<>(this.listener)) {
if (l != null) {
l.onUpdate(this.item);
}
}
}catch(NegativeArraySizeException e){
System.out.println("NegativeArraySizeException from ArrayList?! I'm crazy! size()->"+this.listener.size());
}
}
}
@Override
public String getDisplayName() {
if (!this.getTag().hasKey("display")) {
this.getTag().set("display", new NBTTagCompound());
}
return this.hasDisplayName() ? this.getTag().getCompound("display").getString("Name") : "";
}
@Override
public List<String> getLore() {
if (!this.hasLore()) {
return new ArrayList<String>();
}
List<String> l = new ArrayList<>();
NBTTagList x = this.getTag().getCompound("display").getList("Lore");
for (int y = 0; y < x.size(); y++) {
l.add(x.getString(y));
}
return l;
}
@Override
public NBTTagCompound getTag() {
if (this.item.getTag() == null) {
this.item.setTag(new NBTTagCompound());
}
return this.item.getTag();
}
@Override
public boolean hasDisplayName() {
return this.getTag() != null && this.getTag().hasKey("display") && this.getTag().getCompound("display").hasKey("Name");
}
@Override
public boolean hasGlow() {
return this.getTag() != null && this.getTag().hasKey("ench");
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.item == null ? 0 : this.item.hashCode());
return result;
}
@Override
public boolean hasLore() {
return this.getTag() != null && this.getTag().hasKey("display") && this.getTag().getCompound("display").hasKey("Lore");
}
@Override
public boolean hasTag() {
return this.getTag() != null && !this.getTag().isEmpty();
}
public boolean isClickSync(){
if(this.sync == -1){
if(!(this.item instanceof ItemStack)) {
this.sync = 0;
return false;
}
ItemStack is = (ItemStack) this.item;
try{
Method m = UtilReflection.getMethod(is.getClass(), "click", Click.class);
this.sync = m.isAnnotationPresent(SyncHandle.class) ? 1 : 0;
BungeeUtil.getInstance();
BungeeUtil.debug((this.sync == 1 ? "Sync-":"Ansync-")+"Click handeling for "+m.getDeclaringClass().getName()+"#"+m.getName());
}catch(Exception e){
BungeeUtil.getInstance();
BungeeUtil.debug(e, "Exception while try to detect sync handeling....");
}
}
return this.sync == 1;
}
public void removeMetaListener(MetaListener listener) {
this.listener.remove(listener);
}
@Override
public void setDisplayName(String name) {
if (!this.getTag().hasKey("display")) {
this.getTag().set("display", new NBTTagCompound());
}
this.getTag().getCompound("display").set("Name", new NBTTagString(name));
this.fireUpdate();
}
@Override
public void setGlow(boolean flag) {
if (flag) {
if (!this.getTag().hasKey("ench")) {
NBTTagList list;
this.getTag().set("ench", list = new NBTTagList());
list.add(new NBTTagCompound());
this.getTag().set("HideFlags", new NBTTagInt(1));
}
} else if (!flag) {
if (this.getTag().hasKey("ench")) {
this.getTag().remove("ench");
}
}
this.fireUpdate();
}
@Override
public void setLore(List<String> lore) {
if (!this.getTag().hasKey("display")) {
this.getTag().set("display", new NBTTagCompound());
}
NBTTagList l = new NBTTagList();
for (String s : lore) {
l.add(new NBTTagString(s));
}
this.getTag().getCompound("display").set("Lore", l);
this.fireUpdate();
}
@Override
public String toString() {
return "CraftItemMeta@" + System.identityHashCode(this) + "[listener=" + this.listener + "]";
}
}