Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Commit 59bd18f

Browse files
committed
inventories stufsf
1 parent fdb1679 commit 59bd18f

39 files changed

+1947
-90
lines changed

proxy/src/main/java/org/dragonet/inventory/PEWindowConstantID.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public enum PEWindowConstantID {
1717

1818
private final byte id;
1919

20-
private PEWindowConstantID(int id) {
20+
PEWindowConstantID(int id) {
2121
this.id = (byte) id;
2222
}
2323

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package org.dragonet.proxy.nbt;
2+
3+
import org.dragonet.proxy.nbt.stream.NBTInputStream;
4+
import org.dragonet.proxy.nbt.stream.NBTOutputStream;
5+
import org.dragonet.proxy.nbt.tag.CompoundTag;
6+
import org.dragonet.proxy.nbt.tag.Tag;
7+
8+
import java.io.*;
9+
import java.nio.ByteOrder;
10+
import java.nio.file.Files;
11+
import java.nio.file.StandardCopyOption;
12+
import java.util.Collection;
13+
import java.util.zip.Deflater;
14+
import java.util.zip.DeflaterOutputStream;
15+
import java.util.zip.GZIPInputStream;
16+
import java.util.zip.GZIPOutputStream;
17+
18+
public class NBTIO {
19+
20+
/**
21+
* A Named Binary Tag library for Nukkit Project
22+
*/
23+
24+
public static CompoundTag read(File file) throws IOException {
25+
return read(file, ByteOrder.BIG_ENDIAN);
26+
}
27+
28+
public static CompoundTag read(File file, ByteOrder endianness) throws IOException {
29+
if (!file.exists()) return null;
30+
return read(new FileInputStream(file), endianness);
31+
}
32+
33+
public static CompoundTag read(InputStream inputStream) throws IOException {
34+
return read(inputStream, ByteOrder.BIG_ENDIAN);
35+
}
36+
37+
public static CompoundTag read(InputStream inputStream, ByteOrder endianness) throws IOException {
38+
return read(inputStream, endianness, false);
39+
}
40+
41+
public static CompoundTag read(InputStream inputStream, ByteOrder endianness, boolean network) throws IOException {
42+
try (NBTInputStream stream = new NBTInputStream(inputStream, endianness, network)) {
43+
Tag tag = Tag.readNamedTag(stream);
44+
if (tag instanceof CompoundTag) {
45+
return (CompoundTag) tag;
46+
}
47+
throw new IOException("Root tag must be a named compound tag");
48+
}
49+
}
50+
51+
public static CompoundTag read(byte[] data) throws IOException {
52+
return read(data, ByteOrder.BIG_ENDIAN);
53+
}
54+
55+
public static CompoundTag read(byte[] data, ByteOrder endianness) throws IOException {
56+
return read(new ByteArrayInputStream(data), endianness);
57+
}
58+
59+
public static CompoundTag read(byte[] data, ByteOrder endianness, boolean network) throws IOException {
60+
return read(new ByteArrayInputStream(data), endianness, network);
61+
}
62+
63+
public static CompoundTag readCompressed(InputStream inputStream) throws IOException {
64+
return readCompressed(inputStream, ByteOrder.BIG_ENDIAN);
65+
}
66+
67+
public static CompoundTag readCompressed(InputStream inputStream, ByteOrder endianness) throws IOException {
68+
return read(new BufferedInputStream(new GZIPInputStream(inputStream)), endianness);
69+
}
70+
71+
public static CompoundTag readCompressed(byte[] data) throws IOException {
72+
return readCompressed(data, ByteOrder.BIG_ENDIAN);
73+
}
74+
75+
public static CompoundTag readCompressed(byte[] data, ByteOrder endianness) throws IOException {
76+
return read(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(data))), endianness, true);
77+
}
78+
79+
public static CompoundTag readNetworkCompressed(InputStream inputStream) throws IOException {
80+
return readNetworkCompressed(inputStream, ByteOrder.BIG_ENDIAN);
81+
}
82+
83+
public static CompoundTag readNetworkCompressed(InputStream inputStream, ByteOrder endianness) throws IOException {
84+
return read(new BufferedInputStream(new GZIPInputStream(inputStream)), endianness);
85+
}
86+
87+
public static CompoundTag readNetworkCompressed(byte[] data) throws IOException {
88+
return readNetworkCompressed(data, ByteOrder.BIG_ENDIAN);
89+
}
90+
91+
public static CompoundTag readNetworkCompressed(byte[] data, ByteOrder endianness) throws IOException {
92+
return read(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(data))), endianness, true);
93+
}
94+
95+
public static byte[] write(CompoundTag tag) throws IOException {
96+
return write(tag, ByteOrder.BIG_ENDIAN);
97+
}
98+
99+
public static byte[] write(CompoundTag tag, ByteOrder endianness) throws IOException {
100+
return write(tag, endianness, false);
101+
}
102+
103+
public static byte[] write(CompoundTag tag, ByteOrder endianness, boolean network) throws IOException {
104+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
105+
try (NBTOutputStream stream = new NBTOutputStream(baos, endianness, network)) {
106+
Tag.writeNamedTag(tag, stream);
107+
return baos.toByteArray();
108+
}
109+
}
110+
111+
public static byte[] write(Collection<CompoundTag> tags) throws IOException {
112+
return write(tags, ByteOrder.BIG_ENDIAN);
113+
}
114+
115+
public static byte[] write(Collection<CompoundTag> tags, ByteOrder endianness) throws IOException {
116+
return write(tags, endianness, false);
117+
}
118+
119+
public static byte[] write(Collection<CompoundTag> tags, ByteOrder endianness, boolean network) throws IOException {
120+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
121+
try (NBTOutputStream stream = new NBTOutputStream(baos, endianness, network)) {
122+
for (CompoundTag tag : tags) {
123+
Tag.writeNamedTag(tag, stream);
124+
}
125+
return baos.toByteArray();
126+
}
127+
}
128+
129+
public static void write(CompoundTag tag, File file) throws IOException {
130+
write(tag, file, ByteOrder.BIG_ENDIAN);
131+
}
132+
133+
public static void write(CompoundTag tag, File file, ByteOrder endianness) throws IOException {
134+
write(tag, new FileOutputStream(file), endianness);
135+
}
136+
137+
public static void write(CompoundTag tag, OutputStream outputStream) throws IOException {
138+
write(tag, outputStream, ByteOrder.BIG_ENDIAN);
139+
}
140+
141+
public static void write(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException {
142+
write(tag, outputStream, endianness, false);
143+
}
144+
145+
public static void write(CompoundTag tag, OutputStream outputStream, ByteOrder endianness, boolean network) throws IOException {
146+
try (NBTOutputStream stream = new NBTOutputStream(outputStream, endianness, network)) {
147+
Tag.writeNamedTag(tag, stream);
148+
}
149+
}
150+
151+
public static byte[] writeGZIPCompressed(CompoundTag tag) throws IOException {
152+
return writeGZIPCompressed(tag, ByteOrder.BIG_ENDIAN);
153+
}
154+
155+
public static byte[] writeGZIPCompressed(CompoundTag tag, ByteOrder endianness) throws IOException {
156+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
157+
writeGZIPCompressed(tag, baos, endianness);
158+
return baos.toByteArray();
159+
}
160+
161+
public static void writeGZIPCompressed(CompoundTag tag, OutputStream outputStream) throws IOException {
162+
writeGZIPCompressed(tag, outputStream, ByteOrder.BIG_ENDIAN);
163+
}
164+
165+
public static void writeGZIPCompressed(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException {
166+
write(tag, new GZIPOutputStream(outputStream), endianness);
167+
}
168+
169+
public static byte[] writeNetworkGZIPCompressed(CompoundTag tag) throws IOException {
170+
return writeNetworkGZIPCompressed(tag, ByteOrder.BIG_ENDIAN);
171+
}
172+
173+
public static byte[] writeNetworkGZIPCompressed(CompoundTag tag, ByteOrder endianness) throws IOException {
174+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
175+
writeNetworkGZIPCompressed(tag, baos, endianness);
176+
return baos.toByteArray();
177+
}
178+
179+
public static void writeNetworkGZIPCompressed(CompoundTag tag, OutputStream outputStream) throws IOException {
180+
writeNetworkGZIPCompressed(tag, outputStream, ByteOrder.BIG_ENDIAN);
181+
}
182+
183+
public static void writeNetworkGZIPCompressed(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException {
184+
write(tag, new GZIPOutputStream(outputStream), endianness, true);
185+
}
186+
187+
public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream) throws IOException {
188+
writeZLIBCompressed(tag, outputStream, ByteOrder.BIG_ENDIAN);
189+
}
190+
191+
public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException {
192+
writeZLIBCompressed(tag, outputStream, Deflater.DEFAULT_COMPRESSION, endianness);
193+
}
194+
195+
public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream, int level) throws IOException {
196+
writeZLIBCompressed(tag, outputStream, level, ByteOrder.BIG_ENDIAN);
197+
}
198+
199+
public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream, int level, ByteOrder endianness) throws IOException {
200+
write(tag, new DeflaterOutputStream(outputStream, new Deflater(level)), endianness);
201+
}
202+
203+
public static void safeWrite(CompoundTag tag, File file) throws IOException {
204+
File tmpFile = new File(file.getAbsolutePath() + "_tmp");
205+
if (tmpFile.exists()) {
206+
tmpFile.delete();
207+
}
208+
write(tag, tmpFile);
209+
Files.move(tmpFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
210+
}
211+
212+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package org.dragonet.proxy.nbt.stream;
2+
3+
import org.dragonet.proxy.utilities.VarInt;
4+
5+
import java.io.DataInput;
6+
import java.io.DataInputStream;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.nio.ByteOrder;
10+
import java.nio.charset.StandardCharsets;
11+
12+
/**
13+
* author: MagicDroidX
14+
* Nukkit Project
15+
*/
16+
public class NBTInputStream implements DataInput, AutoCloseable {
17+
private final DataInputStream stream;
18+
private final ByteOrder endianness;
19+
private final boolean network;
20+
21+
public NBTInputStream(InputStream stream) {
22+
this(stream, ByteOrder.BIG_ENDIAN);
23+
}
24+
25+
public NBTInputStream(InputStream stream, ByteOrder endianness) {
26+
this(stream, endianness, false);
27+
}
28+
29+
public NBTInputStream(InputStream stream, ByteOrder endianness, boolean network) {
30+
this.stream = stream instanceof DataInputStream ? (DataInputStream) stream : new DataInputStream(stream);
31+
this.endianness = endianness;
32+
this.network = network;
33+
}
34+
35+
public ByteOrder getEndianness() {
36+
return endianness;
37+
}
38+
39+
public boolean isNetwork() {
40+
return network;
41+
}
42+
43+
@Override
44+
public void readFully(byte[] b) throws IOException {
45+
this.stream.readFully(b);
46+
}
47+
48+
@Override
49+
public void readFully(byte[] b, int off, int len) throws IOException {
50+
this.stream.readFully(b, off, len);
51+
}
52+
53+
@Override
54+
public int skipBytes(int n) throws IOException {
55+
return this.stream.skipBytes(n);
56+
}
57+
58+
@Override
59+
public boolean readBoolean() throws IOException {
60+
return this.stream.readBoolean();
61+
}
62+
63+
@Override
64+
public byte readByte() throws IOException {
65+
return this.stream.readByte();
66+
}
67+
68+
@Override
69+
public int readUnsignedByte() throws IOException {
70+
return this.stream.readUnsignedByte();
71+
}
72+
73+
@Override
74+
public short readShort() throws IOException {
75+
short s = this.stream.readShort();
76+
if (endianness == ByteOrder.LITTLE_ENDIAN) {
77+
s = Short.reverseBytes(s);
78+
}
79+
return s;
80+
}
81+
82+
@Override
83+
public int readUnsignedShort() throws IOException {
84+
int s = this.stream.readUnsignedShort();
85+
if (endianness == ByteOrder.LITTLE_ENDIAN) {
86+
s = Integer.reverseBytes(s) >> 16;
87+
}
88+
return s;
89+
}
90+
91+
@Override
92+
public char readChar() throws IOException {
93+
char c = this.stream.readChar();
94+
if (endianness == ByteOrder.LITTLE_ENDIAN) {
95+
c = Character.reverseBytes(c);
96+
}
97+
return c;
98+
}
99+
100+
@Override
101+
public int readInt() throws IOException {
102+
if (network) {
103+
return VarInt.readVarInt(this.stream);
104+
}
105+
int i = this.stream.readInt();
106+
if (endianness == ByteOrder.LITTLE_ENDIAN) {
107+
i = Integer.reverseBytes(i);
108+
}
109+
return i;
110+
}
111+
112+
@Override
113+
public long readLong() throws IOException {
114+
if (network) {
115+
return VarInt.readVarLong(this.stream);
116+
}
117+
long l = this.stream.readLong();
118+
if (endianness == ByteOrder.LITTLE_ENDIAN) {
119+
l = Long.reverseBytes(l);
120+
}
121+
return l;
122+
}
123+
124+
@Override
125+
public float readFloat() throws IOException {
126+
return Float.intBitsToFloat(this.readInt());
127+
}
128+
129+
@Override
130+
public double readDouble() throws IOException {
131+
return Double.longBitsToDouble(this.readLong());
132+
}
133+
134+
@Override
135+
@Deprecated
136+
public String readLine() throws IOException {
137+
return this.stream.readLine();
138+
}
139+
140+
@Override
141+
public String readUTF() throws IOException {
142+
int length = (int) (network ? VarInt.readUnsignedVarInt(stream) : this.readUnsignedShort());
143+
byte[] bytes = new byte[length];
144+
this.stream.read(bytes);
145+
return new String(bytes, StandardCharsets.UTF_8);
146+
}
147+
148+
public int available() throws IOException {
149+
return this.stream.available();
150+
}
151+
152+
@Override
153+
public void close() throws IOException {
154+
this.stream.close();
155+
}
156+
}

0 commit comments

Comments
 (0)