Search in sources :

Example 1 with FluidType

use of org.spongepowered.api.fluid.FluidType in project SpongeCommon by SpongePowered.

the class SpongeFluidStackSnapshotBuilder method buildContent.

@Override
@NonNull
protected Optional<FluidStackSnapshot> buildContent(@NonNull final DataView container) throws InvalidDataException {
    try {
        if (container.contains(Constants.Fluids.FLUID_TYPE, Constants.Fluids.FLUID_VOLUME)) {
            final String rawFluid = container.getString(Constants.Fluids.FLUID_TYPE).get();
            final Optional<FluidType> type = Sponge.game().registry(RegistryTypes.FLUID_TYPE).findValue(ResourceKey.resolve(rawFluid));
            if (!type.isPresent()) {
                throw new InvalidDataException("Unknown fluid id found: " + rawFluid);
            }
            final FluidType fluidType = type.get();
            final int volume = container.getInt(Constants.Fluids.FLUID_VOLUME).get();
            final SpongeFluidStackSnapshotBuilder builder = new SpongeFluidStackSnapshotBuilder();
            builder.fluid(fluidType).volume(volume);
            if (container.contains(Constants.Sponge.UNSAFE_NBT)) {
                builder.container = container.getView(Constants.Sponge.UNSAFE_NBT).get().copy();
            }
            return Optional.of(builder.build());
        }
    } catch (final Exception e) {
        throw new InvalidDataException("Something went wrong deserializing.", e);
    }
    return Optional.empty();
}
Also used : FluidType(org.spongepowered.api.fluid.FluidType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 2 with FluidType

use of org.spongepowered.api.fluid.FluidType in project SpongeCommon by SpongePowered.

the class SpongeFluidStack method setRawData.

@Override
public void setRawData(@NonNull final DataView container) throws InvalidDataException {
    try {
        final int contentVersion = container.getInt(Queries.CONTENT_VERSION).get();
        if (contentVersion != this.contentVersion()) {
            throw new InvalidDataException("Older content found! Cannot set raw data of older content!");
        }
        final String rawFluid = container.getString(Constants.Fluids.FLUID_TYPE).get();
        final int volume = container.getInt(Constants.Fluids.FLUID_VOLUME).get();
        final Optional<FluidType> fluidType = Sponge.game().registry(RegistryTypes.FLUID_TYPE).findValue(ResourceKey.resolve(rawFluid));
        if (!fluidType.isPresent()) {
            throw new InvalidDataException("Unknown FluidType found! Requested: " + rawFluid + "but got none.");
        }
        this.fluidType = fluidType.get();
        this.volume = volume;
        if (container.contains(Constants.Sponge.UNSAFE_NBT)) {
            this.extraData = container.getView(Constants.Sponge.UNSAFE_NBT).get().copy();
        }
    } catch (final Exception e) {
        throw new InvalidDataException("DataContainer contained invalid data!", e);
    }
}
Also used : FluidType(org.spongepowered.api.fluid.FluidType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Aggregations

InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)2 FluidType (org.spongepowered.api.fluid.FluidType)2 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1