Advertisement
Guest User

Untitled

a guest
Mar 13th, 2016
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.97 KB | None | 0 0
  1. public class SchematicsManager {
  2.  
  3. private byte[] blocks, metadata;
  4. private short width, height, length;
  5.  
  6. public void loadSchematic(DataInput in) throws Exception{
  7. NBT_Tag compound = NBT_Tag.readTag(in);
  8. NBT_Tag_Compound tag = (NBT_Tag_Compound) compound;
  9.  
  10. this.width = ((NBT_Tag_Short)tag.payload.get("Width")).payload;
  11. this.height = ((NBT_Tag_Short)tag.payload.get("Height")).payload;
  12. this.length = ((NBT_Tag_Short)tag.payload.get("Length")).payload;
  13.  
  14. this.blocks = ((NBT_Tag_Byte_Array)tag.payload.get("Blocks")).payload;
  15. this.metadata = ((NBT_Tag_Byte_Array)tag.payload.get("Data")).payload;
  16. }
  17.  
  18. public void writeUncompressedSchematic(DataOutput out) throws IOException {
  19. NBT_Tag_Compound root = new NBT_Tag_Compound("Schematic");
  20. root.payload.put("Width", new NBT_Tag_Short("Width",this.width));
  21. root.payload.put("Height", new NBT_Tag_Short("Height",this.height));
  22. root.payload.put("Length", new NBT_Tag_Short("Length",this.length));
  23.  
  24. root.payload.put("Blocks", new NBT_Tag_Byte_Array("Blocks",this.blocks));
  25. root.payload.put("Data", new NBT_Tag_Byte_Array("Data",this.metadata));
  26. root.writeTag(out);
  27. }
  28.  
  29. public void writeUncompressedSchematic(File f) throws IOException {
  30. DataOutputStream out = new DataOutputStream(new FileOutputStream(f));
  31. writeUncompressedSchematic(out);
  32. out.close();
  33. }
  34.  
  35. public void writeGzipedSchematic(File f) throws IOException {
  36. DataOutputStream out = new DataOutputStream(new GZIPOutputStream(new FileOutputStream(f)));
  37. writeUncompressedSchematic(out);
  38. out.close();
  39. }
  40.  
  41. public void loadGzipedSchematic(InputStream is) throws Exception {
  42. DataInputStream in = new DataInputStream(new GZIPInputStream(is));
  43. loadSchematic(in);
  44. in.close();
  45. }
  46.  
  47. public void loadUncompressedSchematic(InputStream is) throws Exception {
  48. DataInputStream in = new DataInputStream(is);
  49. loadSchematic(in);
  50. in.close();
  51. }
  52.  
  53. private int getBlockOffset(int x, int y, int z) {
  54. return y * width * length + z * width + x;
  55. }
  56.  
  57. public byte getBlockIdAt(int x, int y, int z) {
  58. int offset = getBlockOffset(x, y, z);
  59. if (offset < this.blocks.length && offset >= 0)
  60. return this.blocks[offset];
  61. else
  62. return -1;
  63. }
  64.  
  65. public void setBlockIdAt(int x, int y, int z, byte id) {
  66. int offset = getBlockOffset(x, y, z);
  67. if (offset < this.blocks.length && offset >= 0)
  68. this.blocks[offset] = id;
  69. }
  70.  
  71. public byte getMetadataAt(int x, int y, int z) {
  72. int offset = getBlockOffset(x, y, z);
  73. if (offset < this.metadata.length && offset >= 0)
  74. return this.metadata[offset];
  75. else
  76. return 0;
  77. }
  78.  
  79. public void setMetadataIdAt(int x, int y, int z, byte data) {
  80. int offset = getBlockOffset(x, y, z);
  81. if (offset < this.metadata.length && offset >= 0)
  82. this.metadata[offset] = data;
  83. }
  84.  
  85. public short getWidth() {
  86. return width;
  87. }
  88.  
  89. public void setWidth(short width) {
  90. this.width = width;
  91. }
  92.  
  93. public short getHeight() {
  94. return height;
  95. }
  96.  
  97. public void setHeight(short height) {
  98. this.height = height;
  99. }
  100.  
  101. public short getLength() {
  102. return length;
  103. }
  104.  
  105. public void setLength(short length) {
  106. this.length = length;
  107. }
  108.  
  109. public byte[] getBlocks() {
  110. return blocks;
  111. }
  112.  
  113. public void setBlocks(byte[] blocks) {
  114. this.blocks = blocks;
  115. }
  116.  
  117. public byte[] getMetadata() {
  118. return metadata;
  119. }
  120.  
  121. public void setMetadata(byte[] metadata) {
  122. this.metadata = metadata;
  123. }
  124.  
  125. }
  126.  
  127.  
  128. class NBT_Tag_End extends NBT_Tag{
  129.  
  130. public NBT_Tag_End(String name){
  131. super(0, "");
  132. }
  133.  
  134. public NBT_Tag_End(String name, int payload){
  135. super(8, name);
  136. }
  137.  
  138. @Override
  139. public void readTagPayload(DataInput in) throws IOException {
  140. System.out.println("An error has occoured. An named binary tree tag 'end' has had it's payload read. It doesn't have a payload. Fix your code :D");
  141. }
  142.  
  143. @Override
  144. public void writeTag(DataOutput out) throws IOException {
  145. out.write(this.id);
  146. }
  147.  
  148. public void writePayload(DataOutput out) throws IOException {}
  149.  
  150. }
  151.  
  152. class NBT_Tag_Double extends NBT_Tag{
  153.  
  154. public double payload;
  155.  
  156. public NBT_Tag_Double(String name){
  157. super(6, name);
  158. }
  159.  
  160. public NBT_Tag_Double(String name, double payload){
  161. super(8, name);
  162. this.payload = payload;
  163. }
  164.  
  165. @Override
  166. public void readTagPayload(DataInput in) throws IOException {
  167. this.payload = in.readDouble();
  168. }
  169.  
  170. public void writeTag(DataOutput out) throws IOException {
  171. out.write(this.id);
  172. out.writeUTF(this.name);
  173. this.writePayload(out);
  174. }
  175. public void writePayload(DataOutput out) throws IOException {
  176. out.writeDouble(this.payload);
  177. }
  178. }
  179.  
  180. class NBT_Tag_Compound extends NBT_Tag{
  181.  
  182. public Map<String, NBT_Tag> payload;
  183.  
  184. public NBT_Tag_Compound(String name){
  185. super(10, name);
  186. }
  187. public Map<String, NBT_Tag> getPayload() {
  188. return payload;
  189. }
  190. public NBT_Tag_Compound(String name, Map<String, NBT_Tag> payload){
  191. super(8, name);
  192. this.payload = payload;
  193. }
  194.  
  195. public NBT_Tag_Compound(Map<String, NBT_Tag> payload) {
  196. super(8, "");
  197. this.payload = payload;
  198. }
  199. @Override
  200. public void readTagPayload(DataInput in) throws IOException {
  201. payload = new HashMap<String,NBT_Tag>();
  202. NBT_Tag tag;
  203.  
  204. while ((tag = NBT_Tag.readTag(in)).id != 0) {
  205. this.payload.put(tag.name,tag);
  206. }
  207. this.payload.put("__end",new NBT_Tag_End("__end"));
  208. }
  209.  
  210. public void writeTag(DataOutput out) throws IOException {
  211. out.write(this.id);
  212. out.writeUTF(this.name);
  213. this.writePayload(out);
  214. }
  215.  
  216. public void writePayload(DataOutput out) throws IOException {
  217. for(String key : payload.keySet()) {
  218. NBT_Tag tag = payload.get(key);
  219. tag.writeTag(out);
  220. }
  221. }
  222. }
  223.  
  224. class NBT_Tag_Byte extends NBT_Tag{
  225.  
  226. public byte payload;
  227.  
  228. public NBT_Tag_Byte(String name){
  229. super(1,name);
  230. }
  231. public NBT_Tag_Byte(String name, byte payload){
  232. super(8, name);
  233. this.payload = payload;
  234. }
  235.  
  236. @Override
  237. public void readTagPayload(DataInput in) throws IOException {
  238. this.payload = in.readByte();
  239. }
  240.  
  241. public void writeTag(DataOutput out) throws IOException {
  242. out.write(this.id);
  243. out.writeUTF(this.name);
  244. this.writePayload(out);
  245. }
  246.  
  247. public void writePayload(DataOutput out) throws IOException {
  248. out.write(this.payload);
  249. }
  250. }
  251.  
  252. class NBT_Tag_Byte_Array extends NBT_Tag{
  253.  
  254. public int size;
  255. public byte[] payload;
  256.  
  257. public NBT_Tag_Byte_Array(String name){
  258. super(7, name);
  259. }
  260.  
  261. public NBT_Tag_Byte_Array(String name, byte[] payload){
  262. super(8, name);
  263. this.payload = payload;
  264. }
  265.  
  266. @Override
  267. public void readTagPayload(DataInput in) throws IOException {
  268. int size = in.readInt();
  269. this.size = size;
  270. this.payload = new byte[size];
  271.  
  272. in.readFully(this.payload);
  273. }
  274.  
  275. public void writeTag(DataOutput out) throws IOException {
  276. out.write(this.id);
  277. out.writeUTF(this.name);
  278. out.writeInt(this.size);
  279. this.writePayload(out);
  280. }
  281.  
  282. public void writePayload(DataOutput out) throws IOException {
  283. for (byte i: this.payload) {
  284. out.writeByte(i);
  285. }
  286. }
  287. }
  288.  
  289. class NBT_Tag_String extends NBT_Tag{
  290.  
  291. public String payload;
  292.  
  293. public NBT_Tag_String(String name){
  294. super(8, name);
  295. }
  296. public NBT_Tag_String(String name, String payload){
  297. super(8, name);
  298. this.payload = payload;
  299. }
  300.  
  301. @Override
  302. public void readTagPayload(DataInput in) throws IOException {
  303. this.payload = in.readUTF();
  304. }
  305.  
  306. public void writeTag(DataOutput out) throws IOException {
  307. out.write(this.id);
  308. out.writeUTF(this.name);
  309. this.writePayload(out);
  310. }
  311.  
  312. public void writePayload(DataOutput out) throws IOException {
  313. out.writeUTF(this.payload);
  314. }
  315. public String getPayload() {
  316. return payload;
  317. }
  318. }
  319.  
  320. class NBT_Tag_Short extends NBT_Tag{
  321.  
  322. public short payload;
  323.  
  324. public NBT_Tag_Short(String name){
  325. super(2, name);
  326. }
  327. public NBT_Tag_Short(String name, short payload){
  328. super(8, name);
  329. this.payload = payload;
  330. }
  331.  
  332. @Override
  333. public void readTagPayload(DataInput in) throws IOException {
  334. this.payload = in.readShort();
  335. }
  336.  
  337. public void writeTag(DataOutput out) throws IOException {
  338. out.write(this.id);
  339. out.writeUTF(this.name);
  340. this.writePayload(out);
  341. }
  342.  
  343. public void writePayload(DataOutput out) throws IOException {
  344. out.writeShort(this.payload);
  345. }
  346.  
  347. }
  348.  
  349. class NBT_Tag_Long extends NBT_Tag{
  350.  
  351. public long payload;
  352.  
  353. public NBT_Tag_Long(String name){
  354. super(4, name);
  355. }
  356.  
  357. public NBT_Tag_Long(String name, Long payload){
  358. super(8, name);
  359. this.payload = payload;
  360. }
  361.  
  362. @Override
  363. public void readTagPayload(DataInput in) throws IOException {
  364. this.payload = in.readLong();
  365. }
  366.  
  367. public void writeTag(DataOutput out) throws IOException {
  368. out.write(this.id);
  369. out.writeUTF(this.name);
  370. this.writePayload(out);
  371. }
  372.  
  373. public void writePayload(DataOutput out) throws IOException {
  374. out.writeLong(this.payload);
  375. }
  376.  
  377. }
  378.  
  379. class NBT_Tag_List extends NBT_Tag{
  380.  
  381. public byte tag_type;
  382. public int size;
  383. public List<NBT_Tag> payload;
  384.  
  385. public NBT_Tag_List(String name){
  386. super(9, name);
  387. }
  388.  
  389. public NBT_Tag_List(String name, List<NBT_Tag> payload){
  390. super(8, name);
  391. this.payload = payload;
  392. }
  393.  
  394. @Override
  395. public void readTagPayload(DataInput in) throws IOException {
  396. this.tag_type = in.readByte();
  397. int size = in.readInt();
  398. this.size = size;
  399. this.payload = new ArrayList<NBT_Tag>();
  400.  
  401. for (int i = 0; i < size; i++) {
  402. NBT_Tag tag = NBT_Tag.getNewTag(this.tag_type, "");
  403. tag.readTagPayload(in);
  404. this.payload.add(tag);
  405. }
  406. }
  407.  
  408. public void writeTag(DataOutput out) throws IOException {
  409. out.write(this.id);
  410. out.writeUTF(this.name);
  411. out.writeInt(this.size);
  412.  
  413. this.writePayload(out);
  414. }
  415.  
  416. public void writePayload(DataOutput out) throws IOException {
  417. for (NBT_Tag tag : this.payload) {
  418. tag.writePayload(out);
  419. }
  420. }
  421. }
  422.  
  423. class NBT_Tag_Int extends NBT_Tag{
  424.  
  425. public int payload;
  426.  
  427. public NBT_Tag_Int(String name){
  428. super(3, name);
  429. }
  430.  
  431. public int getPayload() {
  432. return payload;
  433. }
  434.  
  435. @Override
  436. public void readTagPayload(DataInput in) throws IOException {
  437. this.payload = in.readInt();
  438. }
  439.  
  440. public NBT_Tag_Int(String name, int payload){
  441. super(8, name);
  442. this.payload = payload;
  443. }
  444.  
  445. public void writeTag(DataOutput out) throws IOException {
  446. out.write(this.id);
  447. out.writeUTF(this.name);
  448. this.writePayload(out);
  449. }
  450.  
  451. public void writePayload(DataOutput out) throws IOException {
  452. out.writeInt(this.payload);
  453. }
  454.  
  455. }
  456.  
  457. class NBT_Tag_Int_Array extends NBT_Tag{
  458.  
  459. public int size;
  460. public int[] payload;
  461.  
  462. public NBT_Tag_Int_Array(String name){
  463. super(11, name);
  464. }
  465.  
  466. public NBT_Tag_Int_Array(String name, int[] payload){
  467. super(8, name);
  468. this.payload = payload;
  469. }
  470.  
  471. @Override
  472. public void readTagPayload(DataInput in) throws IOException {
  473. int size = in.readInt();
  474. this.size = size;
  475. this.payload = new int[size];
  476.  
  477. for (int i = 0; i < size; i++) {
  478. this.payload[i] = in.readInt();
  479. }
  480. }
  481.  
  482. public void writeTag(DataOutput out) throws IOException {
  483. out.write(this.id);
  484. out.writeUTF(this.name);
  485. out.writeInt(this.size);
  486. this.writePayload(out);
  487. }
  488.  
  489. public void writePayload(DataOutput out) throws IOException {
  490. for (int i: this.payload) {
  491. out.writeInt(i);
  492. }
  493. }
  494. }
  495.  
  496. class NBT_Tag_Float extends NBT_Tag{
  497.  
  498. public float payload;
  499.  
  500. public NBT_Tag_Float(String name){
  501. super(5, name);
  502. }
  503. public NBT_Tag_Float(String name, float payload){
  504. super(8, name);
  505. this.payload = payload;
  506. }
  507.  
  508. @Override
  509. public void readTagPayload(DataInput in) throws IOException {
  510. this.payload = in.readFloat();
  511. }
  512.  
  513. public void writeTag(DataOutput out) throws IOException {
  514. out.write(this.id);
  515. out.writeUTF(this.name);
  516. this.writePayload(out);
  517. }
  518.  
  519. public void writePayload(DataOutput out) throws IOException {
  520. out.writeFloat(this.payload);
  521. }
  522.  
  523. }
  524.  
  525. abstract class NBT_Tag {
  526.  
  527. public static NBT_Tag getNewTag(int id,String name) {
  528. switch (id) {
  529. case 0 : return new NBT_Tag_End("");
  530. case 1 : return new NBT_Tag_Byte(name);
  531. case 2 : return new NBT_Tag_Short(name);
  532. case 3 : return new NBT_Tag_Int(name);
  533. case 4 : return new NBT_Tag_Long(name);
  534. case 5 : return new NBT_Tag_Float(name);
  535. case 6 : return new NBT_Tag_Double(name);
  536. case 7 : return new NBT_Tag_Byte_Array(name);
  537. case 8 : return new NBT_Tag_String(name);
  538. case 9 : return new NBT_Tag_List(name);
  539. case 10 : return new NBT_Tag_Compound(name);
  540. case 11 : return new NBT_Tag_Int_Array(name);
  541. default : return null;
  542. }
  543. }
  544. public static NBT_Tag readTag(DataInput in) throws IOException {
  545. NBT_Tag tag;
  546. byte tag_id = in.readByte();
  547. if (tag_id == 0) return new NBT_Tag_End("");
  548. String tag_name = in.readUTF();
  549. tag = getNewTag(tag_id,tag_name);
  550. tag.readTagPayload(in);
  551. return tag;
  552. }
  553.  
  554. byte id;
  555.  
  556. String name;
  557.  
  558. public NBT_Tag(String name) {
  559. this.id = 0;
  560. this.name = name;
  561. }
  562. protected NBT_Tag(int id, String name) {
  563. this.id = (byte) id;
  564. this.name = name;
  565. }
  566.  
  567. public abstract void readTagPayload(DataInput in) throws IOException;
  568.  
  569. public abstract void writeTag(DataOutput out) throws IOException;
  570.  
  571. public abstract void writePayload(DataOutput out) throws IOException;
  572. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement