Search in sources :

Example 26 with ResourceKey

use of org.spongepowered.api.ResourceKey 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);
    });
}
Also used : LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ReportedException(net.minecraft.ReportedException) ResourceKey(org.spongepowered.api.ResourceKey) LevelStem(net.minecraft.world.level.dimension.LevelStem) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel) JsonObject(com.google.gson.JsonObject) WorldTemplate(org.spongepowered.api.world.server.WorldTemplate)

Example 27 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class SchematicTranslator method writePaletteToView.

private static <T, P> void writePaletteToView(final DataView view, final Palette.Mutable<T, P> palette, final Registry<P> parentRegistryType, final DataQuery paletteQuery, final Function<T, P> parentGetter, final Set<String> requiredMods) {
    palette.streamWithIds().forEach(entry -> {
        // getOrAssign to skip the optional, it will never assign
        final String stringified = palette.type().stringifier().apply(parentRegistryType, entry.getKey());
        view.set(paletteQuery.then(stringified), entry.getValue());
        final ResourceKey blockKey = parentRegistryType.findValueKey(parentGetter.apply(entry.getKey())).orElseThrow(() -> new IllegalStateException("Somehow have a BlockState that is not registered in the global BlockType registry"));
        if (!ResourceKey.MINECRAFT_NAMESPACE.equals(blockKey.namespace())) {
            requiredMods.add(blockKey.namespace());
        }
    });
}
Also used : ResourceKey(org.spongepowered.api.ResourceKey)

Example 28 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class SpongeServerLocationBuilder method buildContent.

@Override
protected Optional<ServerLocation> buildContent(final DataView container) throws InvalidDataException {
    final ResourceKey worldKey = container.getResourceKey(Queries.WORLD_KEY).orElseThrow(() -> new InvalidDataException("Missing [" + Queries.WORLD_KEY + "] entry"));
    final int x = container.getInt(Queries.POSITION_X).orElseThrow(() -> new InvalidDataException("Missing [" + Queries.POSITION_X + "] entry"));
    final int y = container.getInt(Queries.POSITION_Y).orElseThrow(() -> new InvalidDataException("Missing [" + Queries.POSITION_Y + "] entry"));
    final int z = container.getInt(Queries.POSITION_Z).orElseThrow(() -> new InvalidDataException("Missing [" + Queries.POSITION_Z + "] entry"));
    return Optional.of(ServerLocation.of(worldKey, x, y, z));
}
Also used : InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) ResourceKey(org.spongepowered.api.ResourceKey)

Example 29 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class ItemStackMixin_API method toContainer.

@Override
public DataContainer toContainer() {
    final ResourceKey key = (ResourceKey) (Object) Registry.ITEM.getKey((Item) this.itemStack$type());
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.ItemStack.TYPE, key).set(Constants.ItemStack.COUNT, this.itemStack$quantity()).set(Constants.ItemStack.DAMAGE_VALUE, this.shadow$getDamageValue());
    if (this.shadow$hasTag()) {
        // no tag? no data, simple as that.
        final CompoundTag compound = this.shadow$getTag().copy();
        if (compound.contains(Constants.Sponge.Data.V2.SPONGE_DATA)) {
            final CompoundTag spongeCompound = compound.getCompound(Constants.Sponge.Data.V2.SPONGE_DATA);
            if (spongeCompound.contains(Constants.Sponge.Data.V2.CUSTOM_MANIPULATOR_TAG_LIST)) {
                spongeCompound.remove(Constants.Sponge.Data.V2.CUSTOM_MANIPULATOR_TAG_LIST);
            }
        }
        // We must filter the custom data so it isn't stored twice
        Constants.NBT.filterSpongeCustomData(compound);
        if (!compound.isEmpty()) {
            final DataContainer unsafeNbt = NBTTranslator.INSTANCE.translateFrom(compound);
            container.set(Constants.Sponge.UNSAFE_NBT, unsafeNbt);
        }
    }
    try {
        PlatformHooks.INSTANCE.getItemHooks().writeItemStackCapabilitiesToDataView(container, (net.minecraft.world.item.ItemStack) (Object) this);
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CompoundTag(net.minecraft.nbt.CompoundTag) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) ResourceKey(org.spongepowered.api.ResourceKey)

Example 30 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class EntitySelectorParserMixin_API method addGameMode.

@Override
public Selector.@NonNull Builder addGameMode(@NonNull final GameMode mode) {
    final ResourceKey key = Sponge.game().registry(RegistryTypes.GAME_MODE).valueKey(mode);
    this.api$handle("gamemode", key.value(), Tristate.FALSE);
    return this;
}
Also used : ResourceKey(org.spongepowered.api.ResourceKey)

Aggregations

ResourceKey (org.spongepowered.api.ResourceKey)71 Sponge (org.spongepowered.api.Sponge)13 List (java.util.List)11 Map (java.util.Map)11 DataContainer (org.spongepowered.api.data.persistence.DataContainer)11 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 ArrayList (java.util.ArrayList)8 Collection (java.util.Collection)8 CompoundTag (net.minecraft.nbt.CompoundTag)8 NonNull (org.checkerframework.checker.nullness.qual.NonNull)8 RegistryTypes (org.spongepowered.api.registry.RegistryTypes)8 IOException (java.io.IOException)7 Component (net.kyori.adventure.text.Component)7 Nullable (org.checkerframework.checker.nullness.qual.Nullable)7 Keys (org.spongepowered.api.data.Keys)7 Constants (org.spongepowered.common.util.Constants)7 ImmutableList (com.google.common.collect.ImmutableList)6 ServerLevel (net.minecraft.server.level.ServerLevel)6 DataQuery (org.spongepowered.api.data.persistence.DataQuery)6