Search in sources :

Example 6 with Behavior

use of org.lanternpowered.server.behavior.Behavior in project LanternServer by LanternPowered.

the class BlockTypeBuilderImpl method build.

@Override
public LanternBlockType build(String pluginId, String id) {
    MutableBehaviorPipeline<Behavior> behaviorPipeline = this.behaviorPipeline;
    if (behaviorPipeline == null) {
        behaviorPipeline = new MutableBehaviorPipelineImpl<>(Behavior.class, new ArrayList<>());
    } else {
        behaviorPipeline = new MutableBehaviorPipelineImpl<>(Behavior.class, new ArrayList<>(behaviorPipeline.getBehaviors()));
    }
    TranslationProvider translationProvider = this.translationProvider;
    if (translationProvider == null) {
        String path = "tile." + id + ".name";
        if (!pluginId.equals("minecraft")) {
            path = pluginId + '.' + path;
        }
        translationProvider = TranslationProvider.of(tr(path));
    }
    PropertyProviderCollection.Builder properties;
    if (this.propertiesBuilder != null) {
        properties = this.propertiesBuilder;
    } else {
        properties = PropertyProviderCollections.DEFAULT.toBuilder();
    }
    ExtendedBlockStateProvider extendedBlockStateProvider = this.extendedBlockStateProvider;
    if (extendedBlockStateProvider == null) {
        extendedBlockStateProvider = new ExtendedBlockStateProvider() {

            @Override
            public BlockState get(BlockState blockState, @Nullable Location<World> location, @Nullable Direction face) {
                return blockState;
            }

            @Override
            public BlockState remove(BlockState blockState) {
                return blockState;
            }
        };
    }
    final LanternBlockType blockType = new LanternBlockType(pluginId, id, this.traits, translationProvider, behaviorPipeline, this.tileEntityProvider, extendedBlockStateProvider);
    // Override the default solid cube property provider if necessary
    final PropertyProvider<SolidCubeProperty> provider = properties.build().get(SolidCubeProperty.class).orElse(null);
    ObjectProvider<AABB> boundingBoxProvider = this.boundingBoxProvider;
    if (boundingBoxProvider instanceof SimpleObjectProvider) {
        // noinspection unchecked
        boundingBoxProvider = new CachedSimpleObjectProvider(blockType, ((SimpleObjectProvider) boundingBoxProvider).getProvider());
    }
    // noinspection ConstantConditions
    if (provider instanceof ConstantObjectProvider && provider.get(null, null, null).getValue()) {
        if (boundingBoxProvider instanceof ConstantObjectProvider) {
            // noinspection ConstantConditions
            final AABB aabb = boundingBoxProvider.get(null, null, null);
            final boolean isSolid = isSolid(aabb);
            if (isSolid) {
                properties.add(solidCube(true));
                properties.add(solidSide(true));
            } else {
                properties.add(solidCube(false));
                final BitSet solidSides = compileSidePropertyBitSet(aabb);
                // Check if all the direction bits are set
                final byte[] bytes = solidSides.toByteArray();
                if (bytes.length == 0 || bytes[0] != (1 << DIRECTION_INDEXES) - 1) {
                    properties.add(solidSide((blockState, location, face) -> {
                        final int index = getDirectionIndex(face);
                        return index != -1 && solidSides.get(index);
                    }));
                } else {
                    properties.add(solidSide(false));
                }
            }
        } else if (boundingBoxProvider instanceof CachedSimpleObjectProvider) {
            final List<AABB> values = ((CachedSimpleObjectProvider<AABB>) boundingBoxProvider).getValues();
            final BitSet bitSet = new BitSet();
            int count = 0;
            for (int i = 0; i < values.size(); i++) {
                if (isSolid(values.get(i))) {
                    bitSet.set(i);
                    count++;
                }
            }
            final boolean flag1 = count == values.size();
            final boolean flag2 = count == 0;
            // Use the best possible solid cube property
            if (flag1) {
                properties.add(solidCube(true));
                properties.add(solidSide(false));
            } else if (flag2) {
                properties.add(solidCube(false));
            } else {
                properties.add(solidCube(((blockState, location, face) -> bitSet.get(((LanternBlockState) blockState).getInternalId()))));
            }
            if (!flag1) {
                final BitSet[] solidSides = new BitSet[values.size()];
                int solidCount = 0;
                for (int i = 0; i < values.size(); i++) {
                    solidSides[i] = compileSidePropertyBitSet(values.get(i));
                    // Check if all the direction bits are set
                    final byte[] bytes = solidSides[i].toByteArray();
                    if (bytes.length != 0 && bytes[0] == (1 << DIRECTION_INDEXES) - 1) {
                        solidCount++;
                    }
                }
                if (solidCount == 0) {
                    properties.add(solidSide(false));
                } else {
                    properties.add(solidSide((blockState, location, face) -> {
                        final int index = getDirectionIndex(face);
                        if (index == -1) {
                            return false;
                        }
                        final int state = ((LanternBlockState) blockState).getInternalId();
                        return solidSides[state].get(index);
                    }));
                }
            }
        } else {
            final ObjectProvider<AABB> boundingBoxProvider1 = boundingBoxProvider;
            properties.add(solidCube(((blockState, location, face) -> isSolid(boundingBoxProvider1.get(blockState, location, face)))));
            properties.add(solidSide(((blockState, location, face) -> isSideSolid(boundingBoxProvider1.get(blockState, location, face), face))));
        }
    }
    blockType.setBoundingBoxProvider(boundingBoxProvider);
    blockType.setPropertyProviderCollection(properties.build());
    if (this.defaultStateProvider != null) {
        blockType.setDefaultBlockState(this.defaultStateProvider.apply(blockType.getDefaultState()));
    }
    if (this.itemTypeBuilder != null) {
        final ItemType itemType = this.itemTypeBuilder.blockType(blockType).behaviors(pipeline -> {
            // Only add the default behavior if there isn't any interaction behavior present
            if (pipeline.pipeline(InteractWithItemBehavior.class).getBehaviors().isEmpty()) {
                pipeline.add(new InteractWithBlockItemBehavior());
            }
        }).build(blockType.getPluginId(), blockType.getName());
        blockType.setItemType(itemType);
    }
    return blockType;
}
Also used : PropertyProviders.solidSide(org.lanternpowered.server.block.provider.property.PropertyProviders.solidSide) BehaviorPipeline(org.lanternpowered.server.behavior.pipeline.BehaviorPipeline) Vector3d(com.flowpowered.math.vector.Vector3d) AABB(org.spongepowered.api.util.AABB) Behavior(org.lanternpowered.server.behavior.Behavior) InteractWithBlockItemBehavior(org.lanternpowered.server.item.behavior.simple.InteractWithBlockItemBehavior) Function(java.util.function.Function) Supplier(java.util.function.Supplier) BoundingBoxes(org.lanternpowered.server.block.aabb.BoundingBoxes) ArrayList(java.util.ArrayList) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) ObjectProvider(org.lanternpowered.server.block.provider.ObjectProvider) PropertyProviderCollection(org.lanternpowered.server.block.provider.property.PropertyProviderCollection) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) ItemTypeBuilderImpl(org.lanternpowered.server.item.ItemTypeBuilderImpl) SolidCubeProperty(org.spongepowered.api.data.property.block.SolidCubeProperty) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) PropertyProvider(org.lanternpowered.server.block.provider.property.PropertyProvider) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) Nullable(javax.annotation.Nullable) Translation(org.spongepowered.api.text.translation.Translation) MutableBehaviorPipeline(org.lanternpowered.server.behavior.pipeline.MutableBehaviorPipeline) Location(org.spongepowered.api.world.Location) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PropertyProviders.solidCube(org.lanternpowered.server.block.provider.property.PropertyProviders.solidCube) MutableBehaviorPipelineImpl(org.lanternpowered.server.behavior.pipeline.impl.MutableBehaviorPipelineImpl) PropertyProviderCollections(org.lanternpowered.server.block.provider.property.PropertyProviderCollections) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) LanternTileEntityType(org.lanternpowered.server.block.tile.LanternTileEntityType) BlockState(org.spongepowered.api.block.BlockState) Consumer(java.util.function.Consumer) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) ItemTypeBuilder(org.lanternpowered.server.item.ItemTypeBuilder) Direction(org.spongepowered.api.util.Direction) TileEntityType(org.spongepowered.api.block.tileentity.TileEntityType) List(java.util.List) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) World(org.spongepowered.api.world.World) BitSet(java.util.BitSet) ItemType(org.spongepowered.api.item.ItemType) InteractWithBlockItemBehavior(org.lanternpowered.server.item.behavior.simple.InteractWithBlockItemBehavior) PropertyProviderCollection(org.lanternpowered.server.block.provider.property.PropertyProviderCollection) SolidCubeProperty(org.spongepowered.api.data.property.block.SolidCubeProperty) ItemType(org.spongepowered.api.item.ItemType) ArrayList(java.util.ArrayList) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) ArrayList(java.util.ArrayList) List(java.util.List) Behavior(org.lanternpowered.server.behavior.Behavior) InteractWithBlockItemBehavior(org.lanternpowered.server.item.behavior.simple.InteractWithBlockItemBehavior) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) BitSet(java.util.BitSet) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) BlockState(org.spongepowered.api.block.BlockState) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) ObjectProvider(org.lanternpowered.server.block.provider.ObjectProvider) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) AABB(org.spongepowered.api.util.AABB)

