Search in sources :

Example 1 with LanternBlockState

use of org.lanternpowered.server.block.state.LanternBlockState in project LanternServer by LanternPowered.

the class BlockRegistryModule method register.

@Override
public void register(int internalId, BlockType blockType) {
    LanternBlockType blockType0 = (LanternBlockType) checkNotNull(blockType, "blockType");
    checkState(blockType0.getBlockStateBase().getBlockStates().stream().filter(s -> !((LanternBlockState) s).isExtended()).count() <= 1, "You cannot register a blockType with more then one state with this method.");
    register0(internalId, blockType0, blockState -> (byte) 0);
}
Also used : BlockTypeBuilder(org.lanternpowered.server.block.BlockTypeBuilder) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) LanternDirtType(org.lanternpowered.server.data.type.LanternDirtType) JukeboxInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.JukeboxInteractionBehavior) SnowyExtendedBlockStateProvider(org.lanternpowered.server.block.extended.SnowyExtendedBlockStateProvider) LanternBooleanTraits(org.lanternpowered.server.block.trait.LanternBooleanTraits) BoundingBoxes(org.lanternpowered.server.block.aabb.BoundingBoxes) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ChestPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.ChestPlacementBehavior) LanternTreeType(org.lanternpowered.server.data.type.LanternTreeType) RegistrationDependency(org.spongepowered.api.registry.util.RegistrationDependency) LanternStoneType(org.lanternpowered.server.data.type.LanternStoneType) KeyRegistryModule(org.lanternpowered.server.game.registry.type.data.KeyRegistryModule) PropertyProviders.replaceable(org.lanternpowered.server.block.provider.property.PropertyProviders.replaceable) ChestInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.ChestInteractionBehavior) NoteBlockInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.NoteBlockInteractionBehavior) ShulkerBoxInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.ShulkerBoxInteractionBehavior) Object2ShortOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap) LanternIntegerTraits(org.lanternpowered.server.block.trait.LanternIntegerTraits) Short2ObjectOpenHashMap(it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap) LanternQuartzType(org.lanternpowered.server.data.type.LanternQuartzType) LanternDyeColor(org.lanternpowered.server.data.type.LanternDyeColor) BlockTypes(org.spongepowered.api.block.BlockTypes) INSTANT_BROKEN(org.lanternpowered.server.block.provider.property.PropertyProviderCollections.INSTANT_BROKEN) LanternPlantType(org.lanternpowered.server.data.type.LanternPlantType) HopperPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.HopperPlacementBehavior) SimpleBlockDropsProviderBehavior(org.lanternpowered.server.block.behavior.simple.SimpleBlockDropsProviderBehavior) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SimpleBreakBehavior(org.lanternpowered.server.block.behavior.simple.SimpleBreakBehavior) LanternPortionType(org.lanternpowered.server.data.type.LanternPortionType) EquipmentTypeRegistryModule(org.lanternpowered.server.game.registry.type.item.inventory.equipment.EquipmentTypeRegistryModule) BlockState(org.spongepowered.api.block.BlockState) SlabType(org.spongepowered.api.data.type.SlabType) Direction(org.spongepowered.api.util.Direction) SimplePlacementBehavior(org.lanternpowered.server.block.behavior.simple.SimplePlacementBehavior) QuartzLinesRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.QuartzLinesRotationPlacementBehavior) Lantern(org.lanternpowered.server.game.Lantern) BlockSnapshotProviderPlaceBehavior(org.lanternpowered.server.block.behavior.simple.BlockSnapshotProviderPlaceBehavior) RotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.RotationPlacementBehavior) InstrumentTypeRegistryModule(org.lanternpowered.server.game.registry.type.data.InstrumentTypeRegistryModule) BlockType(org.spongepowered.api.block.BlockType) Object2ShortMap(it.unimi.dsi.fastutil.objects.Object2ShortMap) Optional(java.util.Optional) CraftingTableInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.CraftingTableInteractionBehavior) LanternSlabType(org.lanternpowered.server.data.type.LanternSlabType) LanternBedPart(org.lanternpowered.server.data.type.LanternBedPart) InventorySnapshot(org.lanternpowered.server.inventory.InventorySnapshot) BlockTypeBuilderImpl(org.lanternpowered.server.block.BlockTypeBuilderImpl) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EnderChestInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.EnderChestInteractionBehavior) OppositeFaceDirectionalPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.OppositeFaceDirectionalPlacementBehavior) LanternRailDirection(org.lanternpowered.server.data.type.LanternRailDirection) EnumTrait(org.spongepowered.api.block.trait.EnumTrait) Byte2ObjectOpenHashMap(it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap) TranslationProvider(org.lanternpowered.server.block.TranslationProvider) Supplier(java.util.function.Supplier) ItemRegistryModule(org.lanternpowered.server.game.registry.type.item.ItemRegistryModule) TreeType(org.spongepowered.api.data.type.TreeType) PropertyProviders.equipmentType(org.lanternpowered.server.item.PropertyProviders.equipmentType) PropertyProviders.lightEmission(org.lanternpowered.server.block.provider.property.PropertyProviders.lightEmission) HorizontalRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.HorizontalRotationPlacementBehavior) TorchPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.TorchPlacementBehavior) Byte2ObjectMap(it.unimi.dsi.fastutil.bytes.Byte2ObjectMap) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) LogAxisRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.LogAxisRotationPlacementBehavior) PASSABLE(org.lanternpowered.server.block.provider.property.PropertyProviderCollections.PASSABLE) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PropertyProviders.hardness(org.lanternpowered.server.block.provider.property.PropertyProviders.hardness) PropertyProviderCollections(org.lanternpowered.server.block.provider.property.PropertyProviderCollections) LanternSandType(org.lanternpowered.server.data.type.LanternSandType) LanternShrubType(org.lanternpowered.server.data.type.LanternShrubType) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) SpongeTranslationProvider(org.lanternpowered.server.block.translation.SpongeTranslationProvider) LanternSandstoneType(org.lanternpowered.server.data.type.LanternSandstoneType) SlabItemInteractionBehavior(org.lanternpowered.server.item.behavior.vanilla.SlabItemInteractionBehavior) PropertyProviders.blastResistance(org.lanternpowered.server.block.provider.property.PropertyProviders.blastResistance) Short2ObjectMap(it.unimi.dsi.fastutil.shorts.Short2ObjectMap) TileEntityTypes(org.spongepowered.api.block.tileentity.TileEntityTypes) PropertyProviders.flammableInfo(org.lanternpowered.server.block.provider.property.PropertyProviders.flammableInfo) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) LanternEnumTraits(org.lanternpowered.server.block.trait.LanternEnumTraits) AdditionalPluginCatalogRegistryModule(org.lanternpowered.server.game.registry.AdditionalPluginCatalogRegistryModule) OpenableContainerInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.OpenableContainerInteractionBehavior) LanternBlockType(org.lanternpowered.server.block.LanternBlockType)

