crwdns30985:0crwdne30985:0

crwdns30986:0crwdne30986:0

crwdns30987:0crwdne30987:0

  1. crwdns30988:0crwdne30988:0

  2. crwdns30989:0crwdne30989:0

crwdns30990:0crwdne30990:0

  1. crwdns30991:0crwdne30991:0

  2. crwdns30992:0crwdne30992:0

  3. crwdns30993:0crwdne30993:0

  4. crwdns30994:0crwdne30994:0

Note

crwdns30995:0:doc:crwdne30995:0

crwdns30996:0crwdne30996:0

crwdns30997:0crwdne30997:0

public class SpongeHealthData extends AbstractData<HealthData, ImmutableHealthData> implements HealthData {
    [...]
}

crwdns30998:0crwdne30998:0

crwdns30999:0crwdne30999:0

crwdns31000:0crwdne31000:0

  • crwdns31001:0crwdne31001:0

  • crwdns31002:0crwdne31002:0

crwdns31003:0crwdne31003:0

  • crwdns31004:0crwdne31004:0

  • crwdns31005:0crwdne31005:0

  • crwdns31006:0crwdne31006: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();
    }

    ...

}

crwdns31007:0crwdne31007:0

Note

crwdns31008:0crwdne31008:0

crwdns31009:0crwdne31009:0

crwdns31010:0crwdne31010: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

crwdns31011:0crwdne31011:0

crwdns31012:0crwdne31012:0

crwdns31013:0crwdne31013:0

crwdns31014:0crwdne31014:0

crwdns31015:0crwdne31015:0

public DataContainer toContainer() {
    return new MemoryDataContainer()
        .set(Keys.HEALTH, this.currentHealth)
        .set(Keys.MAX_HEALTH, this.maximumHealth);
}

crwdns31016:0crwdne31016:0

crwdns31017:0crwdne31017:0

  • crwdns31018:0crwdne31018:0

  • crwdns31019:0crwdne31019:0

  • crwdns31020:0crwdne31020:0

crwdns31021:0crwdne31021: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);
}

crwdns31022:0crwdne31022:0

Tip

crwdns31023:0crwdne31023:0

crwdns31024:0crwdne31024:0

crwdns31025:0crwdne31025:0

crwdns31026:0crwdne31026:0

crwdns31027:0crwdne31027:0

  • crwdns31028:0crwdne31028:0

  • crwdns31029:0crwdne31029:0

  • crwdns31030:0crwdne31030:0

crwdns31031:0crwdne31031:0

Tip

crwdns31032:0crwdne31032:0

crwdns31033:0crwdne31033:0

crwdns31034:0crwdne31034:0

keyMap.put("health"), makeSingleKey(Double.class, MutableBoundedValue.class, of("Health")));
keyMap.put("max_health", makeSingleKey(Double.class, MutableBoundedValue.class, of("MaxHealth")));

crwdns31035:0crwdne31035:0

crwdns31036:0crwdne31036:0

crwdns31037:0crwdne31037:0

crwdns31038:0crwdne31038:0

crwdns31039:0crwdne31039:0

public class HealthDataProcessor extends AbstractEntityDataProcessor<EntityLivingBase, HealthData, ImmutableHealthData> {
    public HealthDataProcessor() {
        super(EntityLivingBase.class);
    }
    [...]
}

crwdns31040:0crwdne31040:0

Tip

crwdns31041:0crwdne31041:0

crwdns31042:0crwdne31042:0

crwdns31043:0crwdne31043:0

crwdns31044:0crwdne31044:0

crwdns31045:0crwdne31045:0

protected boolean doesDataExist(EntityLivingBase entity) {
    return true;
}

crwdns31046:0crwdne31046:0

crwdns31047:0crwdne31047:0

crwdns31048:0crwdne31048:0

crwdns31049:0crwdne31049: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

crwdns31050:0:doc:crwdnd31050:0:javadoc:crwdne31050:0

Warning

crwdns31051:0crwdne31051:0

crwdns31052:0crwdne31052:0

crwdns31053:0crwdne31053:0

crwdns31054:0crwdne31054:0

crwdns31055:0crwdne31055:0

public DataTransactionResult remove(DataHolder dataHolder) {
    return DataTransactionBuilder.failNoData();
}

crwdns31056:0crwdne31056:0

crwdns31057:0crwdne31057:0

crwdns31058:0crwdne31058:0

Warning

crwdns31059:0crwdne31059: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);
}

crwdns31060:0crwdne31060:0

crwdns31061:0crwdne31061:0

crwdns31062:0crwdne31062: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();
}

crwdns31063:0crwdne31063:0

crwdns31064:0crwdne31064:0

crwdns31065:0crwdne31065:0

crwdns31066:0crwdne31066:0

protected HealthData createManipulator() {
    return new SpongeHealthData();
}

crwdns31067:0crwdne31067:0

crwdns31068:0crwdne31068:0

crwdns31069:0crwdne31069:0

public class HealthValueProcessor extends AbstractSpongeValueProcessor<EntityLivingBase, Double,
    MutableBoundedValue<Double> {

    public HealthValueProcessor() {
        super(EntityLivingBase.class, Keys.HEALTH);
    }

    [...]
}

crwdns31070:0crwdne31070:0

Tip

crwdns31071:0crwdne31071:0

crwdns31072:0crwdne31072: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());
}

crwdns31073:0crwdne31073: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;
}

crwdns31074:0crwdne31074:0

public DataTransactionResult removeFrom(ValueContainer<?> container) {
    return DataTransactionBuilder.failNoData();
}

crwdns31075:0crwdne31075:0

crwdns31076:0crwdne31076:0

crwdns31077:0crwdne31077:0

crwdns31078:0crwdne31078:0

crwdns31079:0crwdne31079:0

dataRegistry.registerDataProcessorAndImpl(HealthData.class, SpongeHealthData.class,
    ImmutableHealthData.class, ImmutableSpongeHealthData.class,
    new HealthDataProcessor());

crwdns31080:0crwdne31080:0

crwdns31081:0crwdne31081:0

dataRegistry.registerValueProcessor(Keys.HEALTH, new HealthValueProcessor());
dataRegistry.registerValueProcessor(Keys.MAX_HEALTH, new MaxHealthValueProcessor());

crwdns31082:0crwdne31082:0

crwdns31083:0crwdne31083:0

crwdns31084:0crwdne31084:0