개체 변경하기
경고
These docs were written for SpongeAPI 7 and are likely out of date. If you feel like you can help update them, please submit a PR!
물론, 평범한 개체를 생성하는 것도 좋지만, 그것보다 흥미로운 게 있지 않을까요? 여기서 DataManipulator 가 등장합니다. Entity 는 DataHolder 입니다. 즉, Entity``가 자료를 담을 수 있다는 뜻입니다. ``DataHolder
에 대한 더 자세한 정보는 자료 설명문서 에 있습니다.
DataManipulator
s that apply to Entity
s are things such as FoodData or HealthData. A
list of applicable DataManipulator
s can be found at org.spongepowered.api.data.manipulator.mutable and
org.spongepowered.api.data.manipulator.mutable.entity. Note that not all DataManipulator
s found there
may apply to all entities.
Entity Type
Before we can jump behind the wheel with our Entity
, we should check what type of Entity
it is, as we may
receive an Entity
we didn’t create and thus, do not know its type. Doing this is a simple equality check. Here is
an example of checking if our Entity
is a creeper:
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityTypes;
public boolean isCreeper(Entity entity) {
return entity.getType().equals(EntityTypes.CREEPER);
}
Entity Data Manipulators
Now that we are certain that our Entity
is a creeper, we can apply creeper specific DataManipulator
s to it.
For example, ExplosionRadiusData is a DataManipulator
that creepers can have, but not all Entity
s.
An example of changing an Entity
s explosive radius to 50 can be seen below:
import org.spongepowered.api.data.manipulator.mutable.entity.ExplosiveRadiusData;
public void explosionRadius50(Entity creeper) {
ExplosionRadiusData radiusData = creeper.get(ExplosionRadiusData.class).get();
creeper.offer(radiusData.explosionRadius().setTo(50));
}
This will get the ExplosiveRadiusData
of our Entity
for our use. We then use that data to set the explosive
radius of our creeper to 50. We then have to offer the data back to the creeper, as the data we received from our
Entity
is only a copy of the live data.
Perhaps we want to give our Entity
a name to customize it a bit! This would be done by using
DisplayNameData. An example of this in action can be seen below:
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.spongepowered.api.data.manipulator.mutable.DisplayNameData;
public void setDisplayName(Entity creeper) {
DisplayNameData displayData = creeper.get(DisplayNameData.class).get();
creeper.offer(displayData.displayName()
.set(Component.text("Inscrutable", NamedTextColor.DARK_AQUA)));
}
Another, shorter way to do this is by just using Keys on our Entity
instead of using
DataManipulator
s. This would be done like so:
import org.spongepowered.api.data.key.Keys;
public void explosionRadius50(Entity creeper) {
creeper.offer(Keys.EXPLOSION_RADIUS, Optional.of(50));
creeper.offer(Keys.DISPLAY_NAME, Component.text("Inscrutable", NamedTextColor.DARK_AQUA));
}
This would neaten our code and is easier to perform. See the data documentation on
the specific benefits of using either DataManipulator
s or just Keys
.