Set fly status

How would I go about setting or removing fly for a player? I don’t see anything and I’m not sure if there’s going to be a key that’s just not implemented yet…

FlyingData

Is the player flying:

Optional<FlyingData> optFlying = player.getData(FlyingData.class);
boolean isFlying = optFlying.isPresent() && optFlying.get().flying().get();

Setting fly ON for a player:

player.offer(player.getOrCreate(FlyingData.class).get().flying().set(true));

Removing fly for a player: (Edited as per @Aaron1011’s comment from below)

player.offer(player.getOrCreate(FlyingData.class).get().flying().set(false));

One thing, @gabizou, there seems to be two ways to represent this, either the presence/absence of FlyingData, or the boolean value flying() on the manipulator. Which is it, and what happens when removing the manipulator?

In the previous incarnation manifestation version of the Data API, many maniuplators, such as WetData, didn’t actually contain any method. Their prescence or abscence was used to indicate a boolean value.

In the current Data API, this isn’t the case. Removing a data maniuplator only makes sense when it’s possible for the underlying data to no longer exist at all on the DataHolder.

For example, take DisplayNameData. Removing it from a mob is possible, since a mob doesn’t have to have a custom display name (and doesn’t be default).

On the other hand, a Player always has a flying state - be it flying or not. There’s no way you can really remove that from a player, so it only makes sense to interact with the boolean Value.

All of this is a really long-winded way of saying to use this to remove flying from a player:

player.offer(player.getOrCreate(FlyingData.class).get().flying().set(false));
1 Like

Is it possible to use Keys for flying data?

@simon816 With your code, I’m getting a couple errors. First, eclipse can’t recognize the getData method, and I just updated my dependencies. I changed it to just .get, which I think is right, but now when I’m trying to enable/disable flying, I get an absent value error on
player.getOrCreate(FlyingData.class).get()
This means the getOrCreate command is messed up since it’s returning an absent value, which I don’t think it should be able to, right?

getData was renamed to get, I keep forgetting that.

The reason why it is returning absent is because FlyingData is not implemented yet. You can track the data manipulator implementation here:

1 Like

When in doubt assume it’s not implemented I guess haha. Thanks!

Every bit of data that is managed by a DataManipulator can be managed by Keys as well. The basis is: If you want to manipulate large sets of data, use manipulators. If you want to just get/set or transform single bits of data, go ahead and use Keys.

In the case of flying status:

player.offer(Keys.CAN_FLY, true);
player.offer(Keys.IS_FLYING, true);

will work just fine.

2 Likes