-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMapChunk.java
More file actions
179 lines (146 loc) · 5.57 KB
/
Copy pathMapChunk.java
File metadata and controls
179 lines (146 loc) · 5.57 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
package xyz.nucleoid.map_templates;
import com.mojang.serialization.Codec;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.*;
import net.minecraft.util.math.ChunkSectionPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.chunk.PalettedContainer;
import org.apache.commons.codec.digest.Blake3;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class MapChunk {
private static final Codec<PalettedContainer<BlockState>> BLOCK_CODEC = PalettedContainer.createPalettedContainerCodec(Block.STATE_IDS, BlockState.CODEC, PalettedContainer.PaletteProvider.BLOCK_STATE, Blocks.AIR.getDefaultState());
private final ChunkSectionPos pos;
/**
* BLAKE3 hash of the block states in this chunk section
*/
private final byte[] blockStatesHash = new byte[32];
/**
* Whether the above hash represents currently-stored block data.
*/
private boolean blockStatesHashDirty = true;
private PalettedContainer<BlockState> container = new PalettedContainer<>(Block.STATE_IDS, Blocks.AIR.getDefaultState(), PalettedContainer.PaletteProvider.BLOCK_STATE);
private final List<MapEntity> entities = new ArrayList<>();
MapChunk(ChunkSectionPos pos) {
this.pos = pos;
}
public void set(int x, int y, int z, BlockState state) {
this.blockStatesHashDirty = true;
this.container.set(x, y, z, state);
}
public BlockState get(int x, int y, int z) {
return this.container.get(x, y, z);
}
/**
* Adds an entity to this chunk.
* <p>
* The position of the entity must be relative to the map template.
*
* @param entity The entity to add.
* @param position The entity position relative to the map.
*/
public void addEntity(Entity entity, Vec3d position) {
var mapEntity = MapEntity.fromEntity(entity, position);
if (mapEntity != null) {
this.entities.add(mapEntity);
}
}
public void addEntity(MapEntity entity) {
this.entities.add(entity);
}
public ChunkSectionPos getPos() {
return this.pos;
}
/**
* Returns the entities in this chunk.
*
* @return The entities in this chunk.
*/
public List<MapEntity> getEntities() {
return this.entities;
}
public void serialize(NbtCompound nbt) {
var blockStatesNbt = BLOCK_CODEC.encodeStart(NbtOps.INSTANCE, this.container).getOrThrow(false, (e) -> {});
nbt.put("block_states", blockStatesNbt);
if (!this.entities.isEmpty()) {
var entitiesNbt = new NbtList();
for (var entity : this.entities) {
entitiesNbt.add(entity.nbt());
}
nbt.put("entities", entitiesNbt);
}
}
public static MapChunk deserialize(ChunkSectionPos pos, NbtCompound nbt) {
var chunk = new MapChunk(pos);
var blockStatesNbt = nbt.getCompound("block_states");
var container = BLOCK_CODEC.parse(NbtOps.INSTANCE, nbt.getCompound("block_states"))
.promotePartial(errorMessage -> {}).get().left();
container.ifPresent(blockStatePalettedContainer -> chunk.container = blockStatePalettedContainer);
if (nbt.contains("entities", NbtElement.LIST_TYPE)) {
var entitiesNbt = nbt.getList("entities", NbtElement.COMPOUND_TYPE);
for (var entityNbt : entitiesNbt) {
chunk.entities.add(MapEntity.fromNbt(pos, (NbtCompound) entityNbt));
}
}
return chunk;
}
private NbtElement serializeBlockStates() {
return BLOCK_CODEC.encodeStart(NbtOps.INSTANCE, this.container).getOrThrow(false, (e) -> {});
}
/**
* Recompute the hash if it is dirty.
*/
private void recomputeHash() {
if (this.blockStatesHashDirty) {
try {
var md = Blake3.initHash();
this.serializeBlockStates().write(new DataOutputStream(new OutputStream() {
private ByteBuffer tmpBuffer;
@Override
public void write(int i) {
if (tmpBuffer == null) {
tmpBuffer = ByteBuffer.allocate(4);
} else {
tmpBuffer.clear();
}
tmpBuffer.putInt(i);
md.update(tmpBuffer.array());
}
@Override
public void write(byte @NotNull [] bytes, int off, int len) {
md.update(bytes, off, len);
}
}));
md.doFinalize(this.blockStatesHash, 0, 32);
} catch (IOException e) {
// Unreachable
throw new RuntimeException(e);
}
this.blockStatesHashDirty = false;
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (!(obj instanceof MapChunk)) {
return false;
}
MapChunk other = (MapChunk) obj;
if (!this.pos.equals(other.pos)) {
return false;
}
this.recomputeHash();
other.recomputeHash();
return Arrays.equals(this.blockStatesHash, other.blockStatesHash);
}
}