Skip to content

Commit 528a22a

Browse files
committed
New Item Attribute API, and some NBT API changes.
[ATTRIBUTES] * NEW: New API to manipulate attributes on items (for now). This API also permits using custom attributes with custom UUIDs. [NBT] * NEW: The NBT API is now a component. * NEW: NBTCompound and NBTList are now public classes. * NEW: NBTList and NBTCompound: get() methods can now take default values. * NEW: NBTList and NBTCompound can now be created without an associated item. * NEW: NBTList and NBTCompound can now be built even if the associated value is not a list or a compound (or does not exist), and the value will be overwritten if anything is added to the tag. * NEW: NBTList.filter() is returns a new iterator, that iterates only over values that can be coerced to the given type. * NEW: Upon failure, NBT classes now throw NBTExceptions instead of generic RuntimeExceptions. * NEW: Added missing Javadoc. * BUG: Boxed native types are now correctly supported. * BUG: Fixed writing NBT data. * BUG: Fixed handling on some null values. [REFLECTION] * NEW: Reflection.instanciate() can now access non-accessible constructors.
1 parent 961aa22 commit 528a22a

File tree

11 files changed

+1691
-386
lines changed

11 files changed

+1691
-386
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
/*
2+
* Copyright or © or Copr. ZLib contributors (2015 - 2016)
3+
*
4+
* This software is governed by the CeCILL-B license under French law and
5+
* abiding by the rules of distribution of free software. You can use,
6+
* modify and/ or redistribute the software under the terms of the CeCILL-B
7+
* license as circulated by CEA, CNRS and INRIA at the following URL
8+
* "http://www.cecill.info".
9+
*
10+
* As a counterpart to the access to the source code and rights to copy,
11+
* modify and redistribute granted by the license, users are provided only
12+
* with a limited warranty and the software's author, the holder of the
13+
* economic rights, and the successive licensors have only limited
14+
* liability.
15+
*
16+
* In this respect, the user's attention is drawn to the risks associated
17+
* with loading, using, modifying and/or developing or reproducing the
18+
* software by the user in light of its specific status of free software,
19+
* that may mean that it is complicated to manipulate, and that also
20+
* therefore means that it is reserved for developers and experienced
21+
* professionals having in-depth computer knowledge. Users are therefore
22+
* encouraged to load and test the software's suitability as regards their
23+
* requirements in conditions enabling the security of their systems and/or
24+
* data to be ensured and, more generally, to use and operate it in the
25+
* same conditions as regards security.
26+
*
27+
* The fact that you are presently reading this means that you have had
28+
* knowledge of the CeCILL-B license and that you accept its terms.
29+
*/
30+
31+
package fr.zcraft.zlib.components.attributes;
32+
33+
import fr.zcraft.zlib.components.nbt.NBT;
34+
import fr.zcraft.zlib.components.nbt.NBTCompound;
35+
import java.util.UUID;
36+
37+
/**
38+
* This class represents an item attribute.
39+
*/
40+
public class Attribute
41+
{
42+
NBTCompound nbt;
43+
44+
/**
45+
* Creates a new empty attribute, not linked to any item.
46+
*/
47+
public Attribute()
48+
{
49+
this(new NBTCompound());
50+
}
51+
52+
Attribute(NBTCompound nbt)
53+
{
54+
this.nbt = nbt;
55+
}
56+
57+
/**
58+
* @return the name of this attribute's modifier. It can be any string.
59+
*/
60+
public final String getName()
61+
{
62+
return nbt.get("Name", null);
63+
}
64+
65+
/**
66+
* Sets the name of this attribute's modifier.
67+
* This name can be set to any string value.
68+
* @param name The name.
69+
*/
70+
public final void setName(String name)
71+
{
72+
nbt.put("Name", name);
73+
}
74+
75+
/**
76+
* Returns the name of the Attribute this Modifier is to act upon.
77+
* Example: generic.attackDamage
78+
* See http://minecraft.gamepedia.com/Attribute#Attributes for more information.
79+
* @return the name of the Attribute this Modifier is to act upon.
80+
*/
81+
public final String getAttributeName()
82+
{
83+
return nbt.get("AttributeName", null);
84+
}
85+
86+
/**
87+
* Sets the name of the Attribute this Modifier is to act upon.
88+
* @param attributeName The attribute name.
89+
*/
90+
public final void setAttributeName(String attributeName)
91+
{
92+
nbt.put("AttributeName", attributeName);
93+
}
94+
95+
/**
96+
* Returns the most significant bytes of this modifier's UUID.
97+
* @return the most significant bytes of this modifier's UUID.
98+
*/
99+
public final Long getUUIDMost()
100+
{
101+
return nbt.get("UUIDMost", null);
102+
}
103+
104+
/**
105+
* Sets the most significant bytes of this modifier's UUID.
106+
* @param uuidMost the bytes.
107+
*/
108+
public final void setUUIDMost(long uuidMost)
109+
{
110+
nbt.put("UUIDMost", uuidMost);
111+
}
112+
113+
/**
114+
* Returns the least significant bytes of this modifier's UUID.
115+
* @return the least significant bytes of this modifier's UUID.
116+
*/
117+
public final Long getUUIDLeast()
118+
{
119+
return nbt.get("UUIDLeast", null);
120+
}
121+
122+
/**
123+
* Sets the least significant bytes of this modifier's UUID.
124+
* @param uuidLeast the bytes.
125+
*/
126+
public final void setUUIDLeast(long uuidLeast)
127+
{
128+
nbt.put("UUIDLeast", uuidLeast);
129+
}
130+
131+
/**
132+
* Returns this modifier's UUID.
133+
* @return this modifier's UUID.
134+
*/
135+
public final UUID getUUID()
136+
{
137+
Long uuidMost = getUUIDMost();
138+
Long uuidLeast = getUUIDLeast();
139+
140+
if(uuidMost == null || uuidLeast == null)
141+
return null;
142+
143+
return new UUID(uuidMost, uuidLeast);
144+
}
145+
146+
/**
147+
* Sets this modifier's UUID.
148+
* @param uuid the new modifier's UUID.
149+
*/
150+
public final void setUUID(UUID uuid)
151+
{
152+
setUUIDMost(uuid.getMostSignificantBits());
153+
setUUIDLeast(uuid.getLeastSignificantBits());
154+
}
155+
156+
/**
157+
* Returns the Minecraft NBT/JSON string representation of this attribute.
158+
* See {@link fr.zcraft.zlib.tools.nbt.NBT#toNBTJSONString(java.lang.Object) } for more information.
159+
* @return the Minecraft NBT/JSON string representation of this attribute.
160+
*/
161+
@Override
162+
public String toString()
163+
{
164+
return NBT.toNBTJSONString(nbt);
165+
}
166+
167+
/**
168+
* Returns the underlying NBT compound of this attribute.
169+
* @return the underlying NBT compound of this attribute.
170+
*/
171+
public final NBTCompound getNBTCompound()
172+
{
173+
return nbt;
174+
}
175+
176+
/**
177+
* Returns the custom data payload associated to this attribute.
178+
* This enabled plugins to put custom Attributes (and data) to any item.<br>
179+
* The data payload is actually stored as the name of the attribute's modifier, which is not used by the game.<br>
180+
* However, as the custom data shows an empty line (per slot in 1.9+ !) in
181+
* the item's tooltip in the client, you may want to hide item attributes.
182+
* @return the custom data payload associated to this attribute.
183+
*/
184+
public final String getCustomData()
185+
{
186+
return getName();
187+
}
188+
189+
/**
190+
* Sets the custom data payload associated to this attribute.
191+
* See {@link #getCustomData() } for additional information.
192+
* @param data the new data.
193+
*/
194+
public final void setCustomData(String data)
195+
{
196+
setName(data);
197+
}
198+
199+
/**
200+
* Returns this modifier's operation.
201+
* @return this modifier's operation.
202+
*/
203+
public final AttributeOperation getOperation()
204+
{
205+
return AttributeOperation.fromCode(nbt.get("Operation", 0));
206+
}
207+
208+
/**
209+
* Sets this modifier's operation.
210+
* @param operation The new operation value.
211+
*/
212+
public final void setOperation(AttributeOperation operation)
213+
{
214+
nbt.put("Operation", operation.getCode());
215+
}
216+
217+
/**
218+
* Returns the slot for which this modfier applies.
219+
* @return the slot for which this modfier applies.
220+
*/
221+
public final String getSlotName()
222+
{
223+
return nbt.get("Slot", null);
224+
}
225+
226+
/**
227+
* Sets the slot for which this modfier applies.
228+
* @param slotName
229+
*/
230+
public final void setSlotName(String slotName)
231+
{
232+
nbt.put("Slot", slotName);
233+
}
234+
235+
/**
236+
* Returns the amount of the modification.
237+
* @return the amount of the modification.
238+
*/
239+
public final double getAmount()
240+
{
241+
return nbt.get("Amount", 0.0);
242+
}
243+
244+
/**
245+
* Sets the amount of the modification.
246+
* @param amount
247+
*/
248+
public final void setAmount(double amount)
249+
{
250+
nbt.put("Amount", amount);
251+
}
252+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright or © or Copr. ZLib contributors (2015 - 2016)
3+
*
4+
* This software is governed by the CeCILL-B license under French law and
5+
* abiding by the rules of distribution of free software. You can use,
6+
* modify and/ or redistribute the software under the terms of the CeCILL-B
7+
* license as circulated by CEA, CNRS and INRIA at the following URL
8+
* "http://www.cecill.info".
9+
*
10+
* As a counterpart to the access to the source code and rights to copy,
11+
* modify and redistribute granted by the license, users are provided only
12+
* with a limited warranty and the software's author, the holder of the
13+
* economic rights, and the successive licensors have only limited
14+
* liability.
15+
*
16+
* In this respect, the user's attention is drawn to the risks associated
17+
* with loading, using, modifying and/or developing or reproducing the
18+
* software by the user in light of its specific status of free software,
19+
* that may mean that it is complicated to manipulate, and that also
20+
* therefore means that it is reserved for developers and experienced
21+
* professionals having in-depth computer knowledge. Users are therefore
22+
* encouraged to load and test the software's suitability as regards their
23+
* requirements in conditions enabling the security of their systems and/or
24+
* data to be ensured and, more generally, to use and operate it in the
25+
* same conditions as regards security.
26+
*
27+
* The fact that you are presently reading this means that you have had
28+
* knowledge of the CeCILL-B license and that you accept its terms.
29+
*/
30+
31+
package fr.zcraft.zlib.components.attributes;
32+
33+
/**
34+
* This enum represents the possible values for an item attribute operation.
35+
*/
36+
public enum AttributeOperation
37+
{
38+
/**
39+
* Operation 0: Additive. Adds all of the modifiers' amounts to the current value of the attribute.
40+
*/
41+
ADDITIVE(0),
42+
/**
43+
* Operation 1: Multiplicative. Multiplies the current value of the attribute by (1 + x), where x is the sum of the modifiers' amounts.
44+
*/
45+
MULTIPLY(1),
46+
/**
47+
* Operation 2: Multiplicative. For every modifier, multiplies the current value of the attribute by (1 + x), where x is the amount of the particular modifier.
48+
* Functions the same as Operation 1 if there is only a single modifier with operation 1 or 2.
49+
* However, for multiple modifiers it will multiply the modifiers rather than adding them.
50+
*/
51+
MULTIPLY_ALL(2);
52+
53+
private final int code;
54+
private AttributeOperation(int code)
55+
{
56+
this.code = code;
57+
}
58+
59+
/**
60+
*
61+
* @return the operation's code.
62+
*/
63+
public int getCode()
64+
{
65+
return code;
66+
}
67+
68+
@Override
69+
public String toString()
70+
{
71+
return String.valueOf(code);
72+
}
73+
74+
/**
75+
* Gets an AttributeOperation from its code.
76+
* @param code The attribute code.
77+
* @return the corresponding attribute operation.
78+
* @throws IllegalArgumentException if the code is invalid.
79+
*/
80+
static public AttributeOperation fromCode(int code) throws IllegalArgumentException
81+
{
82+
for(AttributeOperation operation : values())
83+
{
84+
if(operation.code == code)
85+
return operation;
86+
}
87+
88+
throw new IllegalArgumentException("Illegal Attribute operation code : " + code);
89+
}
90+
}

0 commit comments

Comments
 (0)