-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWindowItems.java
144 lines (126 loc) · 5.03 KB
/
WindowItems.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
package dev.simplix.protocolize.data.packets;
import dev.simplix.protocolize.api.PacketDirection;
import dev.simplix.protocolize.api.item.BaseItemStack;
import dev.simplix.protocolize.api.item.ItemStack;
import dev.simplix.protocolize.api.item.ItemStackSerializer;
import dev.simplix.protocolize.api.mapping.AbstractProtocolMapping;
import dev.simplix.protocolize.api.mapping.ProtocolIdMapping;
import dev.simplix.protocolize.api.packet.AbstractPacket;
import dev.simplix.protocolize.api.util.ProtocolUtil;
import dev.simplix.protocolize.data.util.LazyBuffer;
import io.netty.buffer.ByteBuf;
import lombok.*;
import lombok.experimental.Accessors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static dev.simplix.protocolize.api.util.ProtocolVersions.*;
/**
* Date: 25.08.2021
*
* @author Exceptionflug
*/
@Getter
@Setter
@ToString
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(fluent = true)
public class WindowItems extends AbstractPacket {
/* ClientboundContainerSetContentPacket */
public static final List<ProtocolIdMapping> MAPPINGS = Arrays.asList(
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_8, MINECRAFT_1_8, 0x30),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_9, MINECRAFT_1_12_2, 0x14),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_13, MINECRAFT_1_13_2, 0x15),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_14, MINECRAFT_1_14_4, 0x14),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_15, MINECRAFT_1_15_2, 0x15),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_16, MINECRAFT_1_16_1, 0x14),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_16_2, MINECRAFT_1_16_4, 0x13),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_17, MINECRAFT_1_18_2, 0x14),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_19, MINECRAFT_1_19_2, 0x11),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_19_3, MINECRAFT_1_19_3, 0x10),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_19_4, MINECRAFT_1_20_1, 0x12),
AbstractProtocolMapping.rangedIdMapping(MINECRAFT_1_20_2, MINECRAFT_LATEST, 0x13)
);
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private LazyBuffer lazyBuffer = LazyBuffer.empty();
private int windowId;
private List<BaseItemStack> items = new ArrayList<>();
private int count;
/**
* @since Protocol 756
*/
private BaseItemStack cursorItem = ItemStack.NO_DATA;
/**
* @since Protocol 756
*/
private int stateId;
public WindowItems(int windowId, List<BaseItemStack> items, int stateId) {
this.windowId = windowId;
this.items = items;
this.count = items.size();
this.stateId = stateId;
}
@Override
public void read(ByteBuf buf, PacketDirection packetDirection, int protocolVersion) {
this.windowId = (protocolVersion >= MINECRAFT_1_21_2) ? ProtocolUtil.readVarInt(buf) : buf.readUnsignedByte();
if (protocolVersion >= MINECRAFT_1_17_1) {
this.stateId = ProtocolUtil.readVarInt(buf);
this.count = ProtocolUtil.readVarInt(buf);
} else {
this.count = buf.readShort();
}
byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data);
this.lazyBuffer = new LazyBuffer(data, (wrappedBuf) -> {
for (int i = 0; i < this.count; i++) {
this.items.add(ItemStackSerializer.read(wrappedBuf, protocolVersion));
}
if (protocolVersion >= MINECRAFT_1_17_1) {
this.cursorItem = ItemStackSerializer.read(wrappedBuf, protocolVersion);
}
});
}
@Override
public void write(ByteBuf buf, PacketDirection packetDirection, int protocolVersion) {
if(protocolVersion >= MINECRAFT_1_21_2) {
ProtocolUtil.writeVarInt(buf, this.windowId);
} else {
buf.writeByte(this.windowId & 0xFF);
}
if (protocolVersion >= MINECRAFT_1_17_1) {
ProtocolUtil.writeVarInt(buf, this.stateId);
ProtocolUtil.writeVarInt(buf, this.count);
} else {
buf.writeShort(this.count);
}
this.lazyBuffer.write(buf, () -> {
for (BaseItemStack item : this.items) {
if (item == null) {
item = ItemStack.NO_DATA;
}
ItemStackSerializer.write(buf, item, protocolVersion);
}
if (protocolVersion >= MINECRAFT_1_17_1) {
ItemStackSerializer.write(buf, this.cursorItem, protocolVersion);
}
});
}
public List<BaseItemStack> items() {
this.lazyBuffer.read();
return this.items;
}
public void items(List<BaseItemStack> items) {
this.lazyBuffer = LazyBuffer.empty();
this.items = items;
this.count = items.size();
}
/**
* @since Protocol 756
*/
public BaseItemStack cursorItem() {
this.lazyBuffer.read();
return this.cursorItem;
}
}