crwdns145253:0crwdne145253:0

Warning

crwdns145255:0crwdne145255:0

crwdns145257:0crwdne145257:0

crwdns145259:0:javadoc:crwdne145259:0

  1. crwdns145261:0crwdne145261:0

  2. crwdns145263:0:javadoc:crwdne145263:0

crwdns145265:0crwdne145265:0

  1. crwdns145267:0:javadoc:crwdne145267:0

  2. crwdns145269:0crwdne145269:0

  3. crwdns145271:0:javadoc:crwdne145271:0

crwdns145273:0crwdne145273:0

Note

crwdns145275:0:doc:crwdne145275:0

crwdns145277:0crwdne145277:0

import org.spongepowered.common.data.DataProcessor;
import org.spongepowered.common.data.ValueProcessor;
import org.spongepowered.common.data.manipulator.immutable.entity.ImmutableSpongeHealthData;
import org.spongepowered.common.data.manipulator.mutable.common.AbstractData;
import org.spongepowered.common.data.manipulator.mutable.entity.SpongeHealthData;
import org.spongepowered.common.data.processor.common.AbstractEntityDataProcessor;
import org.spongepowered.common.util.Constants;
import org.spongepowered.common.data.util.NbtDataUtil;
import org.spongepowered.common.registry.type.data.KeyRegistryModule;

crwdns145279:0crwdne145279:0

crwdns145281:0:javadoc:crwdne145281:0

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

crwdns145283:0crwdne145283:0

crwdns145285:0crwdne145285:0

crwdns145287:0crwdne145287:0

  • crwdns145289:0crwdne145289:0

  • crwdns145291:0crwdne145291:0

crwdns145293:0crwdne145293:0

  • crwdns145295:0crwdne145295:0

  • crwdns145297:0crwdne145297:0

  • crwdns145299:0crwdne145299:0

import static com.google.common.base.Preconditions.checkArgument;

public class SpongeHealthData extends AbstractData<HealthData, ImmutableHealthData> implements HealthData {

    private double health;
    private double maxHealth;

    public SpongeHealthData() {
        this(20D, 20D);
    }

    public SpongeHealthData(double health, double maxHealth) {
        super(HealthData.class);
        checkArgument(maxHealth > 0);
        this.health = health;
        this.maxHealth = maxHealth;
        registerGettersAndSetters();
    }

    [...]

}

crwdns145301:0crwdne145301:0

Note

crwdns145303:0crwdne145303:0

crwdns145305:0crwdne145305:0

crwdns145307:0:javadoc:crwdnd145307:0:javadoc:crwdnd145307:0:javadoc:crwdne145307:0

public MutableBoundedValue<Double> health() {
    return SpongeValueFactory.boundedBuilder(Keys.HEALTH)
        .minimum(0)
        .maximum(this.maxHealth)
        .defaultValue(this.maxHealth)
        .actualValue(this.health)
        .build();
}

Tip

crwdns145309:0crwdne145309:0

crwdns145311:0:javadoc:crwdne145311:0

crwdns145313:0crwdne145313:0

crwdns145315:0:javadoc:crwdnd145315:0:javadoc:crwdne145315:0

crwdns145317:0:javadoc:crwdnd145317:0:javadoc:crwdnd145317:0:javadoc:crwdnd145317:0:javadoc:crwdnd145317:0:javadoc:crwdne145317:0

public DataContainer toContainer() {
    return super.toContainer()
        .set(Keys.HEALTH, this.health)
        .set(Keys.MAX_HEALTH, this.maxHealth);
}

crwdns145319:0crwdne145319:0

crwdns145321:0crwdne145321:0

  • crwdns145323:0:javadoc:crwdne145323:0

  • crwdns145325:0:javadoc:crwdne145325:0

  • crwdns145327:0crwdne145327:0

crwdns145329:0crwdne145329:0

private SpongeHealthData setCurrentHealthIfValid(double value) {
    if (value >= 0 && value <= (double) Float.MAX_VALUE) {
        this.health = value;
    } else {
        throw new IllegalArgumentException("Invalid value for current health");
    }
    return this;
}

