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);
});
}
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());
}
});
}
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));
}
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;
}
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;
}
Aggregations