Search in sources :

Example 1 with Home

use of org.spongepowered.test.myhomes.data.home.Home in project SpongeCommon by SpongePowered.

the class HomeDataImpl method from.

// Only required on mutable implementations
@Override
public Optional<HomeData> from(DataContainer container) {
    if (!container.contains(MyHomes.DEFAULT_HOME, MyHomes.HOMES)) {
        return Optional.empty();
    }
    // Loads the structure defined in toContainer
    this.defaultHome = container.getSerializable(MyHomes.DEFAULT_HOME.getQuery(), Home.class).get();
    // Loads the map of homes
    this.homes = Maps.newHashMap();
    DataView homes = container.getView(MyHomes.HOMES.getQuery()).get();
    for (DataQuery homeQuery : homes.getKeys(false)) {
        homes.getSerializable(homeQuery, Home.class).ifPresent(home -> this.homes.put(homeQuery.toString(), home));
    }
    return Optional.of(this);
}
Also used : DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) Home(org.spongepowered.test.myhomes.data.home.Home)

Example 2 with Home

use of org.spongepowered.test.myhomes.data.home.Home in project SpongeCommon by SpongePowered.

the class HomeBuilder method buildContent.

@Override
protected Optional<Home> buildContent(DataView content) throws InvalidDataException {
    if (!content.contains(Home.WORLD_QUERY, Home.POSITION_QUERY, Home.ROTATION_QUERY, Home.NAME_QUERY)) {
        return Optional.empty();
    }
    World world = Sponge.getServer().getWorld(content.getObject(Home.WORLD_QUERY, UUID.class).get()).orElseThrow(InvalidDataException::new);
    Vector3d position = content.getObject(Home.POSITION_QUERY, Vector3d.class).get();
    Vector3d rotation = content.getObject(Home.ROTATION_QUERY, Vector3d.class).get();
    String name = content.getString(Home.NAME_QUERY).get();
    Transform<World> transform = new Transform<>(world, position, rotation);
    return Optional.of(new Home(transform, name));
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) World(org.spongepowered.api.world.World) UUID(java.util.UUID) Transform(org.spongepowered.api.entity.Transform) Home(org.spongepowered.test.myhomes.data.home.Home)

Example 3 with Home

use of org.spongepowered.test.myhomes.data.home.Home in project SpongeCommon by SpongePowered.

the class HomeDataBuilder method buildContent.

@Override
protected Optional<HomeData> buildContent(DataView container) throws InvalidDataException {
    if (!container.contains(MyHomes.HOMES))
        return Optional.empty();
    HomeData data = new HomeDataImpl();
    container.getView(MyHomes.HOMES.getQuery()).get().getKeys(false).forEach(name -> data.homes().put(name.toString(), container.getSerializable(name, Home.class).orElseThrow(InvalidDataException::new)));
    container.getSerializable(MyHomes.DEFAULT_HOME.getQuery(), Home.class).ifPresent(home -> {
        data.set(MyHomes.DEFAULT_HOME, home);
    });
    return Optional.of(data);
}
Also used : HomeData(org.spongepowered.test.myhomes.data.home.HomeData) ImmutableHomeData(org.spongepowered.test.myhomes.data.home.ImmutableHomeData) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) Home(org.spongepowered.test.myhomes.data.home.Home)

Example 4 with Home

use of org.spongepowered.test.myhomes.data.home.Home in project SpongeCommon by SpongePowered.

the class HomeTranslator method translate.

@Override
public Home translate(final DataView content) throws InvalidDataException {
    content.getInt(Queries.CONTENT_VERSION).ifPresent(version -> {
        if (version != CONTENT_VERSION) {
            throw new InvalidDataException("Version incompatible: " + version);
        }
    });
    if (!content.contains(Home.WORLD_QUERY, Home.POSITION_QUERY, Home.ROTATION_QUERY, Home.NAME_QUERY)) {
        throw new InvalidDataException("Incomplete data");
    }
    World world = Sponge.getServer().getWorld(content.getObject(Home.WORLD_QUERY, UUID.class).get()).orElseThrow(InvalidDataException::new);
    Vector3d position = content.getObject(Home.POSITION_QUERY, Vector3d.class).get();
    Vector3d rotation = content.getObject(Home.ROTATION_QUERY, Vector3d.class).get();
    String name = content.getString(Home.NAME_QUERY).get();
    Transform<World> transform = new Transform<>(world, position, rotation);
    return new Home(transform, name);
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) World(org.spongepowered.api.world.World) UUID(java.util.UUID) Transform(org.spongepowered.api.entity.Transform) Home(org.spongepowered.test.myhomes.data.home.Home)

Aggregations

Home (org.spongepowered.test.myhomes.data.home.Home)4 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)3 Vector3d (com.flowpowered.math.vector.Vector3d)2 UUID (java.util.UUID)2 Transform (org.spongepowered.api.entity.Transform)2 World (org.spongepowered.api.world.World)2 DataQuery (org.spongepowered.api.data.DataQuery)1 DataView (org.spongepowered.api.data.DataView)1 HomeData (org.spongepowered.test.myhomes.data.home.HomeData)1 ImmutableHomeData (org.spongepowered.test.myhomes.data.home.ImmutableHomeData)1