Skip to content

Commit 6389ed8

Browse files
committed
Add for 1.21.9/10
1 parent 2b3b5b5 commit 6389ed8

File tree

4 files changed

+1119
-0
lines changed

4 files changed

+1119
-0
lines changed

worldedit-bukkit/adapters/adapter-1_21_9/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_21_9/PaperweightAdapter.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.mojang.serialization.DataResult;
3232
import com.mojang.serialization.JsonOps;
3333
import com.mojang.serialization.Lifecycle;
34+
import com.sk89q.worldedit.EditSession;
35+
import com.sk89q.worldedit.MaxChangedBlocksException;
3436
import com.sk89q.worldedit.WorldEditException;
3537
import com.sk89q.worldedit.blocks.BaseItem;
3638
import com.sk89q.worldedit.blocks.BaseItemStack;
@@ -65,13 +67,16 @@
6567
import com.sk89q.worldedit.world.block.BlockType;
6668
import com.sk89q.worldedit.world.block.BlockTypes;
6769
import com.sk89q.worldedit.world.entity.EntityTypes;
70+
import com.sk89q.worldedit.world.generation.ConfiguredFeatureType;
71+
import com.sk89q.worldedit.world.generation.StructureType;
6872
import com.sk89q.worldedit.world.item.ItemType;
6973
import net.minecraft.SharedConstants;
7074
import net.minecraft.Util;
7175
import net.minecraft.core.BlockPos;
7276
import net.minecraft.core.Holder;
7377
import net.minecraft.core.HolderSet;
7478
import net.minecraft.core.Registry;
79+
import net.minecraft.core.SectionPos;
7580
import net.minecraft.core.component.DataComponentPatch;
7681
import net.minecraft.core.registries.Registries;
7782
import net.minecraft.nbt.ByteArrayTag;
@@ -99,6 +104,7 @@
99104
import net.minecraft.server.level.ChunkResult;
100105
import net.minecraft.server.level.ServerChunkCache;
101106
import net.minecraft.server.level.ServerLevel;
107+
import net.minecraft.util.RandomSource;
102108
import net.minecraft.util.thread.BlockableEventLoop;
103109
import net.minecraft.world.Clearable;
104110
import net.minecraft.world.InteractionHand;
@@ -122,6 +128,10 @@
122128
import net.minecraft.world.level.chunk.status.ChunkStatus;
123129
import net.minecraft.world.level.dimension.LevelStem;
124130
import net.minecraft.world.level.levelgen.WorldOptions;
131+
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
132+
import net.minecraft.world.level.levelgen.structure.BoundingBox;
133+
import net.minecraft.world.level.levelgen.structure.Structure;
134+
import net.minecraft.world.level.levelgen.structure.StructureStart;
125135
import net.minecraft.world.level.storage.LevelStorageSource;
126136
import net.minecraft.world.level.storage.PrimaryLevelData;
127137
import net.minecraft.world.level.storage.TagValueOutput;
@@ -200,6 +210,8 @@ public final class PaperweightAdapter implements BukkitImplAdapter<net.minecraft
200210
private final PaperweightDataConverters dataFixer;
201211
private final Watchdog watchdog;
202212

