/
TagUtil.java
71 lines (60 loc) · 1.9 KB
/
TagUtil.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
package knightminer.inspirations.library.util;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
public final class TagUtil {
private TagUtil() {}
/* Generic Tag Operations */
public static NBTTagCompound getTagSafe(ItemStack stack) {
// yes, the null checks aren't needed anymore, but they don't hurt either.
// After all the whole purpose of this function is safety/processing possibly invalid input ;)
if(stack == null || stack.getItem() == null || !stack.hasTagCompound()) {
return new NBTTagCompound();
}
return stack.getTagCompound();
}
public static NBTTagCompound getTagSafe(NBTTagCompound tag, String key) {
if(tag == null) {
return new NBTTagCompound();
}
return tag.getCompoundTag(key);
}
/* BlockPos */
/**
* Converts a block position to NBT
* @param pos Position
* @return NBT compound
*/
public static NBTTagCompound writePos(BlockPos pos) {
NBTTagCompound tag = new NBTTagCompound();
if(pos != null) {
tag.setInteger("X", pos.getX());
tag.setInteger("Y", pos.getY());
tag.setInteger("Z", pos.getZ());
}
return tag;
}
/**
* Reads a block position from a given tag compound
* @param tag Tag to read
* @return BlockPos, or null if invalid
*/
public static BlockPos readPos(NBTTagCompound tag) {
if(tag != null && tag.hasKey("X", 99) && tag.hasKey("Y", 99) && tag.hasKey("Z", 99)) {
return new BlockPos(tag.getInteger("X"), tag.getInteger("Y"), tag.getInteger("Z"));
}
return null;
}
/* JSON */
public static JsonElement getElement(JsonObject json, String memberName) {
if(json.has(memberName)) {
return json.get(memberName);
}
else {
throw new JsonSyntaxException("Missing " + memberName + " from the current json, Invalid JSON!");
}
}
}