-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathBlockParser118.java
More file actions
189 lines (163 loc) · 4.71 KB
/
BlockParser118.java
File metadata and controls
189 lines (163 loc) · 4.71 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package net.querz.mca.parsers.impl.anvil118;
import net.querz.mca.parsers.BlockParser;
import net.querz.nbt.CompoundTag;
import net.querz.nbt.ListTag;
import net.querz.nbt.Tag;
import java.util.*;
public class BlockParser118 implements BlockParser<CompoundTag> {
protected final CompoundTag section;
private ListTag palette;
private long[] data;
private short[] parsedIndexes;
private Map<CompoundTag, Short> paletteSet;
public BlockParser118(CompoundTag section) {
this.section = section;
if (section.contains("block_states", Tag.Type.COMPOUND)) {
CompoundTag blockStates = section.getCompound("block_states");
if (blockStates.contains("palette", Tag.Type.LIST)) {
palette = blockStates.getList("palette").copy();
}
if (blockStates.contains("data", Tag.Type.LONG_ARRAY)) {
data = blockStates.getLongArray("data");
}
}
}
@Override
public CompoundTag getBlockAt(int blockX, int blockY, int blockZ) {
return getDataAt((blockY & 0xF) * 256 + (blockZ & 0xF) * 16 + (blockX & 0xF));
}
@Override
public CompoundTag getDataAt(int index) {
if (parsedIndexes == null) {
parseIndexes();
}
return palette.getCompound(parsedIndexes[index]);
}
private void parseIndexes() {
parsedIndexes = new short[4096];
if (data == null) {
return;
}
int numberOfBits = data.length * 64 / 4096;
int shortsPerLong = Math.floorDiv(64, numberOfBits);
int mask = (1 << numberOfBits) - 1;
int i = 0;
for (long l : data) {
for (int s = 0; s < shortsPerLong && i < 4096; s++, i++) {
parsedIndexes[i] = (short) (l & mask);
l = l >> numberOfBits;
}
}
}
private void parsePalette() {
paletteSet = new HashMap<>((int) Math.ceil(palette.size() * 1.25f));
for (short i = 0; i < palette.size(); i++) {
paletteSet.put(palette.getCompound(i), i);
}
}
@Override
public void setBlockAt(int blockX, int blockY, int blockZ, CompoundTag data) {
setDataAt((blockY & 0xF) * 256 + (blockZ & 0xF) * 16 + (blockX & 0xF), data);
}
@Override
public void setDataAt(int index, CompoundTag data) {
if (parsedIndexes == null) {
parseIndexes();
}
if (paletteSet == null) {
parsePalette();
}
short i;
if (paletteSet.containsKey(data)) {
i = paletteSet.get(data);
} else {
i = (short) palette.size();
palette.add(data);
paletteSet.put(data, i);
}
parsedIndexes[index] = i;
}
@Override
public int getSize() {
return 4096;
}
@Override
public void apply() {
// collect all used block states
Short[] usedBlockStates = new Short[palette.size()];
if (parsedIndexes == null) {
parseIndexes();
}
for (short b : parsedIndexes) {
usedBlockStates[b] = b;
}
// count number of used block states
int numberOfUsedBlockStates = 0;
for (Short usedBlockState : usedBlockStates) {
if (usedBlockState != null) {
numberOfUsedBlockStates++;
}
}
// cleanup palette if we have unused block states
if (numberOfUsedBlockStates < palette.size()) {
ListTag newPalette = new ListTag();
short[] oldToNewMapping = new short[palette.size()];
for (short i = 0, j = 0; i < palette.size(); i++) {
if (usedBlockStates[i] != null) {
newPalette.add(palette.get(i));
oldToNewMapping[i] = j;
j++;
}
}
for (int i = 0; i < parsedIndexes.length; i++) {
parsedIndexes[i] = oldToNewMapping[parsedIndexes[i]];
}
palette = newPalette;
}
if (numberOfUsedBlockStates == 1) {
data = null;
applyToHandle(palette, null);
return;
}
int numberOfBits = Math.max(32 - Integer.numberOfLeadingZeros(palette.size() - 1), 4);
long[] newBlockStates;
int shortsPerLong = Math.floorDiv(64, numberOfBits);
int freePerLong = Math.floorMod(64, numberOfBits);
int newLength = (int) Math.ceil(4096D / shortsPerLong);
newBlockStates = data == null || newLength != data.length ? new long[newLength] : data;
for (int i = 0; i < newLength - 1; i++) {
long l = 0;
for (int j = 0; j < shortsPerLong - 1; j++) {
l += parsedIndexes[(i + 1) * shortsPerLong - j - 1];
l <<= numberOfBits;
}
l += parsedIndexes[i * shortsPerLong];
if(newBlockStates[i] != l){
int g =4;
}
newBlockStates[i] = l;
}
{
int i = newLength - 1;
long l = 0L;
int jm = 4096 - (newLength - 1) * shortsPerLong;
for(int j = 0; j < jm - 1; j++){
l += parsedIndexes[i * shortsPerLong + jm - j - 1];
l <<= numberOfBits;
}
l += parsedIndexes[i * shortsPerLong];
newBlockStates[newLength - 1] = l;
}
data = newBlockStates;
applyToHandle(palette, data);
}
private void applyToHandle(ListTag palette, long[] data) {
CompoundTag blockStates = section.getCompound("block_states");
blockStates.put("palette", palette);
if (data == null) {
blockStates.remove("data");
} else {
blockStates.putLongArray("data", data);
}
}
}