Транзакции
Чтение результата
For everything you offer
to a data holder, the offer
method will yield a DataTransactionResult. This
object will contain the following:
Тип
DataTransactionResult.Type указывает была ли операция успешно завершена или, если нет, какие ошибки произошли.
|
Отсутствие ясного результата операции - указывает, что произошла какая-то ошибка |
|
Операция была успешно завершена |
|
При выполнении операции произошла ошибка согласно ожиданиям (например, из-за противоречивых данных) |
|
При выполнении операции произошла ошибка по неожиданным причинам |
|
Событие для этой операции было отменено |
Задействованные данные
The result also provides a couple of immutable lists containing immutable value containers representing the data that was involved in the transaction.
|
|
|
|
|
Примеры
Healing a Player
Surely you remember the healing example in the Использование ключей page. Imagine a player who is down to half a heart
(which equals 1 health) being healed that way. The DataTransactionResult
in that case would look like this:
getType()
would returnSUCCESS
getRejectedData()
would be an empty listgetReplacedData()
would contain one value container for theKeys.HEALTH
key with a value of 1.0getSuccessfulData()
would contain one value container for theKeys.HEALTH
key with a value of 20.0
Now what would be different if we used the healing example from the Custom Data page instead? Since the
HealthData
data manipulator contains values for both the current and the maximum health, in addition to the
above result, both the getReplacedData()
list and the getSuccessfulData()
list would contain one more element:
A value container for the Keys.MAX_HEALTH
key with a value of 20.0.
Offering HealthData to a block of stone
Now our above-mentioned examples are coded in a such a way that they will fail silently rather than try to offer the
incompatible data. But imagine we took a (fully healed) player’s HealthData
and tried to offer it to the
Location of the stone block he’s currently standing on. We can do this, since Location
is also a data
holder. And if we do, it would reward us with a DataTransactionResult
like this:
getType()
would returnFAILURE
getRejectedData()
would contain two value containers for theHEALTH
andMAX_HEALTH
keys, each with a value of 20.0getReplacedData()
andgetSuccessfulData()
would be empty lists
Reverting Transactions
Since everything about a transaction result is immutable, it can serve for documentation of data changes. And it
also allows for those changes it documents to be undone. For that, simply pass a transaction result to the data
holder’s undo()
method. This is particularly useful since some data offerings may be partially successful, so
that one or more values are successfully written to the data holder, yet one more value cannot be accepted. Since
you may wish to undo the partial successes.
Code Example: Reverting a transaction
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.manipulator.DataManipulator;
public void safeOffer(DataHolder target, DataManipulator data) {
DataTransactionResult result = target.offer(data);
if (result.getType() != DataTransactionResult.Type.SUCCESS) {
target.undo(result);
}
}