Search in sources :

Example 1 with FluidType

use of org.spongepowered.api.extra.fluid.FluidType in project core by CubeEngine.

the class LocationUtil method getBlockInSight.

/**
 * Returns the block in sight.
 * When in fluids returns the first Air or Solid Block
 * else returns the first Solid or Fluid Block
 *
 * @param player the looking player
 * @return the block in sight
 */
public static Location<World> getBlockInSight(Player player) {
    BlockType headIn = player.getLocation().getRelative(UP).getBlockType();
    List<BlockType> fluidBlocks = Sponge.getRegistry().getAllOf(FluidType.class).stream().map(FluidType::getBlockTypeBase).filter(Optional::isPresent).map(Optional::get).collect(toList());
    boolean headInFluid = fluidBlocks.contains(headIn);
    Iterator<BlockRayHit<World>> it = BlockRay.from(player).distanceLimit(500).iterator();
    BlockRayHit<World> next = null;
    while (it.hasNext()) {
        next = it.next();
        BlockType nextType = next.getLocation().getBlockType();
        if (fluidBlocks.contains(nextType)) {
            if (!headInFluid) {
                break;
            }
        } else if (canPass(nextType)) {
            if (BlockTypes.AIR.equals(nextType) && headInFluid) {
                break;
            }
        } else {
            break;
        }
    }
    return next == null ? null : next.getLocation();
}
Also used : FluidType(org.spongepowered.api.extra.fluid.FluidType) Optional(java.util.Optional) BlockType(org.spongepowered.api.block.BlockType) World(org.spongepowered.api.world.World) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Example 2 with FluidType

use of org.spongepowered.api.extra.fluid.FluidType in project SpongeCommon by SpongePowered.

the class SpongeFluidStack method setRawData.

@Override
public void setRawData(DataView container) throws InvalidDataException {
    try {
        final int contentVersion = container.getInt(Queries.CONTENT_VERSION).get();
        if (contentVersion != this.getContentVersion()) {
            throw new InvalidDataException("Older content found! Cannot set raw data of older content!");
        }
        final String fluidId = container.getString(DataQueries.FLUID_TYPE).get();
        final int volume = container.getInt(DataQueries.FLUID_VOLUME).get();
        final Optional<FluidType> fluidType = Sponge.getRegistry().getType(FluidType.class, fluidId);
        if (!fluidType.isPresent()) {
            throw new InvalidDataException("Unknown FluidType found! Requested: " + fluidId + "but got none.");
        }
        this.fluidType = fluidType.get();
        this.volume = volume;
        if (container.contains(DataQueries.UNSAFE_NBT)) {
            this.extraData = container.getView(DataQueries.UNSAFE_NBT).get().copy();
        }
    } catch (Exception e) {
        throw new InvalidDataException("DataContainer contained invalid data!", e);
    }
}
Also used : FluidType(org.spongepowered.api.extra.fluid.FluidType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Example 3 with FluidType

use of org.spongepowered.api.extra.fluid.FluidType in project SpongeCommon by SpongePowered.

the class SpongeFluidStackSnapshotBuilder method buildContent.

@Override
protected Optional<FluidStackSnapshot> buildContent(DataView container) throws InvalidDataException {
    try {
        if (container.contains(DataQueries.FLUID_TYPE, DataQueries.FLUID_VOLUME)) {
            final String fluidId = container.getString(DataQueries.FLUID_TYPE).get();
            final Optional<FluidType> type = Sponge.getRegistry().getType(FluidType.class, fluidId);
            if (!type.isPresent()) {
                throw new InvalidDataException("Unknown fluid id found: " + fluidId);
            }
            final FluidType fluidType = type.get();
            final int volume = container.getInt(DataQueries.FLUID_VOLUME).get();
            SpongeFluidStackSnapshotBuilder builder = new SpongeFluidStackSnapshotBuilder();
            builder.fluid(fluidType).volume(volume);
            if (container.contains(DataQueries.UNSAFE_NBT)) {
                builder.container = container.getView(DataQueries.UNSAFE_NBT).get().copy();
            }
            return Optional.of(builder.build());
        }
    } catch (Exception e) {
        throw new InvalidDataException("Something went wrong deserializing.", e);
    }
    return Optional.empty();
}
Also used : FluidType(org.spongepowered.api.extra.fluid.FluidType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException)

Aggregations

FluidType (org.spongepowered.api.extra.fluid.FluidType)3 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)2 Optional (java.util.Optional)1 BlockType (org.spongepowered.api.block.BlockType)1 BlockRayHit (org.spongepowered.api.util.blockray.BlockRayHit)1 World (org.spongepowered.api.world.World)1