Search in sources :

Example 1 with Byte2ObjectOpenHashMap

use of it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap 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

Byte2ObjectOpenHashMap (it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap)1 LanternBlockState (org.lanternpowered.server.block.state.LanternBlockState)1 BlockState (org.spongepowered.api.block.BlockState)1