Example 7 with Behavior

use of org.lanternpowered.server.behavior.Behavior in project LanternServer by LanternPowered.

the class SimpleBreakBehavior method tryBreak.

@Override
public BehaviorResult tryBreak(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.create();
    // Apply the creator and notifier to the block
    context.populateBlockSnapshot(builder, BehaviorContext.PopulationFlags.CREATOR_AND_NOTIFIER);
    builder.location(context.requireContext(ContextKeys.BLOCK_LOCATION));
    // The block should be replaced with air
    builder.blockState(BlockTypes.AIR.getDefaultState());
    // Add the block change
    context.addBlockChange(builder.build());
    context.getContext(EventContextKeys.PLAYER).ifPresent(p -> p.get(Keys.EXHAUSTION).ifPresent(value -> p.offer(Keys.EXHAUSTION, value + 0.005)));
    context.process(pipeline.pipeline(BlockDropsProviderBehavior.class), (ctx, behavior) -> {
        behavior.tryAddDrops(pipeline, context);
        return BehaviorResult.CONTINUE;
    });
    return BehaviorResult.CONTINUE;
}
Also used : ContextKeys(org.lanternpowered.server.behavior.ContextKeys) BreakBlockBehavior(org.lanternpowered.server.block.behavior.types.BreakBlockBehavior) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder) Keys(org.spongepowered.api.data.key.Keys) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) BlockTypes(org.spongepowered.api.block.BlockTypes) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) BehaviorPipeline(org.lanternpowered.server.behavior.pipeline.BehaviorPipeline) Behavior(org.lanternpowered.server.behavior.Behavior) BlockDropsProviderBehavior(org.lanternpowered.server.block.behavior.types.BlockDropsProviderBehavior) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder) BlockDropsProviderBehavior(org.lanternpowered.server.block.behavior.types.BlockDropsProviderBehavior)

Aggregations

Behavior (org.lanternpowered.server.behavior.Behavior)7 BehaviorPipeline (org.lanternpowered.server.behavior.pipeline.BehaviorPipeline)7 ContextKeys (org.lanternpowered.server.behavior.ContextKeys)6 Keys (org.spongepowered.api.data.key.Keys)6 Direction (org.spongepowered.api.util.Direction)6 Location (org.spongepowered.api.world.Location)6 World (org.spongepowered.api.world.World)6 Optional (java.util.Optional)5 Supplier (java.util.function.Supplier)5 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)5 BlockState (org.spongepowered.api.block.BlockState)5 GameModes (org.spongepowered.api.entity.living.player.gamemode.GameModes)5 Vector3d (com.flowpowered.math.vector.Vector3d)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Consumer (java.util.function.Consumer)4 Function (java.util.function.Function)4 Nullable (javax.annotation.Nullable)4 BreakBlockBehavior (org.lanternpowered.server.block.behavior.types.BreakBlockBehavior)4