private SpongeHealthData setMaximumHealthIfValid(double value) {
    if (value >= 0 && value <= (double) Float.MAX_VALUE) {
        this.maxHealth = value;
    } else {
        throw new IllegalArgumentException("Invalid value for maximum health");
    }
    return this;
}

private void registerGettersAndSetters() {
    registerFieldGetter(Keys.HEALTH, () -> this.health);
    registerFieldSetter(Keys.HEALTH, this::setCurrentHealthIfValid);
    registerKeyValue(Keys.HEALTH, this::health);

    registerFieldGetter(Keys.MAX_HEALTH, () -> this.maxHealth);
    registerFieldSetter(Keys.MAX_HEALTH, this::setMaximumHealthIfValid);
    registerKeyValue(Keys.MAX_HEALTH, this::maxHealth);
}

crwdns145331:0:javadoc:crwdne145331:0

Tip

crwdns145333:0crwdne145333:0

crwdns145335:0crwdne145335:0

crwdns145337:0crwdne145337:0

crwdns145339:0:javadoc:crwdne145339:0

crwdns145341:0crwdne145341:0

  • crwdns145343:0crwdne145343:0

  • crwdns145345:0crwdne145345:0

  • crwdns145347:0crwdne145347:0

crwdns145349:0crwdne145349:0

Tip

crwdns145351:0crwdne145351:0

crwdns145353:0crwdne145353:0

crwdns145355:0:javadoc:crwdnd145355:0:javadoc:crwdne145355:0

import static org.spongepowered.api.data.DataQuery.of;

this.register("health", Key.builder()
        .type(TypeTokens.BOUNDED_DOUBLE_VALUE_TOKEN)
        .id("health")
        .name("Health")
        .query(of("Health"))
        .build());
this.register("max_health", Key.builder()
        .type(TypeTokens.BOUNDED_DOUBLE_VALUE_TOKEN)
        .id("max_health")
        .name("Max Health")
        .query(of("MaxHealth"))
        .build());

crwdns145357:0:javadoc:crwdnd145357:0:javadoc:crwdne145357:0

crwdns145359:0crwdne145359:0

crwdns145361:0crwdne145361:0

crwdns145363:0crwdne145363:0

crwdns145365:0crwdne145365:0

public class HealthDataProcessor
        extends AbstractEntityDataProcessor<EntityLivingBase, HealthData, ImmutableHealthData> {

    public HealthDataProcessor() {
        super(EntityLivingBase.class);
    }

    [...]

}

crwdns145367:0crwdne145367:0

Tip

crwdns145369:0crwdne145369:0

crwdns145371:0crwdne145371:0

crwdns145373:0crwdne145373:0

crwdns145375:0crwdne145375:0

crwdns145377:0crwdne145377:0

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

crwdns145379:0crwdne145379:0

crwdns145381:0crwdne145381:0

crwdns145383:0crwdne145383:0

crwdns145385:0crwdne145385:0

@Override
protected boolean set(EntityLivingBase entity, Map<Key<?>, Object> keyValues) {
    entity.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH)
            .setBaseValue(((Double) keyValues.get(Keys.MAX_HEALTH)).floatValue());
    float health = ((Double) keyValues.get(Keys.HEALTH)).floatValue();
    entity.setHealth(health);
    return true;
}

Tip

crwdns145387:0:javadoc:crwdnd145387:0:doc:crwdnd145387:0:javadoc:crwdne145387:0

Warning

crwdns145389:0:javadoc:crwdne145389:0

crwdns145391:0crwdne145391:0

crwdns145393:0crwdne145393:0

crwdns145395:0crwdne145395:0

crwdns145397:0:javadoc:crwdne145397:0

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

crwdns145399:0crwdne145399:0

crwdns145401:0crwdne145401:0

crwdns145403:0crwdne145403:0

Warning

crwdns145405:0crwdne145405:0

@Override
protected Map<Key<?>, ?> getValues(EntityLivingBase entity) {
    final double health = entity.getHealth();
    final double maxHealth = entity.getMaxHealth();
    return ImmutableMap.of(Keys.HEALTH, health, Keys.MAX_HEALTH, maxHealth);
}

crwdns145407:0crwdne145407:0

crwdns145409:0crwdne145409:0

crwdns145411:0crwdne145411:0

