crwdns135291:0crwdne135291:0
crwdns135293:0crwdne135293:0
crwdns135295:0crwdne135295:0
crwdns135297:0crwdne135297:0
crwdns135299:0crwdne135299:0
crwdns135301:0crwdne135301:0
crwdns135303:0crwdne135303:0
crwdns135305:0crwdne135305:0
crwdns135307:0crwdne135307:0
crwdns135309:0crwdne135309:0
Note
crwdns135311:0:doc:crwdne135311:0
crwdns135313:0crwdne135313:0
crwdns135315:0crwdne135315:0
public class SpongeHealthData extends AbstractData<HealthData, ImmutableHealthData> implements HealthData {
[...]
}
crwdns135317:0crwdne135317:0
crwdns135319:0crwdne135319:0
crwdns135321:0crwdne135321:0
crwdns135323:0crwdne135323:0
crwdns135325:0crwdne135325:0
crwdns135327:0crwdne135327:0
crwdns135329:0crwdne135329:0
crwdns135331:0crwdne135331:0
crwdns135333:0crwdne135333:0
import static com.google.common.base.Preconditions.checkArgument;
public class SpongeHealthData {
public SpongeHealthData() {
this(DataConstants.DEFAULT_HEALTH, DataConstants.DEFAULT_HEALTH);
}
public SpongeHealthData(double currentHealth, double maxHealth) {
super(HealthData.class);
checkArgument(currentHealth >= DataConstants.MINIMUM_HEALTH && currentHealth <= (double) Float.MAX_VALUE);
checkArgument(maxHealth >= DataConstants.MINIMUM_HEALTH && maxHealth <= (double) Float.MAX_VALUE);
this.currentHealth = currentHealth;
this.maximumHealth = maxHealth;
this.registerGettersAndSetters();
}
...
}
crwdns135335:0crwdne135335:0
Note
crwdns135337:0crwdne135337:0
crwdns135339:0crwdne135339:0
crwdns135341:0crwdne135341:0
public MutableBoundedValue<Double> health() {
return SpongeValueFactory.boundedBuilder(Keys.HEALTH)
.minimum(DataConstants.MINIMUM_HEALTH)
.maximum(this.maximumHealth)
.defaultValue(this.maximumHealth)
.actualValue(this.currentHealth)
.build();
}
Tip
crwdns135343:0crwdne135343:0
crwdns135345:0crwdne135345:0
crwdns135347:0crwdne135347:0
crwdns135349:0crwdne135349:0
crwdns135351:0crwdne135351:0
public DataContainer toContainer() {
return new MemoryDataContainer()
.set(Keys.HEALTH, this.currentHealth)
.set(Keys.MAX_HEALTH, this.maximumHealth);
}
crwdns135353:0crwdne135353:0
crwdns135355:0crwdne135355:0
crwdns135357:0crwdne135357:0
crwdns135359:0crwdne135359:0
crwdns135361:0crwdne135361:0
crwdns135363:0crwdne135363:0
private void setCurrentHealthIfValid(double value) {
if (value >= DataConstants.MINIMUM_HEALTH && value <= (double) Float.MAX_VALUE) {
this.currentHealth = value;
} else {
throw new IllegalArgumentException("Invalid value for current health");
}
}
private void setMaximumHealthIfValid(double value) {
if (value >= DataConstants.MINIMUM_HEALTH && value <= (double) Float.MAX_VALUE) {
this.maximumHealth = value;
} else {
throw new IllegalArgumentException("Invalid value for maximum health");
}
}
private void registerGettersAndSetters() {
registerFieldGetter(Keys.HEALTH, () -> SpongeHealthData.this.currentHealth);
registerFieldSetter(Keys.HEALTH, SpongeHealthData.this::setCurrentHealthIfValid);
registerKeyValue(Keys.HEALTH, SpongeHealthData.this::health);
registerFieldGetter(Keys.MAX_HEALTH, () -> SpongeHealthData.this.maximumHealth);
registerFieldSetter(Keys.MAX_HEALTH, SpongeHealthData.this::setMaximumHealthIfValid);
registerKeyValue(Keys.MAX_HEALTH, SpongeHealthData.this::maxHealth);
}
crwdns135365:0crwdne135365:0
Tip
crwdns135367:0crwdne135367:0
crwdns135369:0crwdne135369:0
crwdns135371:0crwdne135371:0
crwdns135373:0crwdne135373:0
crwdns135375:0crwdne135375:0
crwdns135377:0crwdne135377:0
crwdns135379:0crwdne135379:0
crwdns135381:0crwdne135381:0
crwdns135383:0crwdne135383:0
Tip
crwdns135385:0crwdne135385:0
crwdns135387:0crwdne135387:0
crwdns135389:0crwdne135389:0
keyMap.put("health"), makeSingleKey(Double.class, MutableBoundedValue.class, of("Health")));
keyMap.put("max_health", makeSingleKey(Double.class, MutableBoundedValue.class, of("MaxHealth")));
crwdns135391:0crwdne135391:0
crwdns135393:0crwdne135393:0
crwdns135395:0crwdne135395:0
crwdns135397:0crwdne135397:0
crwdns135399:0crwdne135399:0
public class HealthDataProcessor extends AbstractEntityDataProcessor<EntityLivingBase, HealthData, ImmutableHealthData> {
public HealthDataProcessor() {
super(EntityLivingBase.class);
}
[...]
}
crwdns135401:0crwdne135401:0
Tip
crwdns135403:0crwdne135403:0
crwdns135405:0crwdne135405:0
crwdns135407:0crwdne135407:0
crwdns135409:0crwdne135409:0
crwdns135411:0crwdne135411:0
protected boolean doesDataExist(EntityLivingBase entity) {
return true;
}
crwdns135413:0crwdne135413:0
crwdns135415:0crwdne135415:0
crwdns135417:0crwdne135417:0
crwdns135419:0crwdne135419:0
protected boolean set(EntityLivingBase entity, Map<Key<?>, Object> keyValues) {
entity.getEntityAttribute(SharedMonsterAttributes.maxHealth)
.setBaseValue(((Double) keyValues.get(Keys.MAX_HEALTH)).floatValue());
entity.setHealth(((Double) keyValues.get(Keys.HEALTH)).floatValue());
return true;
}
Tip
crwdns135421:0:doc:crwdnd135421:0:javadoc:crwdne135421:0
Warning
crwdns135423:0crwdne135423:0
crwdns135425:0crwdne135425:0
crwdns135427:0crwdne135427:0
crwdns135429:0crwdne135429:0
crwdns135431:0crwdne135431:0
public DataTransactionResult remove(DataHolder dataHolder) {
return DataTransactionBuilder.failNoData();
}
crwdns135433:0crwdne135433:0
crwdns135435:0crwdne135435:0
crwdns135437:0crwdne135437:0
Warning
crwdns135439:0crwdne135439:0
protected Map<Key<?>, ?> getValues(EntityLivingBase entity) {
final double health = entity.getHealth();
final double maxHealth = entity.getMaxHealth();
return ImmutableMap.<Key<?>, Object>of(Keys.HEALTH, health, Keys.MAX_HEALTH, maxHealth);
}
crwdns135441:0crwdne135441:0
crwdns135443:0crwdne135443:0
crwdns135445:0crwdne135445:0
public Optional<HealthData> fill(DataContainer container, HealthData healthData) {
final Optional<Double> health = container.getDouble(Keys.HEALTH.getQuery());
final Optional<Double> maxHealth = container.getDouble(Keys.MAX_HEALTH.getQuery());
if (health.isPresent() && maxHealth.isPresent()) {
healthData.set(Keys.HEALTH, health.get());
healthData.set(Keys.MAX_HEALTH, maxHealth.get());
return Optional.of(healthData);
}
return Optional.empty();
}
crwdns135447:0crwdne135447:0
crwdns135449:0crwdne135449:0
crwdns135451:0crwdne135451:0
crwdns135453:0crwdne135453:0
protected HealthData createManipulator() {
return new SpongeHealthData();
}
crwdns135455:0crwdne135455:0
crwdns135457:0crwdne135457:0
crwdns135459:0crwdne135459:0
public class HealthValueProcessor extends AbstractSpongeValueProcessor<EntityLivingBase, Double,
MutableBoundedValue<Double> {
public HealthValueProcessor() {
super(EntityLivingBase.class, Keys.HEALTH);
}
[...]
}
crwdns135461:0crwdne135461:0
Tip
crwdns135463:0crwdne135463:0
crwdns135465:0crwdne135465:0
protected MutableBoundedValue<Double> constructValue(Double value) {
return SpongeValueFactory.boundedBuilder(Keys.HEALTH)
.minimum(DataConstants.MINIMUM_HEALTH)
.maximum((double) Float.MAX_VALUE)
.defaultValue(DataConstants.DEFAULT_HEALTH)
.actualValue(value)
.build();
}
protected ImmutableValue<Double> constructImmutableValue(Double value) {
return constructValue(value).asImmutable();
}
protected Optional<Double> getVal(EntityLivingBase container) {
return Optional.of((double) container.getHealth());
}
crwdns135467:0crwdne135467:0
protected boolean set(EntityLivingBase container, Double value) {
if (value >= DataConstants.MINIMUM_HEALTH && value <= (double) Float.MAX_VALUE) {
container.setHealth(value.floatValue());
return true;
}
return false;
}
crwdns135469:0crwdne135469:0
public DataTransactionResult removeFrom(ValueContainer<?> container) {
return DataTransactionBuilder.failNoData();
}
crwdns135471:0crwdne135471:0
crwdns135473:0crwdne135473:0
crwdns135475:0crwdne135475:0
crwdns135477:0crwdne135477:0
crwdns135479:0crwdne135479:0
dataRegistry.registerDataProcessorAndImpl(HealthData.class, SpongeHealthData.class,
ImmutableHealthData.class, ImmutableSpongeHealthData.class,
new HealthDataProcessor());
crwdns135481:0crwdne135481:0
crwdns135483:0crwdne135483:0
dataRegistry.registerValueProcessor(Keys.HEALTH, new HealthValueProcessor());
dataRegistry.registerValueProcessor(Keys.MAX_HEALTH, new MaxHealthValueProcessor());
crwdns135485:0crwdne135485:0
crwdns135487:0crwdne135487:0
crwdns135489:0crwdne135489:0