-
-
Notifications
You must be signed in to change notification settings - Fork 837
/
FabricWorld.java
607 lines (542 loc) · 23.8 KB
/
FabricWorld.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.fabric;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.io.Files;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.block.BlockStateIdAccess;
import com.sk89q.worldedit.internal.util.BiomeMath;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import com.sk89q.worldedit.world.AbstractWorld;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.weather.WeatherType;
import com.sk89q.worldedit.world.weather.WeatherTypes;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldGenerationProgressListener;
import net.minecraft.server.world.ServerChunkManager;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Clearable;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldSaveHandler;
import net.minecraft.world.biome.DefaultBiomeFeatures;
import net.minecraft.world.biome.source.BiomeArray;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkManager;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.gen.feature.BranchedTreeFeatureConfig;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.OakTreeFeature;
import net.minecraft.world.level.LevelProperties;
import javax.annotation.Nullable;
import java.io.File;
import java.lang.ref.WeakReference;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An adapter to Minecraft worlds for WorldEdit.
*/
public class FabricWorld extends AbstractWorld {
private static final Random random = new Random();
private static final int UPDATE = 1, NOTIFY = 2;
private final WeakReference<World> worldRef;
/**
* Construct a new world.
*
* @param world the world
*/
FabricWorld(World world) {
checkNotNull(world);
this.worldRef = new WeakReference<>(world);
}
/**
* Get the underlying handle to the world.
*
* @return the world
* @throws WorldEditException thrown if a reference to the world was lost (i.e. world was unloaded)
*/
public World getWorldChecked() throws WorldEditException {
World world = worldRef.get();
if (world != null) {
return world;
} else {
throw new WorldReferenceLostException("The reference to the world was lost (i.e. the world may have been unloaded)");
}
}
/**
* Get the underlying handle to the world.
*
* @return the world
* @throws RuntimeException thrown if a reference to the world was lost (i.e. world was unloaded)
*/
public World getWorld() {
World world = worldRef.get();
if (world != null) {
return world;
} else {
throw new RuntimeException("The reference to the world was lost (i.e. the world may have been unloaded)");
}
}
@Override
public String getName() {
return getWorld().getLevelProperties().getLevelName();
}
@Override
public String getId() {
return getWorld().getLevelProperties().getLevelName()
.replace(" ", "_").toLowerCase(Locale.ROOT)
+ getWorld().dimension.getType().getSuffix();
}
@Override
public Path getStoragePath() {
final World world = getWorld();
if (world instanceof ServerWorld) {
return ((ServerWorld) world).getSaveHandler().getWorldDir().toPath();
}
return null;
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, boolean notifyAndLight) throws WorldEditException {
checkNotNull(position);
checkNotNull(block);
World world = getWorldChecked();
int x = position.getBlockX();
int y = position.getBlockY();
int z = position.getBlockZ();
// First set the block
Chunk chunk = world.getChunk(x >> 4, z >> 4);
BlockPos pos = new BlockPos(x, y, z);
net.minecraft.block.BlockState old = chunk.getBlockState(pos);
int stateId = BlockStateIdAccess.getBlockStateId(block.toImmutableState());
net.minecraft.block.BlockState newState =
BlockStateIdAccess.isValidInternalId(stateId)
? Block.getStateFromRawId(stateId)
: FabricAdapter.adapt(block.toImmutableState());
net.minecraft.block.BlockState successState = chunk.setBlockState(pos, newState, false);
boolean successful = successState != null;
// Create the TileEntity
if (successful || old == newState) {
if (block instanceof BaseBlock) {
CompoundTag tag = ((BaseBlock) block).getNbtData();
if (tag != null) {
tag = tag.createBuilder()
.putInt("x", x)
.putInt("y", y)
.putInt("z", z)
.build();
net.minecraft.nbt.CompoundTag nativeTag = NBTConverter.toNative(tag);
BlockEntity tileEntity = getWorld().getWorldChunk(pos).getBlockEntity(pos);
if (tileEntity != null) {
tileEntity.setLocation(world, pos);
tileEntity.fromTag(nativeTag);
successful = true; // update if TE changed as well
}
}
}
}
if (successful && notifyAndLight) {
world.getChunkManager().getLightingProvider().checkBlock(pos);
world.checkBlockRerender(pos, old, newState);
world.updateListeners(pos, old, newState, UPDATE | NOTIFY);
world.updateNeighbors(pos, newState.getBlock());
if (old.hasComparatorOutput()) {
world.updateHorizontalAdjacent(pos, newState.getBlock());
}
}
return successful;
}
@Override
public boolean notifyAndLightBlock(BlockVector3 position, BlockState previousType) throws WorldEditException {
BlockPos pos = new BlockPos(position.getX(), position.getY(), position.getZ());
net.minecraft.block.BlockState state = getWorld().getBlockState(pos);
getWorld().updateListeners(pos, FabricAdapter.adapt(previousType), state, 1 | 2);
getWorld().updateNeighbors(pos, state.getBlock());
return true;
}
@Override
public int getBlockLightLevel(BlockVector3 position) {
checkNotNull(position);
return getWorld().getLightLevel(FabricAdapter.toBlockPos(position));
}
@Override
public boolean clearContainerBlockContents(BlockVector3 position) {
checkNotNull(position);
BlockEntity tile = getWorld().getBlockEntity(FabricAdapter.toBlockPos(position));
if ((tile instanceof Clearable)) {
((Clearable) tile).clear();
return true;
}
return false;
}
@Override
public BiomeType getBiome(BlockVector2 position) {
checkNotNull(position);
return FabricAdapter.adapt(getWorld().getBiome(new BlockPos(position.getBlockX(), 0, position.getBlockZ())));
}
@Override
public boolean setBiome(BlockVector2 position, BiomeType biome) {
checkNotNull(position);
checkNotNull(biome);
Chunk chunk = getWorld().getChunk(position.getBlockX() >> 4, position.getBlockZ() >> 4, ChunkStatus.FULL, false);
if (chunk == null) {
return false;
}
MutableBiomeArray biomeArray = MutableBiomeArray.inject(chunk.getBiomeArray());
// Temporary, while biome setting is 2D only
for (int i = 0; i < BiomeMath.VERTICAL_BIT_MASK; i++) {
biomeArray.setBiome(position.getX(), i, position.getZ(), FabricAdapter.adapt(biome));
}
chunk.setShouldSave(true);
return true;
}
private static final LoadingCache<ServerWorld, WorldEditFakePlayer> fakePlayers
= CacheBuilder.newBuilder().weakKeys().softValues().build(CacheLoader.from(WorldEditFakePlayer::new));
@Override
public boolean useItem(BlockVector3 position, BaseItem item, Direction face) {
ItemStack stack = FabricAdapter.adapt(new BaseItemStack(item.getType(), item.getNbtData(), 1));
ServerWorld world = (ServerWorld) getWorld();
final WorldEditFakePlayer fakePlayer;
try {
fakePlayer = fakePlayers.get(world);
} catch (ExecutionException ignored) {
return false;
}
fakePlayer.setStackInHand(Hand.MAIN_HAND, stack);
fakePlayer.updatePositionAndAngles(position.getBlockX(), position.getBlockY(), position.getBlockZ(),
(float) face.toVector().toYaw(), (float) face.toVector().toPitch());
final BlockPos blockPos = FabricAdapter.toBlockPos(position);
final BlockHitResult rayTraceResult = new BlockHitResult(FabricAdapter.toVec3(position),
FabricAdapter.adapt(face), blockPos, false);
ItemUsageContext itemUseContext = new ItemUsageContext(fakePlayer, Hand.MAIN_HAND, rayTraceResult);
ActionResult used = stack.useOnBlock(itemUseContext);
if (used != ActionResult.SUCCESS) {
// try activating the block
used = getWorld().getBlockState(blockPos).onUse(world, fakePlayer, Hand.MAIN_HAND, rayTraceResult);
}
if (used != ActionResult.SUCCESS) {
used = stack.use(world, fakePlayer, Hand.MAIN_HAND).getResult();
}
return used == ActionResult.SUCCESS;
}
@Override
public void dropItem(Vector3 position, BaseItemStack item) {
checkNotNull(position);
checkNotNull(item);
if (item.getType() == ItemTypes.AIR) {
return;
}
ItemEntity entity = new ItemEntity(getWorld(), position.getX(), position.getY(), position.getZ(), FabricAdapter.adapt(item));
entity.setPickupDelay(10);
getWorld().spawnEntity(entity);
}
@Override
public void simulateBlockMine(BlockVector3 position) {
BlockPos pos = FabricAdapter.toBlockPos(position);
getWorld().breakBlock(pos, true);
}
@Override
public boolean regenerate(Region region, EditSession editSession) {
// Don't even try to regen if it's going to fail.
ChunkManager provider = getWorld().getChunkManager();
if (!(provider instanceof ServerChunkManager)) {
return false;
}
File saveFolder = Files.createTempDir();
// register this just in case something goes wrong
// normally it should be deleted at the end of this method
saveFolder.deleteOnExit();
try {
ServerWorld originalWorld = (ServerWorld) getWorld();
MinecraftServer server = originalWorld.getServer();
WorldSaveHandler saveHandler = new WorldSaveHandler(saveFolder, originalWorld.getSaveHandler().getWorldDir().getName(), server, server.getDataFixer());
World freshWorld = new ServerWorld(server, server.getWorkerExecutor(), saveHandler, originalWorld.getLevelProperties(),
originalWorld.dimension.getType(), originalWorld.getProfiler(), new NoOpChunkStatusListener());
// Pre-gen all the chunks
// We need to also pull one more chunk in every direction
CuboidRegion expandedPreGen = new CuboidRegion(region.getMinimumPoint().subtract(16, 0, 16), region.getMaximumPoint().add(16, 0, 16));
for (BlockVector2 chunk : expandedPreGen.getChunks()) {
freshWorld.getChunk(chunk.getBlockX(), chunk.getBlockZ());
}
FabricWorld from = new FabricWorld(freshWorld);
for (BlockVector3 vec : region) {
editSession.setBlock(vec, from.getFullBlock(vec));
}
} catch (MaxChangedBlocksException e) {
throw new RuntimeException(e);
} finally {
saveFolder.delete();
}
return true;
}
@Nullable
private static ConfiguredFeature<?, ?> createTreeFeatureGenerator(TreeType type) {
switch (type) {
// Based off of the SaplingGenerator class, as well as uses of DefaultBiomeFeatures fields
case TREE: return Feature.NORMAL_TREE.configure(DefaultBiomeFeatures.OAK_TREE_CONFIG);
case BIG_TREE: return Feature.FANCY_TREE.configure(DefaultBiomeFeatures.FANCY_TREE_CONFIG);
case REDWOOD: return Feature.NORMAL_TREE.configure(DefaultBiomeFeatures.SPRUCE_TREE_CONFIG);
case TALL_REDWOOD: return Feature.MEGA_SPRUCE_TREE.configure(DefaultBiomeFeatures.MEGA_SPRUCE_TREE_CONFIG);
case MEGA_REDWOOD: return Feature.MEGA_SPRUCE_TREE.configure(DefaultBiomeFeatures.MEGA_PINE_TREE_CONFIG);
case BIRCH: return Feature.NORMAL_TREE.configure(DefaultBiomeFeatures.BIRCH_TREE_CONFIG);
case JUNGLE: return Feature.MEGA_JUNGLE_TREE.configure(DefaultBiomeFeatures.MEGA_JUNGLE_TREE_CONFIG);
case SMALL_JUNGLE: return Feature.NORMAL_TREE.configure(DefaultBiomeFeatures.JUNGLE_TREE_CONFIG);
case SHORT_JUNGLE: return new OakTreeFeature(BranchedTreeFeatureConfig::deserialize)
.configure(DefaultBiomeFeatures.JUNGLE_SAPLING_TREE_CONFIG);
case JUNGLE_BUSH: return Feature.JUNGLE_GROUND_BUSH.configure(DefaultBiomeFeatures.JUNGLE_GROUND_BUSH_CONFIG);
case SWAMP: return Feature.NORMAL_TREE.configure(DefaultBiomeFeatures.SWAMP_TREE_CONFIG);
case ACACIA: return Feature.ACACIA_TREE.configure(DefaultBiomeFeatures.ACACIA_TREE_CONFIG);
case DARK_OAK: return Feature.DARK_OAK_TREE.configure(DefaultBiomeFeatures.DARK_OAK_TREE_CONFIG);
case TALL_BIRCH: return Feature.NORMAL_TREE.configure(DefaultBiomeFeatures.LARGE_BIRCH_TREE_CONFIG);
case RED_MUSHROOM: return Feature.HUGE_RED_MUSHROOM.configure(DefaultBiomeFeatures.HUGE_RED_MUSHROOM_CONFIG);
case BROWN_MUSHROOM: return Feature.HUGE_BROWN_MUSHROOM.configure(DefaultBiomeFeatures.HUGE_BROWN_MUSHROOM_CONFIG);
case RANDOM: return createTreeFeatureGenerator(TreeType.values()[ThreadLocalRandom.current().nextInt(TreeType.values().length)]);
default:
return null;
}
}
@Override
public boolean generateTree(TreeType type, EditSession editSession, BlockVector3 position) {
ConfiguredFeature<?, ?> generator = createTreeFeatureGenerator(type);
ServerChunkManager chunkManager = (ServerChunkManager) getWorld().getChunkManager();
return generator != null && generator.generate(
getWorld(), chunkManager.getChunkGenerator(), random,
FabricAdapter.toBlockPos(position)
);
}
@Override
public void checkLoadedChunk(BlockVector3 pt) {
getWorld().getChunk(FabricAdapter.toBlockPos(pt));
}
@Override
public void fixAfterFastMode(Iterable<BlockVector2> chunks) {
fixLighting(chunks);
}
@Override
public void fixLighting(Iterable<BlockVector2> chunks) {
World world = getWorld();
for (BlockVector2 chunk : chunks) {
world.getChunkManager().getLightingProvider().setLightEnabled(
new ChunkPos(chunk.getBlockX(), chunk.getBlockZ()), true
);
}
}
@Override
public boolean playEffect(Vector3 position, int type, int data) {
getWorld().playLevelEvent(type, FabricAdapter.toBlockPos(position.toBlockPoint()), data);
return true;
}
@Override
public WeatherType getWeather() {
LevelProperties info = getWorld().getLevelProperties();
if (info.isThundering()) {
return WeatherTypes.THUNDER_STORM;
}
if (info.isRaining()) {
return WeatherTypes.RAIN;
}
return WeatherTypes.CLEAR;
}
@Override
public long getRemainingWeatherDuration() {
LevelProperties info = getWorld().getLevelProperties();
if (info.isThundering()) {
return info.getThunderTime();
}
if (info.isRaining()) {
return info.getRainTime();
}
return info.getClearWeatherTime();
}
@Override
public void setWeather(WeatherType weatherType) {
setWeather(weatherType, 0);
}
@Override
public void setWeather(WeatherType weatherType, long duration) {
LevelProperties info = getWorld().getLevelProperties();
if (weatherType == WeatherTypes.THUNDER_STORM) {
info.setClearWeatherTime(0);
info.setThundering(true);
info.setThunderTime((int) duration);
} else if (weatherType == WeatherTypes.RAIN) {
info.setClearWeatherTime(0);
info.setRaining(true);
info.setRainTime((int) duration);
} else if (weatherType == WeatherTypes.CLEAR) {
info.setRaining(false);
info.setThundering(false);
info.setClearWeatherTime((int) duration);
}
}
@Override
public int getMaxY() {
return getWorld().getHeight() - 1;
}
@Override
public BlockVector3 getSpawnPosition() {
return FabricAdapter.adapt(getWorld().getSpawnPos());
}
@Override
public BlockState getBlock(BlockVector3 position) {
net.minecraft.block.BlockState mcState = getWorld()
.getChunk(position.getBlockX() >> 4, position.getBlockZ() >> 4)
.getBlockState(FabricAdapter.toBlockPos(position));
BlockState matchingBlock = BlockStateIdAccess.getBlockStateById(Block.getRawIdFromState(mcState));
if (matchingBlock != null) {
return matchingBlock;
}
return FabricAdapter.adapt(mcState);
}
@Override
public BaseBlock getFullBlock(BlockVector3 position) {
BlockPos pos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ());
// Avoid creation by using the CHECK mode -- if it's needed, it'll be re-created anyways
BlockEntity tile = ((WorldChunk) getWorld().getChunk(pos)).getBlockEntity(pos, WorldChunk.CreationType.CHECK);
if (tile != null) {
net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
tile.toTag(tag);
return getBlock(position).toBaseBlock(NBTConverter.fromNative(tag));
} else {
return getBlock(position).toBaseBlock();
}
}
@Override
public int hashCode() {
return getWorld().hashCode();
}
@Override
public boolean equals(Object o) {
if ((o instanceof FabricWorld)) {
FabricWorld other = ((FabricWorld) o);
World otherWorld = other.worldRef.get();
World thisWorld = worldRef.get();
return otherWorld != null && otherWorld.equals(thisWorld);
} else if (o instanceof com.sk89q.worldedit.world.World) {
return ((com.sk89q.worldedit.world.World) o).getName().equals(getName());
} else {
return false;
}
}
@Override
public List<? extends Entity> getEntities(Region region) {
final World world = getWorld();
if (!(world instanceof ServerWorld)) {
return Collections.emptyList();
}
return ((ServerWorld) world).getEntities(null, entity -> true)
.stream()
.filter(e -> region.contains(FabricAdapter.adapt(e.getBlockPos())))
.map(FabricEntity::new).collect(Collectors.toList());
}
@Override
public List<? extends Entity> getEntities() {
final World world = getWorld();
if (!(world instanceof ServerWorld)) {
return Collections.emptyList();
}
return ((ServerWorld) world).getEntities(null, entity -> true)
.stream()
.map(FabricEntity::new)
.collect(Collectors.toList());
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity) {
World world = getWorld();
final Optional<EntityType<?>> entityType = EntityType.get(entity.getType().getId());
if (!entityType.isPresent()) return null;
net.minecraft.entity.Entity createdEntity = entityType.get().create(world);
if (createdEntity != null) {
CompoundTag nativeTag = entity.getNbtData();
if (nativeTag != null) {
net.minecraft.nbt.CompoundTag tag = NBTConverter.toNative(entity.getNbtData());
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
createdEntity.fromTag(tag);
}
createdEntity.updatePositionAndAngles(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
world.spawnEntity(createdEntity);
return new FabricEntity(createdEntity);
} else {
return null;
}
}
/**
* Thrown when the reference to the world is lost.
*/
@SuppressWarnings("serial")
private static final class WorldReferenceLostException extends WorldEditException {
private WorldReferenceLostException(String message) {
super(message);
}
}
private static class NoOpChunkStatusListener implements WorldGenerationProgressListener {
@Override
public void start(ChunkPos chunkPos) {
}
@Override
public void setChunkStatus(ChunkPos chunkPos, @Nullable ChunkStatus chunkStatus) {
}
@Override
public void stop() {
}
}
}