How to use Data API 2.0

There are two ways (though currently one at present time due to a missing Key for Velocity):

Optional<Vector3d> optional = entity.get(Keys.VELOCITY);
if (optional.isPresent()) {
  entity.set(Keys.VELOCITY, optional.get().add(0,1,0);
}

Or

Optional<VelocityData> optional = entity.get(VelocityData.class);
if (optional.isPresent()) {
  final Value<Vector3d> velocityVal = optional.get().velocity();
  entity.offer(optional.get().set(velocityVal.set(velocityVal.get().add(0,1,0)));
}

OR! (this is a bit more interesting if you’re just wanting to transform the velocity)

entity.transform(Keys.VELOCITY, new Function<Vector3d, Vector3d>() {
  @Override
  public Vector3d apply(Vector3d input) {
    return input.add(0,1,0);
  }
}

It’s threadsafe to manipulate a DataManipulator or Value after you’ve retrieved it from a ValueContainer/DataHolder, but it is NOT thread safe to offer it back or retrieve it.

The primary reason for immutable stuff is so that events can especially rely on original values/manipulators not being meddled with, let alone being passed to external threads.

With the Causes PR I’m hoping to provide a feasible way to damage an entity with some provided stuff, including a DamageSource and a bunch of other things.

You can use the AttributeData which currently needs to have some work done due to the Attributes API being a near impossible to actually implement, let alone use.

A custom DataManipulator is quite literally a DataManipulator that you want to be able to store custom data that isn’t already covered by the Data API. So, for example, if you have some Locations of "home"s you want to attach to a Player, you definitely could do so by providing your own custom DataManipulator. Alternatively, you could go the more traditional route of storing that stuff to a config file. More examples of custom DataManipulators can range from storing custom information that you don’t want to be storing onto a map, linking to various entities and possibly having to manage references without leaking them, because you’ll know that the custom data is persisted.

1 Like