-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
WorldEditSessionImpl.java
258 lines (209 loc) · 11.5 KB
/
WorldEditSessionImpl.java
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.bgsoftware.superiorskyblock.nms.v1181.world;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.SequentialListBuilder;
import com.bgsoftware.superiorskyblock.core.debug.PluginDebugger;
import com.bgsoftware.superiorskyblock.island.IslandUtils;
import com.bgsoftware.superiorskyblock.nms.world.WorldEditSession;
import com.bgsoftware.superiorskyblock.tag.ByteTag;
import com.bgsoftware.superiorskyblock.tag.CompoundTag;
import com.bgsoftware.superiorskyblock.tag.IntArrayTag;
import com.bgsoftware.superiorskyblock.tag.StringTag;
import com.bgsoftware.superiorskyblock.tag.Tag;
import com.bgsoftware.superiorskyblock.world.generator.IslandsGenerator;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.WorldGenRegion;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.PalettedContainer;
import net.minecraft.world.level.chunk.ProtoChunk;
import net.minecraft.world.level.chunk.UpgradeData;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.lighting.LevelLightEngine;
import net.minecraft.world.ticks.ProtoChunkTicks;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_18_R1.CraftChunk;
import org.bukkit.craftbukkit.v1_18_R1.block.CraftBlock;
import org.bukkit.craftbukkit.v1_18_R1.generator.CustomChunkGenerator;
import org.bukkit.generator.ChunkGenerator;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class WorldEditSessionImpl implements WorldEditSession {
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
private final Map<Long, ChunkData> chunks = new HashMap<>();
private final List<Pair<BlockPos, BlockState>> blocksToUpdate = new LinkedList<>();
private final List<BlockPos> lights = new LinkedList<>();
private final List<Pair<BlockPos, CompoundTag>> blockEntities = new LinkedList<>();
private final ServerLevel serverLevel;
public WorldEditSessionImpl(ServerLevel serverLevel) {
this.serverLevel = serverLevel;
}
@Override
public void setBlock(Location location, int combinedId, @Nullable CompoundTag statesTag,
@Nullable CompoundTag blockEntityData) {
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
if (!isValidPosition(blockPos))
return;
BlockState blockState = Block.stateById(combinedId);
if (statesTag != null) {
for (Map.Entry<String, Tag<?>> entry : statesTag.entrySet()) {
try {
// noinspection rawtypes
Property property = PropertiesMapper.getProperty(entry.getKey());
if (property != null) {
if (entry.getValue() instanceof ByteTag) {
// noinspection unchecked
blockState = blockState.setValue(property, ((ByteTag) entry.getValue()).getValue() == 1);
} else if (entry.getValue() instanceof IntArrayTag) {
int[] data = ((IntArrayTag) entry.getValue()).getValue();
// noinspection unchecked
blockState = blockState.setValue(property, data[0]);
} else if (entry.getValue() instanceof StringTag) {
String data = ((StringTag) entry.getValue()).getValue();
// noinspection unchecked
blockState = blockState.setValue(property, Enum.valueOf(property.getValueClass(), data));
}
}
} catch (Exception error) {
PluginDebugger.debug(error);
}
}
}
if ((blockState.getMaterial().isLiquid() && plugin.getSettings().isLiquidUpdate()) ||
blockState.getBlock() instanceof BedBlock) {
blocksToUpdate.add(new Pair<>(blockPos, blockState));
return;
}
ChunkPos chunkPos = new ChunkPos(blockPos);
if (blockState.getLightEmission() > 0)
lights.add(blockPos);
if (blockEntityData != null)
blockEntities.add(new Pair<>(blockPos, blockEntityData));
ChunkData chunkData = this.chunks.computeIfAbsent(chunkPos.toLong(), ChunkData::new);
LevelChunkSection levelChunkSection = chunkData.chunkSections[serverLevel.getSectionIndex(blockPos.getY())];
int blockX = blockPos.getX() & 15;
int blockY = blockPos.getY();
int blockZ = blockPos.getZ() & 15;
levelChunkSection.setBlockState(blockX, blockY & 15, blockZ, blockState, false);
chunkData.heightmaps.get(Heightmap.Types.MOTION_BLOCKING).update(blockX, blockY, blockZ, blockState);
chunkData.heightmaps.get(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES).update(blockX, blockY, blockZ, blockState);
chunkData.heightmaps.get(Heightmap.Types.OCEAN_FLOOR).update(blockX, blockY, blockZ, blockState);
chunkData.heightmaps.get(Heightmap.Types.WORLD_SURFACE).update(blockX, blockY, blockZ, blockState);
}
@Override
public List<ChunkPosition> getAffectedChunks() {
World bukkitWorld = serverLevel.getWorld();
return new SequentialListBuilder<Long>().map(chunks.keySet(), chunkKey -> {
ChunkPos chunkPos = new ChunkPos(chunkKey);
return ChunkPosition.of(bukkitWorld, chunkPos.x, chunkPos.z);
});
}
@Override
public void applyBlocks(Chunk bukkitChunk) {
LevelChunk levelChunk = ((CraftChunk) bukkitChunk).getHandle();
ChunkData chunkData = this.chunks.remove(levelChunk.getPos().toLong());
if (chunkData == null)
return;
int chunkSectionsCount = Math.min(chunkData.chunkSections.length, levelChunk.getSections().length);
for (int i = 0; i < chunkSectionsCount; ++i) {
levelChunk.getSections()[i] = chunkData.chunkSections[i];
}
chunkData.heightmaps.forEach(((type, heightmap) -> {
levelChunk.setHeightmap(type, heightmap.getRawData());
}));
}
@Override
public void finish(Island island) {
// Update blocks
blocksToUpdate.forEach(data -> serverLevel.setBlock(data.getKey(), data.getValue(), 3));
// Update lights
LevelLightEngine levelLightEngine = serverLevel.getLightEngine();
lights.forEach(levelLightEngine::checkBlock);
// Update block entities
blockEntities.forEach(data -> {
net.minecraft.nbt.CompoundTag blockEntityCompound = (net.minecraft.nbt.CompoundTag) data.getValue().toNBT();
if (blockEntityCompound != null) {
BlockPos blockPos = data.getKey();
blockEntityCompound.putInt("x", blockPos.getX());
blockEntityCompound.putInt("y", blockPos.getY());
blockEntityCompound.putInt("z", blockPos.getZ());
BlockEntity worldBlockEntity = serverLevel.getBlockEntity(blockPos);
if (worldBlockEntity != null)
worldBlockEntity.load(blockEntityCompound);
}
});
}
private boolean isValidPosition(BlockPos blockPos) {
return blockPos.getX() >= -30000000 && blockPos.getZ() >= -30000000 &&
blockPos.getX() < 30000000 && blockPos.getZ() < 30000000 &&
blockPos.getY() >= serverLevel.getMinBuildHeight() && blockPos.getY() < serverLevel.getMaxBuildHeight();
}
private class ChunkData {
private final LevelChunkSection[] chunkSections = new LevelChunkSection[serverLevel.getSectionsCount()];
private final Map<Heightmap.Types, Heightmap> heightmaps = new EnumMap<>(Heightmap.Types.class);
public ChunkData(long chunkKey) {
ChunkPos chunkPos = new ChunkPos(chunkKey);
Registry<Biome> biomesRegistry = MinecraftServer.getServer().registryAccess().registryOrThrow(Registry.BIOME_REGISTRY);
createChunkSections(biomesRegistry);
ProtoChunk tempChunk = new ProtoChunk(chunkPos, UpgradeData.EMPTY, this.chunkSections,
new ProtoChunkTicks<>(), new ProtoChunkTicks<>(), serverLevel, biomesRegistry, null);
createHeightmaps(tempChunk);
runCustomWorldGenerator(tempChunk);
}
private void createChunkSections(Registry<Biome> biomesRegistry) {
Biome biome = CraftBlock.biomeToBiomeBase(biomesRegistry,
IslandUtils.getDefaultWorldBiome(serverLevel.getWorld().getEnvironment()));
for (int i = 0; i < this.chunkSections.length; ++i) {
int chunkSectionPos = serverLevel.getSectionYFromSectionIndex(i);
PalettedContainer<Biome> biomesContainer = new PalettedContainer<>(biomesRegistry,
biome, PalettedContainer.Strategy.SECTION_BIOMES);
PalettedContainer<BlockState> statesContainer = new PalettedContainer<>(Block.BLOCK_STATE_REGISTRY,
Blocks.AIR.defaultBlockState(), PalettedContainer.Strategy.SECTION_STATES);
this.chunkSections[i] = new LevelChunkSection(chunkSectionPos, statesContainer, biomesContainer);
}
}
private void runCustomWorldGenerator(ProtoChunk tempChunk) {
ChunkGenerator bukkitGenerator = serverLevel.getWorld().getGenerator();
if (bukkitGenerator == null || bukkitGenerator instanceof IslandsGenerator)
return;
CustomChunkGenerator chunkGenerator = new CustomChunkGenerator(serverLevel,
serverLevel.getChunkSource().getGenerator(),
bukkitGenerator);
WorldGenRegion region = new WorldGenRegion(serverLevel, Collections.singletonList(tempChunk),
ChunkStatus.SURFACE, 0);
chunkGenerator.buildSurface(region,
serverLevel.structureFeatureManager().forWorldGenRegion(region),
tempChunk);
// We want to copy the level chunk sections back
System.arraycopy(tempChunk.getSections(), 0, this.chunkSections, 0, this.chunkSections.length);
}
private void createHeightmaps(ProtoChunk tempChunk) {
for (Heightmap.Types heightmapType : Heightmap.Types.values()) {
if (ChunkStatus.FULL.heightmapsAfter().contains(heightmapType)) {
this.heightmaps.put(heightmapType, new Heightmap(tempChunk, heightmapType));
}
}
}
}
}