summaryrefslogtreecommitdiff
path: root/src/main/java/com/encrox/zombie/Schematic.java
blob: 16d4f5bc9c6e8e5bc289825c7c4e11ac570dd8aa (plain)
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
/*
 * Dynamic Schematic object
 * 
 * Currently not supporting entities.
 */

package com.encrox.zombie;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.bukkit.Material;
import org.jnbt.ByteArrayTag;
import org.jnbt.CompoundTag;
import org.jnbt.ListTag;
import org.jnbt.NBTInputStream;
import org.jnbt.ShortTag;
import org.jnbt.StringTag;
import org.jnbt.Tag;

public class Schematic {
	
	public int width, height, length, dataReadIndex, dataWriteIndex, blockReadIndex, blockWriteIndex;
	public String materials;
	public byte[] blocks, data;
	public List<Tag> entities, tileEntities;
	public File file;
	
	public Schematic(int width, int height, int length, String materials, byte[] blocks, byte[] data, List<Tag> entities, List<Tag> tileEntities) {
		this.width = width;
		this.height = height;
		this.length = length;
		this.materials = materials;
		this.blocks = blocks;
		this.data = data;
		this.entities = entities;
		this.tileEntities = tileEntities;
		blockReadIndex = 0;
		blockWriteIndex = 0;
		dataReadIndex = 0;
		dataWriteIndex = 0;
	}
	
	public Schematic(File file) {
		this.file = file;
		CompoundTag schematicTag = null;
		try {
			NBTInputStream in = new NBTInputStream(new FileInputStream(file));
			schematicTag = (CompoundTag)in.readTag();
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String,Tag> schematic = schematicTag.getValue();
		width = ((ShortTag)schematic.get("Width")).getValue();
		height = ((ShortTag)schematic.get("Height")).getValue();
		length = ((ShortTag)schematic.get("Length")).getValue();
		materials = ((StringTag)schematic.get("Materials")).getValue();
		blocks = ((ByteArrayTag)schematic.get("Blocks")).getValue();
		data = ((ByteArrayTag)schematic.get("Data")).getValue();
		entities = ((ListTag)schematic.get("Entities")).getValue();
		tileEntities = ((ListTag)schematic.get("TileEntities")).getValue();
		blockReadIndex = 0;
		blockWriteIndex = 0;
		dataReadIndex = 0;
		dataWriteIndex = 0;
	}
	
	public byte getBlockIdAt(int x, int y, int z) {
		return blocks[(y*length + z)*width + x];
	}
	
	public byte getNextBlock() {
		return blocks[blockReadIndex++];
	}
	
	public byte getDataAt(int x, int y, int z) {
		return data[(y*length + z)*width + x];
	}
	
	public byte getNextData() {
		return data[dataReadIndex++];
	}
	
	public void setBlockIdAt(int x, int y, int z, byte blockId) {
		blocks[(y*length + z)*width + x] = blockId;
	}
	
	public void setNextBlock(byte blockId) {
		blocks[blockWriteIndex++] = blockId;
	}
	
	public void setDataAt(int x, int y, int z, byte newData) {
		data[(y*length + z)*width + x] = newData;
	}
	
	public void setNextData(byte newData) {
		data[dataWriteIndex++] = newData;
	}
	
	public void write(File file) {
		
	}
	
	public void reset() {
		blockReadIndex = 0;
		blockWriteIndex = 0;
		dataReadIndex = 0;
		dataWriteIndex = 0;
	}

}