-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCachedBlocks.java
More file actions
89 lines (75 loc) · 2.67 KB
/
CachedBlocks.java
File metadata and controls
89 lines (75 loc) · 2.67 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
package fr.hugman.build_rush.misc;
import fr.hugman.build_rush.BuildRush;
import net.minecraft.block.BlockState;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.storage.NbtReadView;
import net.minecraft.storage.NbtWriteView;
import net.minecraft.util.ErrorReporter;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;
import xyz.nucleoid.map_templates.BlockBounds;
import java.util.HashMap;
public class CachedBlocks {
private final HashMap<Vec3i, BlockState> states;
private final HashMap<Vec3i, NbtCompound> nbtCompounds;
public CachedBlocks(HashMap<Vec3i, BlockState> states, HashMap<Vec3i, NbtCompound> nbtCompounds) {
this.states = states;
this.nbtCompounds = nbtCompounds;
}
public static CachedBlocks from(ServerWorld world, BlockBounds bounds) {
var size = bounds.size();
HashMap<Vec3i, BlockState> states = new HashMap<>();
HashMap<Vec3i, NbtCompound> nbt = new HashMap<>();
for (int x = 0; x <= size.getX(); x++) {
for (int y = 0; y <= size.getY(); y++) {
for (int z = 0; z <= size.getZ(); z++) {
var targetPos = new BlockPos(x, y, z);
var sourcePos = bounds.min().add(x, y, z);
// state
states.put(targetPos, world.getBlockState(sourcePos));
// nbt
var sourceEntity = world.getBlockEntity(sourcePos);
if (sourceEntity != null) {
var sourceNbt = sourceEntity.createNbt(world.getRegistryManager());
nbt.put(targetPos, sourceNbt);
}
}
}
}
return new CachedBlocks(states, nbt);
}
public void place(ServerWorld world, BlockPos origin) {
for (var entry : this.states.entrySet()) {
var targetPos = origin.add(entry.getKey());
var state = entry.getValue();
world.setBlockState(targetPos, state);
}
for (var entry : this.nbtCompounds.entrySet()) {
var targetPos = origin.add(entry.getKey());
var nbt = entry.getValue();
var entity = world.getBlockEntity(targetPos);
if (entity != null) {
try (ErrorReporter.Logging errorReporter = new ErrorReporter.Logging(entity.getReporterContext(), BuildRush.LOGGER)) {
var view = NbtReadView.create(errorReporter, world.getRegistryManager(), nbt);
entity.read(view);
}
}
}
}
public Iterable<Vec3i> positions() {
return this.states.keySet();
}
public BlockState state(Vec3i pos) {
return this.states.get(pos);
}
public BlockState state(int x, int y, int z) {
return this.states.get(new Vec3i(x, y, z));
}
public NbtCompound nbt(Vec3i pos) {
return this.nbtCompounds.get(pos);
}
public NbtCompound nbt(int x, int y, int z) {
return this.nbtCompounds.get(new Vec3i(x, y, z));
}
}