# Handle player data

### Introduction

When setting a player is staff mod, we'll **modify** his inventory and eventually some other data. However, we would like to `keep` some data (game mode, potion effects, health, food level) to **restore** them later. So, the library provides a class called `PlayerDataHandler` which will allow us to do that.

### How to create it ?

To create a new `PlayerDataHandler` instance, you will need a list of object of classes that implement the `DataHandler<Player>` interface. Each of which handle a specific data type.&#x20;

#### Predefined data classes

| Class                  | Description                                     |
| ---------------------- | ----------------------------------------------- |
| `InventoryDataHandler` | Handle player's inventory including his armors. |
| `PotionDataHandler`    | Handle active player's potion effects.          |
| `HealthDataHandler`    | Handle player's health.                         |
| `FoodDataHandler`      | Handle player's saturation and food level.      |
| `GameModeDataHandler`  | Handle player's game mode.                      |

#### Create your data handler

```java
List<DataHandler<Player>> data = Arrays.asList(
    new InventoryDataHandler(),
    new PotionDataHandler(),
    new GameModeDataHandler(),
    new HealthDataHandler(),
    new FoodDataHandler()
);

PlayerDataHandler dataHandler = new PlayerDataHandler(data);
```

### Create your own data class

To create a your own data class, you only have to implements the `DataHandler<T>` interface and to redefine the required methods. Let's see an example in which we'll create a `FlyDataHandler` class to handle player's fly state.

```java
public class FlyDataHandler implements DataHandler<Player> {

    private boolean flightAllowed;

    @Override
    public void save(Player player) {
        this.flightAllowed = player.getAllowFlight();
    }

    @Override
    public void clear(Player player) {
        player.setAllowFlight(false);
    }

    @Override
    public void restore(Player player) {
        player.setAllowFlight(this.flightAllowed);
    }
}
```

{% hint style="info" %}
The generic type is used to specify on which object that represents a player are applied the methods.&#x20;
{% endhint %}
