1414
1515import java .io .ByteArrayOutputStream ;
1616import java .io .DataOutputStream ;
17- import java .nio .ByteOrder ;
1817import org .dragonet .net .packet .minecraft .FullChunkPacket ;
1918import org .dragonet .net .packet .minecraft .PEPacket ;
20- import org .dragonet .proxy .nbt .PENBT ;
2119import org .dragonet .proxy .network .UpstreamSession ;
2220import org .dragonet .proxy .network .translator .ItemBlockTranslator ;
2321import org .dragonet .proxy .network .translator .PCPacketTranslator ;
@@ -30,93 +28,114 @@ public class PCMultiChunkDataPacketTranslator implements PCPacketTranslator<Serv
3028 public PEPacket [] translate (UpstreamSession session , ServerMultiChunkDataPacket packet ) {
3129
3230 session .getProxy ().getGeneralThreadPool ().execute (() -> {
33- ByteArrayOutputStream bos1 = new ByteArrayOutputStream ();
34- DataOutputStream dos1 = new DataOutputStream (bos1 );
35-
36- ByteArrayOutputStream bos2 = new ByteArrayOutputStream ();
37- DataOutputStream dos2 = new DataOutputStream (bos2 );
38-
39- ByteArrayOutputStream bosTiles = new ByteArrayOutputStream ();
40-
41- try {
42- for (int col = 0 ; col < packet .getColumns (); col ++) {
43- bos1 .reset ();
44- bos2 .reset ();
45- bosTiles .reset ();
46-
47- FullChunkPacket chunkToSend = new FullChunkPacket ();
48- chunkToSend .chunkX = packet .getX (col );
49- chunkToSend .chunkZ = packet .getZ (col );
50- chunkToSend .order = FullChunkPacket .ChunkOrder .COLUMNS ;
51- Chunk [] pcChunks = packet .getChunks (col );
52- for (int x = 0 ; x < 16 ; x ++) {
53- for (int z = 0 ; z < 16 ; z ++) {
54- for (int y = 0 ; y < 128 ; y ++) {
55- if (pcChunks [y >> 4 ] == null || pcChunks [y >> 4 ].isEmpty ()) {
56- dos1 .writeByte ((byte ) 0 );
57- } else {
58- int pcBlock = pcChunks [y >> 4 ].getBlocks ().getBlock (x , y % 16 , z );
59- int peBlock = ItemBlockTranslator .translateToPE (pcBlock );
60- dos1 .writeByte ((byte ) (peBlock & 0xFF ));
61- if (peBlock == 63 || peBlock == 68 ) {
62- PENBT .write (ItemBlockTranslator .newTileTag ("Sign" , (chunkToSend .chunkX << 4 ) | x , y , (chunkToSend .chunkZ << 4 ) | z ), bosTiles , ByteOrder .LITTLE_ENDIAN );
63- } else if (peBlock == 54 || peBlock == 146 ) {
64- //TODO: Chest paring
65- /**
66- * META & 0b111 => [0] z- = x-,x+ [1]
67- * z-. [2] z-. [3] z+ = x-,x+. [4] x- =
68- * z-,z+. [5] x+ = z-,z+.
69- */
70- PENBT .write (ItemBlockTranslator .newTileTag ("Chest" , (chunkToSend .chunkX << 4 ) | x , y , (chunkToSend .chunkZ << 4 ) | z ), bosTiles , ByteOrder .LITTLE_ENDIAN );
71- }
72- }
73- }
74- }
75- }
76- for (int x = 0 ; x < 16 ; x ++) {
77- for (int z = 0 ; z < 16 ; z ++) {
78- for (int y = 0 ; y < 128 ; y += 2 ) {
79- byte data1 = pcChunks [y >> 4 ] == null || pcChunks [y >> 4 ].isEmpty () ? (byte ) 0 : (byte ) ((pcChunks [y >> 4 ].getBlocks ().getData (x , y % 16 , z ) & 0xF ) << 4 );
80- data1 |= pcChunks [(y + 1 ) >> 4 ] == null || pcChunks [(y + 1 ) >> 4 ].isEmpty () ? (byte ) 0 : (byte ) (pcChunks [(y + 1 ) >> 4 ].getBlocks ().getData (x , (y + 1 ) % 16 , z ) & 0xF );
81- dos1 .writeByte (data1 );
82- byte data2 = pcChunks [y >> 4 ] == null || pcChunks [y >> 4 ].isEmpty () ? (byte ) 0 : (byte ) ((pcChunks [y >> 4 ].getSkyLight ().get (x , y % 16 , z ) & 0xF ) << 4 );
83- data2 |= pcChunks [(y + 1 ) >> 4 ] == null || pcChunks [(y + 1 ) >> 4 ].isEmpty () ? (byte ) 0 : (byte ) (pcChunks [(y + 1 ) >> 4 ].getSkyLight ().get (x , (y + 1 ) % 16 , z ) & 0xF );
84- dos2 .writeByte (data2 );
85- }
86- }
87- }
88- dos1 .write (bos2 .toByteArray ()); //Not bos1 contains previously generated data! Don't reset!
89- bos2 .reset ();//Now it's empty
90- for (int x = 0 ; x < 16 ; x ++) {
91- for (int z = 0 ; z < 16 ; z ++) {
92- for (int y = 0 ; y < 128 ; y += 2 ) {
93- byte data = pcChunks [y >> 4 ] == null || pcChunks [y >> 4 ].isEmpty () ? (byte ) 0 : (byte ) ((pcChunks [y >> 4 ].getBlockLight ().get (x , y % 16 , z ) & 0xF ) << 4 );
94- data |= pcChunks [(y + 1 ) >> 4 ] == null || pcChunks [(y + 1 ) >> 4 ].isEmpty () ? (byte ) 0 : (byte ) (pcChunks [(y + 1 ) >> 4 ].getBlockLight ().get (x , (y + 1 ) % 16 , z ) & 0xF );
95- dos1 .writeByte (data );
96- }
97- }
98- }
99- //Height Map
100- for (int i = 0 ; i < 256 ; i ++) {
101- dos1 .writeByte ((byte ) 0xFF );
102- }
103-
104- //Biome Colors
105- for (int i = 0 ; i < 256 ; i ++) {
106- dos1 .writeByte ((byte ) 0x01 );
107- dos1 .writeByte ((byte ) 0x85 );
108- dos1 .writeByte ((byte ) 0xB2 );
109- dos1 .writeByte ((byte ) 0x4A );
110- }
111-
112- bos2 .reset ();
113-
114- dos1 .writeInt (0 );//Extra data, should be little-endian but it's 0 here for now so it's okay.
115-
116- dos1 .write (bosTiles .toByteArray ());
117-
118- chunkToSend .chunkData = bos1 .toByteArray ();
119- session .sendPacket (chunkToSend , true );
31+ ByteArrayOutputStream bos1 = new ByteArrayOutputStream ();
32+ DataOutputStream dos1 = new DataOutputStream (bos1 );
33+
34+ ByteArrayOutputStream bos2 = new ByteArrayOutputStream ();
35+ DataOutputStream dos2 = new DataOutputStream (bos2 );
36+
37+ ByteArrayOutputStream bosTiles = new ByteArrayOutputStream ();
38+ DataOutputStream dosTiles = new DataOutputStream (bosTiles );
39+
40+ try {
41+ for (int col = 0 ; col < packet .getColumns (); col ++) {
42+ bos1 .reset ();
43+ bos2 .reset ();
44+ bosTiles .reset ();
45+
46+ FullChunkPacket pePacket = new FullChunkPacket ();
47+
48+ pePacket .chunkX = packet .getX (col );
49+ pePacket .chunkZ = packet .getZ (col );
50+ pePacket .order = FullChunkPacket .ChunkOrder .COLUMNS ;
51+ Chunk [] pcChunks = packet .getChunks (col );
52+ for (int x = 0 ; x < 16 ; x ++) {
53+ for (int z = 0 ; z < 16 ; z ++) {
54+ for (int y = 0 ; y < 128 ; y ++) {
55+ if (pcChunks [y >> 4 ] == null || pcChunks [y >> 4 ].isEmpty ()) {
56+ dos1 .writeByte ((byte ) 0 );
57+ } else {
58+ int pcBlock = pcChunks [y >> 4 ].getBlocks ().getBlock (x , y % 16 , z );
59+ int peBlock = pcBlock ;
60+ peBlock = ItemBlockTranslator .translateToPE (peBlock );
61+ dos1 .writeByte ((byte ) (peBlock & 0xFF ));
62+ // dos1.writeByte((byte) 1);
63+ }
64+ }
65+ }
66+ }
67+ for (int x = 0 ; x < 16 ; x ++) {
68+ for (int z = 0 ; z < 16 ; z ++) {
69+ for (int y = 0 ; y < 128 ; y += 2 ) {
70+ byte data1 = 0 ;
71+ byte data2 = 0 ;
72+ try {
73+ data1 = (byte ) pcChunks [y >> 4 ].getBlocks ().getData (x , y % 16 , z );
74+ } catch (Exception e ) {
75+
76+ }
77+ try {
78+ data2 = (byte ) pcChunks [y >> 4 ].getBlocks ().getData (x , (y + 1 ) % 16 , z );
79+ } catch (Exception e ) {
80+
81+ }
82+
83+ data1 |= ((data2 & 0xF ) << 4 );
84+
85+ dos1 .writeByte (data1 );
86+ }
87+ }
88+ }
89+ for (int x = 0 ; x < 16 ; x ++) {
90+ for (int z = 0 ; z < 16 ; z ++) {
91+ for (int y = 0 ; y < 128 ; y += 2 ) {
92+ //if (noSkyLight) {
93+ // temp.writeByte(0);
94+ //} else {
95+ byte data = 0 ;
96+ try {
97+ data = (byte ) (pcChunks [y >> 4 ].getSkyLight ().get (x , y & 0xF , z ) & 0xF );
98+ data |= (pcChunks [y >> 4 ].getSkyLight ().get (x , (y + 1 ) & 0xF , z ) & 0xF );
99+ } catch (Exception e ) {
100+
101+ }
102+ dos1 .writeByte (data );
103+ //}
104+ }
105+ }
106+ }
107+ dos1 .write (bos2 .toByteArray ()); //Not bos1 contains previously generated data! Don't reset!
108+ bos2 .reset ();//Now it's empty
109+ for (int x = 0 ; x < 16 ; x ++) {
110+ for (int z = 0 ; z < 16 ; z ++) {
111+ for (int y = 0 ; y < 128 ; y += 2 ) {
112+ byte data = pcChunks [y >> 4 ] == null || pcChunks [y >> 4 ].isEmpty () ? (byte ) 0 : (byte ) ((pcChunks [y >> 4 ].getBlockLight ().get (x , y % 16 , z ) & 0xF ) << 4 );
113+ data |= pcChunks [(y + 1 ) >> 4 ] == null || pcChunks [(y + 1 ) >> 4 ].isEmpty () ? (byte ) 0 : (byte ) (pcChunks [(y + 1 ) >> 4 ].getBlockLight ().get (x , (y + 1 ) % 16 , z ) & 0xF );
114+ dos1 .writeByte (data );
115+ }
116+ }
117+ }
118+ //Height Map
119+ for (int i = 0 ; i < 256 ; i ++) {
120+ dos1 .writeByte ((byte ) 0xFF );
121+ }
122+
123+ //Biome Colors
124+ for (int i = 0 ; i < 256 ; i ++) {
125+ dos1 .writeByte ((byte ) 0x01 );
126+ dos1 .writeByte ((byte ) 0x85 );
127+ dos1 .writeByte ((byte ) 0xB2 );
128+ dos1 .writeByte ((byte ) 0x4A );
129+ }
130+
131+ bos2 .reset ();
132+
133+ dos1 .writeInt (0 );//Extra data, should be little-endian but it's 0 here for now so it's okay.
134+
135+ dos1 .write (bosTiles .toByteArray ());
136+
137+ pePacket .chunkData = bos1 .toByteArray ();
138+ session .sendPacket (pePacket , true );
120139 }
121140 } catch (Exception e ) {
122141 }
0 commit comments