SpongePowered/SpongeAPI — pull request #236 by sk89q (2014-11-26 10:28:30+00:00)
Title: Get block data via Block.getData(Class<T> dataClass)

One needs a way to access the data stored in tile entities conveniently, without having to dump the TE to NBT, editing the NBT, and then rebuilding the block.

## The proposal

This proposal adds the following method to `Block`:

```java
/**
* Get an instance of the given data class for this block.
*
*

For example, if this block represents a sign,
* {@code getData(Sign.class)} would yield an instance of
* {@link Sign} to change the contents of the sign. However, if
* this block does not represent a sign, then an instance will not
* be returned.

*
* @param dataClass The data class
* @param The type of data
* @return An instance of the class
*/
Optional getData(Class dataClass);
```

Here are some examples of it in use:

```java
block.getData(Sign.class).get().setLines(newLines);
```

```java
Inventory inventory = block.getData(Inventory.class).get();
inventory.addItem(new ItemStack(...));
```

```java
assert !brickBlock.getData(Inventory.class).isPresent()
```

### Advantages

* Highly reusuable
* Highly generic
* Highly extensible
* Allows wrapper classes to work properly

## Regarding entities

This is currently in contrast to how entities handle the problem of accessing their state data. When one gets an instance of `Entity`, it is a subclass of `Entity`, such as a `Player` or `Cow`.

However, this proposal could also be applied to entities:

```java
chestMinecart.getData(Inventory.class).get().doStuff()
```