213+
private static final RandomSource random = RandomSource.create();
214+
203215
private static final String WRONG_VERSION =
204216
"""
205217
This version of FastAsyncWorldEdit has not been tested with the current Minecraft version.
@@ -894,6 +906,20 @@ public void initializeRegistries() {
894906
}
895907
}
896908

909+
// Features
910+
for (ResourceLocation name: server.registryAccess().lookupOrThrow(Registries.CONFIGURED_FEATURE).keySet()) {
911+
if (ConfiguredFeatureType.REGISTRY.get(name.toString()) == null) {
912+
ConfiguredFeatureType.REGISTRY.register(name.toString(), new ConfiguredFeatureType(name.toString()));
913+
}
914+
}
915+
916+
// Structures
917+
for (ResourceLocation name : server.registryAccess().lookupOrThrow(Registries.STRUCTURE).keySet()) {
918+
if (StructureType.REGISTRY.get(name.toString()) == null) {
919+
StructureType.REGISTRY.register(name.toString(), new StructureType(name.toString()));
920+
}
921+
}
922+
897923
// BiomeCategories
898924
Registry<Biome> biomeRegistry = server.registryAccess().lookupOrThrow(Registries.BIOME);
899925
biomeRegistry.getTags().forEach(tag -> {
@@ -912,6 +938,60 @@ public void initializeRegistries() {
912938
});
913939
}
914940

941+
public boolean generateFeature(ConfiguredFeatureType type, World world, EditSession session, BlockVector3 pt) {
942+
ServerLevel originalWorld = ((CraftWorld) world).getHandle();
943+
ConfiguredFeature<?, ?> feature = originalWorld.registryAccess().lookupOrThrow(Registries.CONFIGURED_FEATURE).getValue(ResourceLocation.tryParse(type.id()));
944+
ServerChunkCache chunkManager = originalWorld.getChunkSource();
945+
try (PaperweightServerLevelDelegateProxy.LevelAndProxy proxyLevel =
946+
PaperweightServerLevelDelegateProxy.newInstance(session, originalWorld, this)) {
947+
return feature != null && feature.place(proxyLevel.level(), chunkManager.getGenerator(), random, new BlockPos(pt.x(), pt.y(), pt.z()));
948+
} catch (MaxChangedBlocksException e) {
949+
throw new RuntimeException(e);
950+
}
951+
}
952+
953+
public boolean generateStructure(StructureType type, World world, EditSession session, BlockVector3 pt) {
954+
ServerLevel originalWorld = ((CraftWorld) world).getHandle();
955+
Registry<Structure> structureRegistry = originalWorld.registryAccess().lookupOrThrow(Registries.STRUCTURE);
956+
Structure structure = structureRegistry.getValue(ResourceLocation.tryParse(type.id()));
957+
if (structure == null) {
958+
return false;
959+
}
960+
961+
ServerChunkCache chunkManager = originalWorld.getChunkSource();
962+
try (PaperweightServerLevelDelegateProxy.LevelAndProxy proxyLevel =
963+
PaperweightServerLevelDelegateProxy.newInstance(session, originalWorld, this)) {
964+
ChunkPos chunkPos = new ChunkPos(new BlockPos(pt.x(), pt.y(), pt.z()));
965+
StructureStart structureStart = structure.generate(
966+
structureRegistry.wrapAsHolder(structure), originalWorld.dimension(), originalWorld.registryAccess(),
967+
chunkManager.getGenerator(), chunkManager.getGenerator().getBiomeSource(), chunkManager.randomState(),
968+
originalWorld.getStructureManager(), originalWorld.getSeed(), chunkPos, 0,
969+
proxyLevel.level(), biome -> true
970+
);
971+
972+
if (!structureStart.isValid()) {
973+
return false;
974+
} else {
975+
BoundingBox boundingBox = structureStart.getBoundingBox();
976+
ChunkPos min = new ChunkPos(SectionPos.blockToSectionCoord(boundingBox.minX()), SectionPos.blockToSectionCoord(boundingBox.minZ()));
977+
ChunkPos max = new ChunkPos(SectionPos.blockToSectionCoord(boundingBox.maxX()), SectionPos.blockToSectionCoord(boundingBox.maxZ()));
978+
ChunkPos.rangeClosed(min, max).forEach((chunkPosx) ->
979+
structureStart.placeInChunk(
980+
proxyLevel.level(), originalWorld.structureManager(), chunkManager.getGenerator(),
981+
originalWorld.getRandom(),
982+
new BoundingBox(
983+
chunkPosx.getMinBlockX(), originalWorld.getMinY(), chunkPosx.getMinBlockZ(),
984+
chunkPosx.getMaxBlockX(), originalWorld.getMaxY(), chunkPosx.getMaxBlockZ()
985+
), chunkPosx
986+
)
987+
);
988+
return true;
989+
}
990+
} catch (MaxChangedBlocksException e) {
991+
throw new RuntimeException(e);
992+
}
993+
}
994+
915995
@Override
916996
public void sendBiomeUpdates(World world, Iterable<BlockVector2> chunks) {
917997
ServerLevel originalWorld = ((CraftWorld) world).getHandle();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_21_9;
2+
3+
import com.fastasyncworldedit.core.util.MathMan;
4+
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.core.Direction;
7+
import net.minecraft.core.Holder;
8+
import net.minecraft.core.SectionPos;
9+
import net.minecraft.server.MinecraftServer;
10+
import net.minecraft.server.level.ServerLevel;
11+
import net.minecraft.util.RandomSource;
12+
import net.minecraft.world.DifficultyInstance;
13+
import net.minecraft.world.entity.Entity;
14+
import net.minecraft.world.flag.FeatureFlagSet;
15+
import net.minecraft.world.level.biome.Biome;
16+
import net.minecraft.world.level.biome.BiomeManager;
17+
import net.minecraft.world.level.block.state.BlockState;
18+
import net.minecraft.world.level.border.WorldBorder;
19+
import net.minecraft.world.level.chunk.ChunkAccess;
20+
import net.minecraft.world.level.chunk.ChunkSource;
21+
import net.minecraft.world.level.chunk.status.ChunkStatus;
22+
import net.minecraft.world.level.lighting.LevelLightEngine;
23+
import net.minecraft.world.level.material.FluidState;
24+
import org.bukkit.craftbukkit.util.BlockStateListPopulator;
25+
26+
import javax.annotation.Nonnull;
27+
import javax.annotation.Nullable;
28+
29+
public class FaweBlockStateListPopulator extends BlockStateListPopulator {
30+
31+
private final Long2ObjectOpenHashMap<PaperweightChunkAccessProxy> chunkProxies = new Long2ObjectOpenHashMap<>();
32+
private final ServerLevel world;
33+
34+
public FaweBlockStateListPopulator(ServerLevel world) {
35+
super(world);
36+
this.world = world;
37+
}
38+
39+
@Override
40+
public long getSeed() {
41+
return world.getSeed();
42+
}
43+
44+
@Override
45+
@Nonnull
46+
public ServerLevel getLevel() {
47+
return world.getLevel();
48+
}
49+
50+
@Override
51+
@Nonnull
52+
public DifficultyInstance getCurrentDifficultyAt(final BlockPos pos) {
53+
return world.getCurrentDifficultyAt(pos);
54+
}
55+
56+
@Override
57+
public MinecraftServer getServer() {
58+
return world.getServer();
59+
}
60+
61+
@Override
62+
@Nonnull
63+
public ChunkSource getChunkSource() {
64+
return world.getChunkSource();
65+
}
66+
67+
@Override
68+
@Nonnull
69+
public RandomSource getRandom() {
70+
return world.getRandom();
71+
}
72+
73+
@Override
74+
public ChunkAccess getChunk(final int chunkX, final int chunkZ, final ChunkStatus leastStatus, final boolean create) {
75+
ChunkAccess worldChunk = world.getChunk(chunkX, chunkZ, leastStatus, create);
76+
PaperweightChunkAccessProxy proxy = chunkProxies.compute(
77+
MathMan.pairInt(chunkX, chunkZ),
78+
(k, v) -> v == null ? PaperweightChunkAccessProxy.getInstance() : v
79+
);
80+
proxy.parent = worldChunk;
81+
return proxy;
82+
}
83+
84+
@Override
85+
@Nonnull
86+
public BiomeManager getBiomeManager() {
87+
return world.getBiomeManager();
88+
}
89+
90+
@Override
91+
@Nonnull
92+
public Holder<Biome> getUncachedNoiseBiome(final int biomeX, final int biomeY, final int biomeZ) {
93+
return world.getUncachedNoiseBiome(biomeX, biomeY, biomeZ);
94+
}
95+
96+
@Override
97+
public int getSeaLevel() {
98+
return world.getSeaLevel();
99+
}
100+
101+
@Override
102+
public @Nonnull ChunkAccess getChunk(final @Nonnull BlockPos pos) {
103+
ChunkAccess worldChunk = world.getChunk(pos);
104+
PaperweightChunkAccessProxy proxy = chunkProxies.compute(
105+
MathMan.pairInt(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ())),
106+
(k, v) -> v == null ? PaperweightChunkAccessProxy.getInstance() : v
107+
);
108+
proxy.parent = worldChunk;
109+
return proxy;
110+
}
111+
112+
@Override
113+
public @Nonnull ChunkAccess getChunk(final int chunkX, final int chunkZ) {
114+
ChunkAccess worldChunk = world.getChunk(chunkX, chunkZ);
115+
PaperweightChunkAccessProxy proxy = chunkProxies.compute(
116+
MathMan.pairInt(chunkX, chunkZ),
117+
(k, v) -> v == null ? PaperweightChunkAccessProxy.getInstance() : v
118+
);
119+
proxy.parent = worldChunk;
120+
return proxy;
121+
}
122+
123+
@Override
124+
public @Nonnull ChunkAccess getChunk(final int chunkX, final int chunkZ, final @Nonnull ChunkStatus chunkStatus) {
125+
ChunkAccess worldChunk = world.getChunk(chunkX, chunkZ, chunkStatus);
126+
PaperweightChunkAccessProxy proxy = chunkProxies.compute(
127+
MathMan.pairInt(chunkX, chunkZ),
128+
(k, v) -> v == null ? PaperweightChunkAccessProxy.getInstance() : v
129+
);
130+
proxy.parent = worldChunk;
131+
return proxy;
132+
}
133+
134+
@Override
135+
@Nonnull
136+
public FeatureFlagSet enabledFeatures() {
137+
return world.enabledFeatures();
138+
}
139+
140+
@Override
141+
public float getShade(final Direction direction, final boolean shaded) {
142+
return world.getShade(direction, shaded);
143+
}
144+
145+
@Override
146+
@Nonnull
147+
public LevelLightEngine getLightEngine() {
148+
return world.getLightEngine();
149+
}
150+
151+
@Nullable
152+
@Override
153+
public ChunkAccess getChunkIfLoadedImmediately(final int x, final int z) {
154+
return world.getChunkIfLoadedImmediately(x, z);
155+
}
156+
157+
@Override
158+
public BlockState getBlockStateIfLoaded(final BlockPos blockposition) {
159+
return world.getBlockStateIfLoaded(blockposition);
160+
}
161+
162+
@Override
163+
public FluidState getFluidIfLoaded(final BlockPos blockposition) {
164+
return world.getFluidIfLoaded(blockposition);
165+
}
166+
167+
@Override
168+
@Nonnull
169+
public WorldBorder getWorldBorder() {
170+
return world.getWorldBorder();
171+
}
172+
173+
@Override
174+
public boolean setBlock(final BlockPos pos, final BlockState state, final int flags, final int maxUpdateDepth) {
175+
return world.setBlock(pos, state, flags, maxUpdateDepth);
176+
}
177+
178+
@Override
179+
public boolean removeBlock(final BlockPos pos, final boolean move) {
180+
return world.removeBlock(pos, move);
181+
}
182+
183+
@Override
184+
public boolean destroyBlock(final BlockPos pos, final boolean drop, final Entity breakingEntity, final int maxUpdateDepth) {
185+
return world.destroyBlock(pos, drop, breakingEntity, maxUpdateDepth);
186+
}
187+
188+
@Override
189+
@Nonnull
190+
public BlockState getBlockState(final BlockPos pos) {
191+
return world.getBlockState(pos);
192+
}
193+
194+
@Override
195+
public boolean setBlock(final BlockPos pos, final BlockState state, final int flags) {
196+
return world.setBlock(pos, state, flags);
197+
}
198+
199+
}

0 commit comments

Comments
 (0)