use of net.minecraft.world.level.Level in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method deleteWorld.
@Override
public CompletableFuture<Boolean> deleteWorld(final ResourceKey key) {
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(key, "key"));
if (Level.OVERWORLD.equals(registryKey)) {
return CompletableFuture.completedFuture(false);
}
if (!this.worldExists(key)) {
return CompletableFuture.completedFuture(false);
}
final ServerLevel loadedWorld = this.worlds.get(registryKey);
if (loadedWorld != null) {
final boolean disableLevelSaving = loadedWorld.noSave;
loadedWorld.noSave = true;
try {
this.unloadWorld0(loadedWorld);
} catch (final IOException e) {
loadedWorld.noSave = disableLevelSaving;
return FutureUtil.completedWithException(e);
}
}
final boolean isVanillaWorld = this.isVanillaWorld(key);
final String directoryName = this.getDirectoryName(key);
final Path directory = isVanillaWorld ? this.defaultWorldDirectory.resolve(directoryName) : this.customWorldsDirectory.resolve(key.namespace()).resolve(key.value());
if (Files.exists(directory)) {
try {
for (final Path path : Files.walk(directory).sorted(Comparator.reverseOrder()).collect(Collectors.toList())) {
Files.deleteIfExists(path);
}
} catch (final IOException e) {
return FutureUtil.completedWithException(e);
}
}
final Path configFile = SpongeCommon.spongeConfigDirectory().resolve(Launch.instance().id()).resolve("worlds").resolve(key.namespace()).resolve(key.value() + ".conf");
try {
Files.deleteIfExists(configFile);
} catch (final IOException e) {
return FutureUtil.completedWithException(e);
}
final Path dimensionTemplate = this.getDataPackFile(key);
try {
Files.deleteIfExists(dimensionTemplate);
} catch (final IOException e) {
FutureUtil.completedWithException(e);
}
return CompletableFuture.completedFuture(true);
}
use of net.minecraft.world.level.Level in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method moveWorld.
@Override
public CompletableFuture<Boolean> moveWorld(final ResourceKey key, final ResourceKey movedKey) {
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(key, "key"));
if (Level.OVERWORLD.equals(registryKey)) {
return CompletableFuture.completedFuture(false);
}
if (!this.worldExists(key)) {
return CompletableFuture.completedFuture(false);
}
if (this.worldExists(movedKey)) {
return CompletableFuture.completedFuture(false);
}
final ServerLevel loadedWorld = this.worlds.get(registryKey);
if (loadedWorld != null) {
try {
this.unloadWorld0(loadedWorld);
} catch (final IOException e) {
return FutureUtil.completedWithException(e);
}
}
final boolean isVanillaWorld = this.isVanillaWorld(key);
final String directoryName = this.getDirectoryName(key);
final Path originalDirectory = isVanillaWorld ? this.defaultWorldDirectory.resolve(directoryName) : this.customWorldsDirectory.resolve(key.namespace()).resolve(key.value());
final boolean isVanillaMoveWorld = this.isVanillaWorld(movedKey);
final String moveDirectoryName = this.getDirectoryName(movedKey);
final Path moveDirectory = isVanillaMoveWorld ? this.defaultWorldDirectory.resolve(moveDirectoryName) : this.customWorldsDirectory.resolve(key.namespace()).resolve(key.value());
try {
Files.createDirectories(moveDirectory);
Files.move(originalDirectory, moveDirectory, StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) {
return FutureUtil.completedWithException(e);
}
final Path configFile = SpongeCommon.spongeConfigDirectory().resolve(Launch.instance().id()).resolve("worlds").resolve(key.namespace()).resolve(key.value() + ".conf");
final Path copiedConfigFile = SpongeCommon.spongeConfigDirectory().resolve(Launch.instance().id()).resolve("worlds").resolve(movedKey.namespace()).resolve(movedKey.value() + ".conf");
try {
Files.createDirectories(copiedConfigFile.getParent());
Files.move(configFile, copiedConfigFile, StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) {
return FutureUtil.completedWithException(e);
}
final Path dimensionTemplate = this.getDataPackFile(key);
final Path copiedDimensionTemplate = this.getDataPackFile(movedKey);
try {
Files.createDirectories(copiedDimensionTemplate.getParent());
Files.move(dimensionTemplate, copiedDimensionTemplate, StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) {
FutureUtil.completedWithException(e);
}
return CompletableFuture.completedFuture(true);
}
use of net.minecraft.world.level.Level in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method saveProperties.
@Override
public CompletableFuture<Boolean> saveProperties(final ServerWorldProperties properties) {
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(properties, "properties").key());
if (this.worlds.get(registryKey) != null) {
return CompletableFuture.completedFuture(false);
}
final ResourceKey key = properties.key();
final boolean isVanillaWorld = this.isVanillaWorld(key);
final String directoryName = this.getDirectoryName(key);
final LevelStorageSource.LevelStorageAccess storageSource;
try {
if (isVanillaWorld) {
storageSource = LevelStorageSource.createDefault(this.defaultWorldDirectory).createAccess(directoryName);
} else {
storageSource = LevelStorageSource.createDefault(this.customWorldsDirectory).createAccess(key.namespace() + File.separator + key.value());
}
} catch (final IOException e) {
return FutureUtil.completedWithException(e);
}
try {
try {
storageSource.saveDataTag(BootstrapProperties.registries, (WorldData) properties, null);
} catch (final Exception ex) {
return FutureUtil.completedWithException(ex);
}
} finally {
try {
storageSource.close();
} catch (final IOException ex) {
return FutureUtil.completedWithException(ex);
}
}
// Properties doesn't have everything we need...namely the generator, load the template and set values we actually got
return this.loadTemplate(key).thenCompose(r -> {
final WorldTemplate template = r.orElse(null);
if (template != null) {
final LevelStem scratch = ((SpongeWorldTemplate) template).asDimension();
((LevelStemBridge) (Object) scratch).bridge$populateFromLevelData((PrimaryLevelData) properties);
return this.saveTemplate(((LevelStemBridge) (Object) scratch).bridge$asTemplate());
}
return CompletableFuture.completedFuture(true);
});
}
use of net.minecraft.world.level.Level in project SpongeCommon by SpongePowered.
the class ServerLocationData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ServerLocation.class).create(Keys.BIOME_TEMPERATURE).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
final Biome biome = world.getBiome(pos);
return (double) biome.getBaseTemperature();
}).create(Keys.BLOCK_LIGHT).get(h -> {
final Level world = (Level) h.world();
return world.getBrightness(LightLayer.BLOCK, VecHelper.toBlockPos(h));
}).create(Keys.BLOCK_TEMPERATURE).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
final Biome biome = world.getBiome(pos);
return (double) biome.getTemperature(pos);
}).create(Keys.SKY_LIGHT).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
return world.getBrightness(LightLayer.SKY, pos);
}).create(Keys.IS_FULL_BLOCK).get(h -> {
final BlockState block = (BlockState) h.block();
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h.position());
return block.isSolidRender(world, pos);
}).create(Keys.IS_INDIRECTLY_POWERED).get(h -> {
final Level world = (Level) h.world();
final BlockPos pos = VecHelper.toBlockPos(h);
return world.getBestNeighborSignal(pos) > 0;
}).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(((Nameable) h.blockEntity().get()).getDisplayName())).supports(h -> h.blockEntity().isPresent() && h.blockEntity().get() instanceof NameableBlockEntity).create(Keys.CUSTOM_NAME).get(h -> {
final BlockEntity blockEntity = h.blockEntity().get();
return ((Nameable) blockEntity).hasCustomName() ? SpongeAdventure.asAdventure(((Nameable) blockEntity).getCustomName()) : null;
}).set((h, v) -> (((CustomNameableBridge) h.blockEntity().get())).bridge$setCustomDisplayName(SpongeAdventure.asVanilla(v))).delete(h -> (((CustomNameableBridge) h.blockEntity().get())).bridge$setCustomDisplayName(null)).supports(h -> h.blockEntity().isPresent() && h.blockEntity().get() instanceof NameableBlockEntity).create(Keys.CREATOR).get(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$getBlockCreatorUUID(VecHelper.toBlockPos(h.blockPosition())).orElse(null)).set((h, v) -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockCreator(VecHelper.toBlockPos(h.blockPosition()), v)).delete(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockCreator(VecHelper.toBlockPos(h.blockPosition()), null)).create(Keys.NOTIFIER).get(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$getBlockNotifierUUID(VecHelper.toBlockPos(h.blockPosition())).orElse(null)).set((h, v) -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockNotifier(VecHelper.toBlockPos(h.blockPosition()), v)).delete(h -> ((LevelChunkBridge) h.world().chunk(h.chunkPosition())).bridge$setBlockNotifier(VecHelper.toBlockPos(h.blockPosition()), null));
}
use of net.minecraft.world.level.Level in project SpongeCommon by SpongePowered.
the class MapInfoItemStackData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.MAP_INFO).supports(item -> item.getItem() instanceof MapItem).get(itemStack -> {
if (itemStack.getTag() == null) {
return null;
}
return (MapInfo) ((Level) Sponge.server().worldManager().defaultWorld()).getMapData(Constants.Map.MAP_PREFIX + itemStack.getTag().getInt(Constants.Map.MAP_ID));
}).set((itemStack, mapInfo) -> {
@Nullable CompoundTag nbt = itemStack.getTag();
if (nbt == null) {
nbt = new CompoundTag();
}
nbt.putInt(Constants.Map.MAP_ID, ((MapItemSavedDataBridge) mapInfo).bridge$getMapId());
itemStack.setTag(nbt);
});
}
Aggregations