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
|
package kubatech.api;
import cpw.mods.fml.common.registry.GameRegistry;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.charset.StandardCharsets;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;
public class ConstructableItemStack {
public final GameRegistry.UniqueIdentifier itemIdentifier;
public final int meta;
public final int size;
public final NBTTagCompound tagCompound;
private ConstructableItemStack(
GameRegistry.UniqueIdentifier itemIdentifier, int meta, int size, NBTTagCompound tagCompound) {
this.itemIdentifier = itemIdentifier;
this.meta = meta;
this.size = size;
this.tagCompound = tagCompound;
}
public ConstructableItemStack(ItemStack stack) {
itemIdentifier = GameRegistry.findUniqueIdentifierFor(stack.getItem());
meta = stack.getItemDamage();
size = stack.stackSize;
tagCompound = stack.stackTagCompound;
}
public ItemStack construct() {
if (itemIdentifier == null) return null;
Item it = GameRegistry.findItem(itemIdentifier.modId, itemIdentifier.name);
if (it == null) return null;
ItemStack stack = new ItemStack(it, size, meta);
stack.stackTagCompound = tagCompound;
return stack;
}
public boolean isSame(ConstructableItemStack stack, boolean ignoreSize) {
if (!stack.itemIdentifier.modId.equals(itemIdentifier.modId)) return false;
if (!stack.itemIdentifier.name.equals(itemIdentifier.name)) return false;
return ignoreSize || stack.size == size;
}
private static final ByteBuf BufHelper = Unpooled.buffer();
public void writeToByteBuf(ByteBuf byteBuf) {
BufHelper.clear();
byte[] bytes = itemIdentifier.modId.getBytes(StandardCharsets.UTF_8);
BufHelper.writeInt(bytes.length);
BufHelper.writeBytes(bytes);
bytes = itemIdentifier.name.getBytes(StandardCharsets.UTF_8);
BufHelper.writeInt(bytes.length);
BufHelper.writeBytes(bytes);
BufHelper.writeInt(meta);
BufHelper.writeInt(size);
BufHelper.writeBoolean(tagCompound != null);
if (tagCompound != null) {
try {
bytes = CompressedStreamTools.compress(tagCompound);
} catch (Exception ignored) {
bytes = new byte[0];
}
BufHelper.writeInt(bytes.length);
BufHelper.writeBytes(bytes);
}
byteBuf.writeInt(BufHelper.readableBytes());
byteBuf.writeBytes(BufHelper);
}
public static ConstructableItemStack readFromByteBuf(ByteBuf byteBuf) {
int size = byteBuf.readInt();
byte[] bytes = new byte[byteBuf.readInt()];
byteBuf.readBytes(bytes);
String modid = new String(bytes, StandardCharsets.UTF_8);
bytes = new byte[byteBuf.readInt()];
byteBuf.readBytes(bytes);
String name = new String(bytes, StandardCharsets.UTF_8);
int meta = byteBuf.readInt();
int stacksize = byteBuf.readInt();
NBTTagCompound nbtTagCompound = null;
if (byteBuf.readBoolean()) {
bytes = new byte[byteBuf.readInt()];
byteBuf.readBytes(bytes);
try {
nbtTagCompound = CompressedStreamTools.func_152457_a(bytes, new NBTSizeTracker(2097152L));
} catch (Exception ignored) {
}
}
return new ConstructableItemStack(
new GameRegistry.UniqueIdentifier(modid + ":" + name), meta, stacksize, nbtTagCompound);
}
}
|