Example 2 with LanternBlockState

use of org.lanternpowered.server.block.state.LanternBlockState 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 3 with LanternBlockState

use of org.lanternpowered.server.block.state.LanternBlockState in project LanternServer by LanternPowered.

the class BlockRegistryModule method register0.

private void register0(int internalId, LanternBlockType blockType, BlockState2DataFunction stateToDataConverter) {
    checkNotNull(stateToDataConverter, "stateToDataConverter");
    checkState(internalId >= 0, "The internal id cannot be negative: %s", internalId);
    checkState(internalId <= 0xfff, "The internal id exceeded the internal id limit: %s > %s", internalId, 0xfff);
    final short internalId0 = (short) internalId;
    checkState(!this.blockTypeByInternalId.containsKey(internalId0), "The internal id is already used: %s", internalId);
    super.register(blockType);
    this.blockTypeByInternalId.put(internalId0, blockType);
    this.internalIdByBlockType.put(blockType, internalId0);
    Byte2ObjectMap<BlockState> usedValues = new Byte2ObjectOpenHashMap<>();
    int internalStateIdBase = (internalId & 0xfff) << 4;
    for (BlockState blockState : blockType.getBlockStateBase().getBlockStates()) {
        if (((LanternBlockState) blockState).isExtended()) {
            continue;
        }
        byte value = checkNotNull(stateToDataConverter.apply(blockState));
        if (usedValues.containsKey(value)) {
            throw new IllegalStateException("The data value " + value + " for state '" + blockState.getId() + "' is already used by '" + usedValues.get(value).getId() + "'");
        }
        usedValues.put(value, blockState);
        final short internalStateId = (short) (internalStateIdBase | value & 0xf);
        this.blockStateByPackedType.put(internalStateId, blockState);
        this.packedTypeByBlockState.put(blockState, internalStateId);
    }
    final BlockState defaultBlockState = blockType.getDefaultState();
    for (byte b = 0; b <= 0xf; b++) {
        if (!usedValues.containsKey(b)) {
            final short internalStateId = (short) (internalStateIdBase | b & 0xf);
            this.blockStateByPackedType.put(internalStateId, defaultBlockState);
        }
    }
    for (BlockState blockState : blockType.getBlockStateBase().getBlockStates()) {
        if (!((LanternBlockState) blockState).isExtended()) {
            continue;
        }
        blockState = blockType.getExtendedBlockStateProvider().remove(blockState);
        this.packedTypeByBlockState.put(blockState, checkNotNull(this.packedTypeByBlockState.get(blockState)));
    }
    final BlockStateRegistryModule blockStateRegistryModule = Lantern.getRegistry().getRegistryModule(BlockStateRegistryModule.class).get();
    blockType.getAllBlockStates().forEach(blockStateRegistryModule::registerState);
    blockType.getItem().ifPresent(itemType -> ItemRegistryModule.get().register(internalId, itemType));
    Lantern.getGame().getPropertyRegistry().registerBlockPropertyStores(blockType.getPropertyProviderCollection());
}
Also used : Byte2ObjectOpenHashMap(it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap) BlockState(org.spongepowered.api.block.BlockState) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Byte2ObjectOpenHashMap (it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap)2 Supplier (java.util.function.Supplier)2 BoundingBoxes (org.lanternpowered.server.block.aabb.BoundingBoxes)2 PropertyProviderCollections (org.lanternpowered.server.block.provider.property.PropertyProviderCollections)2 LanternBlockState (org.lanternpowered.server.block.state.LanternBlockState)2 BlockState (org.spongepowered.api.block.BlockState)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Byte2ObjectMap (it.unimi.dsi.fastutil.bytes.Byte2ObjectMap)1 Object2ShortMap (it.unimi.dsi.fastutil.objects.Object2ShortMap)1 Object2ShortOpenHashMap (it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap)1 Short2ObjectMap (it.unimi.dsi.fastutil.shorts.Short2ObjectMap)1 Short2ObjectOpenHashMap (it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1