What to do if getOrCreate() returns Optional.absent()?

Hello!

I try to set the vehicle of a player via setVehicle(…). Therefore I need the PassengerData of the player. As far as I know I get it with player.getOrCreate(PassengerData.class). This returns Optional.absent().

JavaDoc says(extract):

In the event the DataManipulator can not represent any data pertaining to this DataHolder, Optional#absent() is returned.

So, what to do to create a PassengerData?

When all data is implemented, it won’t return absent. It’s returning absent because PassengerData is not implemented.

1 Like

Oh, I saw that something for PassengerData is implemented here https://github.com/SpongePowered/SpongeCommon/issues/7 so I thought it could work.

Thank you and sorry(for another stupid “It doesn’t work” - “It hasn’t been implemented yet”-thread)

There’s two parts to implementation,

  1. The data manipulators themselves (storing data in an object)
  2. Attaching manipulators to holders.

PassengerData (and many other data types) have stage 1 complete, but stage 2 is more difficult therefore not as many manipulators are fully usable yet.

1 Like

I wonder why a method that should create an object, returns an Optional ?

It will work when the implementation is finished. I think we should be happy that we can test a server without waiting for everything being implemented. → There has to be a compromise: Not everything works.

(Difficult for me to explain in English :smile:)

It creates an object if the type requested is compatible to the DataHolder. If not, Optional.absent() is returned. The alternative would have been making it throw an IllegalArgumentException or something like that.

4 Likes

Don’t worry, you worded it well :smile:

Where in the code are manipulators attached to holders?

Using Data Processors, https://github.com/SpongePowered/SpongeCommon/tree/master/src/main/java/org/spongepowered/common/data/processor