@Override
public Optional<HealthData> fill(DataContainer container, HealthData healthData) {
    if (!container.contains(Keys.MAX_HEALTH.getQuery()) || !container.contains(Keys.HEALTH.getQuery())) {
        return Optional.empty();
    }
    healthData.set(Keys.MAX_HEALTH, getData(container, Keys.MAX_HEALTH));
    healthData.set(Keys.HEALTH, getData(container, Keys.HEALTH));
    return Optional.of(healthData);
}

crwdns145413:0crwdne145413:0

crwdns145415:0crwdne145415:0

crwdns145417:0crwdne145417:0

crwdns145419:0crwdne145419:0

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

crwdns145421:0crwdne145421:0

crwdns145423:0crwdne145423:0

crwdns145425:0crwdne145425:0

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

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

    [...]

}

crwdns145427:0crwdne145427:0

Tip

crwdns145429:0crwdne145429:0

crwdns145431:0crwdne145431:0

@Override
protected MutableBoundedValue<Double> constructValue(Double health) {
    return SpongeValueFactory.boundedBuilder(Keys.HEALTH)
        .minimum(0D)
        .maximum(((Float) Float.MAX_VALUE).doubleValue())
        .defaultValue(20D)
        .actualValue(health)
        .build();
}

@Override
protected ImmutableBoundedValue<Double> constructImmutableValue(Double value) {
    return constructValue(value).asImmutable();
}
@Override
protected Optional<Double> getVal(EntityLivingBase container) {
    return Optional.of((double) container.getHealth());
}

crwdns145433:0crwdne145433:0

@Override
protected boolean set(EntityLivingBase container, Double value) {
    if (value >= 0 && value <= (double) Float.MAX_VALUE) {
        container.setHealth(value.floatValue());
        return true;
    }
    return false;
}

crwdns145435:0crwdne145435:0

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

crwdns145437:0crwdne145437:0

crwdns145439:0crwdne145439:0

crwdns145441:0crwdne145441:0

crwdns145443:0crwdne145443:0

crwdns145445:0crwdne145445:0

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

crwdns145447:0crwdne145447:0

crwdns145449:0crwdne145449:0

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

crwdns145451:0crwdne145451:0

crwdns145453:0crwdne145453:0

@Mixin(BlockHorizontal.class)
public abstract class MixinBlockHorizontal extends MixinBlock {

    [...]

}

crwdns145455:0crwdne145455:0

@Override
public boolean supports(Class<? extends ImmutableDataManipulator<?, ?>> immutable) {
    return super.supports(immutable) || ImmutableDirectionalData.class.isAssignableFrom(immutable);
}

crwdns145457:0crwdne145457:0

@Override
public Optional<BlockState> getStateWithData(IBlockState blockState, ImmutableDataManipulator<?, ?> manipulator) {
    if (manipulator instanceof ImmutableDirectionalData) {
        final Direction direction = ((ImmutableDirectionalData) manipulator).direction().get();
        final EnumFacing facing = DirectionResolver.getFor(direction);
        return Optional.of((BlockState) blockState.withProperty(BlockHorizontal.FACING, facing));
    }
    return super.getStateWithData(blockState, manipulator);
}

crwdns145459:0crwdne145459:0

@Override
public <E> Optional<BlockState> getStateWithValue(IBlockState blockState, Key<? extends BaseValue<E>> key, E value) {
    if (key.equals(Keys.DIRECTION)) {
        final Direction direction = (Direction) value;
        final EnumFacing facing = DirectionResolver.getFor(direction);
        return Optional.of((BlockState) blockState.withProperty(BlockHorizontal.FACING, facing));
    }
    return super.getStateWithValue(blockState, key, value);
}

crwdns145461:0crwdne145461:0

@Override
public List<ImmutableDataManipulator<?, ?>> getManipulators(IBlockState blockState) {
    return ImmutableList.<ImmutableDataManipulator<?, ?>>builder()
            .addAll(super.getManipulators(blockState))
            .add(new ImmutableSpongeDirectionalData(DirectionResolver.getFor(blockState.getValue(BlockHorizontal.FACING))))
            .build();
}

crwdns145463:0crwdne145463:0

crwdns145465:0crwdne145465:0

crwdns145467:0crwdne145467:0